This commit is contained in:
DashyFox 2024-03-07 15:33:00 +03:00
parent 13e3addae4
commit 2a8fba23d3
2 changed files with 39 additions and 33 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
namespace EasingFunc { namespace EasingFunc {
typedef float(*eFunc)(float);
float easeLinear(float t) { float easeLinear(float t) {
return t; return t;

67
Tween.h
View File

@ -1,30 +1,17 @@
#pragma once #pragma once
#include "Easing.h" #include "Easing.h"
// namespace TweenNameSpace {
typedef float(*eFunc)(float);
// };
// using namespace TweenNameSpace;
typedef float(*eFunc)(float);
class Tween { class Tween {
///////
private: private:
static inline Tween* head = nullptr; static inline Tween* head = nullptr;
static inline Tween* last = nullptr; static inline Tween* last = nullptr;
static inline uint32_t frameRateTimer; static inline uint32_t frameRateTimer;
Tween* next; Tween* next;
public:
static void tick() {
Tween* current = Tween::head;
while (current != nullptr) {
current->update();
current = current->next;
}
}
public:
Tween(uint16_t fps) { Tween(uint16_t fps) {
frameTime = 1000 / fps; setFps(fps);
if (Tween::head == nullptr) { if (Tween::head == nullptr) {
Tween::head = this; Tween::head = this;
} }
@ -33,34 +20,30 @@ public:
} }
last = this; last = this;
} }
static void tick() {
Tween* current = Tween::head;
while (current != nullptr) {
current->update();
current = current->next;
}
}
/////// ///////
private: private:
uint16_t frameTime; uint16_t frameTime;
float dt; float dt;
uint32_t oldMillis; uint32_t oldMillis;
static float lerp(float a, float b, float t) {
return a + (b - a) * t;
}
float from; float from;
float to; float to;
float duration; float duration;
eFunc easing = nullptr; EasingFunc::eFunc easing = nullptr;
float progress; float progress;
bool isPlayingF; bool isPlayingF;
public: 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; float current;
void dtTick() { void dtTick() {
@ -69,7 +52,19 @@ public:
oldMillis = loopStartTime; 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; current = from;
if (from == to) { return; } if (from == to) { return; }
this->from = from; this->from = from;
@ -92,7 +87,17 @@ public:
bool isPlaying() { bool isPlaying() {
return isPlayingF; return isPlayingF;
} }
void setFps(uint16_t fps){
frameTime = 1000 / fps;
}
private: private:
static float lerp(float a, float b, float t) {
return a + (b - a) * t;
}
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;