mirror of
https://github.com/Show-maket/Tween.git
synced 2025-06-28 05:09:30 +00:00
Compare commits
9 Commits
2a8fba23d3
...
main
Author | SHA1 | Date | |
---|---|---|---|
18e03b1f26 | |||
cfc67b8138 | |||
77e4385c14 | |||
c6c72fcca8 | |||
9635922022 | |||
663b10f898 | |||
1fda2daaec | |||
bca2439c15 | |||
2de931e9a6 |
259
Easing.h
259
Easing.h
@ -1,172 +1,163 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace EasingFunc {
|
namespace EasingFunc {
|
||||||
typedef float(*eFunc)(float);
|
typedef float(*eFunc)(float);
|
||||||
|
|
||||||
float easeLinear(float t) {
|
static float easeLinear(float t) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInSine(float t) {
|
static float easeInSine(float t) {
|
||||||
return sin(1.5707963 * t);
|
return sinf((t * M_PI) / 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeOutSine(float t) {
|
static float easeOutSine(float t) {
|
||||||
return 1 + sin(1.5707963 * (--t));
|
return sinf((t * M_PI) / 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInQuad(float t) {
|
static float easeInQuad(float t) {
|
||||||
return t * t;
|
return t * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeOutQuad(float t) {
|
static float easeOutQuad(float t) {
|
||||||
return t * (2 - t);
|
return 1 - (1 - t) * (1 - t);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInCubic(float t) {
|
static float easeInCubic(float t) {
|
||||||
return t * t * t;
|
return t * t * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeOutCubic(float t) {
|
static float easeOutCubic(float t) {
|
||||||
return 1 + (--t) * t * t;
|
return 1 - powf(1 - t, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInQuart(float t) {
|
static float easeInQuart(float t) {
|
||||||
t *= t;
|
return t * t * t * t;
|
||||||
return t * t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeOutQuart(float t) {
|
static float easeOutQuart(float t) {
|
||||||
t = (--t) * t;
|
return 1 - powf(1 - t, 4);
|
||||||
return 1 - t * t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
|
static float easeInQuint(float t) {
|
||||||
|
return powf(t, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutQuint(float t) {
|
||||||
|
return 1 - powf(1 - t, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInOutQuint(float t) {
|
||||||
|
return t < 0.5f ? 16 * powf(t, 5) : 1 - powf(-2 * t + 2, 5) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInExpo(float t) {
|
||||||
|
return t == 0 ? 0 : powf(2, 10 * t - 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutExpo(float t) {
|
||||||
|
return t == 1 ? 1 : 1 - powf(2, -10 * t);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInOutExpo(float t) {
|
||||||
|
if (t == 0) return 0;
|
||||||
|
if (t == 1) return 1;
|
||||||
|
return t < 0.5f ? powf(2, 20 * t - 10) / 2 : (2 - powf(2, -20 * t + 10)) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInCirc(float t) {
|
||||||
|
return 1 - sqrtf(1 - t * t);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutCirc(float t) {
|
||||||
|
return sqrtf(1 - powf(t - 1, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInOutCirc(float t) {
|
||||||
|
return t < 0.5f
|
||||||
|
? (1 - sqrtf(1 - 4 * t * t)) / 2
|
||||||
|
: (sqrtf(1 - powf(-2 * t + 2, 2)) + 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInBack(float t) {
|
||||||
|
constexpr float c1 = 1.70158f;
|
||||||
|
constexpr float c3 = c1 + 1;
|
||||||
|
return c3 * t * t * t - c1 * t * t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutBack(float t) {
|
||||||
|
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) {
|
||||||
|
constexpr float c1 = 1.70158f;
|
||||||
|
constexpr float c2 = c1 * 1.525f;
|
||||||
|
return t < 0.5f
|
||||||
|
? 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) {
|
||||||
|
constexpr float c4 = (2 * M_PI) / 3;
|
||||||
|
return t == 0 ? 0 : t == 1 ? 1 : -powf(2, 10 * t - 10) * sinf((t * 10 - 10.75f) * c4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeOutElastic(float t) {
|
||||||
|
constexpr float c4 = (2 * M_PI) / 3;
|
||||||
|
return t == 0 ? 0 : t == 1 ? 1 : powf(2, -10 * t) * sinf((t * 10 - 0.75f) * c4) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float easeInOutElastic(float t) {
|
||||||
|
constexpr float c5 = (2 * M_PI) / 4.5f;
|
||||||
|
if (t == 0) return 0;
|
||||||
|
if (t == 1) return 1;
|
||||||
|
return t < 0.5f
|
||||||
|
? -(powf(2, 20 * t - 10) * sinf((20 * t - 11.125f) * c5)) / 2
|
||||||
|
: (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 {
|
||||||
t = (--t) * t;
|
t -= 2.625f / 2.75f;
|
||||||
return 1 - 8 * t * t;
|
return 7.5625f * t * t + 0.984375f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInQuint(float t) {
|
static float easeInBounce(float t) {
|
||||||
float t2 = t * t;
|
return 1 - easeOutBounce(1 - t);
|
||||||
return t * t2 * t2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeOutQuint(float t) {
|
static float easeInOutBounce(float t) {
|
||||||
float t2 = (--t) * t;
|
return t < 0.5f
|
||||||
return 1 + t * t2 * t2;
|
? (1 - easeOutBounce(1 - 2 * t)) / 2
|
||||||
|
: (1 + easeOutBounce(2 * t - 1)) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
float easeInOutQuint(float t) {
|
}
|
||||||
float t2;
|
|
||||||
if (t < 0.5) {
|
|
||||||
t2 = t * t;
|
|
||||||
return 16 * t * t2 * t2;
|
|
||||||
} else {
|
|
||||||
t2 = (--t) * t;
|
|
||||||
return 1 + 16 * t * t2 * t2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInExpo(float t) {
|
|
||||||
return (pow(2, 8 * t) - 1) / 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeOutExpo(float t) {
|
|
||||||
return 1 - pow(2, -8 * t);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInOutExpo(float t) {
|
|
||||||
if (t < 0.5) {
|
|
||||||
return (pow(2, 16 * t) - 1) / 510;
|
|
||||||
} else {
|
|
||||||
return 1 - 0.5 * pow(2, -16 * (t - 0.5));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInCirc(float t) {
|
|
||||||
return 1 - sqrt(1 - t);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeOutCirc(float t) {
|
|
||||||
return sqrt(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInOutCirc(float t) {
|
|
||||||
if (t < 0.5) {
|
|
||||||
return (1 - sqrt(1 - 2 * t)) * 0.5;
|
|
||||||
} else {
|
|
||||||
return (1 + sqrt(2 * t - 1)) * 0.5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInBack(float t) {
|
|
||||||
return t * t * (2.70158 * t - 1.70158);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeOutBack(float t) {
|
|
||||||
return 1 + (--t) * t * (2.70158 * t + 1.70158);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInOutBack(float t) {
|
|
||||||
if (t < 0.5) {
|
|
||||||
return t * t * (7 * t - 2.5) * 2;
|
|
||||||
} else {
|
|
||||||
return 1 + (--t) * t * 2 * (7 * t + 2.5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInElastic(float t) {
|
|
||||||
float t2 = t * t;
|
|
||||||
return t2 * t2 * sin(t * PI * 4.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeOutElastic(float t) {
|
|
||||||
float t2 = (t - 1) * (t - 1);
|
|
||||||
return 1 - t2 * t2 * cos(t * PI * 4.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInOutElastic(float t) {
|
|
||||||
float t2;
|
|
||||||
if (t < 0.45) {
|
|
||||||
t2 = t * t;
|
|
||||||
return 8 * t2 * t2 * sin(t * PI * 9);
|
|
||||||
} else if (t < 0.55) {
|
|
||||||
return 0.5 + 0.75 * sin(t * PI * 4);
|
|
||||||
} else {
|
|
||||||
t2 = (t - 1) * (t - 1);
|
|
||||||
return 1 - 8 * t2 * t2 * sin(t * PI * 9);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInBounce(float t) {
|
|
||||||
return pow(2, 6 * (t - 1)) * abs(sin(t * PI * 3.5));
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeOutBounce(float t) {
|
|
||||||
return 1 - pow(2, -6 * t) * abs(cos(t * PI * 3.5));
|
|
||||||
}
|
|
||||||
|
|
||||||
float easeInOutBounce(float t) {
|
|
||||||
if (t < 0.5) {
|
|
||||||
return 8 * pow(2, 8 * (t - 1)) * abs(sin(t * PI * 7));
|
|
||||||
} else {
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
};
|
267
Tween.h
267
Tween.h
@ -1,7 +1,49 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Easing.h"
|
#include "Easing.h"
|
||||||
|
#include "TimeWarp.h"
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// EASING HELPERS
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
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 eFunc withTimeWarp(eFunc easing, const TimeWarp& warp) {
|
||||||
|
g_warp = &warp;
|
||||||
|
g_baseEasing = easing;
|
||||||
|
return _easingWithWarp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // 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;
|
||||||
@ -9,8 +51,10 @@ private:
|
|||||||
static inline uint32_t frameRateTimer;
|
static inline uint32_t frameRateTimer;
|
||||||
Tween* next;
|
Tween* next;
|
||||||
|
|
||||||
|
TweenListener* listener = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Tween(uint16_t fps) {
|
Tween(uint16_t fps = 30) {
|
||||||
setFps(fps);
|
setFps(fps);
|
||||||
if (Tween::head == nullptr) {
|
if (Tween::head == nullptr) {
|
||||||
Tween::head = this;
|
Tween::head = this;
|
||||||
@ -29,6 +73,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setListener(TweenListener* l) { listener = l; }
|
||||||
|
TweenListener* getListener() const { return listener; }
|
||||||
|
|
||||||
///////
|
///////
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -42,6 +89,7 @@ private:
|
|||||||
EasingFunc::eFunc easing = nullptr;
|
EasingFunc::eFunc easing = nullptr;
|
||||||
float progress;
|
float progress;
|
||||||
bool isPlayingF;
|
bool isPlayingF;
|
||||||
|
bool triggerLastTick = false; // Флаг последнего тика
|
||||||
|
|
||||||
public:
|
public:
|
||||||
float current;
|
float current;
|
||||||
@ -54,27 +102,52 @@ public:
|
|||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
if (millis() - frameRateTimer > frameTime) {
|
if (millis() - frameRateTimer > frameTime) {
|
||||||
dtTick();
|
uint32_t now = millis();
|
||||||
if (!isPlayingF || easing == nullptr) return;
|
dt = (now - oldMillis) / 1000.0;
|
||||||
if (((uint16_t)current) == ((uint16_t)to) /* || progress > duration */) { stop(); return; }
|
oldMillis = now;
|
||||||
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
|
|
||||||
progress = progress + Tween::dt;
|
if (!isPlayingF && triggerLastTick) {
|
||||||
frameRateTimer = millis();
|
triggerLastTick = false;
|
||||||
|
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);
|
||||||
|
|
||||||
|
current = Tween::lerp(from, to, easing(normProgress));
|
||||||
|
|
||||||
|
if (progress >= duration) {
|
||||||
|
current = to;
|
||||||
|
triggerLastTick = true;
|
||||||
|
isPlayingF = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listener != nullptr) listener->onTweenUpdate(*this);
|
||||||
|
|
||||||
|
frameRateTimer = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
from = from_;
|
||||||
|
to = to_;
|
||||||
|
duration = durationMs / 1000.0f;
|
||||||
|
progress = 0;
|
||||||
current = from;
|
current = from;
|
||||||
if (from == to) { return; }
|
oldMillis = millis();
|
||||||
this->from = from;
|
|
||||||
this->to = to;
|
|
||||||
this->duration = duration / 1000.0;
|
|
||||||
this->easing = easing;
|
|
||||||
|
|
||||||
resetToStart();
|
|
||||||
dtTick();
|
|
||||||
isPlayingF = true;
|
isPlayingF = true;
|
||||||
|
triggerLastTick = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetToStart() {
|
void resetToStart() {
|
||||||
@ -82,30 +155,164 @@ public:
|
|||||||
}
|
}
|
||||||
void stop() {
|
void stop() {
|
||||||
isPlayingF = false;
|
isPlayingF = false;
|
||||||
|
current = to;
|
||||||
|
triggerLastTick = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isPlaying() {
|
bool isPlaying() const {
|
||||||
return isPlayingF;
|
return isPlayingF || triggerLastTick; // Учитываем последний тик
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFps(uint16_t fps){
|
void setFps(uint16_t fps){
|
||||||
frameTime = 1000 / fps;
|
frameTime = 1000 / fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float getFrom() const {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getTo() const {
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float integrateEasing(EasingFunc::eFunc easing, int steps = 100) {
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 0; i <= steps; ++i) {
|
||||||
|
float t = i / (float)steps;
|
||||||
|
sum += easing(t);
|
||||||
|
}
|
||||||
|
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) {
|
private:
|
||||||
if (a > b) {
|
// int clamp(int value, int a, int b) {
|
||||||
a ^= b;
|
// if (a > b) {
|
||||||
b ^= a;
|
// a ^= b;
|
||||||
a ^= b;
|
// b ^= a;
|
||||||
}
|
// a ^= b;
|
||||||
if (value < a) return a;
|
// }
|
||||||
else if (value > b) return b;
|
// if (value < a) return a;
|
||||||
else return value;
|
// else if (value > b) return b;
|
||||||
}
|
// else return value;
|
||||||
};
|
// }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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];
|
||||||
|
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});
|
||||||
|
}
|
||||||
|
void addAnim(FromFunc fromF, float to, uint16_t duration, EasingFunc::eFunc easing = nullptr) {
|
||||||
|
animations.push_back({0, fromF, to, duration, easing});
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
// 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