From 16f7789a5278d55b7befc10a489cb8bbfbe17fd3 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 7 Jun 2024 16:09:11 +0300 Subject: [PATCH] SequenceAnimationController class --- .../Scene/SequenceAnimationController.cpp | 78 +++++++++++++++++++ .../Scene/SequenceAnimationController.hpp | 45 +++++++++++ 2 files changed, 123 insertions(+) create mode 100644 openVulkanoCpp/Scene/SequenceAnimationController.cpp create mode 100644 openVulkanoCpp/Scene/SequenceAnimationController.hpp diff --git a/openVulkanoCpp/Scene/SequenceAnimationController.cpp b/openVulkanoCpp/Scene/SequenceAnimationController.cpp new file mode 100644 index 0000000..3f0c76f --- /dev/null +++ b/openVulkanoCpp/Scene/SequenceAnimationController.cpp @@ -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(); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Scene/SequenceAnimationController.hpp b/openVulkanoCpp/Scene/SequenceAnimationController.hpp new file mode 100644 index 0000000..0d711eb --- /dev/null +++ b/openVulkanoCpp/Scene/SequenceAnimationController.hpp @@ -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 +#include + +namespace OpenVulkano::Scene +{ + class SequenceAnimationController : public SimpleAnimationController + { + using PoseDurationPair = std::pair; + + std::vector m_steps; + int m_currentStep = 0; + bool m_loop = false; + + void AdvanceToNextStep(); + + public: + SequenceAnimationController(); + + Event 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; + }; +} \ No newline at end of file