SequenceAnimationController class

This commit is contained in:
Vladyslav Baranovskyi
2024-06-07 16:09:11 +03:00
parent 14407eaff9
commit 16f7789a52
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Scene/SequenceAnimationController.hpp"
namespace OpenVulkano::Scene
{
SequenceAnimationController::SequenceAnimationController()
{
// m_animationController.m_completionEvent += EventHandler(this, &SequenceAnimationController::OnCurrentFrameFinished);
m_completionEvent += EventHandler(this, &SequenceAnimationController::OnCurrentFrameFinished);
}
void SequenceAnimationController::AdvanceToNextStep()
{
if(m_steps.empty())
return;
if(m_currentStep < m_steps.size() - 1)
{
++m_currentStep;
SetPoses(GetTargetPose(), m_steps[m_currentStep].first);
SetDuration(m_steps[m_currentStep-1].second);
SimpleAnimationController::Reset();
}
else
{
if(m_loop)
{
m_currentStep = 0;
SetPoses(GetTargetPose(), m_steps[m_currentStep].first);
// We don't set the duration because transition from the last step to the first
// requires to use duration of the last step
SimpleAnimationController::Reset();
}
else
{
m_sequenceCompletionEvent.NotifyAll(this);
}
}
}
void SequenceAnimationController::Tick()
{
SimpleAnimationController::Tick();
}
void SequenceAnimationController::Restart()
{
m_currentStep = 0;
if(!m_steps.empty())
{
SetPoses(GetTargetPose(), m_steps[m_currentStep].first);
SetDuration(m_steps.back().second);
}
SimpleAnimationController::Reset();
}
void SequenceAnimationController::AddAnimationStep(const Math::PoseF &pose, double duration)
{
m_steps.emplace_back(pose, duration);
if(m_steps.size() == 2)
{
m_currentStep = 1;
SetPoses(m_steps[0].first, m_steps[1].first);
SetDuration(m_steps[0].second);
Reset();
}
}
void SequenceAnimationController::OnCurrentFrameFinished(SimpleAnimationController *animationController)
{
AdvanceToNextStep();
}
}

View File

@@ -0,0 +1,45 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Scene/SimpleAnimationController.hpp"
#include "Base/Event.hpp"
#include "Base/ITickable.hpp"
#include "Math/Math.hpp"
#include "Math/Pose.hpp"
#include <vector>
#include <utility>
namespace OpenVulkano::Scene
{
class SequenceAnimationController : public SimpleAnimationController
{
using PoseDurationPair = std::pair<Math::PoseF, double>;
std::vector<PoseDurationPair> m_steps;
int m_currentStep = 0;
bool m_loop = false;
void AdvanceToNextStep();
public:
SequenceAnimationController();
Event<SequenceAnimationController *> m_sequenceCompletionEvent;
void EnableLoop(bool value) { m_loop = value; }
void Restart();
void AddAnimationStep(const Math::PoseF &pose, double duration);
bool IsFinished() { if(m_loop) return false; return m_currentStep >= m_steps.size(); }
void OnCurrentFrameFinished(SimpleAnimationController *animationController);
void Tick() override;
};
}