Compare commits

...

2 Commits

Author SHA1 Message Date
cc5158eef0 common TimeWarp things was detected 2025-09-04 16:46:03 +03:00
c4995cf075 common frameRate Timer was detected 2025-09-04 16:41:56 +03:00

80
Tween.h
View File

@ -11,22 +11,18 @@ namespace EasingFunc {
// typedef float (*eFunc)(float); // тип easing-функции, как у Вас // typedef float (*eFunc)(float); // тип easing-функции, как у Вас
// Статические данные для обёртки
static const TimeWarp* g_warp = nullptr;
static eFunc g_baseEasing = nullptr;
// Обёртка с модификатором времени (вызывается Tween'ом) // Обёртка с модификатором времени (вызывается Tween'ом)
inline float _easingWithWarp(float t) { inline float _easingWithWarp(float t, const TimeWarp* warp, eFunc baseEasing) {
if (!g_warp || !g_baseEasing) return t; if (!warp || !baseEasing) return t;
float warpedT = g_warp->apply(t); float warpedT = warp->apply(t);
return g_baseEasing(warpedT); return baseEasing(warpedT);
} }
// Установить текущий модификатор времени // Создать обёртку с модификатором времени
inline eFunc withTimeWarp(eFunc easing, const TimeWarp& warp) { inline std::function<float(float)> withTimeWarp(eFunc easing, const TimeWarp& warp) {
g_warp = &warp; return [easing, &warp](float t) -> float {
g_baseEasing = easing; return _easingWithWarp(t, &warp, easing);
return _easingWithWarp; };
} }
}; // namespace EasingFunc }; // namespace EasingFunc
@ -50,7 +46,6 @@ class Tween {
private: private:
static inline Tween* head = nullptr; static inline Tween* head = nullptr;
static inline Tween* last = nullptr; static inline Tween* last = nullptr;
static inline uint32_t frameRateTimer;
Tween* next; Tween* next;
TweenListener* listener = nullptr; TweenListener* listener = nullptr;
@ -58,6 +53,7 @@ private:
public: public:
Tween(uint16_t fps = 30) { Tween(uint16_t fps = 30) {
setFps(fps); setFps(fps);
frameRateTimer = millis();
if (Tween::head == nullptr) { if (Tween::head == nullptr) {
Tween::head = this; Tween::head = this;
} }
@ -68,9 +64,10 @@ public:
} }
static void tick() { static void tick() {
uint32_t now = millis();
Tween* current = Tween::head; Tween* current = Tween::head;
while (current != nullptr) { while (current != nullptr) {
current->update(); current->update(now);
current = current->next; current = current->next;
} }
} }
@ -84,11 +81,13 @@ private:
uint16_t frameTime; uint16_t frameTime;
float dt; float dt;
uint32_t oldMillis; uint32_t oldMillis;
uint32_t frameRateTimer;
float from; float from;
float to; float to;
float duration; float duration;
EasingFunc::eFunc easing = nullptr; EasingFunc::eFunc easing = nullptr;
std::function<float(float)> easingFunc = nullptr;
float progress; float progress;
bool isPlayingF; bool isPlayingF;
bool triggerLastTick = false; // Флаг последнего тика bool triggerLastTick = false; // Флаг последнего тика
@ -102,9 +101,8 @@ public:
oldMillis = loopStartTime; oldMillis = loopStartTime;
} }
void update() { void update(uint32_t now) {
if (millis() - frameRateTimer > frameTime) { if (now - frameRateTimer > frameTime) {
uint32_t now = millis();
dt = (now - oldMillis) / 1000.0; dt = (now - oldMillis) / 1000.0;
oldMillis = now; oldMillis = now;
@ -120,7 +118,13 @@ public:
progress = constrain(progress + dt, 0, duration); progress = constrain(progress + dt, 0, duration);
float normProgress = constrain(progress / duration, 0, 1); float normProgress = constrain(progress / duration, 0, 1);
if (easingFunc) {
current = Tween::lerp(from, to, easingFunc(normProgress));
} else if (easing) {
current = Tween::lerp(from, to, easing(normProgress)); current = Tween::lerp(from, to, easing(normProgress));
} else {
current = Tween::lerp(from, to, normProgress);
}
if (progress >= duration) { if (progress >= duration) {
current = to; current = to;
@ -130,7 +134,7 @@ public:
if (listener != nullptr) listener->onTweenUpdate(*this); if (listener != nullptr) listener->onTweenUpdate(*this);
frameRateTimer = millis(); frameRateTimer = now;
} }
} }
@ -141,13 +145,32 @@ public:
void start(float from_, float to_, uint16_t durationMs, EasingFunc::eFunc easing_ = nullptr) { void start(float from_, float to_, uint16_t durationMs, EasingFunc::eFunc easing_ = nullptr) {
// если easing не задан, используем линейную // если easing не задан, используем линейную
easing = (easing_ != nullptr) ? easing_ : EasingFunc::easeLinear; easing = (easing_ != nullptr) ? easing_ : EasingFunc::easeLinear;
easingFunc = nullptr; // сбрасываем функцию с TimeWarp
from = from_; from = from_;
to = to_; to = to_;
duration = durationMs / 1000.0f; duration = durationMs / 1000.0f;
progress = 0; progress = 0;
current = from; current = from;
oldMillis = millis(); uint32_t now = millis();
oldMillis = now;
frameRateTimer = now;
isPlayingF = true;
triggerLastTick = false;
}
void start(float from_, float to_, uint16_t durationMs, std::function<float(float)> easingFunc_) {
easing = nullptr; // сбрасываем обычную функцию
easingFunc = easingFunc_;
from = from_;
to = to_;
duration = durationMs / 1000.0f;
progress = 0;
current = from;
uint32_t now = millis();
oldMillis = now;
frameRateTimer = now;
isPlayingF = true; isPlayingF = true;
triggerLastTick = false; triggerLastTick = false;
} }
@ -218,7 +241,8 @@ class AnimationChain : public TweenListener {
FromFunc fromFunc = nullptr; FromFunc fromFunc = nullptr;
float to; float to;
uint16_t duration; // мс uint16_t duration; // мс
EasingFunc::eFunc easing; EasingFunc::eFunc easing = nullptr;
std::function<float(float)> easingFunc = nullptr;
}; };
private: private:
@ -230,8 +254,12 @@ class AnimationChain : public TweenListener {
void launchCurrent() { void launchCurrent() {
const Anim& a = animations[currentIndex]; const Anim& a = animations[currentIndex];
if (a.easingFunc) {
tween.start(a.fromFunc ? a.fromFunc() : a.from, a.to, a.duration, a.easingFunc);
} else {
tween.start(a.fromFunc ? a.fromFunc() : a.from, a.to, a.duration, a.easing); tween.start(a.fromFunc ? a.fromFunc() : a.from, a.to, a.duration, a.easing);
} }
}
public: public:
float current = 0; // актуальное значение наружу float current = 0; // актуальное значение наружу
@ -245,10 +273,16 @@ class AnimationChain : public TweenListener {
//------------------------------------------------------------------ //------------------------------------------------------------------
void addAnim(float from, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) { void addAnim(float from, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) {
animations.push_back({from, {}, to, duration, easing}); animations.push_back({from, {}, to, duration, easing, nullptr});
} }
void addAnim(FromFunc fromF, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) { void addAnim(FromFunc fromF, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) {
animations.push_back({0, fromF, to, duration, easing}); animations.push_back({0, fromF, to, duration, easing, nullptr});
}
void addAnim(float from, float to, uint16_t duration, std::function<float(float)> easingFunc) {
animations.push_back({from, {}, to, duration, nullptr, easingFunc});
}
void addAnim(FromFunc fromF, float to, uint16_t duration, std::function<float(float)> easingFunc) {
animations.push_back({0, fromF, to, duration, nullptr, easingFunc});
} }
void clear() { animations.clear(); } void clear() { animations.clear(); }