-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweenComponent.cpp
More file actions
222 lines (192 loc) · 4.61 KB
/
TweenComponent.cpp
File metadata and controls
222 lines (192 loc) · 4.61 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
#include "TweenComponent.h"
#include "TweenManager.h"
#include "DekiTime.h"
#include "DekiObject.h"
TweenComponent::TweenComponent()
: target_type(TweenTargetType::Position)
, end_value(0.0f, 0.0f, 0.0f)
, duration(1.0f)
, delay(0.0f)
, ease_type(deki::EaseType::Linear)
, loops(0)
, ping_pong(false)
, auto_play(true)
, relative(false)
, m_Elapsed(0.0f)
, m_DelayElapsed(0.0f)
, m_CurrentLoop(0)
, m_Reversed(false)
, m_HasCompleted(false)
, m_IsPlaying(false)
, m_IsPaused(false)
, m_StartValue(0.0f, 0.0f, 0.0f)
{
SetNeedsUpdate(true);
}
TweenComponent::~TweenComponent()
{
}
void TweenComponent::Awake()
{
// Nothing to cache - we capture start value when Play() is called
}
void TweenComponent::Start()
{
if (auto_play)
{
Play();
}
}
void TweenComponent::Update()
{
// Also update the global TweenManager for static API tweens
deki::TweenManager::Instance().EnsureUpdatedThisFrame();
if (!m_IsPlaying || m_IsPaused)
return;
float deltaSeconds = DekiTime::GetDeltaTimeF() / 1000.0f;
// Handle delay
if (m_DelayElapsed < delay)
{
m_DelayElapsed += deltaSeconds;
if (m_DelayElapsed < delay)
return;
// Apply remaining time after delay
deltaSeconds = m_DelayElapsed - delay;
}
m_Elapsed += deltaSeconds;
float progress = duration > 0.0f ? m_Elapsed / duration : 1.0f;
if (progress >= 1.0f)
{
progress = 1.0f;
}
// Apply easing
float t = m_Reversed ? 1.0f - progress : progress;
float easedT = GetEasedProgress(t);
// Apply interpolated value
ApplyValue(easedT);
// Call update callback
if (on_update)
{
on_update(progress);
}
// Check completion
if (progress >= 1.0f)
{
HandleLoopOrComplete();
}
}
void TweenComponent::Play()
{
m_IsPlaying = true;
m_IsPaused = false;
m_HasCompleted = false;
m_Elapsed = 0.0f;
m_DelayElapsed = 0.0f;
m_CurrentLoop = 0;
m_Reversed = false;
// Capture current value as start value
m_StartValue = GetCurrentValue();
}
void TweenComponent::Pause()
{
if (m_IsPlaying)
{
m_IsPaused = true;
}
}
void TweenComponent::Resume()
{
if (m_IsPlaying && m_IsPaused)
{
m_IsPaused = false;
}
}
void TweenComponent::Stop()
{
m_IsPlaying = false;
m_IsPaused = false;
m_Elapsed = 0.0f;
m_DelayElapsed = 0.0f;
m_CurrentLoop = 0;
m_Reversed = false;
}
void TweenComponent::SetOnComplete(std::function<void()> callback)
{
on_complete = callback;
}
float TweenComponent::GetProgress() const
{
if (duration <= 0.0f)
return 1.0f;
return m_Elapsed / duration;
}
Vector3 TweenComponent::GetCurrentValue() const
{
DekiObject* owner = GetOwner();
if (!owner)
return Vector3(0.0f, 0.0f, 0.0f);
switch (target_type)
{
case TweenTargetType::Position:
return Vector3(owner->GetX(), owner->GetY(), 0.0f);
case TweenTargetType::Scale:
return Vector3(owner->GetScaleX(), owner->GetScaleY(), 0.0f);
case TweenTargetType::Rotation:
return Vector3(0.0f, 0.0f, owner->GetLocalRotation());
default:
return Vector3(0.0f, 0.0f, 0.0f);
}
}
void TweenComponent::ApplyValue(float easedT)
{
DekiObject* owner = GetOwner();
if (!owner)
return;
// Calculate target value (relative adds to start, absolute uses end_value directly)
Vector3 targetValue = relative ? m_StartValue + end_value : end_value;
// Interpolate from start to target
Vector3 value = Vector3::Lerp(m_StartValue, targetValue, easedT);
switch (target_type)
{
case TweenTargetType::Position:
owner->SetLocalPosition(value.x, value.y);
break;
case TweenTargetType::Scale:
owner->SetScale(value.x, value.y);
break;
case TweenTargetType::Rotation:
owner->SetRotation(value.z);
break;
case TweenTargetType::COUNT:
// Not a valid target type, ignore
break;
}
}
float TweenComponent::GetEasedProgress(float t) const
{
deki::EasingFunc func = deki::Ease::GetFunction(ease_type);
return func(t);
}
void TweenComponent::HandleLoopOrComplete()
{
// Check if we should loop
if (loops == -1 || m_CurrentLoop < loops - 1)
{
m_CurrentLoop++;
m_Elapsed = 0.0f;
if (ping_pong)
{
m_Reversed = !m_Reversed;
}
}
else
{
// Complete
m_IsPlaying = false;
m_HasCompleted = true;
if (on_complete)
{
on_complete();
}
}
}