SpiecsEngine
 
Loading...
Searching...
No Matches
MeshController.cpp
Go to the documentation of this file.
1#include "Pchheader.h"
3#include "World/Entity.h"
4#include "Render/FrameInfo.h"
5#include "Core/Input/KeyCodes.h"
6#include "Core/Input/Input.h"
7#include "Core/Input/MouseButtonCodes.h"
8
9namespace Spices {
10
12 {
13 Entity entity(m_Owner, FrameInfo::Get().m_World.get());
14 m_CameraTranComp = &entity.GetComponent<TransformComponent>();
15 }
16
18 {
20 {
21 const glm::vec2& mouse { Input::GetMouseX(), Input::GetMouseY() };
22 const glm::vec2 delta = (mouse - m_InitialMousePosition) * 0.003f;
23 m_InitialMousePosition = mouse;
24
26 else if (Input::IsMouseButtonPressed(Mouse::ButtonLeft)) MouseRotate(delta);
27 else if (Input::IsMouseButtonPressed(Mouse::ButtonRight)) { MouseZoom(delta.y); UpdateView(); }
28 }
29 }
30
32 {
33 EventDispatcher dispatcher(e);
34
36 }
37
39 {
40 return true;
41 }
42
43 void MeshController::MousePan(const glm::vec2& delta)
44 {
45 auto [xSpeed, ySpeed] = PanSpeed();
46 m_FocalPoint += -GetRightDirection() * delta.x * xSpeed * m_Distance;
47 m_FocalPoint += GetUpDirection() * delta.y * ySpeed * m_Distance;
48 }
49
50 void MeshController::MouseRotate(const glm::vec2& delta) const
51 {
52 glm::vec3 rot = std::any_cast<TransformComponent*>(m_CameraTranComp)->GetRotation();
53 const float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f;
54
55 rot.y += glm::degrees(yawSign * delta.x * RotationSpeed());
56 rot.x += glm::degrees(delta.y * RotationSpeed());
57
58 std::any_cast<TransformComponent*>(m_CameraTranComp)->SetRotation(rot);
59 }
60
61 void MeshController::MouseZoom(float delta)
62 {
63 m_Distance -= delta * ZoomSpeed();
64 if (m_Distance < 1.0f)
65 {
66 m_FocalPoint += GetForwardDirection();
67 m_Distance = 1.0f;
68 }
69 }
70
71 std::pair<float, float> MeshController::PanSpeed() const
72 {
73 const float x = std::min(m_ViewportWidth / 1000.0f, 2.4f); // max = 2.4f
74 float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f;
75
76 const float y = std::min(m_ViewportHeight / 1000.0f, 2.4f); // max = 2.4f
77 float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f;
78
79 return { xFactor, yFactor };
80 }
81
83 {
84 return 0.8f;
85 }
86
88 {
89 float distance = m_Distance * 0.2f;
90 distance = std::max(distance, 0.0f);
91 float speed = distance * distance;
92 speed = std::min(speed, 100.0f); // max speed = 100
93 return speed;
94 }
95
97 {
98 const glm::vec3 pos = CalculatePosition();
99 std::any_cast<TransformComponent*>(m_CameraTranComp)->SetPosition(pos);
100 // m_Yaw = m_Pitch = 0.0f; // Lock the camera's rotation
101
102 }
103
105 {
106 return m_FocalPoint - GetForwardDirection() * m_Distance;
107 }
108
110 {
111 return glm::rotate(GetOrientation(), glm::vec3(0.0f, -1.0f, 0.0f));
112 }
113
115 {
116 return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f));
117 }
118
120 {
121 return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, 1.0f));
122 }
123
125 {
126 const glm::vec3& rot = std::any_cast<TransformComponent*>(m_CameraTranComp)->GetRotation();
127 return glm::quat(glm::vec3(rot.x, rot.y, rot.z));
128 }
129}
#define BIND_EVENT_FN(x)
Bind Event.
Definition Event.h:88
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
EventDispatcher(Event &event)
Constructor Function.
Definition Event.h:172
This Class store a Specific Event type first and Dispatch a event handle function to it.
Definition Event.h:156
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
static bool IsKeyPressed(const int &keycode)
Query If given Key is Pressed.
Definition Input.h:34
static bool IsMouseButtonPressed(const int &button)
Query If given Mouse Button is Pressed.
Definition Input.h:41
This Class Is a wrapper of Platform Specific Input Query.
Definition Input.h:16
This Class is inherited from KeyEvent Class.
Definition KeyEvent.h:57
void MouseZoom(float delta)
bool OnKeyPressed(KeyPressedEvent &e)
glm::vec3 GetForwardDirection() const
glm::vec3 GetRightDirection() const
virtual void OnEvent(Event &e) override
This interface defines the behaves on specific component event happened.
virtual void OnTick(TimeStep &ts) override
This interface defines the behaves on specific component tick every frame.
std::pair< float, float > PanSpeed() const
void MouseRotate(const glm::vec2 &delta) const
glm::vec3 CalculatePosition() const
virtual void OnConstruction() override
This interface defines the behaves on specific component added.
glm::quat GetOrientation() const
void MousePan(const glm::vec2 &delta)
glm::vec3 GetUpDirection() const
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22