Merge pull request #194 from SHADE-DP/SP3-141-Camera-System
Sp3 141 camera system Added EditorCameraArm functionality Added helper functions in SceneManager to help check scene node and component actives
This commit is contained in:
commit
897294426f
|
@ -7,13 +7,15 @@
|
|||
#include "ECS_Base/Managers/SHComponentManager.h"
|
||||
#include "Math/Transform/SHTransformComponent.h"
|
||||
#include <math.h>
|
||||
|
||||
#include "Scene/SHSceneManager.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
void SHCameraSystem::UpdateEditorCamera(double dt) noexcept
|
||||
{
|
||||
|
||||
|
||||
|
||||
auto& camera = editorCamera;
|
||||
SHVec3 view, right, UP;
|
||||
GetCameraAxis(camera, view, right, UP);
|
||||
|
@ -62,62 +64,50 @@ namespace SHADE
|
|||
|
||||
UpdateCameraComponent(editorCamera);
|
||||
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::LEFT_ALT))
|
||||
{
|
||||
UpdateEditorArm(dt, true, SHVec3{ 0.0f });
|
||||
}
|
||||
else
|
||||
UpdateEditorArm(dt, false, SHVec3{ 0.0f });
|
||||
|
||||
}
|
||||
void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept
|
||||
{
|
||||
SHCameraSystem* system = static_cast<SHCameraSystem*>(GetSystem());
|
||||
auto& camera = system->editorCamera;
|
||||
SHVec3 view, right, UP;
|
||||
system->GetCameraAxis(camera, view, right, UP);
|
||||
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A))
|
||||
void SHCameraSystem::UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept
|
||||
{
|
||||
if (active == false)
|
||||
{
|
||||
//std::cout << "Camera movement: "<<right.x<<", " << right.y << std::endl;
|
||||
camera.position -= right * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D))
|
||||
{
|
||||
camera.position += right * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W))
|
||||
{
|
||||
camera.position += view * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S))
|
||||
{
|
||||
camera.position -= view * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::Q))
|
||||
{
|
||||
camera.position += UP * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::E))
|
||||
{
|
||||
camera.position -= UP * dt * camera.movementSpeed;
|
||||
camera.dirtyView = true;
|
||||
}
|
||||
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::RMB))
|
||||
{
|
||||
double mouseX, mouseY;
|
||||
SHInputManager::GetMouseVelocity(&mouseX,&mouseY);
|
||||
|
||||
//std::cout << camera.yaw << std::endl;
|
||||
|
||||
camera.pitch -= mouseY * dt * camera.turnSpeed.x;
|
||||
camera.yaw -= mouseX * dt * camera.turnSpeed.y;
|
||||
camera.dirtyView = true;
|
||||
editorCameraArm.offset = SHVec3{0.0f};
|
||||
return;
|
||||
}
|
||||
|
||||
//std::cout << "Camera position: " << camera.position.x << " " << camera.position.y << std::endl;
|
||||
system->UpdateCameraComponent(system->editorCamera);
|
||||
editorCamera.SetPosition(targetPos);
|
||||
double mouseX, mouseY;
|
||||
SHInputManager::GetMouseVelocity(&mouseX, &mouseY);
|
||||
|
||||
editorCameraArm.pitch -= mouseY * dt * editorCamera.turnSpeed.x;
|
||||
editorCameraArm.yaw -= mouseX * dt * editorCamera.turnSpeed.y;
|
||||
|
||||
constexpr float pitchClamp = 85.0f;
|
||||
|
||||
if (editorCameraArm.pitch > pitchClamp)
|
||||
editorCameraArm.pitch = pitchClamp;
|
||||
if (editorCameraArm.pitch < -pitchClamp)
|
||||
editorCameraArm.pitch = -pitchClamp;
|
||||
|
||||
editorCameraArm.armLength -= SHInputManager::GetMouseWheelVerticalDelta() * dt;
|
||||
|
||||
if (editorCameraArm.armLength < 1.0f)
|
||||
editorCameraArm.armLength = 1.0f;
|
||||
|
||||
UpdatePivotArmComponent(editorCameraArm);
|
||||
|
||||
editorCamera.offset = editorCameraArm.GetOffset();
|
||||
|
||||
CameraLookAt(editorCamera, targetPos);
|
||||
|
||||
}
|
||||
|
||||
system->DecomposeViewMatrix(camera.viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position);
|
||||
}
|
||||
|
||||
void SHCameraSystem::Init(void)
|
||||
{
|
||||
|
@ -164,6 +154,9 @@ namespace SHADE
|
|||
|
||||
void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept
|
||||
{
|
||||
if (camera.isActive == false)
|
||||
return;
|
||||
|
||||
if (SHComponentManager::HasComponent<SHTransformComponent>(camera.GetEID()) == true && &camera != &editorCamera)
|
||||
{
|
||||
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(camera.GetEID());
|
||||
|
@ -183,11 +176,17 @@ namespace SHADE
|
|||
if (SHComponentManager::HasComponent<SHCameraArmComponent>(camera.GetEID()))
|
||||
{
|
||||
auto arm = SHComponentManager::GetComponent<SHCameraArmComponent>(camera.GetEID());
|
||||
camera.offset = arm->GetOffset();
|
||||
if(arm->lookAtCameraOrigin)
|
||||
CameraLookAt(camera, camera.position);
|
||||
if (arm->isActive == true)
|
||||
{
|
||||
camera.offset = arm->GetOffset();
|
||||
if (arm->lookAtCameraOrigin)
|
||||
CameraLookAt(camera, camera.position);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
SHVec3 view, right, UP;
|
||||
|
||||
|
||||
|
@ -287,12 +286,14 @@ namespace SHADE
|
|||
|
||||
for (auto& pivot : pivotDense)
|
||||
{
|
||||
system->UpdatePivotArmComponent(pivot);
|
||||
if(SHSceneManager::CheckNodeAndComponentsActive<SHCameraArmComponent>(pivot.GetEID()))
|
||||
system->UpdatePivotArmComponent(pivot);
|
||||
}
|
||||
|
||||
for (auto& cam : dense)
|
||||
{
|
||||
system->UpdateCameraComponent(cam);
|
||||
if (SHSceneManager::CheckNodeAndComponentsActive<SHCameraComponent>(cam.GetEID()))
|
||||
system->UpdateCameraComponent(cam);
|
||||
}
|
||||
for (auto& handle : system->directorHandleList)
|
||||
{
|
||||
|
@ -399,7 +400,7 @@ namespace SHADE
|
|||
SHVec3 up = { 0.0f,1.0f,0.0f };
|
||||
|
||||
|
||||
////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
|
||||
//SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
|
||||
|
||||
//target = SHVec3::Normalise(target);
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "ECS_Base/System/SHSystemRoutine.h"
|
||||
#include "Resource/SHResourceLibrary.h"
|
||||
#include "SHCameraDirector.h"
|
||||
#include "SHCameraArmComponent.h"
|
||||
#include "SH_API.h"
|
||||
|
||||
namespace SHADE
|
||||
|
@ -18,6 +19,7 @@ namespace SHADE
|
|||
//A camera component that represents editor camera.
|
||||
//This is not tied to any entity. Hence this EID should not be used.
|
||||
SHCameraComponent editorCamera;
|
||||
SHCameraArmComponent editorCameraArm;
|
||||
|
||||
SHResourceLibrary<SHCameraDirector> directorLibrary;
|
||||
std::vector<DirectorHandle> directorHandleList;
|
||||
|
@ -34,14 +36,7 @@ namespace SHADE
|
|||
void Init (void);
|
||||
void Exit (void);
|
||||
|
||||
class SH_API EditorCameraUpdate final : public SHSystemRoutine
|
||||
{
|
||||
public:
|
||||
|
||||
EditorCameraUpdate() : SHSystemRoutine("Editor Camera Update", true) { };
|
||||
virtual void Execute(double dt) noexcept override final;
|
||||
|
||||
};
|
||||
friend class EditorCameraUpdate;
|
||||
|
||||
class SH_API CameraSystemUpdate final: public SHSystemRoutine
|
||||
|
@ -63,6 +58,7 @@ namespace SHADE
|
|||
void DecomposeViewMatrix(SHMatrix const& matrix, float& pitch, float& yaw, float& roll, SHVec3& pos) noexcept;
|
||||
void SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept;
|
||||
void CameraLookAt(SHCameraComponent& camera, SHVec3 target) noexcept;
|
||||
void UpdateEditorArm(double dt,bool active ,SHVec3 const& targetPos) noexcept;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "SH_API.h"
|
||||
#include "ECS_Base/General/SHFamily.h"
|
||||
#include "Assets/SHAssetMacros.h"
|
||||
#include "ECS_Base/Managers/SHComponentManager.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
@ -116,6 +117,56 @@ namespace SHADE
|
|||
sceneChanged = true;
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
* \brief
|
||||
* Check if the Entity's scene node is active and all the
|
||||
* components specified are active.
|
||||
* This does not check if the entity HasComponent. Please use
|
||||
* CheckNodeAndHasComponentActive for that.
|
||||
* \param eid
|
||||
* EntityID of the entity to check for.
|
||||
* \return
|
||||
* true if scene node is active and all the components specified
|
||||
* are also active.
|
||||
********************************************************************/
|
||||
template<typename ...ComponentTypes>
|
||||
static std::enable_if_t<(... && std::is_base_of_v<SHComponent, ComponentTypes>), bool> CheckNodeAndComponentsActive(EntityID eid)
|
||||
{
|
||||
return CheckNodeActive(eid) && (... && SHComponentManager::GetComponent_s<ComponentTypes>(eid)->isActive);
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
* \brief
|
||||
* Check if the Entity's scene node is active and all the
|
||||
* components specified are active.
|
||||
* This also checks to verify that the entity has such components.
|
||||
* \param eid
|
||||
* EntityID of the entity to check for.
|
||||
* \return
|
||||
* true if scene node is active and all the components specified
|
||||
* are also active.
|
||||
********************************************************************/
|
||||
template<typename ...ComponentTypes>
|
||||
static std::enable_if_t<(... && std::is_base_of_v<SHComponent, ComponentTypes>), bool> CheckNodeAndHasComponentsActive(EntityID eid)
|
||||
{
|
||||
return CheckNodeActive(eid)
|
||||
&& (... && SHComponentManager::HasComponent<ComponentTypes>(eid))
|
||||
&& (... && SHComponentManager::GetComponent_s<ComponentTypes>(eid)->isActive);
|
||||
}
|
||||
|
||||
/********************************************************************
|
||||
* \brief
|
||||
* Check if Scene node is active.
|
||||
* \param eid
|
||||
* EntityID of the entity to check for.
|
||||
* \return
|
||||
* true if scene node is active
|
||||
********************************************************************/
|
||||
static bool CheckNodeActive(EntityID eid)
|
||||
{
|
||||
return GetCurrentSceneGraph().IsActiveInHierarchy(eid);
|
||||
}
|
||||
|
||||
|
||||
/*!*************************************************************************
|
||||
* \brief
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include "SH_API.h"
|
||||
#include "ECS_Base/Components/SHComponent.h"
|
||||
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
||||
class SH_API SHCanvasComponent final: public SHComponent
|
||||
{
|
||||
using CanvasSizeType = uint32_t;
|
||||
|
||||
|
||||
public:
|
||||
SHCanvasComponent();
|
||||
~SHCanvasComponent() = default;
|
||||
|
||||
void SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept;
|
||||
void SetCanvasWidth(CanvasSizeType width) noexcept;
|
||||
void SetCanvasHeight(CanvasSizeType height) noexcept;
|
||||
|
||||
CanvasSizeType const GetCanvasWidth() const noexcept;
|
||||
CanvasSizeType const GetCanvasHeight() const noexcept;
|
||||
|
||||
private:
|
||||
CanvasSizeType width;
|
||||
CanvasSizeType height;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue