mirror of
https://github.com/Show-maket/Tween.git
synced 2026-09-22 12:59:35 +00:00
Compare commits
11 Commits
1fda2daaec
...
car-v4.3.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d25037b83 | |||
| 7f22882900 | |||
| cc5158eef0 | |||
| c4995cf075 | |||
| 3a46538696 | |||
| 18e03b1f26 | |||
| cfc67b8138 | |||
| 77e4385c14 | |||
| c6c72fcca8 | |||
| 9635922022 | |||
| 663b10f898 |
139
Easing.h
139
Easing.h
@ -1,21 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace EasingFunc {
|
namespace EasingFunc {
|
||||||
typedef float(*eFunc)(float);
|
typedef float(*eFunc)(float);
|
||||||
|
|
||||||
static float easeLinear(float t) {
|
static float easeLinear(float t) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInSine(float t) {
|
static float easeInSine(float t) {
|
||||||
return sin(1.5707963 * t);
|
return sinf((t * M_PI) / 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutSine(float t) {
|
static float easeOutSine(float t) {
|
||||||
return 1 + sin(1.5707963 * (--t));
|
return sinf((t * M_PI) / 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutSine(float t) {
|
static float easeInOutSine(float t) {
|
||||||
return 0.5 * (1 + sin(3.1415926 * (t - 0.5)));
|
return -(cosf(M_PI * t) - 1.0f) / 2.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInQuad(float t) {
|
static float easeInQuad(float t) {
|
||||||
@ -23,11 +25,11 @@ namespace EasingFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutQuad(float t) {
|
static float easeOutQuad(float t) {
|
||||||
return t * (2 - t);
|
return 1 - (1 - t) * (1 - t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutQuad(float t) {
|
static float easeInOutQuad(float t) {
|
||||||
return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1;
|
return t < 0.5f ? 2 * t * t : 1 - powf(-2 * t + 2, 2) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInCubic(float t) {
|
static float easeInCubic(float t) {
|
||||||
@ -35,138 +37,127 @@ namespace EasingFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutCubic(float t) {
|
static float easeOutCubic(float t) {
|
||||||
return 1 + (--t) * t * t;
|
return 1 - powf(1 - t, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutCubic(float t) {
|
static float easeInOutCubic(float t) {
|
||||||
return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
|
return t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInQuart(float t) {
|
static float easeInQuart(float t) {
|
||||||
t *= t;
|
return t * t * t * t;
|
||||||
return t * t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutQuart(float t) {
|
static float easeOutQuart(float t) {
|
||||||
t = (--t) * t;
|
return 1 - powf(1 - t, 4);
|
||||||
return 1 - t * t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutQuart(float t) {
|
static float easeInOutQuart(float t) {
|
||||||
if (t < 0.5) {
|
return t < 0.5f ? 8 * powf(t, 4) : 1 - powf(-2 * t + 2, 4) / 2;
|
||||||
t *= t;
|
|
||||||
return 8 * t * t;
|
|
||||||
} else {
|
|
||||||
t = (--t) * t;
|
|
||||||
return 1 - 8 * t * t;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInQuint(float t) {
|
static float easeInQuint(float t) {
|
||||||
float t2 = t * t;
|
return powf(t, 5);
|
||||||
return t * t2 * t2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutQuint(float t) {
|
static float easeOutQuint(float t) {
|
||||||
float t2 = (--t) * t;
|
return 1 - powf(1 - t, 5);
|
||||||
return 1 + t * t2 * t2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutQuint(float t) {
|
static float easeInOutQuint(float t) {
|
||||||
float t2;
|
return t < 0.5f ? 16 * powf(t, 5) : 1 - powf(-2 * t + 2, 5) / 2;
|
||||||
if (t < 0.5) {
|
|
||||||
t2 = t * t;
|
|
||||||
return 16 * t * t2 * t2;
|
|
||||||
} else {
|
|
||||||
t2 = (--t) * t;
|
|
||||||
return 1 + 16 * t * t2 * t2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInExpo(float t) {
|
static float easeInExpo(float t) {
|
||||||
return (pow(2, 8 * t) - 1) / 255;
|
return t == 0 ? 0 : powf(2, 10 * t - 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutExpo(float t) {
|
static float easeOutExpo(float t) {
|
||||||
return 1 - pow(2, -8 * t);
|
return t == 1 ? 1 : 1 - powf(2, -10 * t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutExpo(float t) {
|
static float easeInOutExpo(float t) {
|
||||||
if (t < 0.5) {
|
if (t == 0) return 0;
|
||||||
return (pow(2, 16 * t) - 1) / 510;
|
if (t == 1) return 1;
|
||||||
} else {
|
return t < 0.5f ? powf(2, 20 * t - 10) / 2 : (2 - powf(2, -20 * t + 10)) / 2;
|
||||||
return 1 - 0.5 * pow(2, -16 * (t - 0.5));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInCirc(float t) {
|
static float easeInCirc(float t) {
|
||||||
return 1 - sqrt(1 - t);
|
return 1 - sqrtf(1 - t * t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutCirc(float t) {
|
static float easeOutCirc(float t) {
|
||||||
return sqrt(t);
|
return sqrtf(1 - powf(t - 1, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutCirc(float t) {
|
static float easeInOutCirc(float t) {
|
||||||
if (t < 0.5) {
|
return t < 0.5f
|
||||||
return (1 - sqrt(1 - 2 * t)) * 0.5;
|
? (1 - sqrtf(1 - 4 * t * t)) / 2
|
||||||
} else {
|
: (sqrtf(1 - powf(-2 * t + 2, 2)) + 1) / 2;
|
||||||
return (1 + sqrt(2 * t - 1)) * 0.5;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInBack(float t) {
|
static float easeInBack(float t) {
|
||||||
return t * t * (2.70158 * t - 1.70158);
|
constexpr float c1 = 1.70158f;
|
||||||
|
constexpr float c3 = c1 + 1;
|
||||||
|
return c3 * t * t * t - c1 * t * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutBack(float t) {
|
static float easeOutBack(float t) {
|
||||||
return 1 + (--t) * t * (2.70158 * t + 1.70158);
|
constexpr float c1 = 1.70158f;
|
||||||
|
constexpr float c3 = c1 + 1;
|
||||||
|
return 1 + c3 * powf(t - 1, 3) + c1 * powf(t - 1, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutBack(float t) {
|
static float easeInOutBack(float t) {
|
||||||
if (t < 0.5) {
|
constexpr float c1 = 1.70158f;
|
||||||
return t * t * (7 * t - 2.5) * 2;
|
constexpr float c2 = c1 * 1.525f;
|
||||||
} else {
|
return t < 0.5f
|
||||||
return 1 + (--t) * t * 2 * (7 * t + 2.5);
|
? powf(2 * t, 2) * ((c2 + 1) * 2 * t - c2) / 2
|
||||||
}
|
: (powf(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInElastic(float t) {
|
static float easeInElastic(float t) {
|
||||||
float t2 = t * t;
|
constexpr float c4 = (2 * M_PI) / 3;
|
||||||
return t2 * t2 * sin(t * PI * 4.5);
|
return t == 0 ? 0 : t == 1 ? 1 : -powf(2, 10 * t - 10) * sinf((t * 10 - 10.75f) * c4);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeOutElastic(float t) {
|
static float easeOutElastic(float t) {
|
||||||
float t2 = (t - 1) * (t - 1);
|
constexpr float c4 = (2 * M_PI) / 3;
|
||||||
return 1 - t2 * t2 * cos(t * PI * 4.5);
|
return t == 0 ? 0 : t == 1 ? 1 : powf(2, -10 * t) * sinf((t * 10 - 0.75f) * c4) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutElastic(float t) {
|
static float easeInOutElastic(float t) {
|
||||||
float t2;
|
constexpr float c5 = (2 * M_PI) / 4.5f;
|
||||||
if (t < 0.45) {
|
if (t == 0) return 0;
|
||||||
t2 = t * t;
|
if (t == 1) return 1;
|
||||||
return 8 * t2 * t2 * sin(t * PI * 9);
|
return t < 0.5f
|
||||||
} else if (t < 0.55) {
|
? -(powf(2, 20 * t - 10) * sinf((20 * t - 11.125f) * c5)) / 2
|
||||||
return 0.5 + 0.75 * sin(t * PI * 4);
|
: (powf(2, -20 * t + 10) * sinf((20 * t - 11.125f) * c5)) / 2 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutBounce(float t) {
|
||||||
|
if (t < 1 / 2.75f) {
|
||||||
|
return 7.5625f * t * t;
|
||||||
|
} else if (t < 2 / 2.75f) {
|
||||||
|
t -= 1.5f / 2.75f;
|
||||||
|
return 7.5625f * t * t + 0.75f;
|
||||||
|
} else if (t < 2.5f / 2.75f) {
|
||||||
|
t -= 2.25f / 2.75f;
|
||||||
|
return 7.5625f * t * t + 0.9375f;
|
||||||
} else {
|
} else {
|
||||||
t2 = (t - 1) * (t - 1);
|
t -= 2.625f / 2.75f;
|
||||||
return 1 - 8 * t2 * t2 * sin(t * PI * 9);
|
return 7.5625f * t * t + 0.984375f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInBounce(float t) {
|
static float easeInBounce(float t) {
|
||||||
return pow(2, 6 * (t - 1)) * abs(sin(t * PI * 3.5));
|
return 1 - easeOutBounce(1 - t);
|
||||||
}
|
|
||||||
|
|
||||||
static float easeOutBounce(float t) {
|
|
||||||
return 1 - pow(2, -6 * t) * abs(cos(t * PI * 3.5));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static float easeInOutBounce(float t) {
|
static float easeInOutBounce(float t) {
|
||||||
if (t < 0.5) {
|
return t < 0.5f
|
||||||
return 8 * pow(2, 8 * (t - 1)) * abs(sin(t * PI * 7));
|
? (1 - easeOutBounce(1 - 2 * t)) / 2
|
||||||
} else {
|
: (1 + easeOutBounce(2 * t - 1)) / 2;
|
||||||
return 1 - 8 * pow(2, -8 * t) * abs(sin(t * PI * 7));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
104
TimeWarp.h
Normal file
104
TimeWarp.h
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <utility> // std::pair
|
||||||
|
#include <cstdlib> // rand()
|
||||||
|
#include <algorithm> // std::sort
|
||||||
|
|
||||||
|
class TimeWarp {
|
||||||
|
public:
|
||||||
|
struct Segment {
|
||||||
|
float t_start, t_end; // входной интервал
|
||||||
|
float y_start, y_end; // выходной интервал
|
||||||
|
bool is_plateau;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Segment> segments;
|
||||||
|
|
||||||
|
public:
|
||||||
|
TimeWarp(int numPlateaus, float minWidth, float maxWidth, unsigned int seed = 0) {
|
||||||
|
if (seed != 0) std::srand(seed);
|
||||||
|
|
||||||
|
std::vector<std::pair<float, float>> plateaus;
|
||||||
|
|
||||||
|
int maxTries = 1000;
|
||||||
|
int tries = 0;
|
||||||
|
float totalPlateauWidth = 0;
|
||||||
|
|
||||||
|
// Сгенерировать непересекающиеся плато
|
||||||
|
while ((int)plateaus.size() < numPlateaus && tries++ < maxTries) {
|
||||||
|
float width = randRange(minWidth, maxWidth);
|
||||||
|
float start = randRange(0.0f, 1.0f - width);
|
||||||
|
float end = start + width;
|
||||||
|
|
||||||
|
bool overlaps = false;
|
||||||
|
for (auto& p : plateaus) {
|
||||||
|
if (!(end <= p.first || start >= p.second)) {
|
||||||
|
overlaps = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!overlaps) {
|
||||||
|
plateaus.emplace_back(start, end);
|
||||||
|
totalPlateauWidth += width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Сортировать по началу
|
||||||
|
std::sort(plateaus.begin(), plateaus.end());
|
||||||
|
|
||||||
|
// Собрать сегменты
|
||||||
|
float currentT = 0.0f;
|
||||||
|
float currentY = 0.0f;
|
||||||
|
float dynamicSpan = 1.0f - totalPlateauWidth;
|
||||||
|
|
||||||
|
for (auto& p : plateaus) {
|
||||||
|
float start = p.first;
|
||||||
|
float end = p.second;
|
||||||
|
|
||||||
|
// Рамп перед плато
|
||||||
|
if (start > currentT) {
|
||||||
|
float t0 = currentT;
|
||||||
|
float t1 = start;
|
||||||
|
float ratio = (t1 - t0) / dynamicSpan;
|
||||||
|
float y1 = currentY + ratio;
|
||||||
|
segments.push_back({ t0, t1, currentY, y1, false });
|
||||||
|
currentY = y1;
|
||||||
|
currentT = t1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Плато
|
||||||
|
float width = end - start;
|
||||||
|
segments.push_back({ start, end, currentY, currentY, true });
|
||||||
|
currentT = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Рамп после последнего плато
|
||||||
|
if (currentT < 1.0f) {
|
||||||
|
float t0 = currentT;
|
||||||
|
float t1 = 1.0f;
|
||||||
|
float ratio = (t1 - t0) / dynamicSpan;
|
||||||
|
float y1 = currentY + ratio;
|
||||||
|
segments.push_back({ t0, t1, currentY, y1, false });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float apply(float t) const {
|
||||||
|
for (const auto& seg : segments) {
|
||||||
|
if (t >= seg.t_start && t <= seg.t_end) {
|
||||||
|
if (seg.is_plateau) {
|
||||||
|
return seg.y_start;
|
||||||
|
} else {
|
||||||
|
float localT = (t - seg.t_start) / (seg.t_end - seg.t_start);
|
||||||
|
return seg.y_start + localT * (seg.y_end - seg.y_start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1.0f; // если что-то пошло не так
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
float randRange(float minVal, float maxVal) {
|
||||||
|
return minVal + (std::rand() / (float)RAND_MAX) * (maxVal - minVal);
|
||||||
|
}
|
||||||
|
};
|
||||||
464
Tween.h
464
Tween.h
@ -1,17 +1,64 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Easing.h"
|
#include "Easing.h"
|
||||||
|
#include "TimeWarp.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// EASING HELPERS
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
namespace EasingFunc {
|
||||||
|
|
||||||
|
// typedef float (*eFunc)(float); // тип easing-функции, как у Вас
|
||||||
|
|
||||||
|
// Обёртка с модификатором времени (вызывается Tween'ом)
|
||||||
|
inline float _easingWithWarp(float t, const TimeWarp* warp, eFunc baseEasing) {
|
||||||
|
if (!warp || !baseEasing) return t;
|
||||||
|
float warpedT = warp->apply(t);
|
||||||
|
return baseEasing(warpedT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Создать обёртку с модификатором времени
|
||||||
|
inline std::function<float(float)> withTimeWarp(eFunc easing, const TimeWarp& warp) {
|
||||||
|
return [easing, &warp](float t) -> float {
|
||||||
|
return _easingWithWarp(t, &warp, easing);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace EasingFunc
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// LISTENER‑ИНТЕРФЕЙС
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
class Tween; // forward
|
||||||
|
|
||||||
|
class TweenListener {
|
||||||
|
public:
|
||||||
|
virtual void onTweenFinished(Tween& tween) = 0;
|
||||||
|
virtual void onTweenUpdate(Tween& tween) = 0;
|
||||||
|
virtual ~TweenListener() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// TWEEN
|
||||||
|
//----------------------------------------------------------------------
|
||||||
class Tween {
|
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 = nullptr;
|
||||||
Tween* next;
|
|
||||||
|
TweenListener* listener = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Tween(uint16_t fps) {
|
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);
|
setFps(fps);
|
||||||
|
frameRateTimer = millis();
|
||||||
if (Tween::head == nullptr) {
|
if (Tween::head == nullptr) {
|
||||||
Tween::head = this;
|
Tween::head = this;
|
||||||
}
|
}
|
||||||
@ -22,26 +69,47 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setListener(TweenListener* l) { listener = l; }
|
||||||
|
TweenListener* getListener() const { return listener; }
|
||||||
|
|
||||||
///////
|
///////
|
||||||
|
|
||||||
private:
|
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;
|
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; // Флаг последнего тика
|
||||||
|
|
||||||
public:
|
public:
|
||||||
float current;
|
float current;
|
||||||
@ -52,29 +120,78 @@ public:
|
|||||||
oldMillis = loopStartTime;
|
oldMillis = loopStartTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() {
|
void update(uint32_t now) {
|
||||||
if (millis() - frameRateTimer > frameTime) {
|
if (now - frameRateTimer > frameTime) {
|
||||||
dtTick();
|
dt = (now - oldMillis) / 1000.0;
|
||||||
if (!isPlayingF || easing == nullptr) return;
|
oldMillis = now;
|
||||||
if (((uint16_t)current) == ((uint16_t)to) /* || progress > duration */) { stop(); return; }
|
|
||||||
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
|
if (!isPlayingF && triggerLastTick) {
|
||||||
progress = progress + Tween::dt;
|
triggerLastTick = false;
|
||||||
frameRateTimer = millis();
|
current = to;
|
||||||
|
if (listener != nullptr) listener->onTweenFinished(*this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isPlayingF) return;
|
||||||
|
|
||||||
|
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;
|
||||||
|
triggerLastTick = true;
|
||||||
|
isPlayingF = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listener != nullptr) listener->onTweenUpdate(*this);
|
||||||
|
|
||||||
|
frameRateTimer = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float getProgress() {
|
||||||
|
return constrain(progress / duration, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
void start(float from, float to, uint16_t duration, EasingFunc::eFunc easing) {
|
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;
|
current = from;
|
||||||
if (from == to) { return; }
|
uint32_t now = millis();
|
||||||
this->from = from;
|
oldMillis = now;
|
||||||
this->to = to;
|
frameRateTimer = now;
|
||||||
this->duration = duration / 1000.0;
|
|
||||||
this->easing = easing;
|
|
||||||
|
|
||||||
resetToStart();
|
|
||||||
dtTick();
|
|
||||||
isPlayingF = true;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetToStart() {
|
void resetToStart() {
|
||||||
@ -82,16 +199,106 @@ public:
|
|||||||
}
|
}
|
||||||
void stop() {
|
void stop() {
|
||||||
isPlayingF = false;
|
isPlayingF = false;
|
||||||
|
current = to;
|
||||||
|
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 {
|
bool isPlaying() const {
|
||||||
return isPlayingF;
|
return isPlayingF || triggerLastTick; // Учитываем последний тик
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFps(uint16_t fps){
|
void setFps(uint16_t fps){
|
||||||
frameTime = 1000 / fps;
|
frameTime = 1000 / fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t getFrameTime() const { return frameTime; }
|
||||||
|
|
||||||
float getFrom() const {
|
float getFrom() const {
|
||||||
return from;
|
return from;
|
||||||
}
|
}
|
||||||
@ -109,19 +316,210 @@ public:
|
|||||||
return sum / (steps + 1); // Среднее значение функции
|
return sum / (steps + 1); // Среднее значение функции
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
static float lerp(float a, float b, float t) {
|
static float lerp(float a, float b, float t) {
|
||||||
return a + (b - a) * t;
|
return a + (b - a) * t;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int clamp(int value, int a, int b) {
|
//----------------------------------------------------------------------
|
||||||
if (a > b) {
|
// TWEEN MOVE SEMANTICS
|
||||||
a ^= b;
|
//----------------------------------------------------------------------
|
||||||
b ^= a;
|
inline Tween::Tween(Tween&& other) noexcept {
|
||||||
a ^= b;
|
unlinkFromList();
|
||||||
}
|
|
||||||
if (value < a) return a;
|
next = other.next;
|
||||||
else if (value > b) return b;
|
listener = other.listener;
|
||||||
else return value;
|
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
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
class AnimationChain : public TweenListener {
|
||||||
|
public:
|
||||||
|
using FromFunc = std::function<float()>;
|
||||||
|
struct Anim {
|
||||||
|
float from;
|
||||||
|
FromFunc fromFunc = nullptr;
|
||||||
|
float to;
|
||||||
|
uint16_t duration; // мс
|
||||||
|
EasingFunc::eFunc easing = nullptr;
|
||||||
|
std::function<float(float)> easingFunc = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Anim> animations;
|
||||||
|
Tween tween; // единственный Tween
|
||||||
|
size_t currentIndex = 0;
|
||||||
|
bool chainPlaying = false;
|
||||||
|
float* currentUserPtr = nullptr;
|
||||||
|
|
||||||
|
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; // актуальное значение наружу
|
||||||
|
|
||||||
|
explicit AnimationChain(uint16_t fps = 30) : tween(fps) {
|
||||||
|
tween.setListener(this); // подписываемся
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
// API ЦЕПОЧКИ
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
|
void addAnim(float from, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) {
|
||||||
|
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, 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 start() {
|
||||||
|
if (animations.empty()) return;
|
||||||
|
currentIndex = 0;
|
||||||
|
chainPlaying = true;
|
||||||
|
launchCurrent();
|
||||||
|
|
||||||
|
// Serial.printf("Chain start [%d]\n", currentIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
chainPlaying = false;
|
||||||
|
tween.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCustomParam(float* val) {
|
||||||
|
currentUserPtr = val;
|
||||||
|
}
|
||||||
|
void resetCustomParam() {
|
||||||
|
currentUserPtr = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isStaticAnim() {
|
||||||
|
if(!isChainPlaying()) return true;
|
||||||
|
const Anim& anim = animations[currentIndex];
|
||||||
|
float from = anim.fromFunc ? anim.fromFunc() : anim.from;
|
||||||
|
float to = anim.to;
|
||||||
|
return from == to;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tween& getTween() { return tween; }
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
// STATE
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
bool isChainPlaying() const { return chainPlaying; }
|
||||||
|
|
||||||
|
bool isAnimPlaying(size_t index) const {
|
||||||
|
return chainPlaying && index == currentIndex && tween.isPlaying();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getCurrentIndex() const { return currentIndex; }
|
||||||
|
// void setCurrentIndex(size_t index){ currentIndex = index; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
// LISTENER CALLBACK
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
|
void onTweenFinished(Tween& /*t*/) override {
|
||||||
|
++currentIndex;
|
||||||
|
if (currentIndex < animations.size()) {
|
||||||
|
// Serial.printf("Chain next [%d]\n", currentIndex);
|
||||||
|
launchCurrent();
|
||||||
|
} else {
|
||||||
|
// Serial.printf("Chain stop [x]\n", currentIndex);
|
||||||
|
chainPlaying = false; // вся цепочка закончилась
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onTweenUpdate(Tween& /*t*/) override {
|
||||||
|
if (chainPlaying) {
|
||||||
|
current = tween.current; // даём наружу актуальное значение
|
||||||
|
if(currentUserPtr != nullptr) *currentUserPtr = current;
|
||||||
|
} else {
|
||||||
|
current = *currentUserPtr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Reference in New Issue
Block a user