From 2a8fba23d3738064226cd520003b951a75784d28 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Thu, 7 Mar 2024 15:33:00 +0300 Subject: [PATCH] upd --- Easing.h | 3 ++- Tween.h | 69 ++++++++++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/Easing.h b/Easing.h index 202d6cc..6b4b329 100644 --- a/Easing.h +++ b/Easing.h @@ -1,6 +1,7 @@ #pragma once namespace EasingFunc { - + typedef float(*eFunc)(float); + float easeLinear(float t) { return t; } diff --git a/Tween.h b/Tween.h index 273c25f..dca02e2 100644 --- a/Tween.h +++ b/Tween.h @@ -1,30 +1,17 @@ #pragma once #include "Easing.h" -// namespace TweenNameSpace { -typedef float(*eFunc)(float); -// }; -// using namespace TweenNameSpace; -typedef float(*eFunc)(float); + class Tween { - - /////// private: static inline Tween* head = nullptr; static inline Tween* last = nullptr; static inline uint32_t frameRateTimer; Tween* next; -public: - static void tick() { - Tween* current = Tween::head; - while (current != nullptr) { - current->update(); - current = current->next; - } - } +public: Tween(uint16_t fps) { - frameTime = 1000 / fps; + setFps(fps); if (Tween::head == nullptr) { Tween::head = this; } @@ -33,34 +20,30 @@ public: } last = this; } + + static void tick() { + Tween* current = Tween::head; + while (current != nullptr) { + current->update(); + current = current->next; + } + } + /////// private: uint16_t frameTime; float dt; uint32_t oldMillis; - static float lerp(float a, float b, float t) { - return a + (b - a) * t; - } float from; float to; float duration; - eFunc easing = nullptr; + EasingFunc::eFunc easing = nullptr; float progress; bool isPlayingF; public: - void update() { - if (millis() - frameRateTimer > frameTime) { - dtTick(); - if (!isPlayingF || easing == nullptr) return; - if (current == to/* || progress >= duration */) { stop(); return; } - current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to); - progress = progress + Tween::dt; - frameRateTimer = millis(); - } - } float current; void dtTick() { @@ -69,14 +52,26 @@ public: oldMillis = loopStartTime; } - void start(float from, float to, uint16_t duration, eFunc easing) { + 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(); + } + } + + + void start(float from, float to, uint16_t duration, EasingFunc::eFunc easing) { current = from; if (from == to) { return; } this->from = from; this->to = to; this->duration = duration / 1000.0; this->easing = easing; - + resetToStart(); dtTick(); isPlayingF = true; @@ -92,7 +87,17 @@ public: bool isPlaying() { return isPlayingF; } + + void setFps(uint16_t fps){ + frameTime = 1000 / fps; + } + + 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;