-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHImGuiAnimation.cpp
More file actions
152 lines (133 loc) · 3.9 KB
/
HImGuiAnimation.cpp
File metadata and controls
152 lines (133 loc) · 3.9 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
//birth: created by HalfPeople on 2024/4/14
#include "HImGuiAnimation.h"
#include <algorithm>
std::vector<HAnimationSystem::AnimationSequence> Sequences;
std::vector<HAnimationSystem::AnimationSequence**> WaitingForDeletePointers;
void HAnimationSystem::updata(float delta_time, int MaxFPS)
{
static float buffer;
if (buffer > 1.f / MaxFPS)
{
for (HAnimationSystem::AnimationSequence& Sequence : Sequences)
Sequence.updata(delta_time);
buffer = 0;
}
else
buffer += delta_time;
}
void HAnimationSystem::updata(float delta_time)
{
if (!WaitingForDeletePointers.empty())
{
for (HAnimationSystem::AnimationSequence** DeletePointers : WaitingForDeletePointers)
if (DeletePointers)
{
*DeletePointers = 0;
}
WaitingForDeletePointers.clear();
}
for (HAnimationSystem::AnimationSequence& Sequence : Sequences)
Sequence.updata_MaxFPS(delta_time);
}
void HAnimationSystem::Play(PlayerCallBack::AnimationPlayerCallBack callback, size_t MaxFrame, HValue value, HAnimationSystem::AnimationSequence** out, float speed, int FPS, bool IsLoop)
{
for (HAnimationSystem::AnimationSequence& Sequence : Sequences)
{
HAnimationSystem::AnimationSequence::information& info = Sequence.getinfo();
if (info.callback == callback && info.data == value)
return;
}
Sequences.push_back(HAnimationSystem::AnimationSequence(FPS, out, value, speed, MaxFrame, IsLoop, callback));
if (out)
{
*out = &Sequences.back();
}
return;
}
void HAnimationSystem::AnimationSequence::Stop()
{
UnloadFromArray();
WaitingForDeletePointers.push_back(info.pos);
}
void HAnimationSystem::AnimationSequence::updata_MaxFPS(float delta_time)
{
if (info.FPS_delta_time)
{
static float delta_time_buffer;
if (delta_time_buffer > info.FPS_delta_time)
{
updata(delta_time_buffer);
delta_time_buffer = 0;
}
else
delta_time_buffer += delta_time;
}
else
updata(delta_time);
}
void HAnimationSystem::AnimationSequence::updata(float delta_time)
{
if (!Playing)
return;
float buff = delta_time * (info.speed * 100);
info.CurrentFrame += buff;
if (info.CurrentFrame >= info.MaxFrame)
{
if (info.IsLoop)
info.CurrentFrame = 0;
else
Stop();
}
info.callback(info.CurrentFrame, info.data);
return;
}
void HAnimationSystem::AnimationSequence::UnloadFromArray()
{
for (size_t i = 0; i < Sequences.size(); i++)
{
if (this == &Sequences[i])
{
Sequences.erase(Sequences.begin() + i);
return;
}
}
}
HAnimationSystem::PlayerCallBack::HInterpolationInfo HAnimationSystem::PlayerCallBack::GetInterpolationInfoFromKeys(const std::vector<int> keys, int32_t Frame)
{
HInterpolationInfo info;
// Binary search finds the closest key
auto upper = std::upper_bound(keys.begin(), keys.end(), Frame);
info.LastOneKey = std::distance(keys.begin(), upper);
info.PreviousKey = info.LastOneKey - 1;
if (info.LastOneKey < keys.size() && info.PreviousKey >= 0) {
int32_t max = keys[info.LastOneKey];
int32_t min = keys[info.PreviousKey];
if (max != min) {
info.alpha = static_cast<float>(Frame - min) / (max - min);
}
}
return info;
}
float HAnimationSystem::PlayerCallBack::SimpleBezierInterpolation(float a, float b, float alpha)
{
float u = 1.0f - alpha;
float tt = alpha * alpha;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * alpha;
float p = uuu * a + ttt * b;
return p;
}
float HAnimationSystem::PlayerCallBack::CubicBezierInterpolation(float a, float control_point_a, float control_point_b, float b, float alpha)
{
float offset = a + (b - a);
control_point_a = offset * control_point_a;
control_point_b = offset * control_point_b;
float u = 1.0f - alpha;
float tt = alpha * alpha;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * alpha;
float p = uuu * a + 3.0f * uu * alpha * control_point_a + 3.0f * u * tt * control_point_b + ttt * b;
return p;
}