Compare commits

..

4 Commits

Author SHA1 Message Date
7d25037b83 Update Tween.h 2026-02-18 16:40:11 +03:00
7f22882900 forceUpdate methods 2025-12-24 17:26:26 +03:00
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

273
Tween.h
View File

@ -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,14 +46,19 @@ class Tween {
private:
static inline Tween* head = nullptr;
static inline Tween* last = nullptr;
static inline uint32_t frameRateTimer;
Tween* next;
Tween* next = nullptr;
TweenListener* listener = nullptr;
public:
Tween(const Tween&) = delete;
Tween& operator=(const Tween&) = delete;
Tween(Tween&& other) noexcept;
Tween& operator=(Tween&& other) noexcept;
Tween(uint16_t fps = 30) {
setFps(fps);
frameRateTimer = millis();
if (Tween::head == nullptr) {
Tween::head = this;
}
@ -68,9 +69,10 @@ public:
}
static void tick() {
uint32_t now = millis();
Tween* current = Tween::head;
while (current != nullptr) {
current->update();
current->update(now);
current = current->next;
}
}
@ -81,14 +83,30 @@ public:
///////
private:
void unlinkFromList() {
if (Tween::head == nullptr) return;
if (this == Tween::head) {
Tween::head = this->next;
if (this == Tween::last) Tween::last = nullptr;
} else {
Tween* prev = Tween::head;
while (prev && prev->next != this) prev = prev->next;
if (prev) prev->next = this->next;
if (this == Tween::last) Tween::last = prev;
}
this->next = nullptr;
}
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 +120,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 +137,13 @@ public:
progress = constrain(progress + dt, 0, duration);
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));
} else {
current = Tween::lerp(from, to, normProgress);
}
if (progress >= duration) {
current = to;
@ -130,7 +153,7 @@ public:
if (listener != nullptr) listener->onTweenUpdate(*this);
frameRateTimer = millis();
frameRateTimer = now;
}
}
@ -141,13 +164,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;
}
@ -161,6 +203,92 @@ public:
triggerLastTick = false;
}
// =========================================================
// Force Update API - принудительное обновление параметров
// =========================================================
// Принудительное обновление начальной точки (from) без сброса прогресса
// Пересчитывает current на основе нового from и текущего прогресса
void forceUpdateFrom(float newFrom) {
from = newFrom;
if (isPlayingF && duration > 0.0f) {
// Пересчитываем current на основе нового from и текущего прогресса
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));
} else {
current = Tween::lerp(from, to, normProgress);
}
} else {
// Если не играет или duration = 0, просто обновляем from
current = from;
}
}
// Принудительное обновление конечной точки (to) без сброса прогресса
// Пересчитывает current на основе нового to и текущего прогресса
void forceUpdateTo(float newTo) {
to = newTo;
if (isPlayingF && duration > 0.0f) {
// Пересчитываем current на основе нового to и текущего прогресса
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));
} else {
current = Tween::lerp(from, to, normProgress);
}
} else {
// Если не играет или duration = 0, просто обновляем to и current
current = to;
}
}
// Принудительное обновление обеих точек (from и to) без сброса прогресса
// Пересчитывает current на основе новых точек и текущего прогресса
void forceUpdateFromTo(float newFrom, float newTo) {
from = newFrom;
to = newTo;
if (isPlayingF && duration > 0.0f) {
// Пересчитываем current на основе новых точек и текущего прогресса
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));
} else {
current = Tween::lerp(from, to, normProgress);
}
} else {
// Если не играет или duration = 0, устанавливаем current в from
current = from;
}
}
// Принудительное обновление прогресса (progress) без сброса других параметров
// Пересчитывает current на основе нового прогресса
// newProgress - абсолютное значение прогресса (0..duration)
void forceUpdateProgress(float newProgress) {
progress = constrain(newProgress, 0, duration);
if (duration > 0.0f) {
// Пересчитываем current на основе нового прогресса
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));
} else {
current = Tween::lerp(from, to, normProgress);
}
} else {
// Если duration = 0, устанавливаем current в to
current = to;
}
}
bool isPlaying() const {
return isPlayingF || triggerLastTick; // Учитываем последний тик
}
@ -191,21 +319,83 @@ public:
static float lerp(float a, float b, float t) {
return a + (b - a) * t;
}
private:
// int clamp(int value, int a, int b) {
// if (a > b) {
// a ^= b;
// b ^= a;
// a ^= b;
// }
// if (value < a) return a;
// else if (value > b) return b;
// else return value;
// }
};
//----------------------------------------------------------------------
// TWEEN MOVE SEMANTICS
//----------------------------------------------------------------------
inline Tween::Tween(Tween&& other) noexcept {
unlinkFromList();
next = other.next;
listener = other.listener;
frameTime = other.frameTime;
dt = other.dt;
oldMillis = other.oldMillis;
frameRateTimer = other.frameRateTimer;
from = other.from;
to = other.to;
duration = other.duration;
easing = other.easing;
easingFunc = std::move(other.easingFunc);
progress = other.progress;
isPlayingF = other.isPlayingF;
triggerLastTick = other.triggerLastTick;
current = other.current;
other.next = nullptr;
other.listener = nullptr;
other.easing = nullptr;
other.easingFunc = nullptr;
other.isPlayingF = false;
other.triggerLastTick = false;
if (Tween::head == &other) Tween::head = this;
else {
Tween* prev = Tween::head;
while (prev && prev->next != &other) prev = prev->next;
if (prev) prev->next = this;
}
if (Tween::last == &other) Tween::last = this;
}
inline Tween& Tween::operator=(Tween&& other) noexcept {
if (this != &other) {
unlinkFromList();
next = other.next;
listener = other.listener;
frameTime = other.frameTime;
dt = other.dt;
oldMillis = other.oldMillis;
frameRateTimer = other.frameRateTimer;
from = other.from;
to = other.to;
duration = other.duration;
easing = other.easing;
easingFunc = std::move(other.easingFunc);
progress = other.progress;
isPlayingF = other.isPlayingF;
triggerLastTick = other.triggerLastTick;
current = other.current;
other.next = nullptr;
other.listener = nullptr;
other.easing = nullptr;
other.easingFunc = nullptr;
other.isPlayingF = false;
other.triggerLastTick = false;
if (Tween::head == &other) Tween::head = this;
else {
Tween* prev = Tween::head;
while (prev && prev->next != &other) prev = prev->next;
if (prev) prev->next = this;
}
if (Tween::last == &other) Tween::last = this;
}
return *this;
}
//----------------------------------------------------------------------
// ANIMATION CHAIN
@ -218,7 +408,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,8 +421,12 @@ class AnimationChain : public TweenListener {
void launchCurrent() {
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);
}
}
public:
float current = 0; // актуальное значение наружу
@ -245,10 +440,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(); }