mirror of
https://github.com/Show-maket/Tween.git
synced 2025-09-13 11:22:49 +00:00
Compare commits
2 Commits
3a46538696
...
cc5158eef0
Author | SHA1 | Date | |
---|---|---|---|
cc5158eef0 | |||
c4995cf075 |
84
Tween.h
84
Tween.h
@ -11,22 +11,18 @@ namespace EasingFunc {
|
||||
|
||||
// typedef float (*eFunc)(float); // тип easing-функции, как у Вас
|
||||
|
||||
// Статические данные для обёртки
|
||||
static const TimeWarp* g_warp = nullptr;
|
||||
static eFunc g_baseEasing = nullptr;
|
||||
|
||||
// Обёртка с модификатором времени (вызывается Tween'ом)
|
||||
inline float _easingWithWarp(float t) {
|
||||
if (!g_warp || !g_baseEasing) return t;
|
||||
float warpedT = g_warp->apply(t);
|
||||
return g_baseEasing(warpedT);
|
||||
inline float _easingWithWarp(float t, const TimeWarp* warp, eFunc baseEasing) {
|
||||
if (!warp || !baseEasing) return t;
|
||||
float warpedT = warp->apply(t);
|
||||
return baseEasing(warpedT);
|
||||
}
|
||||
|
||||
// Установить текущий модификатор времени
|
||||
inline eFunc withTimeWarp(eFunc easing, const TimeWarp& warp) {
|
||||
g_warp = &warp;
|
||||
g_baseEasing = easing;
|
||||
return _easingWithWarp;
|
||||
// Создать обёртку с модификатором времени
|
||||
inline std::function<float(float)> withTimeWarp(eFunc easing, const TimeWarp& warp) {
|
||||
return [easing, &warp](float t) -> float {
|
||||
return _easingWithWarp(t, &warp, easing);
|
||||
};
|
||||
}
|
||||
|
||||
}; // namespace EasingFunc
|
||||
@ -50,7 +46,6 @@ class Tween {
|
||||
private:
|
||||
static inline Tween* head = nullptr;
|
||||
static inline Tween* last = nullptr;
|
||||
static inline uint32_t frameRateTimer;
|
||||
Tween* next;
|
||||
|
||||
TweenListener* listener = nullptr;
|
||||
@ -58,6 +53,7 @@ private:
|
||||
public:
|
||||
Tween(uint16_t fps = 30) {
|
||||
setFps(fps);
|
||||
frameRateTimer = millis();
|
||||
if (Tween::head == nullptr) {
|
||||
Tween::head = this;
|
||||
}
|
||||
@ -68,9 +64,10 @@ public:
|
||||
}
|
||||
|
||||
static void tick() {
|
||||
uint32_t now = millis();
|
||||
Tween* current = Tween::head;
|
||||
while (current != nullptr) {
|
||||
current->update();
|
||||
current->update(now);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
@ -84,11 +81,13 @@ private:
|
||||
uint16_t frameTime;
|
||||
float dt;
|
||||
uint32_t oldMillis;
|
||||
uint32_t frameRateTimer;
|
||||
|
||||
float from;
|
||||
float to;
|
||||
float duration;
|
||||
EasingFunc::eFunc easing = nullptr;
|
||||
std::function<float(float)> easingFunc = nullptr;
|
||||
float progress;
|
||||
bool isPlayingF;
|
||||
bool triggerLastTick = false; // Флаг последнего тика
|
||||
@ -102,9 +101,8 @@ public:
|
||||
oldMillis = loopStartTime;
|
||||
}
|
||||
|
||||
void update() {
|
||||
if (millis() - frameRateTimer > frameTime) {
|
||||
uint32_t now = millis();
|
||||
void update(uint32_t now) {
|
||||
if (now - frameRateTimer > frameTime) {
|
||||
dt = (now - oldMillis) / 1000.0;
|
||||
oldMillis = now;
|
||||
|
||||
@ -120,7 +118,13 @@ public:
|
||||
progress = constrain(progress + dt, 0, duration);
|
||||
float normProgress = constrain(progress / duration, 0, 1);
|
||||
|
||||
current = Tween::lerp(from, to, easing(normProgress));
|
||||
if (easingFunc) {
|
||||
current = Tween::lerp(from, to, easingFunc(normProgress));
|
||||
} else if (easing) {
|
||||
current = Tween::lerp(from, to, easing(normProgress));
|
||||
} else {
|
||||
current = Tween::lerp(from, to, normProgress);
|
||||
}
|
||||
|
||||
if (progress >= duration) {
|
||||
current = to;
|
||||
@ -130,7 +134,7 @@ public:
|
||||
|
||||
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) {
|
||||
// если easing не задан, используем линейную
|
||||
easing = (easing_ != nullptr) ? easing_ : EasingFunc::easeLinear;
|
||||
easingFunc = nullptr; // сбрасываем функцию с TimeWarp
|
||||
|
||||
from = from_;
|
||||
to = to_;
|
||||
duration = durationMs / 1000.0f;
|
||||
progress = 0;
|
||||
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;
|
||||
triggerLastTick = false;
|
||||
}
|
||||
@ -218,7 +241,8 @@ class AnimationChain : public TweenListener {
|
||||
FromFunc fromFunc = nullptr;
|
||||
float to;
|
||||
uint16_t duration; // мс
|
||||
EasingFunc::eFunc easing;
|
||||
EasingFunc::eFunc easing = nullptr;
|
||||
std::function<float(float)> easingFunc = nullptr;
|
||||
};
|
||||
|
||||
private:
|
||||
@ -230,7 +254,11 @@ class AnimationChain : public TweenListener {
|
||||
|
||||
void launchCurrent() {
|
||||
const Anim& a = animations[currentIndex];
|
||||
tween.start(a.fromFunc ? a.fromFunc() : a.from, a.to, a.duration, a.easing);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
@ -245,10 +273,16 @@ class AnimationChain : public TweenListener {
|
||||
//------------------------------------------------------------------
|
||||
|
||||
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) {
|
||||
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(); }
|
||||
|
Reference in New Issue
Block a user