Update MorphableCameraController
This commit is contained in:
@@ -33,7 +33,7 @@ namespace OpenVulkano
|
||||
|
||||
void SetCamera(Scene::Camera* camera) { m_camera = camera; }
|
||||
|
||||
Scene::Camera* GetCamera() { return m_camera; }
|
||||
[[nodiscard]] Scene::Camera* GetCamera() const { return m_camera; }
|
||||
|
||||
virtual void SetActive() {}
|
||||
};
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
*/
|
||||
|
||||
#include "MorphableCameraController.hpp"
|
||||
#include "Base/FrameMetadata.hpp"
|
||||
#include "Input/InputManager.hpp"
|
||||
#include "Input/InputKey.hpp"
|
||||
#include "Base/FrameMetadata.hpp"
|
||||
#include "Scene/Camera.hpp"
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
@@ -18,33 +18,19 @@ namespace OpenVulkano::Scene
|
||||
, m_currentTime(0.0f)
|
||||
, m_isMorphing(false)
|
||||
, m_targetMorphStatePerspective(true)
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
m_actionMorph = input->GetAction("morph");
|
||||
}
|
||||
{}
|
||||
|
||||
void MorphableCameraController::Init(MorphableCamera* camera)
|
||||
{
|
||||
CameraController::Init(camera);
|
||||
m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P);
|
||||
}
|
||||
|
||||
void MorphableCameraController::Tick()
|
||||
{
|
||||
auto input = OpenVulkano::Input::InputManager::GetInstance();
|
||||
|
||||
bool isMorphingDown = input->GetButton(m_actionMorph);
|
||||
if (!m_wasMorphingKeyDown && isMorphingDown)
|
||||
{
|
||||
m_isMorphing = true;
|
||||
m_currentTime = 0.0f;
|
||||
m_targetMorphStatePerspective = !m_targetMorphStatePerspective;
|
||||
}
|
||||
m_wasMorphingKeyDown = isMorphingDown;
|
||||
|
||||
if (m_isMorphing)
|
||||
{
|
||||
m_currentTime += input->GetTimeScale();
|
||||
m_currentTime += CURRENT_FRAME.frameTime;
|
||||
if (m_currentTime > m_animationDuration) m_currentTime = m_animationDuration;
|
||||
float t = m_currentTime / m_animationDuration;
|
||||
if (t >= 1.0f)
|
||||
@@ -52,15 +38,32 @@ namespace OpenVulkano::Scene
|
||||
t = 1.0f;
|
||||
m_isMorphing = false;
|
||||
}
|
||||
float newState = m_targetMorphStatePerspective ? (1.0f - t) : t;
|
||||
const float newState = m_targetMorphStatePerspective ? (1.0f - t) : t;
|
||||
static_cast<MorphableCamera*>(GetCamera())->SetMorphState(newState);
|
||||
}
|
||||
}
|
||||
|
||||
void MorphableCameraController::SetTargetState(bool toPerspective)
|
||||
void MorphableCameraController::SetTargetState(const bool toPerspective)
|
||||
{
|
||||
if (toPerspective == m_targetMorphStatePerspective) return;
|
||||
m_targetMorphStatePerspective = toPerspective;
|
||||
m_isMorphing = true;
|
||||
}
|
||||
}
|
||||
|
||||
MorphableCameraControllerWithInput::MorphableCameraControllerWithInput(MorphableCamera* camera)
|
||||
: m_controller(camera)
|
||||
, m_actionMorph(Input::InputManager::GetInstance()->GetAction("morph"))
|
||||
{
|
||||
m_actionMorph->BindKey(Input::InputKey::Keyboard::KEY_P);
|
||||
}
|
||||
|
||||
void MorphableCameraControllerWithInput::Tick()
|
||||
{
|
||||
if (Input::InputManager::GetInstance()->GetButtonDown(m_actionMorph))
|
||||
{
|
||||
SetTargetState(!m_controller.IsTargetPerspective());
|
||||
m_controller.Reset();
|
||||
}
|
||||
m_controller.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* 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 "MorphableCamera.hpp"
|
||||
@@ -24,9 +25,6 @@ namespace OpenVulkano
|
||||
bool m_isMorphing;
|
||||
bool m_targetMorphStatePerspective; // true for perspective, false for orthographic
|
||||
|
||||
Input::InputAction* m_actionMorph;
|
||||
bool m_wasMorphingKeyDown = false;
|
||||
|
||||
public:
|
||||
MorphableCameraController(MorphableCamera* camera = nullptr);
|
||||
|
||||
@@ -34,12 +32,39 @@ namespace OpenVulkano
|
||||
#pragma clang diagnostic ignored "-Woverloaded-virtual"
|
||||
void Init(MorphableCamera* camera);
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
void Tick() override;
|
||||
|
||||
void SetDuration(const double duration) { m_animationDuration = duration; }
|
||||
|
||||
[[nodiscard]] double GetDuration() const { return m_animationDuration; }
|
||||
|
||||
void SetTargetState(bool toPerspective);
|
||||
|
||||
[[nodiscard]] bool IsTargetPerspective() const { return m_targetMorphStatePerspective; }
|
||||
|
||||
void Reset() { m_currentTime = 0; }
|
||||
};
|
||||
|
||||
class MorphableCameraControllerWithInput final
|
||||
{
|
||||
MorphableCameraController m_controller;
|
||||
Input::InputAction* m_actionMorph;
|
||||
|
||||
public:
|
||||
MorphableCameraControllerWithInput(MorphableCamera* camera = nullptr);
|
||||
|
||||
void Init(MorphableCamera* camera) { m_controller.Init(camera); }
|
||||
|
||||
void Tick();
|
||||
|
||||
void SetDuration(const double duration) { m_controller.SetDuration(duration); }
|
||||
|
||||
[[nodiscard]] double GetDuration() const { return m_controller.GetDuration(); }
|
||||
|
||||
void SetTargetState(const bool toPerspective) { m_controller.SetTargetState(toPerspective); }
|
||||
|
||||
void Reset() { m_controller.Reset(); }
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user