Repository navigation
Expand file tree
/
Copy pathTweenManager.cpp
More file actions
144 lines (120 loc) · 4.07 KB
/
Copy pathTweenManager.cpp
File metadata and controls
144 lines (120 loc) · 4.07 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
#include "TweenManager.h"
#include <deki/Time.h>
#include <deki/profiling/Profiler.h>
#include <algorithm>
namespace DekiTween
{
TweenManager::TweenManager()
: m_NextId(1),
m_IsUpdating(false),
m_LastUpdateTime(0)
{
}
TweenManager::~TweenManager()
{
KillAll();
}
TweenManager& TweenManager::Instance()
{
static TweenManager s_Instance;
return s_Instance;
}
void TweenManager::Update(float deltaTimeSeconds)
{
DEKI_PROFILE_SCOPE_N("TweenManager::Update");
if (m_Tweens.empty())
{
return;
}
m_IsUpdating = true;
for (auto& tween : m_Tweens)
{
if (tween && !tween->IsComplete())
{
tween->Update(deltaTimeSeconds);
}
}
m_IsUpdating = false;
// Add the tweens created during the update.
for (auto& tween : m_TweensToAdd)
{
m_Tweens.push_back(std::move(tween));
}
m_TweensToAdd.clear();
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 = Deki::Time::GetTime();
if (currentTime != m_LastUpdateTime)
{
m_LastUpdateTime = currentTime;
float deltaSeconds = Deki::Time::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<Deki::Vector2>& TweenManager::To(Deki::Vector2* target, const Deki::Vector2& endValue, float duration)
{
return FromTo(target, target ? *target : Deki::Vector2::Zero(), endValue, duration);
}
Tween<Deki::Vector2>& TweenManager::FromTo(Deki::Vector2* target, const Deki::Vector2& startValue,
const Deki::Vector2& endValue, float duration)
{
auto tween = std::make_unique<Tween<Deki::Vector2>>();
tween->SetTarget(target).From(startValue).To(endValue).Duration(duration).Start();
return Instance().AddTween(std::move(tween));
}
Tween<Deki::Color>& TweenManager::To(Deki::Color* target, const Deki::Color& endValue, float duration)
{
// Deki::Color(255, 255, 255), not Deki::Color::White, which fails across the DLL boundary (dllimport).
return FromTo(target, target ? *target : Deki::Color(255, 255, 255), endValue, duration);
}
Tween<Deki::Color>& TweenManager::FromTo(Deki::Color* target, const Deki::Color& startValue,
const Deki::Color& endValue, float duration)
{
auto tween = std::make_unique<Tween<Deki::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 DekiTween