2022-02-28 23:04:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
|
|
|
class animator
|
|
|
|
{
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> animation_start_time;
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> animation_current_time;
|
2023-03-01 21:27:15 +00:00
|
|
|
float animation_curve(float t)
|
|
|
|
{
|
|
|
|
return 3 * pow(1.f - t, 2.f) * t + 3 * (1.f - t) * pow(t, 2.f) + pow(t, 3.f);
|
2022-02-28 23:04:56 +01:00
|
|
|
}
|
2023-03-01 21:27:15 +00:00
|
|
|
|
2022-02-28 23:04:56 +01:00
|
|
|
public:
|
2023-03-01 21:27:15 +00:00
|
|
|
void animate(int milliseconds, std::function<void(float&)> cb)
|
|
|
|
{
|
2022-02-28 23:04:56 +01:00
|
|
|
animation_current_time = std::chrono::system_clock::now();
|
2023-03-01 21:27:15 +00:00
|
|
|
float timeDiff = (float)std::chrono::duration_cast<std::chrono::milliseconds>(animation_current_time - animation_start_time)
|
|
|
|
.count();
|
2022-02-28 23:04:56 +01:00
|
|
|
float t = timeDiff / milliseconds;
|
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
if (t > 1)
|
|
|
|
{
|
2022-02-28 23:04:56 +01:00
|
|
|
t = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float progress = animation_curve(t);
|
|
|
|
cb(progress);
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:27:15 +00:00
|
|
|
void reset()
|
|
|
|
{
|
2022-02-28 23:04:56 +01:00
|
|
|
animation_start_time = std::chrono::system_clock::now();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|