-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweenManager.cpp
More file actions
143 lines (119 loc) · 3.93 KB
/
TweenManager.cpp
File metadata and controls
143 lines (119 loc) · 3.93 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
#include "TweenManager.h"
#include "DekiTime.h"
#include "profiling/DekiProfiler.h"
#include <algorithm>
namespace deki {
TweenManager::TweenManager()
: m_NextId(1)
, m_IsUpdating(false)
, m_LastUpdateTime(0)
{
}
TweenManager::~TweenManager()
{
KillAll();
}
TweenManager& TweenManager::Instance()
{
static TweenManager instance;
return instance;
}
void TweenManager::Update(float deltaTimeSeconds)
{
DEKI_PROFILE_SCOPE_N("TweenManager::Update");
if (m_Tweens.empty())
return;
m_IsUpdating = true;
// Update all tweens
for (auto& tween : m_Tweens)
{
if (tween && !tween->IsComplete())
{
tween->Update(deltaTimeSeconds);
}
}
m_IsUpdating = false;
// Add any tweens that were created during update
for (auto& tween : m_TweensToAdd)
{
m_Tweens.push_back(std::move(tween));
}
m_TweensToAdd.clear();
// Remove completed tweens
m_Tweens.erase(
std::remove_if(m_Tweens.begin(), m_Tweens.end(),
[](const std::unique_ptr<ITween>& t) { return !t || t->IsComplete(); }),
m_Tweens.end());
}
void TweenManager::EnsureUpdatedThisFrame()
{
uint32_t currentTime = DekiTime::GetTime();
// Only update if we haven't already this frame
if (currentTime != m_LastUpdateTime)
{
m_LastUpdateTime = currentTime;
float deltaSeconds = DekiTime::GetDeltaTimeF() / 1000.0f;
Update(deltaSeconds);
}
}
void TweenManager::KillAll()
{
for (auto& tween : m_Tweens)
{
if (tween)
{
tween->Kill();
}
}
m_Tweens.clear();
m_TweensToAdd.clear();
}
// ========== Static Factory API ==========
Tween<float>& TweenManager::To(float* target, float endValue, float duration)
{
return FromTo(target, target ? *target : 0.0f, endValue, duration);
}
Tween<float>& TweenManager::FromTo(float* target, float startValue, float endValue, float duration)
{
auto tween = std::make_unique<Tween<float>>();
tween->SetTarget(target).From(startValue).To(endValue).Duration(duration).Start();
return Instance().AddTween(std::move(tween));
}
Tween<int32_t>& TweenManager::To(int32_t* target, int32_t endValue, float duration)
{
return FromTo(target, target ? *target : 0, endValue, duration);
}
Tween<int32_t>& TweenManager::FromTo(int32_t* target, int32_t startValue, int32_t endValue, float duration)
{
auto tween = std::make_unique<Tween<int32_t>>();
tween->SetTarget(target).From(startValue).To(endValue).Duration(duration).Start();
return Instance().AddTween(std::move(tween));
}
Tween<Vector2>& TweenManager::To(Vector2* target, const Vector2& endValue, float duration)
{
return FromTo(target, target ? *target : Vector2::Zero(), endValue, duration);
}
Tween<Vector2>& TweenManager::FromTo(Vector2* target, const Vector2& startValue, const Vector2& endValue, float duration)
{
auto tween = std::make_unique<Tween<Vector2>>();
tween->SetTarget(target).From(startValue).To(endValue).Duration(duration).Start();
return Instance().AddTween(std::move(tween));
}
Tween<Color>& TweenManager::To(Color* target, const Color& endValue, float duration)
{
// Use inline Color constructor instead of Color::White to avoid dllimport issues
return FromTo(target, target ? *target : Color(255, 255, 255), endValue, duration);
}
Tween<Color>& TweenManager::FromTo(Color* target, const Color& startValue, const Color& endValue, float duration)
{
auto tween = std::make_unique<Tween<Color>>();
tween->SetTarget(target).From(startValue).To(endValue).Duration(duration).Start();
return Instance().AddTween(std::move(tween));
}
Tween<float>& TweenManager::DelayedCall(float delay, std::function<void()> callback)
{
auto tween = std::make_unique<Tween<float>>();
tween->From(0.0f).To(1.0f).Duration(0.001f).Delay(delay).OnComplete(callback).Start();
return Instance().AddTween(std::move(tween));
}
} // namespace deki