PowerToys/src/common/animation.cpp
Andrey Nekrasov cf1b53831f
Formatting chores (#1441)
* format_sources: exclude 3rd party sources

* format common project

* format leftovers in runner & settings projects

* move source formatting-related files according to #939
2020-03-05 13:07:06 +03:00

52 lines
1.3 KiB
C++

#include "pch.h"
#include "animation.h"
Animation::Animation(double duration, double start, double stop) :
duration(duration), start_value(start), end_value(stop), start(std::chrono::high_resolution_clock::now()) {}
void Animation::reset()
{
start = std::chrono::high_resolution_clock::now();
}
void Animation::reset(double duration)
{
this->duration = duration;
reset();
}
void Animation::reset(double duration, double start, double stop)
{
start_value = start;
end_value = stop;
reset(duration);
}
static double ease_out_expo(double t)
{
return 1 - pow(2, -8 * t);
}
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;
}
}
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);
}
bool Animation::done() const
{
return std::chrono::high_resolution_clock::now() - start >= std::chrono::duration<double>(duration);
}