-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.h
163 lines (131 loc) · 3.64 KB
/
Animation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef _ANIMATION_H
#define _ANIMATION_H
#include <memory>
#include <functional>
#include <vector>
#include <cmath>
#include "Freq.h"
#include "math/common.h"
template<class T> class Animation;
struct LinearInterpolation
{
template<class T>
T operator()(const T& a, const T& b, float t) const {
return a * (1.0f - t) + b * t;
}
};
struct CosineInterpolation
{
template<class T>
T operator()(const T& a, const T& b, float t) const {
float ft = (1.0f - cos(M_PI * t)) * 0.5f;
return a + (b - a) * ft;
}
};
struct SquareRootInterpolation
{
template<class T>
T operator()(const T& a, const T& b, float t) const {
return a + (b - a) * std::sqrt(t);
}
};
struct NoInterpolation
{
template<class T>
T operator()(const T& a, const T& b, float t) const {
return a;
}
};
// TODO requirements: should work with vectors and orthogonal matrices
template<class T>
class Waypoint
{
private:
// TODO: execution callback?
// TODO: expiry callback?
// TODO: interpolative callback?
T m_Position;
unsigned long m_ulStartTime; // transition start time
unsigned long m_ulAlarmTime; // transition end time
std::function<T (const T&, const T&, float)> m_Interpolation;
Animation<T>* m_Animation;
public:
Waypoint(T position, Freq::Time time, Animation<T>* nav):
m_Position(position),
m_ulStartTime(0L),
m_ulAlarmTime(0L),
m_Animation(nav)
{
m_ulStartTime = Freq::get().getElapsedTime(); //ms
m_ulAlarmTime = m_ulStartTime + time.get();
}
virtual ~Waypoint() {}
virtual void poll() {
// TODO: not necessary, but trigger callbacks if possible
}
virtual bool elapsed() {
poll();
return Freq::get().getElapsedTime() >= m_ulAlarmTime;
}
// may be negative
long remaining() {
return m_ulAlarmTime - Freq::get().getElapsedTime();
}
void setTimer(Freq::Time time) {
}
T& position() {
return m_Position;
}
void interp(std::function<T (const T&, const T&, float)> func) {
m_Interpolation = func;
}
};
template<class T>
class Animation
{
private:
std::vector<Waypoint<T>> m_Waypoints;
T m_default;
float m_fSpeed;
public:
Animation(
Waypoint<T> initial,
T default_value=T(),
Freq::Accumulator* accum = Freq::get().accumulator()
):
m_default(default_value),
m_fSpeed(1.0f)
{
m_Waypoints.push_back(std::move(initial));
}
virtual ~Animation() {}
void addWaypoint(T position, Freq::Time time) {
m_Waypoints.push_back(Waypoint<T>(std::move(position), time, this));
}
void clear(T position) {
m_Waypoints.clear();
m_Waypoints.push_back(Waypoint<T>(position, Freq::Time(0)));
}
T get() {
for(auto itr = m_Waypoints.begin();
itr != m_Waypoints.end();
)
{
if(itr->elapsed())
itr = itr->erase();
else
++itr;
}
return m_Waypoints.front().position();
}
bool empty() {
return m_Waypoints.size() <= 1;
}
void speed(float s) {
m_fSpeed = s;
}
float speed() const {
return m_fSpeed;
}
};
#endif