GlistEngine
Loading...
Searching...
No Matches
gTweenAnimation.h
Go to the documentation of this file.
1/*
2 * gTweenAnimation.h
3 *
4 * Created on: 17 Jul 2026
5 * Author: Remzi ISCI
6 */
7
8#ifndef G_TWEEN_ANIMATION_H
9#define G_TWEEN_ANIMATION_H
10
11#include <cmath>
12#include <functional>
13
14
15#ifndef M_PI_F
16#define M_PI_F 3.14159265358979323846f
17#endif
18
19#ifndef M_PI_2_F
20#define M_PI_2_F 1.57079632679489661923f
21#endif
22
23// Easing functions
24static inline float EaseLinear(float t) { return t; }
25
26static inline float EaseIn(float t) { return t * t; }
27
28static inline float EaseOut(float t) { return t * (2.0f - t); }
29
30static inline float EaseInOut(float t) {
31 return (t < 0.5f) ? (2.0f * t * t) : (-1.0f + (4.0f - 2.0f * t) * t);
32}
33
34static inline float EaseInCubic(float t) { return t * t * t; }
35
36static inline float EaseOutCubic(float t) {
37 const float tMinusOne = t - 1.0f;
38 return tMinusOne * tMinusOne * tMinusOne + 1.0f;
39}
40
41static inline float EaseInOutCubic(float t) {
42 if (t < 0.5f) return 4.0f * t * t * t;
43 const float tMinusOne = t - 1.0f;
44 const float doubleTMinusTwo = 2.0f * t - 2.0f;
45 return tMinusOne * doubleTMinusTwo * doubleTMinusTwo + 1.0f;
46}
47
48static inline float EaseInQuart(float t) { return t * t * t * t; }
49
50static inline float EaseOutQuart(float t) {
51 const float tMinusOne = t - 1.0f;
52 return 1.0f - (tMinusOne * tMinusOne * tMinusOne * tMinusOne);
53}
54
55static inline float EaseInOutQuart(float t) {
56 if (t < 0.5f) return 8.0f * t * t * t * t;
57 const float tMinusOne = t - 1.0f;
58 return 1.0f - 8.0f * tMinusOne * tMinusOne * tMinusOne * tMinusOne;
59}
60
61static inline float EaseInQuint(float t) { return t * t * t * t * t; }
62
63static inline float EaseOutQuint(float t) {
64 const float tMinusOne = t - 1.0f;
65 return 1.0f + (tMinusOne * tMinusOne * tMinusOne * tMinusOne * tMinusOne);
66}
67
68static inline float EaseInOutQuint(float t) {
69 const float tMinusOne = t - 1.0f;
70 if (t < 0.5f) return 16.0f * t * t * t * t * t;
71 return 1.0f + 16.0f * tMinusOne * tMinusOne * tMinusOne * tMinusOne * tMinusOne;
72}
73
74static inline float EaseInSine(float t) { return 1.0f - cosf(t * M_PI_2_F); }
75
76static inline float EaseOutSine(float t) { return sinf(t * M_PI_2_F); }
77
78static inline float EaseInOutSine(float t) { return 0.5f * (1.0f - cosf(M_PI_F * t)); }
79
80static inline float EaseInExpo(float t) { return (t == 0.0f) ? 0.0f : powf(2.0f, 10.0f * (t - 1.0f)); }
81
82static inline float EaseOutExpo(float t) { return (t == 1.0f) ? 1.0f : 1.0f - powf(2.0f, -10.0f * t); }
83
84static inline float EaseInOutExpo(float t) {
85 if (t == 0.0f) return 0.0f;
86 if (t == 1.0f) return 1.0f;
87 if (t < 0.5f) return 0.5f * powf(2.0f, 20.0f * t - 10.0f);
88 return 1.0f - 0.5f * powf(2.0f, -20.0f * t + 10.0f);
89}
90
91static inline float EaseInCircular(float t) { return 1.0f - sqrtf(1.0f - t * t); }
92
93static inline float EaseOutCircular(float t) {
94 const float tMinusOne = t - 1.0f;
95 return sqrtf(1.0f - (tMinusOne * tMinusOne));
96}
97
98static inline float EaseInOutCircular(float t) {
99 if (t < 0.5f) return 0.5f * (1.0f - sqrtf(1.0f - 4.0f * t * t));
100 const float tMinusOne = t - 1.0f;
101 return 0.5f * (sqrtf(1.0f - 4.0f * tMinusOne * tMinusOne) + 1.0f);
102}
103
104static inline float EaseInBack(float t) {
105 const float s = 1.70158f;
106 return t * t * ((s + 1.0f) * t - s);
107}
108
109static inline float EaseOutBack(float t) {
110 const float s = 1.70158f;
111 const float tMinusOne = t - 1.0f;
112 return tMinusOne * tMinusOne * ((s + 1.0f) * tMinusOne + s) + 1.0f;
113}
114
115static inline float EaseInOutBack(float t) {
116 const float s = 1.70158f;
117 if (t < 0.5f) return 0.5f * (t * t * ((s + 1.0f) * t - s));
118 const float tMinusOne = t - 1.0f;
119 return 0.5f * (tMinusOne * tMinusOne * ((s + 1.0f) * tMinusOne + s) + 2.0f);
120}
121
122static inline float EaseOutBounce(float t) {
123 if (t < (1.0f / 2.75f)) {
124 return 7.5625f * t * t;
125 } else if (t < (2.0f / 2.75f)) {
126 const float tTemp = t - (1.5f / 2.75f);
127 return 7.5625f * tTemp * tTemp + 0.75f;
128 } else if (t < (2.5f / 2.75f)) {
129 const float tTemp = t - (2.25f / 2.75f);
130 return 7.5625f * tTemp * tTemp + 0.9375f;
131 } else {
132 const float tTemp = t - (2.625f / 2.75f);
133 return 7.5625f * tTemp * tTemp + 0.984375f;
134 }
135}
136
137static inline float EaseInBounce(float t) { return 1.0f - EaseOutBounce(1.0f - t); }
138
139static inline float EaseInOutBounce(float t) {
140 if (t < 0.5f) return 0.5f * EaseInBounce(t * 2.0f);
141 return 0.5f * EaseOutBounce(t * 2.0f - 1.0f) + 0.5f;
142}
143
145public:
157
158 float startvalue = 0.0f;
159 float endvalue = 0.0f;
160 float duration = 0.0f;
161 float elapsedtime = 0.0f;
162 float delay = 0.0f;
163 float delaytimer = 0.0f;
164 //Target valu will be changed by tween system
165 float *targetptr = nullptr;
166 int repeatcount = 0;
167 bool isactive = false;
168 bool isfinished = false;
169 std::function<void()> FinishCallback = nullptr;
170
173
174 void Set(float startvalue, float endvalue, float duration, float *targetptr, EASE_TYPE easetype = EASE_LINEAR, std::function<void()> FinishCallback = nullptr, float delay = 0.0f);
175 void setDelay(float delay);
176 float getDelay() const;
177 bool isDelaying() const;
178 void SetEasetype(EASE_TYPE easetype);
179 void Update(float deltaTime);
180 void Destroy();
181 void Reset(int repeatCount);
182 //-1 means work forever
183 void Start(int repeatCount);
184 void Stop();
185
186private:
187 float (*easeFunc)(float) = nullptr;
189 void AssignEaseFunction();
190};
191
192#endif /* G_TWEEN_ANIMATION_H */
void Reset(int repeatCount)
std::function< void()> FinishCallback
Definition gTweenAnimation.h:169
EASE_TYPE
Definition gTweenAnimation.h:146
@ EASE_IN_EXPO
Definition gTweenAnimation.h:152
@ EASE_LINEAR
Definition gTweenAnimation.h:147
@ EASE_IN_OUT_SINE
Definition gTweenAnimation.h:151
@ EASE_OUT_CUBIC
Definition gTweenAnimation.h:148
@ EASE_OUT_BACK
Definition gTweenAnimation.h:154
@ EASE_OUT_SINE
Definition gTweenAnimation.h:151
@ EASE_IN_OUT_QUINT
Definition gTweenAnimation.h:150
@ EASE_OUT
Definition gTweenAnimation.h:147
@ EASE_IN_CUBIC
Definition gTweenAnimation.h:148
@ EASE_IN_QUART
Definition gTweenAnimation.h:149
@ EASE_IN_OUT_BACK
Definition gTweenAnimation.h:154
@ EASE_OUT_QUINT
Definition gTweenAnimation.h:150
@ EASE_IN_OUT_CUBIC
Definition gTweenAnimation.h:148
@ EASE_OUT_EXPO
Definition gTweenAnimation.h:152
@ EASE_IN_BOUNCE
Definition gTweenAnimation.h:155
@ EASE_IN_QUINT
Definition gTweenAnimation.h:150
@ EASE_OUT_BOUNCE
Definition gTweenAnimation.h:155
@ EASE_IN_OUT_BOUNCE
Definition gTweenAnimation.h:155
@ EASE_OUT_CIRCULAR
Definition gTweenAnimation.h:153
@ EASE_IN_SINE
Definition gTweenAnimation.h:151
@ EASE_IN_BACK
Definition gTweenAnimation.h:154
@ EASE_IN_CIRCULAR
Definition gTweenAnimation.h:153
@ EASE_IN_OUT
Definition gTweenAnimation.h:147
@ EASE_OUT_QUART
Definition gTweenAnimation.h:149
@ EASE_IN_OUT_QUART
Definition gTweenAnimation.h:149
@ EASE_IN_OUT_EXPO
Definition gTweenAnimation.h:152
@ EASE_IN
Definition gTweenAnimation.h:147
@ EASE_IN_OUT_CIRCULAR
Definition gTweenAnimation.h:153
bool isactive
Definition gTweenAnimation.h:167
float endvalue
Definition gTweenAnimation.h:159
float startvalue
Definition gTweenAnimation.h:158
float delay
Definition gTweenAnimation.h:162
int repeatcount
Definition gTweenAnimation.h:166
float elapsedtime
Definition gTweenAnimation.h:161
void setDelay(float delay)
float getDelay() const
float delaytimer
Definition gTweenAnimation.h:163
bool isDelaying() const
void Start(int repeatCount)
void Set(float startvalue, float endvalue, float duration, float *targetptr, EASE_TYPE easetype=EASE_LINEAR, std::function< void()> FinishCallback=nullptr, float delay=0.0f)
float * targetptr
Definition gTweenAnimation.h:165
void Update(float deltaTime)
void SetEasetype(EASE_TYPE easetype)
bool isfinished
Definition gTweenAnimation.h:168
float duration
Definition gTweenAnimation.h:160
#define M_PI_2_F
Definition gTweenAnimation.h:20
#define M_PI_F
Definition gTweenAnimation.h:16