-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimation.h
More file actions
267 lines (241 loc) · 6.53 KB
/
animation.h
File metadata and controls
267 lines (241 loc) · 6.53 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* @file animation.h
* @author Charliechen114514 (chengh1922@mails.jlu.edu.cn)
* @brief Abstract Animation Interface
* @version 0.1
* @date 2026-02-28
*
* @copyright Copyright (c) 2026
*
* @details
* Defines the base animation interface for all animation types.
* Provides common animation states, direction control, and lifecycle methods.
*
* @ingroup ui_components
*/
#pragma once
#include "base/weak_ptr/weak_ptr.h"
#include "export.h"
#include <QObject>
#include <QTimer>
namespace cf::ui::components {
/**
* @brief Abstract animation base class.
*
* @details Provides the common interface for all animations including
* state management, direction control, and lifecycle methods.
*
* @since 0.1
* @ingroup ui_components
*/
class CF_UI_EXPORT ICFAbstractAnimation : public QObject {
Q_OBJECT
public:
friend class ICFAnimationManagerFactory;
explicit ICFAbstractAnimation(QObject* parent = nullptr);
/**
* @brief Animation states.
*
* @since 0.1
* @ingroup ui_components
*/
enum class State { Idle, Running, Paused, Finished };
Q_ENUM(State)
/**
* @brief Animation playback direction.
*
* @since 0.1
* @ingroup ui_components
*/
enum class Direction { Forward, Backward };
Q_ENUM(Direction)
/**
* @brief Starts the animation in the specified direction.
*
* @param[in] dir Direction to play the animation (default: Forward).
*
* @throws None
* @note If already running, this may restart the animation.
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual void start(Direction dir = Direction::Forward) = 0;
/**
* @brief Pauses the animation.
*
* @throws None
* @note Does nothing if the animation is not running.
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual void pause() = 0;
/**
* @brief Stops the animation and resets to initial state.
*
* @throws None
* @note Emits the stopped signal.
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual void stop() = 0;
/**
* @brief Reverses the animation direction.
*
* @details Stops the current session and plays in opposite direction.
*
* @throws None
* @note Emits the reversed signal.
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual void reverse() = 0;
/**
* @brief Updates the animation state.
*
* @details Called every frame by the animator. Subclasses implement
* specific interpolation logic.
*
* @param[in] dt Time interval since the last call (milliseconds).
*
* @return true if animation continues, false if finished.
*
* @throws None
* @note Implementations should update m_progress and apply
* the interpolated value to the target.
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual bool tick(int dt) = 0;
/**
* @brief Gets a weak pointer to this animation.
*
* @details Each concrete animation class must implement this using
* its WeakPtrFactory.
*
* @return WeakPtr to this animation.
*
* @throws None
* @note None
* @warning None
* @since 0.1
* @ingroup ui_components
*/
virtual cf::WeakPtr<ICFAbstractAnimation> GetWeakPtr() = 0;
/**
* @brief Gets the enabled state of the animation.
*
* @return true if animation is enabled, false otherwise.
*
* @throws None
* @note None
* @warning None
* @since 0.1
* @ingroup ui_components
*/
bool getEnabled() const { return enabled; }
/**
* @brief Set the target FPS for this animation.
*
* @details Sets the desired frame rate for this animation's timer updates.
* This affects the tick interval for this animation instance.
*
* @param[in] fps Target frames per second (e.g., 60.0f).
*
* @throws None
* @note Default is 60.0f. Takes effect on the next animation start.
* @warning None
* @since 0.1
* @ingroup ui_components
*
* @code
* animation->setTargetFps(30.0f); // 30 FPS (lower CPU usage)
* @endcode
*/
void setTargetFps(float fps);
/**
* @brief Calculate the timer interval based on target FPS.
*
* @details Returns the interval in milliseconds for the timer
* based on the current target FPS setting.
*
* @return Timer interval in milliseconds.
*
* @throws None
* @note None
* @warning None
* @since 0.1
* @ingroup ui_components
*/
int calculateInterval() const;
signals:
/**
* @brief Signal emitted when animation starts.
*
* @since 0.1
* @ingroup ui_components
*/
void started();
/**
* @brief Signal emitted when animation is paused.
*
* @since 0.1
* @ingroup ui_components
*/
void paused();
/**
* @brief Signal emitted when animation stops.
*
* @since 0.1
* @ingroup ui_components
*/
void stopped();
/**
* @brief Signal emitted when animation reverses direction.
*
* @since 0.1
* @ingroup ui_components
*/
void reversed();
/**
* @brief Signal emitted when animation finishes.
*
* @since 0.1
* @ingroup ui_components
*/
void finished();
/**
* @brief Signal emitted when animation progress changes.
*
* @param[in] progress Current progress value (0.0 to 1.0).
*
* @since 0.1
* @ingroup ui_components
*/
void progressChanged(float progress);
protected:
QTimer* driven_internal_timer{nullptr};
float m_progress = 0.0f;
State m_state = State::Idle;
/// Target FPS for this animation (default 60.0f)
float targetFps_ = 60.0f;
/**
* @brief Sets the enabled state of the animation.
*
* @param[in] enabled true to enable, false to disable.
*
* @throws None
* @note None
* @warning None
* @since 0.1
* @ingroup ui_components
*/
void setEnabled(bool enabled) { this->enabled = enabled; }
private:
bool enabled;
};
} // namespace cf::ui::components