Compare commits

...

9 Commits

Author SHA1 Message Date
18e03b1f26 Update Tween.h 2025-05-22 13:38:16 +03:00
cfc67b8138 AnimChain 2025-05-13 17:23:59 +03:00
77e4385c14 TimeWarp and fix last tick 2025-05-12 17:26:13 +03:00
c6c72fcca8 Create TimeWarp.h 2025-05-12 17:25:43 +03:00
9635922022 fix easing 2025-05-12 17:25:26 +03:00
663b10f898 fix last play tick 2025-05-06 13:58:21 +03:00
1fda2daaec Update Tween.h 2025-04-14 16:59:32 +03:00
bca2439c15 static fix 2025-02-27 10:53:35 +03:00
2de931e9a6 add const 2025-02-27 10:06:26 +03:00
3 changed files with 466 additions and 164 deletions

259
Easing.h
View File

@ -1,172 +1,163 @@
#pragma once
#include <cmath>
namespace EasingFunc {
typedef float(*eFunc)(float);
float easeLinear(float t) {
static float easeLinear(float t) {
return t;
}
float easeInSine(float t) {
return sin(1.5707963 * t);
static float easeInSine(float t) {
return sinf((t * M_PI) / 2.0f);
}
float easeOutSine(float t) {
return 1 + sin(1.5707963 * (--t));
static float easeOutSine(float t) {
return sinf((t * M_PI) / 2.0f);
}
float easeInOutSine(float t) {
return 0.5 * (1 + sin(3.1415926 * (t - 0.5)));
static float easeInOutSine(float t) {
return -(cosf(M_PI * t) - 1.0f) / 2.0f;
}
float easeInQuad(float t) {
static float easeInQuad(float t) {
return t * t;
}
float easeOutQuad(float t) {
return t * (2 - t);
static float easeOutQuad(float t) {
return 1 - (1 - t) * (1 - t);
}
float easeInOutQuad(float t) {
return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1;
static float easeInOutQuad(float t) {
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;
}
float easeOutCubic(float t) {
return 1 + (--t) * t * t;
static float easeOutCubic(float t) {
return 1 - powf(1 - t, 3);
}
float easeInOutCubic(float t) {
return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
static float easeInOutCubic(float t) {
return t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2;
}
float easeInQuart(float t) {
t *= t;
return t * t;
static float easeInQuart(float t) {
return t * t * t * t;
}
float easeOutQuart(float t) {
t = (--t) * t;
return 1 - t * t;
static float easeOutQuart(float t) {
return 1 - powf(1 - t, 4);
}
float easeInOutQuart(float t) {
if (t < 0.5) {
t *= t;
return 8 * t * t;
static float easeInOutQuart(float t) {
return t < 0.5f ? 8 * powf(t, 4) : 1 - powf(-2 * t + 2, 4) / 2;
}
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 {
t = (--t) * t;
return 1 - 8 * t * t;
t -= 2.625f / 2.75f;
return 7.5625f * t * t + 0.984375f;
}
}
float easeInQuint(float t) {
float t2 = t * t;
return t * t2 * t2;
static float easeInBounce(float t) {
return 1 - easeOutBounce(1 - t);
}
float easeOutQuint(float t) {
float t2 = (--t) * t;
return 1 + t * t2 * t2;
static float easeInOutBounce(float t) {
return t < 0.5f
? (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
View 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
View File

@ -1,7 +1,49 @@
#pragma once
#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 {
private:
static inline Tween* head = nullptr;
@ -9,8 +51,10 @@ private:
static inline uint32_t frameRateTimer;
Tween* next;
TweenListener* listener = nullptr;
public:
Tween(uint16_t fps) {
Tween(uint16_t fps = 30) {
setFps(fps);
if (Tween::head == nullptr) {
Tween::head = this;
@ -29,6 +73,9 @@ public:
}
}
void setListener(TweenListener* l) { listener = l; }
TweenListener* getListener() const { return listener; }
///////
private:
@ -42,6 +89,7 @@ private:
EasingFunc::eFunc easing = nullptr;
float progress;
bool isPlayingF;
bool triggerLastTick = false; // Флаг последнего тика
public:
float current;
@ -54,27 +102,52 @@ public:
void update() {
if (millis() - frameRateTimer > frameTime) {
dtTick();
if (!isPlayingF || easing == nullptr) return;
if (((uint16_t)current) == ((uint16_t)to) /* || progress > duration */) { stop(); return; }
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
progress = progress + Tween::dt;
frameRateTimer = millis();
uint32_t now = millis();
dt = (now - oldMillis) / 1000.0;
oldMillis = now;
if (!isPlayingF && triggerLastTick) {
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;
if (from == to) { return; }
this->from = from;
this->to = to;
this->duration = duration / 1000.0;
this->easing = easing;
resetToStart();
dtTick();
oldMillis = millis();
isPlayingF = true;
triggerLastTick = false;
}
void resetToStart() {
@ -82,30 +155,164 @@ public:
}
void stop() {
isPlayingF = false;
current = to;
triggerLastTick = false;
}
bool isPlaying() {
return isPlayingF;
bool isPlaying() const {
return isPlayingF || triggerLastTick; // Учитываем последний тик
}
void setFps(uint16_t 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) {
return a + (b - a) * t;
}
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;
}
};
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;
// }
};
//----------------------------------------------------------------------
// 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;
}
}
};