mirror of
https://github.com/Show-maket/Tween.git
synced 2025-06-27 20:59:36 +00:00
fix last play tick
This commit is contained in:
21
Tween.h
21
Tween.h
@ -42,6 +42,7 @@ private:
|
|||||||
EasingFunc::eFunc easing = nullptr;
|
EasingFunc::eFunc easing = nullptr;
|
||||||
float progress;
|
float progress;
|
||||||
bool isPlayingF;
|
bool isPlayingF;
|
||||||
|
bool isLastPlay;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
float current;
|
float current;
|
||||||
@ -56,13 +57,24 @@ public:
|
|||||||
if (millis() - frameRateTimer > frameTime) {
|
if (millis() - frameRateTimer > frameTime) {
|
||||||
dtTick();
|
dtTick();
|
||||||
if (!isPlayingF || easing == nullptr) return;
|
if (!isPlayingF || easing == nullptr) return;
|
||||||
if (((uint16_t)current) == ((uint16_t)to) /* || progress > duration */) { stop(); return; }
|
if(!isLastPlay){
|
||||||
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
|
if (/* ((uint16_t)current) == ((uint16_t)to) || */ progress > duration) {
|
||||||
progress = progress + Tween::dt;
|
current = to;
|
||||||
|
isLastPlay = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
|
||||||
|
progress = progress + Tween::dt;
|
||||||
|
} else {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
frameRateTimer = millis();
|
frameRateTimer = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float getProgress() {
|
||||||
|
return clamp(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;
|
||||||
@ -82,6 +94,7 @@ public:
|
|||||||
}
|
}
|
||||||
void stop() {
|
void stop() {
|
||||||
isPlayingF = false;
|
isPlayingF = false;
|
||||||
|
isLastPlay = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isPlaying() const {
|
bool isPlaying() const {
|
||||||
@ -109,11 +122,11 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
Reference in New Issue
Block a user