SpiecsEngine
 
Loading...
Searching...
No Matches
CameraController.h
Go to the documentation of this file.
1/**
2* @file CameraController.h.
3* @brief The CameraController Class Definitions.
4* @author The Cherno & Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "Camera.h"
10#include "World/Components/NativeScriptComponent.h"
11#include "World/Components/TransformComponent.h"
12
13#include <any>
14
15namespace Spices {
16
17 /**
18 * @brief CameraController Class.
19 * This class inherit from NativeScriptComponent.
20 * It receives Mouse Event and Key Event to control the Camera movement which defined by it's owner.
21 */
23 {
24 public:
25
26 /**
27 * @brief Constructor Function.
28 */
29 CameraController() = default;
30
31 /**
32 * @brief Destructor Function.
33 */
34 virtual ~CameraController() override = default;
35
36 /**
37 * @brief The interface inherited from Component, which is used for serialize this class.
38 * @todo Implemented it.
39 */
40 virtual void OnSerialize() override {}
41
42 /**
43 * @brief The interface inherited from Component, which is used for deserialize this class.
44 * @todo Implemented it.
45 */
46 virtual void OnDeSerialize() override {}
47
48 /**
49 * @brief The interface inherited from NativeScriptComponent, which is called on this attached to an entity.
50 * We get the camera we want control here.
51 * We get the camera transform component here.
52 */
53 virtual void OnConstruction() override;
54
55 /**
56 * @brief The interface inherited from NativeScriptComponent, which is called every engine loop frame.
57 * We implemented mainly camera control here, the reason is query input has better performance than event.
58 */
59 virtual void OnTick(TimeStep& ts) override;
60
61 /**
62 * @brief The interface inherited from NativeScriptComponent, which is called on the global event function pointer called.
63 */
64 virtual void OnEvent(Event& e) override;
65
66 /**
67 * @brief Get Camera Up(y) Direction.
68 * @return The Camera Up(y) Direction.
69 */
70 glm::vec3 GetUpDirection() const;
71
72 /**
73 * @brief Get Camera Right(x) Direction.
74 * @return The Camera Right(x) Direction.
75 */
76 glm::vec3 GetRightDirection() const;
77
78 /**
79 * @brief Get Camera Forward(z) Direction.
80 * @return The Camera Forward(z) Direction.
81 */
83
84 /**
85 * @brief Get Camera quaternion rotation.
86 * @return The Camera quaternion rotation.
87 */
88 glm::quat GetOrientation() const;
89
90 private:
91
92 /**
93 * @brief Event OnKeyPressed.
94 * We do nothing here.
95 * @param[in] e Event Wrapper.
96 * @return true if we need block the event.
97 * @todo Implemented it.
98 */
100
101 /**
102 * @brief Event OnMouseScroll.
103 * Scale the camera.
104 * @param[in] e Event Wrapper.
105 * @return true if we need block the event
106 */
108
109 /**
110 * @brief Event OnWindowResized.
111 * Reset Camera aspect ratio.
112 * @param[in] e Event Wrapper.
113 * @return true if we need block the event
114 */
116
117 /**
118 * @brief Calculate Camera Drag.
119 * @param[in] delta Mouse position delta on viewport during frames.
120 */
121 void MousePan(const glm::vec2& delta);
122
123 /**
124 * @brief Calculate Camera Rotate.
125 * @param[in] delta Mouse position delta on viewport during frames.
126 */
127 void MouseRotate(const glm::vec2& delta) const;
128
129 /**
130 * @brief Calculate Camera Zoom.
131 * @param[in] delta Mouse position delta y on viewport during frames.
132 */
133 void MouseZoom(const float& delta);
134
135 /**
136 * @brief Calculate Camera Drag speed.
137 * @return Returns the camera drag speed in x direction and y direction.
138 */
139 std::pair<float, float> PanSpeed() const;
140
141 /**
142 * @brief Calculate Camera Rotate speed.
143 * @return Returns the speed camera rotate.
144 */
145 float RotationSpeed() const;
146
147 /**
148 * @brief Calculate Camera Zoom speed.
149 * @return Returns the speed camera zoom.
150 */
151 float ZoomSpeed() const;
152
153 /**
154 * @brief Setting camera transform component position.
155 * We already set rotation in MouseRotate().
156 */
157 void UpdateView() const;
158
159 /**
160 * @brief Calculate camera transform component position using the class member parameters.
161 * @return Returns camera transform component position.
162 */
163 glm::vec3 CalculatePosition() const;
164
165 private:
166
167 /**
168 * @brief The camera smart pointer get from owner's camera component.
169 */
170 std::shared_ptr<Camera> m_Camera;
171
172 /**
173 * @brief The camera transform component pointer get from owner's transform component.
174 */
176
177 /**
178 * @brief The Viewport size.
179 * @todo Make it param.
180 */
181 uint32_t m_ViewportWidth = 1920, m_ViewportHeight = 1080;
182
183 /**
184 * @brief Zoom Level, for orthographic type camera.
185 */
186 float m_ZoomLevel = 1.0f;
187
188 /**
189 * @brief The camera spring arm.
190 * Init with 10.
191 */
192 float m_Distance = 10.0f;
193
194 /**
195 * @brief The mouse position.
196 * Init with 0.
197 */
198 glm::vec2 m_InitialMousePosition = { 0.0f, 0.0f };
199
200 /**
201 * @brief The focus point.
202 * Init with 0.
203 */
204 glm::vec3 m_FocalPoint = { 0.0f, 0.0f, 0.0f };
205 };
206}
#define SPICES_PROFILE_ZONE
virtual void OnEvent(Event &e) override
The interface inherited from NativeScriptComponent, which is called on the global event function poin...
glm::vec3 GetForwardDirection() const
Get Camera Forward(z) Direction.
glm::vec3 m_FocalPoint
The focus point. Init with 0.
uint32_t m_ViewportWidth
The Viewport size.
glm::vec3 GetUpDirection() const
Get Camera Up(y) Direction.
CameraController()=default
Constructor Function.
std::shared_ptr< Camera > m_Camera
The camera smart pointer get from owner's camera component.
bool OnKeyPressed(KeyPressedEvent &e)
Event OnKeyPressed. We do nothing here.
virtual void OnDeSerialize() override
The interface inherited from Component, which is used for deserialize this class.
bool OnMouseScroll(MouseScrolledEvent &e)
Event OnMouseScroll. Scale the camera.
void MousePan(const glm::vec2 &delta)
Calculate Camera Drag.
void MouseZoom(const float &delta)
Calculate Camera Zoom.
void UpdateView() const
Setting camera transform component position. We already set rotation in MouseRotate().
std::pair< float, float > PanSpeed() const
Calculate Camera Drag speed.
virtual void OnSerialize() override
The interface inherited from Component, which is used for serialize this class.
glm::vec2 m_InitialMousePosition
The mouse position. Init with 0.
glm::quat GetOrientation() const
Get Camera quaternion rotation.
virtual ~CameraController() override=default
Destructor Function.
void MouseRotate(const glm::vec2 &delta) const
Calculate Camera Rotate.
virtual void OnConstruction() override
The interface inherited from NativeScriptComponent, which is called on this attached to an entity....
bool OnSlateResized(SlateResizeEvent &e)
Event OnWindowResized. Reset Camera aspect ratio.
float ZoomSpeed() const
Calculate Camera Zoom speed.
float m_Distance
The camera spring arm. Init with 10.
glm::vec3 GetRightDirection() const
Get Camera Right(x) Direction.
TransformComponent * m_CameraTranComp
The camera transform component pointer get from owner's transform component.
glm::vec3 CalculatePosition() const
Calculate camera transform component position using the class member parameters.
virtual void OnTick(TimeStep &ts) override
The interface inherited from NativeScriptComponent, which is called every engine loop frame....
float RotationSpeed() const
Calculate Camera Rotate speed.
float m_ZoomLevel
Zoom Level, for orthographic type camera.
CameraController Class. This class inherit from NativeScriptComponent. It receives Mouse Event and Ke...
Camera Class. This class just encapsulate Projection Matrix.
Definition Camera.h:59
virtual void OnPreActivate() override
This interface define the specific world behave before on activated.
virtual void OnDeactivate() override
This interface defines the specific world behave after on activated.
virtual void OnActivate(TimeStep &ts) override
This interface define the specific world behave on activated.
EditorWorld Class. This class defines the specific behave of EditorWorld.
Definition EditorWorld.h:18
T & AddComponent(Args &&... args)
Template Function. Used for add specific component to entity.
Definition Entity.h:53
T & GetComponent() const
Get Component owned by this entity.
Definition Entity.h:75
void RemoveComponent() const
Remove Component owned from this entity.
Definition Entity.h:85
Entity()=default
Constructor Function.
void AddToRoot()
Add a entity to this world root.
Definition Entity.h:101
World * m_World
A specific world Pointer.
Definition Entity.h:171
const UUID GetUUID()
Get UUID form UUIDComponent.
Definition Entity.h:121
Entity(entt::entity handle, World *world)
Constructor Function. Init class variable. Usually call it.
Definition Entity.h:35
void RemoveFromRoot()
Remove a entity from this world root.
Definition Entity.h:93
operator bool() const
Empty Operation.
Definition Entity.h:127
virtual ~Entity()=default
Destructor Function.
bool HasComponent() const
If Component is owned by this entity or not.
Definition Entity.h:112
operator uint32_t() const
Empty Operation.
Definition Entity.h:133
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
Wrapper of Gltf file data.
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.
virtual void OnSerialize() override
This interface defines how to serialize.
virtual void OnDeSerialize() override
This interface defines how to deserialize.
std::pair< float, float > PanSpeed() const
virtual ~MeshController() override=default
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 is inherited from Event Class.
Definition MouseEvent.h:86
NativeScriptComponent Class. This class defines the specific behaves of NativeScriptComponent....
This Class is inherited from Event Class. Called by Viewport Resize.
Definition SlateEvent.h:18
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
TransformComponent Class. This class defines the specific behaves of TransformComponent.
UUIDComponent Class. This class defines the specific behaves of UUIDComponent.
This class helps to generate a uuid for one resource.
Definition UUID.h:16
virtual void OnDeactivate() override
This interface defines the specific world behave after on activated.
virtual void OnActivate(TimeStep &ts) override
This interface define the specific world behave on activated.
virtual void OnPreActivate() override
This interface define the specific world behave before on activated.
void RemoveFromRoot(Entity &entity)
Remove a entity from this world root.
Definition World.cpp:58
void AddToRoot(Entity &entity)
Add a entity to this world root.
Definition World.cpp:67
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41