-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
93 lines (74 loc) · 1.79 KB
/
Animation.cpp
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
#include "Animation.h"
bool Animation::operator()(const double timeDelta) {
if (!this->started) {
this->started = true;
this->onStart();
}
this->timeElapsed += timeDelta;
if (this->interrupted || this->stepFrame(this->timeElapsed, timeDelta)) { // if interrupted, don't step
if (!this->finished) {
if (this->interrupted) {
this->onInterrupted();
}
this->finished = true;
this->onFinished();
}
}
return this->finished;
}
void Animation::onStart() {
// empty default
}
void Animation::onFinished() {
// empty default
}
void Animation::onInterrupted() {
// empty default
}
bool Animation::isStarted() const {
return this->started;
}
bool Animation::isFinished() const {
return this->finished;
}
bool Animation::isInterrupted() const {
return this->interrupted;
}
void Animation::interrupt() {
this->interrupted = true;
(*this)(0);
}
AnimationManager::Queue & AnimationManager::getFrontQueue() {
return this->queue[this->queueIndex];
}
AnimationManager::Queue & AnimationManager::getBackQueue() {
return this->queue[(this->queueIndex + 1) % 2];
}
void AnimationManager::swapQueue() {
this->queueIndex++;
this->queueIndex %= 2;
}
AnimationManager & AnimationManager::push(std::shared_ptr<Animation> animation) {
this->getFrontQueue().push_back(animation);
return *this;
}
void AnimationManager::step(const double timeDelta, const bool interrupted) {
this->swapQueue();
while (!this->getBackQueue().empty()) {
std::shared_ptr<Animation> ani = this->getBackQueue().front();
this->getBackQueue().pop_front();
if (interrupted) {
ani->interrupt();
}
else {
if (!(*ani)(timeDelta)) {
this->getFrontQueue().push_back(ani);
}
}
}
}
void AnimationManager::clear() {
this->step(0, true);
this->getFrontQueue().clear();
this->getBackQueue().clear();
}