mirror of
https://github.com/Show-maket/Tween.git
synced 2025-05-04 07:10:20 +00:00
upd
This commit is contained in:
parent
13e3addae4
commit
2a8fba23d3
3
Easing.h
3
Easing.h
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
namespace EasingFunc {
|
||||
|
||||
typedef float(*eFunc)(float);
|
||||
|
||||
float easeLinear(float t) {
|
||||
return t;
|
||||
}
|
||||
|
69
Tween.h
69
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user