Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
InputManager.h
Go to the documentation of this file.
1/**
2 * @file InputManager.h
3 * @ingroup mnd_input
4 * @brief Keyboard and mouse state cache updated each frame by the engine.
5 *
6 * Engine::Run() feeds GLFW callbacks into InputManager; game code reads state
7 * via IsKeyPressed() / IsMouseButtonPressed() and mouse-position deltas.
8 *
9 * Keys are indexed by GLFW key codes (GLFW_KEY_*). Mouse buttons are indexed
10 * by GLFW mouse-button codes (GLFW_MOUSE_BUTTON_*). The manager is not
11 * instantiated directly — access it through Engine::GetInputManager().
12 *
13 * @see Engine::GetInputManager
14 */
15
16#pragma once
17#include <array>
18
19#include "Constants.h"
20#include "Types.h"
21#include "input/Key.h"
22
23namespace mnd
24{
25
26/**
27 * @brief Stores per-frame keyboard and mouse input state.
28 *
29 * Owned by the Engine singleton. Game code reads state; only the Engine writes
30 * state (it is a friend). Arrays are sized for 512 keys and 16 mouse buttons,
31 * matching GLFW's key-code range.
32 */
34{
35 public:
36 InputManager() = default;
37 InputManager(const InputManager &) = delete;
39
42
43 /**
44 * @brief Record a key press or release event.
45 * @param key Key code (out-of-range values are ignored with a warning).
46 * @param pressed True when pressed, false when released.
47 */
48 void SetKeyPressed(Key key, bool pressed);
49
50 /**
51 * @brief Query whether a key is currently held down.
52 * @param key Key code (e.g. Key::W).
53 * @return True if the key is pressed.
54 */
55 [[nodiscard]] bool IsKeyPressed(Key key) const;
56
57 /**
58 * @brief Record a mouse-button press or release event.
59 * @param button Mouse-button code.
60 * @param pressed True when pressed, false when released.
61 */
62 void SetMouseButtonPressed(MouseButton button, bool pressed);
63
64 /**
65 * @brief Query whether a mouse button is currently held down.
66 * @param button Mouse-button code (e.g. MouseButton::Left).
67 * @return True if the button is pressed.
68 */
69 [[nodiscard]] bool IsMouseButtonPressed(MouseButton button) const;
70
71 /**
72 * @brief Store the cursor position from the previous frame.
73 * @param pos Screen-space cursor coordinates.
74 */
75 void SetMousePositionOld(const vec2 &pos);
76
77 /**
78 * @brief Retrieve the cursor position from the previous frame.
79 * @return Screen-space cursor coordinates.
80 */
81 [[nodiscard]] const vec2 GetMousePositionOld() const;
82
83 /**
84 * @brief Store the cursor position for the current frame.
85 * @param pos Screen-space cursor coordinates.
86 */
87 void SetMousePositionCurrent(const vec2 &pos);
88
89 void SetMousePositionChanged(bool changed);
90 [[nodiscard]] bool IsMousePositionChanged() const;
91
92 /**
93 * @brief Retrieve the cursor position for the current frame.
94 *
95 * Subtract GetMousePositionOld() to get a per-frame delta suitable for
96 * camera rotation.
97 *
98 * @return Screen-space cursor coordinates.
99 */
100 [[nodiscard]] const vec2 GetMousePositionCurrent() const;
101
102 private:
103 std::array<bool, kMaxKeys> m_keys = {false}; ///< Pressed state for each GLFW key code.
104 std::array<bool, kMaxMouseButtons> m_mouseKeys = {false}; ///< Pressed state for each GLFW mouse button.
105 vec2 m_mousePositionOld = vec2(0.0F); ///< Cursor position at end of previous frame.
106 vec2 m_mousePositionCurrent = vec2(0.0F); ///< Cursor position at end of current frame.
107
108 bool m_mousePositionChanged = false;
109
110 friend class Engine;
111};
112
113} // namespace mnd
Engine-wide compile-time tunables and ANSI colour codes.
Strongly-typed key codes; values mirror GLFW.
Engine-wide primitive type aliases and GLM math imports.
Singleton façade that drives the full engine lifecycle.
Definition Engine.h:65
Stores per-frame keyboard and mouse input state.
bool IsKeyPressed(Key key) const
Query whether a key is currently held down.
InputManager(const InputManager &)=delete
InputManager(InputManager &&)=delete
bool IsMouseButtonPressed(MouseButton button) const
Query whether a mouse button is currently held down.
const vec2 GetMousePositionOld() const
Retrieve the cursor position from the previous frame.
InputManager & operator=(InputManager &&)=delete
void SetMouseButtonPressed(MouseButton button, bool pressed)
Record a mouse-button press or release event.
void SetMousePositionChanged(bool changed)
const vec2 GetMousePositionCurrent() const
Retrieve the cursor position for the current frame.
InputManager & operator=(const InputManager &)=delete
InputManager()=default
bool IsMousePositionChanged() const
void SetMousePositionOld(const vec2 &pos)
Store the cursor position from the previous frame.
void SetKeyPressed(Key key, bool pressed)
Record a key press or release event.
void SetMousePositionCurrent(const vec2 &pos)
Store the cursor position for the current frame.
glm::vec2 vec2
2-component float vector (e.g. UV coordinates, mouse position).
Definition Types.h:50
Key
Keyboard key + mouse button identifier (GLFW-compatible values).
Definition Key.h:29
MouseButton
Definition Key.h:149