-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTween.h
More file actions
380 lines (325 loc) · 8.02 KB
/
Copy pathTween.h
File metadata and controls
380 lines (325 loc) · 8.02 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#pragma once
#include <cstdint>
#include <functional>
#include "Easing.h"
#include "DekiVector.h"
#include "Color.h"
namespace deki {
/**
* @brief Tween state enum
*/
enum class TweenState : uint8_t
{
Idle, // Not started or completed
Running, // Currently animating
Paused, // Paused mid-animation
Completed // Finished (ready for removal or restart)
};
/**
* @brief Base tween interface for type-erased storage in TweenManager
*/
class DEKI_TWEEN_API ITween
{
public:
virtual ~ITween() = default;
virtual void Update(float deltaTimeSeconds) = 0;
virtual bool IsComplete() const = 0;
virtual void Kill() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual TweenState GetState() const = 0;
virtual void Restart() = 0;
// Unique ID for lookup
uint32_t id = 0;
};
/**
* @brief Templated tween for specific value types
*
* Supports: float, int32_t, DekiVector2, deki::Color
*/
template <typename T>
class Tween : public ITween
{
public:
using UpdateCallback = std::function<void(const T&)>;
using CompleteCallback = std::function<void()>;
Tween()
: m_Target(nullptr)
, m_StartValue{}
, m_EndValue{}
, m_CurrentValue{}
, m_Duration(1.0f)
, m_Elapsed(0.0f)
, m_Delay(0.0f)
, m_DelayElapsed(0.0f)
, m_EaseFunc(Ease::Linear)
, m_State(TweenState::Idle)
, m_Loops(0)
, m_CurrentLoop(0)
, m_PingPong(false)
, m_Reversed(false)
{
}
~Tween() override = default;
// ========== Configuration (Fluent API) ==========
/**
* @brief Set the target pointer to modify directly
*/
Tween<T>& SetTarget(T* target)
{
m_Target = target;
return *this;
}
/**
* @brief Set start value
*/
Tween<T>& From(const T& startValue)
{
m_StartValue = startValue;
return *this;
}
/**
* @brief Set end value
*/
Tween<T>& To(const T& endValue)
{
m_EndValue = endValue;
return *this;
}
/**
* @brief Set duration in seconds.
*/
Tween<T>& Duration(float seconds)
{
m_Duration = seconds > 0.0f ? seconds : 0.001f;
return *this;
}
/**
* @brief Set easing function by type
*/
Tween<T>& SetEase(EaseType ease)
{
m_EaseFunc = Ease::GetFunction(ease);
return *this;
}
/**
* @brief Set easing function directly
*/
Tween<T>& SetEase(EasingFunc easeFunc)
{
m_EaseFunc = easeFunc ? easeFunc : Ease::Linear;
return *this;
}
/**
* @brief Set delay before starting (seconds).
*/
Tween<T>& Delay(float seconds)
{
m_Delay = seconds > 0.0f ? seconds : 0.0f;
return *this;
}
/**
* @brief Set number of loops (-1 = infinite, 0 = no loop)
*/
Tween<T>& SetLoops(int32_t loops)
{
m_Loops = loops;
return *this;
}
/**
* @brief Enable ping-pong (reverse on each loop)
*/
Tween<T>& SetPingPong(bool pingPong)
{
m_PingPong = pingPong;
return *this;
}
/**
* @brief Set update callback (called each frame with current value)
*/
Tween<T>& OnUpdate(UpdateCallback callback)
{
m_OnUpdate = callback;
return *this;
}
/**
* @brief Set completion callback
*/
Tween<T>& OnComplete(CompleteCallback callback)
{
m_OnComplete = callback;
return *this;
}
/**
* @brief Start the tween (auto-called by TweenManager when added)
*/
Tween<T>& Start()
{
if (m_State == TweenState::Idle || m_State == TweenState::Completed)
{
m_State = TweenState::Running;
m_Elapsed = 0.0f;
m_DelayElapsed = 0.0f;
m_CurrentLoop = 0;
m_Reversed = false;
if (m_Target && m_Delay <= 0.0f)
{
m_CurrentValue = m_StartValue;
*m_Target = m_CurrentValue;
}
}
return *this;
}
// ========== Control ==========
void Update(float deltaTimeSeconds) override
{
if (m_State != TweenState::Running)
return;
float dt = deltaTimeSeconds;
// Handle delay
if (m_DelayElapsed < m_Delay)
{
m_DelayElapsed += dt;
if (m_DelayElapsed < m_Delay)
return;
// Apply remaining time after delay
dt = m_DelayElapsed - m_Delay;
}
m_Elapsed += dt;
float progress = m_Elapsed / m_Duration;
if (progress >= 1.0f)
{
progress = 1.0f;
}
// Apply easing
float easedInput = m_Reversed ? (1.0f - progress) : progress;
float easedProgress = m_EaseFunc(easedInput);
// Interpolate
m_CurrentValue = Interpolate(easedProgress);
// Apply value
ApplyValue();
// Check completion
if (progress >= 1.0f)
{
HandleLoopOrComplete();
}
}
bool IsComplete() const override
{
return m_State == TweenState::Completed;
}
void Kill() override
{
m_State = TweenState::Completed;
// Note: Kill does NOT call OnComplete
}
void Pause() override
{
if (m_State == TweenState::Running)
{
m_State = TweenState::Paused;
}
}
void Resume() override
{
if (m_State == TweenState::Paused)
{
m_State = TweenState::Running;
}
}
TweenState GetState() const override
{
return m_State;
}
void Restart() override
{
m_State = TweenState::Idle;
Start();
}
/**
* @brief Get current interpolated value
*/
const T& GetCurrentValue() const
{
return m_CurrentValue;
}
/**
* @brief Get the target pointer
*/
T* GetTarget() const
{
return m_Target;
}
private:
T* m_Target; // Pointer to value being tweened (optional)
T m_StartValue; // Start value
T m_EndValue; // End value
T m_CurrentValue; // Current interpolated value
float m_Duration;
float m_Elapsed;
float m_Delay;
float m_DelayElapsed;
EasingFunc m_EaseFunc; // Easing function
TweenState m_State; // Current state
int32_t m_Loops; // Number of loops (-1 = infinite)
int32_t m_CurrentLoop; // Current loop index
bool m_PingPong; // Reverse direction each loop
bool m_Reversed; // Currently playing in reverse
UpdateCallback m_OnUpdate;
CompleteCallback m_OnComplete;
/**
* @brief Interpolate between start and end values. t is normalized
* progress in [0,1].
*/
T Interpolate(float t) const;
/**
* @brief Apply current value to target and callbacks
*/
void ApplyValue()
{
if (m_Target)
{
*m_Target = m_CurrentValue;
}
if (m_OnUpdate)
{
m_OnUpdate(m_CurrentValue);
}
}
/**
* @brief Handle loop completion or final completion
*/
void HandleLoopOrComplete()
{
// Check if we should loop
if (m_Loops == -1 || m_CurrentLoop < m_Loops)
{
m_CurrentLoop++;
m_Elapsed = 0.0f;
if (m_PingPong)
{
m_Reversed = !m_Reversed;
}
}
else
{
// Complete
m_State = TweenState::Completed;
if (m_OnComplete)
{
m_OnComplete();
}
}
}
};
// ========== Type-specific interpolation specializations ==========
// Implementations in Tween.cpp.
template <>
float Tween<float>::Interpolate(float t) const;
template <>
int32_t Tween<int32_t>::Interpolate(float t) const;
template <>
DekiVector2 Tween<DekiVector2>::Interpolate(float t) const;
template <>
deki::Color Tween<deki::Color>::Interpolate(float t) const;
} // namespace deki