Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <unordered_set>
5
6#include "math/vector_2.h"
7#include "math/vector_3.h"
8
11
12struct GLFWwindow;
13class Window;
14
16enum class CursorMode {
17 Normal,
18 Hidden,
20};
21
23enum class InputButton {
24 UNKNOWN = -1,
25
26 MOUSE_LEFT = 0,
27 MOUSE_RIGHT,
28 MOUSE_MIDDLE,
29 MOUSE_4,
30 MOUSE_5,
31 MOUSE_6,
32 MOUSE_7,
33 MOUSE_8,
34
35 SCROLL_UP = 8,
36 SCROLL_DOWN,
37
38 SPACE = 32,
39 APOSTROPHE = 39,
40 COMMA = 44,
41 MINUS = 45,
42 PERIOD = 46,
43 SLASH = 47,
44 _0 = 48,
45 _1,
46 _2,
47 _3,
48 _4,
49 _5,
50 _6,
51 _7,
52 _8,
53 _9,
54 SEMICOLON = 59,
55 EQUAL = 61,
56 A = 65,
57 B,
58 C,
59 D,
60 E,
61 F,
62 G,
63 H,
64 I,
65 J,
66 K,
67 L,
68 M,
69 N,
70 O,
71 P,
72 Q,
73 R,
74 S,
75 T,
76 U,
77 V,
78 W,
79 X,
80 Y,
81 Z,
82 LEFT_BRACKET = 91,
83 BACKSLASH = 92,
84 RIGHT_BRACKET = 93,
85 GRAVE_ACCENT = 96,
86 WORLD_1 = 161,
87 WORLD_2 = 162,
88 ESCAPE = 256,
89 ENTER,
90 TAB,
91 BACKSPACE,
92 INSERT,
93 DELETE,
94 RIGHT,
95 LEFT,
96 DOWN,
97 UP,
98 PAGE_UP,
99 PAGE_DOWN,
100 HOME,
101 END,
102 CAPS_LOCK = 280,
103 SCROLL_LOCK,
104 NUM_LOCK,
105 PRINT_SCREEN,
106 PAUSE,
107 F1 = 290,
108 F2,
109 F3,
110 F4,
111 F5,
112 F6,
113 F7,
114 F8,
115 F9,
116 F10,
117 F11,
118 F12,
119 F13,
120 F14,
121 F15,
122 F16,
123 F17,
124 F18,
125 F19,
126 F20,
127 F21,
128 F22,
129 F23,
130 F24,
131 F25,
132 KP_0 = 320,
133 KP_1,
134 KP_2,
135 KP_3,
136 KP_4,
137 KP_5,
138 KP_6,
139 KP_7,
140 KP_8,
141 KP_9,
142 KP_DECIMAL,
143 KP_DIVIDE,
144 KP_MULTIPLY,
145 KP_SUBTRACT,
146 KP_ADD,
147 KP_ENTER,
148 KP_EQUAL,
149 LEFT_SHIFT = 340,
150 LEFT_CONTROL,
151 LEFT_ALT,
152 LEFT_SUPER,
153 RIGHT_SHIFT,
154 RIGHT_CONTROL,
155 RIGHT_ALT,
156 RIGHT_SUPER,
157 MENU,
158 LAST
159};
160
161
174using InputButtonMap = std::unordered_map<std::string, std::vector<InputButton>>
175;
176
181public:
185 InputManager(const Window& window, InputButtonMap input_button_map);
186
189 void setCursorMode(CursorMode mode) const;
190
194 void toggleCursor() const;
195
196 static void update();
197
202 bool buttonDown(const std::string& action) const;
203
208 bool buttonUp(const std::string& action) const;
209
214 bool buttonPressed(const std::string& action) const;
215
216
222
226
227
233 coma::Vec3 getMouseRotation(const coma::Vec3& start_rotation) const;
234
240 void setMouseRotation(coma::Vec3& start_rotation, float sensitivity = 1.0f) const;
241
242private:
243 struct KeyState {
244 bool pushed = false;
245 bool pressed = false;
246 bool released = false;
247 };
248
249 static void GenericButtonCallback(int button, int action);
250 static void KeyCallback(GLFWwindow* window, int key, int scancode, int action,
251 int mods);
252 static void MouseButtonCallback(GLFWwindow* window, int button, int action,
253 int mods);
254 static void ScrollCallback(GLFWwindow* window, double xoffset,
255 double yoffset);
256 static void CursorCallback(GLFWwindow* window, double xpos, double ypos);
257
258 // holds the state of the buttons present on map_
259 inline static std::unordered_map<InputButton, KeyState> s_mapped_buttons;
260 // hold states of the buttons modified in the las frame so that they can be
261 // reset at the end of the frame
262 inline static std::vector<KeyState*> s_modified_keys;
263
264 //retrieves the states of all the buttons with an action
265 std::vector<KeyState> findKeyState(const InputButtonMap& map,
266 std::string s) const;
267
268 inline static CursorMode cursor_mode_;
269 inline static float sensitivity_ = 0.1f;
270 inline static float mouse_x_;
271 inline static float mouse_y_;
272 inline static float mouse_offset_x_;
273 inline static float mouse_offset_y_;
274
275 InputButtonMap map_;
276 GLFWwindow* window_;
277};
Interface to retrieve keyboard and mouse input.
Definition: input.hpp:180
coma::Vec2 getMousePosition() const
Gets the position of the mouse.
void setMouseRotation(coma::Vec3 &start_rotation, float sensitivity=1.0f) const
Updates a rotation based on the mouse offset.
bool buttonUp(const std::string &action) const
Checks if any of the buttons assigned to an action have been released during the frame.
coma::Vec3 getMouseRotation(const coma::Vec3 &start_rotation) const
Gets an updated rotation based on the mouse offset.
bool buttonPressed(const std::string &action) const
Checks if any of the buttons assigned to an action is currently held down
bool buttonDown(const std::string &action) const
Checks if any of the buttons assigned to an action have been pressed during the frame.
InputManager(const Window &window, InputButtonMap input_button_map)
Creates and binds an input map.
void toggleCursor() const
Alternates the CursorMode between Normal and Disabled.
coma::Vec2 getMouseOffset() const
Gets the pixels the mouse has displaced since the last frame.
void setCursorMode(CursorMode mode) const
Overrides the CursorMode of the Window.
Creates and updates a window.
Definition: window.hpp:20
represents mathematical vector with 2 components
Definition: vector_2.h:7
represents mathematical vector with 3 components
Definition: vector_3.h:12
CursorMode
Define cursor and mouse behaviors.
Definition: input.hpp:16
@ Hidden
The cursor is not displayed but behaves as if it were there.
@ Normal
Show the cursor.
@ Disabled
The window locks the cursor inside it and hides it, allowing you to infinitely move the mouse without...
std::unordered_map< std::string, std::vector< InputButton > > InputButtonMap
Structure that binds user-created actions and physical input.
Definition: input.hpp:175
InputButton
Allowed keyboard and mouse buttons.
Definition: input.hpp:23