galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
CameraController.cpp
Go to the documentation of this file.
1
7
8#include <glm/trigonometric.hpp>
9
10#include "galaxy/input/Input.hpp"
11
12#include "CameraController.hpp"
13
14namespace galaxy
15{
16 namespace input
17 {
18 CameraController::CameraController(graphics::Camera& camera)
19 : m_camera {std::ref(camera)}
20 {
21 }
22
26
27 void CameraController::on_key_press(events::KeyPress& e)
28 {
29 if (!e.handled && e.pressed)
30 {
31 auto& camera = m_camera.get();
32
33 if (e.keycode == CameraKeys::FORWARD)
34 {
35 camera.m_pos.x -= -glm::sin(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
36 camera.m_pos.y -= glm::cos(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
37 }
38
39 if (e.keycode == CameraKeys::BACKWARD)
40 {
41 camera.m_pos.x += -glm::sin(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
42 camera.m_pos.y += glm::cos(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
43 }
44
45 if (e.keycode == CameraKeys::LEFT)
46 {
47 camera.m_pos.x -= glm::cos(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
48 camera.m_pos.y -= glm::sin(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
49 }
50
51 if (e.keycode == CameraKeys::RIGHT)
52 {
53 camera.m_pos.x += glm::cos(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
54 camera.m_pos.y += glm::sin(glm::radians(camera.m_rotation)) * camera.m_translation_speed * GALAXY_DT;
55 }
56
57 if (camera.m_allow_rotation)
58 {
59 if (e.keycode == CameraKeys::ROTATE_LEFT)
60 {
61 camera.m_rotation += camera.m_rotation_speed * GALAXY_DT;
62 }
63
64 if (e.keycode == CameraKeys::ROTATE_RIGHT)
65 {
66 camera.m_rotation -= camera.m_rotation_speed * GALAXY_DT;
67 }
68
69 if (camera.m_rotation > 180.0f)
70 {
71 camera.m_rotation -= 360.0f;
72 }
73 else if (camera.m_rotation <= -180.0f)
74 {
75 camera.m_rotation += 360.0f;
76 }
77
78 camera.set_rotation(camera.m_rotation);
79 }
80
81 camera.set_pos(camera.m_pos.x, camera.m_pos.y);
82
83 e.handled = true;
84 }
85 }
86
87 void CameraController::on_mouse_wheel(events::MouseWheel& e)
88 {
89 if (!e.handled)
90 {
91 auto& camera = m_camera.get();
92 auto& scale = camera.get_scale();
93
94 camera.set_scale_vertical(std::clamp(static_cast<float>(scale.y - (e.yoff * GALAXY_MIN_CAMERA_ZOOM)), GALAXY_MIN_CAMERA_ZOOM, GALAXY_MAX_CAMERA_ZOOM));
95
96 e.handled = true;
97 }
98 }
99 } // namespace input
100} // namespace galaxy
std::reference_wrapper< graphics::Camera > m_camera
Camera object being manipulated.
void on_key_press(events::KeyPress &e)
Event processing method for input events.
void on_mouse_wheel(events::MouseWheel &e)
Event processing method for scroll events.
CameraController()
Deleted constructor.
Timer.hpp galaxy.
Definition Async.hpp:17
STL namespace.