载入中...
搜索中...
未找到
Tween.h
浏览该文件的文档.
1#pragma once
2
3#include <string>
4#include <unordered_map>
5#include <vector>
6
7namespace eve::animation {
8
9class Animation;
10
21class Tween {
22public:
23 explicit Tween(float duration = 1.f);
24 ~Tween();
25
26 Tween(const Tween &) = delete;
27 Tween &operator=(const Tween &) = delete;
28
29 void setFrom(const std::string &name, float value);
30 void setTo(const std::string &name, float value);
32 void setDelta(const std::string &name, float delta);
33
34 void setFromAngle(const std::string &name, float radians);
35 void setToAngle(const std::string &name, float radians);
36 void setDeltaAngle(const std::string &name, float deltaRadians);
37
38 bool has(const std::string &name) const;
39 float get(const std::string &name) const;
40 float getFrom(const std::string &name) const;
41 float getTo(const std::string &name) const;
42 float getDelta(const std::string &name) const;
43
44 void setDuration(float seconds);
45 float getDuration() const { return duration_; }
46 void setDelay(float seconds);
47 float getDelay() const { return delay_; }
48 void setEase(const std::string &kind);
49 std::string getEase() const { return ease_; }
50
55 void setRepeat(int count);
56 int getRepeat() const { return repeat_; }
57 void setYoyo(bool enabled) { yoyo_ = enabled; }
58 bool getYoyo() const { return yoyo_; }
59
60 void start();
61 void pause();
62 void resume();
63 void stop();
64 void reset();
65
66 bool isRunning() const { return state_ == State::Running; }
67 bool isPaused() const { return state_ == State::Paused; }
68 bool isFinished() const { return state_ == State::Finished; }
69 bool isStopped() const { return state_ == State::Idle || state_ == State::Stopped; }
70 bool isDelayed() const { return state_ == State::Delayed; }
71 bool isActive() const {
72 return state_ == State::Delayed || state_ == State::Running || state_ == State::Paused;
73 }
74
76 float getElapsed() const { return elapsed_; }
78 float getProgress() const;
80 float getEasedProgress() const;
81
86 bool update(float dt);
87
89 float evaluate(const std::string &name, float t) const;
90
92 int getPropertyCount() const { return static_cast<int>(order_.size()); }
93 std::string getPropertyName(int index) const;
94
95private:
96 friend class Animation;
97
98 enum class State { Idle, Delayed, Running, Paused, Finished, Stopped };
99
100 enum class EndMode { Absolute, Delta };
101
102 struct Track {
103 float from = 0.f;
104 float to = 0.f;
105 float delta = 0.f;
106 float current = 0.f;
107 bool hasFrom = false;
108 EndMode endMode = EndMode::Absolute;
109 bool isAngle = false;
110 bool resolved = false;
111 };
112
113 void setOwner(Animation *owner) { owner_ = owner; }
114 Animation *owner() const { return owner_; }
115 Track &ensureTrack(const std::string &name);
116 const Track *findTrack(const std::string &name) const;
117 void resolveTracks();
118 void applyProgress(float linearT);
119 void finishCycle();
120 static float ease(float t, const std::string &kind);
121 static float lerpAngle(float a, float b, float t);
122 static float clamp01(float t);
123
124 Animation *owner_ = nullptr;
125 float duration_ = 1.f;
126 float delay_ = 0.f;
127 float delayLeft_ = 0.f;
128 float elapsed_ = 0.f;
129 int repeat_ = 1;
130 int played_ = 0;
131 bool yoyo_ = false;
132 bool reverse_ = false;
133 std::string ease_ = "linear";
134 State state_ = State::Idle;
135 std::unordered_map<std::string, Track> tracks_;
136 std::vector<std::string> order_;
137};
138
139} // namespace eve::animation
Tok kind
std::string value
uint32_t a
uint32_t b
const char * name
Definition RockMesh.cpp:21
bool enabled
Animation module — tween factory + 2D sprite-sheet / Spine + 3D skeletal playback (player / state mac...
Definition Animation.h:53
Property tween — interpolates named float properties from → to over time.
Definition Tween.h:21
bool isFinished() const
Definition Tween.h:68
void setFrom(const std::string &name, float value)
Definition Tween.cpp:34
bool isRunning() const
Definition Tween.h:66
void setDelay(float seconds)
Definition Tween.cpp:115
int getRepeat() const
Definition Tween.h:56
void setDeltaAngle(const std::string &name, float deltaRadians)
Definition Tween.cpp:74
bool getYoyo() const
Definition Tween.h:58
void setToAngle(const std::string &name, float radians)
Definition Tween.cpp:66
int getPropertyCount() const
Names of all property tracks (stable order = insertion order).
Definition Tween.h:92
void setEase(const std::string &kind)
Definition Tween.cpp:120
float getDuration() const
Definition Tween.h:45
bool isPaused() const
Definition Tween.h:67
float getEasedProgress() const
Eased progress used for interpolation, [0,1].
Definition Tween.cpp:196
bool update(float dt)
Advance by dt seconds. Returns true while the tween is still active (delayed / running / paused).
Definition Tween.cpp:259
void setFromAngle(const std::string &name, float radians)
Definition Tween.cpp:58
float get(const std::string &name) const
Definition Tween.cpp:84
void setYoyo(bool enabled)
Definition Tween.h:57
bool isDelayed() const
Definition Tween.h:70
bool isActive() const
Definition Tween.h:71
float getProgress() const
Linear progress in the current cycle, [0,1].
Definition Tween.cpp:191
void setDuration(float seconds)
Definition Tween.cpp:110
std::string getPropertyName(int index) const
Definition Tween.cpp:306
float evaluate(const std::string &name, float t) const
Sample property at linear t in [0,1] using current from/to (no state change).
Definition Tween.cpp:293
float getFrom(const std::string &name) const
Definition Tween.cpp:90
float getDelay() const
Definition Tween.h:47
void setRepeat(int count)
Play count: 1 = once (default), N = N cycles, -1 = infinite. Values < -1 are clamped to -1.
Definition Tween.cpp:126
void setDelta(const std::string &name, float delta)
Relative change: end = start + delta (resolved when start() runs).
Definition Tween.cpp:50
float getTo(const std::string &name) const
Definition Tween.cpp:96
void setTo(const std::string &name, float value)
Definition Tween.cpp:42
std::string getEase() const
Definition Tween.h:49
Tween(const Tween &)=delete
float getDelta(const std::string &name) const
Definition Tween.cpp:103
friend class Animation
Definition Tween.h:96
bool has(const std::string &name) const
Definition Tween.cpp:82
float getElapsed() const
Elapsed time in the current cycle (excludes delay).
Definition Tween.h:76
bool isStopped() const
Definition Tween.h:69
Tween & operator=(const Tween &)=delete