From 663b10f898af7495497c339b6443253b0459ea80 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Tue, 6 May 2025 13:58:21 +0300 Subject: [PATCH] fix last play tick --- Tween.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Tween.h b/Tween.h index 341ebb5..4b2e2bb 100644 --- a/Tween.h +++ b/Tween.h @@ -42,6 +42,7 @@ private: EasingFunc::eFunc easing = nullptr; float progress; bool isPlayingF; + bool isLastPlay; public: float current; @@ -56,13 +57,24 @@ public: 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; + if(!isLastPlay){ + if (/* ((uint16_t)current) == ((uint16_t)to) || */ progress > duration) { + current = to; + isLastPlay = true; + return; + } + current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to); + progress = progress + Tween::dt; + } else { + stop(); + } frameRateTimer = millis(); } } + float getProgress() { + return clamp(progress / duration, 0, 1); + } void start(float from, float to, uint16_t duration, EasingFunc::eFunc easing) { current = from; @@ -82,6 +94,7 @@ public: } void stop() { isPlayingF = false; + isLastPlay = false; } bool isPlaying() const { @@ -109,11 +122,11 @@ public: return sum / (steps + 1); // Среднее значение функции } -private: static float lerp(float a, float b, float t) { return a + (b - a) * t; } +private: int clamp(int value, int a, int b) { if (a > b) { a ^= b;