vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
eventManager.cpp
Go to the documentation of this file.
1#define GLM_ENABLE_EXPERIMENTAL
2#include <glm/gtx/norm.hpp>
3
5
6bool ven::EventManager::isKeyJustPressed(GLFWwindow* window, const long unsigned int key, std::array<bool, GLFW_KEY_LAST>& keyStates)
7{
8 const bool isPressed = glfwGetKey(window, static_cast<int>(key)) == GLFW_PRESS;
9 const bool wasPressed = keyStates.at(key);
10
11 keyStates.at(key) = isPressed;
12
13 return isPressed && !wasPressed;
14}
15
16template<typename Iterator>
17void ven::EventManager::processKeyActions(GLFWwindow* window, Iterator begin, Iterator end)
18{
19 for (auto it = begin; it != end; ++it) {
20 if (glfwGetKey(window, it->key) == GLFW_PRESS) {
21 *it->dir += it->value;
22 }
23 }
24}
25
26void ven::EventManager::moveCamera(GLFWwindow* window, Camera& camera, const float dt)
27{
28 glm::vec3 rotate{0};
29 glm::vec3 moveDir{0.F};
30 static constexpr glm::vec3 upDir{0.F, -1.F, 0.F};
31 const float yaw = camera.transform.rotation.y;
32 const glm::vec3 forwardDir{std::sin(yaw), 0.F, std::cos(yaw)};
33 const glm::vec3 rightDir{forwardDir.z, 0.F, -forwardDir.x};
34 const std::array<KeyAction, 10> moveActions = {{
35 {.key=DEFAULT_KEY_MAPPINGS.lookLeft, .dir=&rotate, .value={0.F, -1.F, 0.F}},
36 {.key=DEFAULT_KEY_MAPPINGS.lookRight, .dir=&rotate, .value={0.F, 1.F, 0.F}},
37 {.key=DEFAULT_KEY_MAPPINGS.lookUp, .dir=&rotate, .value={1.F, 0.F, 0.F}},
38 {.key=DEFAULT_KEY_MAPPINGS.lookDown, .dir=&rotate, .value={-1.F, 0.F, 0.F}},
39 {.key=DEFAULT_KEY_MAPPINGS.moveForward, .dir=&moveDir, .value=forwardDir},
40 {.key=DEFAULT_KEY_MAPPINGS.moveBackward, .dir=&moveDir, .value=-forwardDir},
41 {.key=DEFAULT_KEY_MAPPINGS.moveRight, .dir=&moveDir, .value=rightDir},
42 {.key=DEFAULT_KEY_MAPPINGS.moveLeft, .dir=&moveDir, .value=-rightDir},
43 {.key=DEFAULT_KEY_MAPPINGS.moveUp, .dir=&moveDir, .value=upDir},
44 {.key=DEFAULT_KEY_MAPPINGS.moveDown, .dir=&moveDir, .value=-upDir}
45 }};
46
47 processKeyActions(window, moveActions.begin(), moveActions.end());
48
49 if (const float lengthRotate = length2(rotate); lengthRotate > EPSILON) {
50 camera.transform.rotation += camera.getLookSpeed() * dt * rotate / std::sqrt(lengthRotate);
51 }
52 if (const float lengthMove = length2(moveDir); lengthMove > EPSILON) {
53 camera.transform.translation += camera.getMoveSpeed() * dt * moveDir / std::sqrt(lengthMove);
54 }
55
56 camera.transform.rotation.x = glm::clamp(camera.transform.rotation.x, -1.5F, 1.5F);
57 camera.transform.rotation.y = glm::mod(camera.transform.rotation.y, glm::two_pi<float>());
58}
59
60void ven::EventManager::handleEvents(GLFWwindow *window, ENGINE_STATE *engineState, Camera& camera, Gui& gui, const float dt) const
61{
62 glfwPollEvents();
63 if (glfwWindowShouldClose(window) == GLFW_TRUE) {
64 updateEngineState(engineState, EXIT);
65 }
66 if (isKeyJustPressed(window, DEFAULT_KEY_MAPPINGS.toggleGui, m_keyState)) {
67 if (gui.getState() != HIDDEN) {
68 gui.setState(HIDDEN);
69 } else {
70 if (*engineState == EDITOR) {
72 } else {
74 }
75 }
76 }
77 moveCamera(window, camera, dt);
78}
This file contains the EventManager class.
Class for camera.
Definition Camera.hpp:28
Transform3D transform
Definition Camera.hpp:60
float getMoveSpeed() const
Definition Camera.hpp:57
float getLookSpeed() const
Definition Camera.hpp:58
void handleEvents(GLFWwindow *window, ENGINE_STATE *engineState, Camera &camera, Gui &gui, float dt) const
static void moveCamera(GLFWwindow *window, Camera &camera, float dt)
static void processKeyActions(GLFWwindow *window, Iterator begin, Iterator end)
static bool isKeyJustPressed(GLFWwindow *window, long unsigned int key, std::array< bool, GLFW_KEY_LAST > &keyStates)
Class for Gui.
Definition Gui.hpp:30
GUI_STATE getState() const
Definition Gui.hpp:53
void setState(const GUI_STATE state)
Definition Gui.hpp:52
glm::vec3 translation
ENGINE_STATE
Definition Utils.hpp:13
@ EXIT
Definition Utils.hpp:17
@ EDITOR
Definition Utils.hpp:14
static constexpr KeyMappings DEFAULT_KEY_MAPPINGS
static constexpr float EPSILON
@ SHOW_PLAYER
Definition Gui.hpp:21
@ SHOW_EDITOR
Definition Gui.hpp:20
@ HIDDEN
Definition Gui.hpp:22