2019-09-04 18:26:26 +02:00
|
|
|
#include "pch.h"
|
|
|
|
#include "animation.h"
|
|
|
|
|
|
|
|
Animation::Animation(double duration, double start, double stop) :
|
2020-03-05 13:07:06 +03:00
|
|
|
duration(duration), start_value(start), end_value(stop), start(std::chrono::high_resolution_clock::now()) {}
|
2019-09-04 18:26:26 +02:00
|
|
|
|
2020-03-05 13:07:06 +03:00
|
|
|
void Animation::reset()
|
|
|
|
{
|
|
|
|
start = std::chrono::high_resolution_clock::now();
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
2020-03-05 13:07:06 +03:00
|
|
|
void Animation::reset(double duration)
|
|
|
|
{
|
|
|
|
this->duration = duration;
|
|
|
|
reset();
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
2020-03-05 13:07:06 +03:00
|
|
|
void Animation::reset(double duration, double start, double stop)
|
|
|
|
{
|
|
|
|
start_value = start;
|
|
|
|
end_value = stop;
|
|
|
|
reset(duration);
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 13:07:06 +03:00
|
|
|
static double ease_out_expo(double t)
|
|
|
|
{
|
|
|
|
return 1 - pow(2, -8 * t);
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 13:07:06 +03:00
|
|
|
double Animation::apply_animation_function(double t, AnimFunctions apply_function)
|
|
|
|
{
|
|
|
|
switch (apply_function)
|
|
|
|
{
|
|
|
|
case EASE_OUT_EXPO:
|
|
|
|
return ease_out_expo(t);
|
|
|
|
case LINEAR:
|
|
|
|
default:
|
|
|
|
return t;
|
|
|
|
}
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 13:07:06 +03:00
|
|
|
double Animation::value(AnimFunctions apply_function) const
|
|
|
|
{
|
|
|
|
auto anim_duration = std::chrono::high_resolution_clock::now() - start;
|
|
|
|
double t = std::chrono::duration<double>(anim_duration).count() / duration;
|
|
|
|
if (t >= 1)
|
|
|
|
return end_value;
|
|
|
|
return start_value + (end_value - start_value) * apply_animation_function(t, apply_function);
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|
2020-03-05 13:07:06 +03:00
|
|
|
bool Animation::done() const
|
|
|
|
{
|
|
|
|
return std::chrono::high_resolution_clock::now() - start >= std::chrono::duration<double>(duration);
|
2019-09-04 18:26:26 +02:00
|
|
|
}
|