diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index ca82b37..a1311f9 100644 --- a/examples/ExampleApps/MovingCubeApp.cpp +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -66,10 +66,16 @@ namespace OpenVulkano m_animationController->SetDuration(3); OpenVulkano::Math::Pose srcPose(OpenVulkano::Math::Quaternion(), OpenVulkano::Math::Vector3f_SIMD(-3, 0, 0)); - m_animationController->SetInitialPose(srcPose); - OpenVulkano::Math::Pose destPose(OpenVulkano::Math::Quaternion(), OpenVulkano::Math::Vector3f_SIMD(3, 0, 0)); - m_animationController->SetTargetPose(destPose); + m_animationController->SetPoses(srcPose, destPose); + + m_animationController->m_completionEvent += EventHandler(this, &MovingCubeAppImpl::OnAnimationCompleted); + } + + void OnAnimationCompleted(OpenVulkano::Scene::SimpleAnimationController *anim) + { + anim->SwapPoses(); + anim->Reset(); } void Tick() override diff --git a/openVulkanoCpp/Scene/SimpleAnimationController.cpp b/openVulkanoCpp/Scene/SimpleAnimationController.cpp index fd3ca2e..5c9e005 100644 --- a/openVulkanoCpp/Scene/SimpleAnimationController.cpp +++ b/openVulkanoCpp/Scene/SimpleAnimationController.cpp @@ -4,32 +4,31 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include - #include "SimpleAnimationController.hpp" #include "Base/FrameMetadata.hpp" namespace OpenVulkano::Scene { - void SimpleAnimationController::OnAnimationCompleted() + double SimpleAnimationController::GetProgress() { - std::swap(m_targetPose, m_initialPose); - m_elapsed = 0; + double progress = m_elapsed / m_duration; + if(progress >= 1.0) + progress = 1; + return progress; } void SimpleAnimationController::Tick() { + // NOTE(vb): We can't early quit by condition elapsed > duration because we need to + // emit an event when animation is completed if(!m_node || m_duration <= 0) return; m_elapsed += OpenVulkano::CURRENT_FRAME.frameTime; - float progress = m_elapsed / m_duration; - if(progress >= 1.0) - { - progress = 1; - m_event.NotifyAll(); - } + if(m_elapsed > m_duration) + m_completionEvent.NotifyAll(this); + double progress = GetProgress(); OpenVulkano::Math::Pose currentPose = m_initialPose.Interpolate(m_targetPose, progress); m_node->SetMatrix(currentPose.ToMatrix()); } diff --git a/openVulkanoCpp/Scene/SimpleAnimationController.hpp b/openVulkanoCpp/Scene/SimpleAnimationController.hpp index 8c2dfc5..4a8481a 100644 --- a/openVulkanoCpp/Scene/SimpleAnimationController.hpp +++ b/openVulkanoCpp/Scene/SimpleAnimationController.hpp @@ -12,26 +12,38 @@ #include "Math/Pose.hpp" #include "Scene/Node.hpp" +#include + namespace OpenVulkano::Scene { class SimpleAnimationController : public ITickable { OpenVulkano::Scene::Node *m_node = nullptr; - OpenVulkano::Event<> m_event; OpenVulkano::Math::Pose m_targetPose; OpenVulkano::Math::Pose m_initialPose; - float m_duration = 0; - float m_elapsed = 0; + double m_duration = 0; + double m_elapsed = 0; public: - SimpleAnimationController() { m_event += EventHandler(this, &SimpleAnimationController::OnAnimationCompleted); } - - void SetNode(OpenVulkano::Scene::Node *node) { m_node = node; } - void SetInitialPose(OpenVulkano::Math::Pose pose) { m_initialPose = pose; } - void SetTargetPose(OpenVulkano::Math::Pose pose) { m_targetPose = pose; } - void SetDuration(float duration) { m_duration = duration; } + OpenVulkano::Event m_completionEvent; + + SimpleAnimationController() = default; + + void Reset() { m_elapsed = 0; } + void SwapPoses() { std::swap(m_initialPose, m_targetPose); } + + OpenVulkano::Scene::Node* GetNode() { return m_node; } + void SetNode(OpenVulkano::Scene::Node *node) { m_node = node; } + + OpenVulkano::Math::Pose GetInitialPose() { return m_initialPose; } + OpenVulkano::Math::Pose GetTargetPose() { return m_targetPose; } + void SetPoses(const OpenVulkano::Math::Pose &initialPose, const OpenVulkano::Math::Pose &targetPose) { m_initialPose = initialPose; m_targetPose = targetPose; } + + double GetDuration() { return m_duration; } + void SetDuration(double duration) { m_duration = duration; } + + double GetProgress(); - void OnAnimationCompleted(); void Tick() override; }; } \ No newline at end of file