77 lines
1.8 KiB
C++
77 lines
1.8 KiB
C++
#include "Animation/AnimationController.h"
|
|
|
|
#include "Animation/AllAnimations.h"
|
|
|
|
AnimationController::AnimationController(Fader *fader)
|
|
: m_fader(fader), m_animation(nullptr), m_frame(0)
|
|
{
|
|
m_updateMutex = xSemaphoreCreateMutex();
|
|
}
|
|
|
|
void AnimationController::changeAnimation(std::unique_ptr<Animation> anim, bool transition)
|
|
{
|
|
xSemaphoreTake(m_updateMutex, portMAX_DELAY);
|
|
|
|
if(transition && m_animation) {
|
|
m_nextAnimation = std::move(anim);
|
|
m_animation->stop();
|
|
} else {
|
|
m_frame = 0;
|
|
|
|
m_animation = std::move(anim);
|
|
m_nextAnimation.reset(nullptr);
|
|
}
|
|
|
|
xSemaphoreGive(m_updateMutex);
|
|
}
|
|
|
|
void AnimationController::changeAnimation(AnimationController::DefaultAnimation animation_id, bool transition)
|
|
{
|
|
std::unique_ptr<Animation> anim(nullptr);
|
|
|
|
switch(animation_id) {
|
|
case FIRE_HOT:
|
|
changeAnimation(std::unique_ptr<Animation>(new FireAnimation(m_fader, false)), transition);
|
|
break;
|
|
|
|
case FIRE_COLD:
|
|
changeAnimation(std::unique_ptr<Animation>(new FireAnimation(m_fader, true)), transition);
|
|
break;
|
|
|
|
default:
|
|
return; // unknown id, do nothing
|
|
}
|
|
}
|
|
|
|
void AnimationController::loop(void)
|
|
{
|
|
xSemaphoreTake(m_updateMutex, portMAX_DELAY);
|
|
|
|
if(m_animation && !m_animation->finished()) {
|
|
m_animation->loop(m_frame);
|
|
}
|
|
|
|
m_frame++;
|
|
|
|
if(m_nextAnimation &&
|
|
(!m_animation || m_animation->finished())) {
|
|
// old animation has finished or is unset -> start the new one
|
|
m_frame = 0;
|
|
m_animation.swap(m_nextAnimation);
|
|
m_nextAnimation.reset(nullptr);
|
|
}
|
|
|
|
xSemaphoreGive(m_updateMutex);
|
|
}
|
|
|
|
void AnimationController::stop(void)
|
|
{
|
|
m_animation->stop();
|
|
}
|
|
|
|
void AnimationController::restart(void)
|
|
{
|
|
m_animation->reset();
|
|
m_frame = 0;
|
|
}
|