mirror of
https://github.com/Show-maket/Tween.git
synced 2025-06-27 20:59:36 +00:00
TimeWarp and fix last tick
This commit is contained in:
97
Tween.h
97
Tween.h
@ -1,6 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Easing.h"
|
#include "Easing.h"
|
||||||
|
#include "TimeWarp.h"
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
class Tween {
|
class Tween {
|
||||||
private:
|
private:
|
||||||
@ -10,7 +34,7 @@ private:
|
|||||||
Tween* next;
|
Tween* next;
|
||||||
|
|
||||||
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;
|
||||||
@ -42,7 +66,7 @@ private:
|
|||||||
EasingFunc::eFunc easing = nullptr;
|
EasingFunc::eFunc easing = nullptr;
|
||||||
float progress;
|
float progress;
|
||||||
bool isPlayingF;
|
bool isPlayingF;
|
||||||
bool isLastPlay;
|
bool triggerLastTick = false; // Флаг последнего тика
|
||||||
|
|
||||||
public:
|
public:
|
||||||
float current;
|
float current;
|
||||||
@ -55,38 +79,54 @@ 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(!isLastPlay){
|
oldMillis = now;
|
||||||
if (/* ((uint16_t)current) == ((uint16_t)to) || */ progress > duration) {
|
|
||||||
|
if (!isPlayingF && triggerLastTick) {
|
||||||
|
triggerLastTick = false;
|
||||||
current = to;
|
current = to;
|
||||||
isLastPlay = true;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
|
|
||||||
progress = progress + Tween::dt;
|
if (!isPlayingF || easing == nullptr) return;
|
||||||
} else {
|
|
||||||
stop();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
frameRateTimer = millis();
|
frameRateTimer = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float getProgress() {
|
float getProgress() {
|
||||||
return clamp(progress / duration, 0, 1);
|
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 duration, EasingFunc::eFunc easing) {
|
||||||
current = from;
|
current = from;
|
||||||
if (from == to) { return; }
|
if (from == to) {
|
||||||
|
current = to;
|
||||||
|
isPlayingF = false;
|
||||||
|
triggerLastTick = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this->from = from;
|
this->from = from;
|
||||||
this->to = to;
|
this->to = to;
|
||||||
this->duration = duration / 1000.0;
|
this->duration = duration / 1000.0;
|
||||||
this->easing = easing;
|
this->easing = easing;
|
||||||
|
progress = 0;
|
||||||
resetToStart();
|
oldMillis = millis();
|
||||||
dtTick();
|
|
||||||
isPlayingF = true;
|
isPlayingF = true;
|
||||||
|
triggerLastTick = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetToStart() {
|
void resetToStart() {
|
||||||
@ -94,11 +134,12 @@ public:
|
|||||||
}
|
}
|
||||||
void stop() {
|
void stop() {
|
||||||
isPlayingF = false;
|
isPlayingF = false;
|
||||||
isLastPlay = false;
|
current = to;
|
||||||
|
triggerLastTick = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isPlaying() const {
|
bool isPlaying() const {
|
||||||
return isPlayingF;
|
return isPlayingF || triggerLastTick; // Учитываем последний тик
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFps(uint16_t fps){
|
void setFps(uint16_t fps){
|
||||||
@ -127,14 +168,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int clamp(int value, int a, int b) {
|
// int clamp(int value, int a, int b) {
|
||||||
if (a > b) {
|
// if (a > b) {
|
||||||
a ^= b;
|
// a ^= b;
|
||||||
b ^= a;
|
// b ^= a;
|
||||||
a ^= b;
|
// a ^= b;
|
||||||
}
|
// }
|
||||||
if (value < a) return a;
|
// if (value < a) return a;
|
||||||
else if (value > b) return b;
|
// else if (value > b) return b;
|
||||||
else return value;
|
// else return value;
|
||||||
}
|
// }
|
||||||
};
|
};
|
Reference in New Issue
Block a user