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
|
#pragma once
#include <map>
#include "base.h"
#include "application.h"
#include "timer.h"
#include "builder.h"
namespace cru::ui::animations
{
class Animation;
class AnimationManager : public Object
{
public:
static AnimationManager* GetInstance()
{
return Application::GetInstance()->GetAnimationManager();
}
public:
AnimationManager();
AnimationManager(const AnimationManager& other) = delete;
AnimationManager(AnimationManager&& other) = delete;
AnimationManager& operator=(const AnimationManager& other) = delete;
AnimationManager& operator=(AnimationManager&& other) = delete;
~AnimationManager() override;
void AddAnimation(Animation* animation);
void RemoveAnimation(Animation* animation);
private:
std::map<String, Animation*> animations_;
std::shared_ptr<ITimerTask> timer_;
std::shared_ptr<Action<>> timer_action_;
};
class Animation : public Object
{
friend class AnimationManager;
protected:
Animation(
String tag,
double duration,
const Vector<std::shared_ptr<Action<Animation*, double>>>& step_handlers,
const Vector<std::shared_ptr<Action<Animation*>>>& start_handlers,
const Vector<std::shared_ptr<Action<Animation*>>>& finish_handlers,
const Vector<std::shared_ptr<Action<Animation*>>>& cancel_handlers
);
public:
Animation(const Animation& other) = delete;
Animation(Animation&& other) = delete;
Animation& operator=(const Animation& other) = delete;
Animation& operator=(Animation&& other) = delete;
~Animation() override = default; // The animation will never destroy by users.
bool Step(double time);
void Cancel();
String GetTag() const
{
return tag_;
}
private:
const String tag_;
const double duration_;
Vector<std::shared_ptr<Action<Animation*, double>>> step_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> start_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> finish_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> cancel_handlers_;
double current_time_ = 0;
public:
class Builder : public OneTimeBuilder<Animation>
{
public:
Builder(String tag, const double duration)
: tag(std::move(tag)), duration(duration)
{
}
String tag;
double duration;
Builder& AddStepHandler(Action<Animation*, double>&& handler)
{
if (IsValid())
step_handlers_.push_back(std::make_shared<Action<Animation*, double>>(std::move(handler)));
return *this;
}
Builder& AddStartHandler(Action<Animation*>&& handler)
{
if (IsValid())
start_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler)));
return *this;
}
Builder& AddFinishHandler(Action<Animation*>&& handler)
{
if (IsValid())
finish_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler)));
return *this;
}
Builder& AddCancelHandler(Action<Animation*>&& handler)
{
if (IsValid())
cancel_handlers_.push_back(std::make_shared<Action<Animation*>>(std::move(handler)));
return *this;
}
protected:
Animation* OnCreate() override
{
return new Animation(std::move(tag), duration, step_handlers_, start_handlers_, finish_handlers_, cancel_handlers_);
}
private:
Vector<std::shared_ptr<Action<Animation*, double>>> step_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> start_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> finish_handlers_;
Vector<std::shared_ptr<Action<Animation*>>> cancel_handlers_;
};
};
}
|