Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Input.hpp
1#pragma once
2
3#include "input/controller/Controller.hpp"
4#include "Common.hpp"
5
10class Input {
11public:
13 enum class Key : uint {
14 Space = 0, Apostrophe, Comma, Minus, Period, Slash,
15 N0, N1, N2, N3, N4, N5, N6, N7, N8, N9, Semicolon, Equal,
16 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S,
17 T, U, V, W, X, Y, Z, LeftBracket, Backslash, RightBracket,
18 GraveAccent, World1, World2, Escape, Enter, Tab, Backspace,
19 Insert, Delete, Right, Left, Down, Up, PageUp, PageDown,
20 Home, End, CapsLock, ScrollLock, NumLock, PrintScreen, Pause,
21 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14,
22 F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, F25,
23 Pad0, Pad1, Pad2, Pad3, Pad4, Pad5, Pad6, Pad7, Pad8, Pad9,
24 PadDecimal, PadDivide, PadMultiply, PadSubtract, PadAdd,
25 PadEnter, PadEqual, LeftShift, LeftControl, LeftAlt, LeftSuper,
26 RightShift, RightControl, RightAlt, RightSuper, Menu, Count
27 };
28
30 enum class Mouse : uint {
31 Left = 0,
32 Right = 1,
33 Middle = 2,
34 Count = 3
35 };
36
44 void preferRawControllers(bool prefer);
45
57 void keyPressedEvent(int key, int action);
58
64 void joystickEvent(int joy, int event);
65
71 void mousePressedEvent(int button, int action);
72
78 void mouseMovedEvent(double x, double y);
79
85 void mouseScrolledEvent(double xoffset, double yoffset);
86
92 void resizeEvent(int width, int height);
93
98 void minimizedEvent(bool minimized);
99
104 void densityEvent(float density);
105
109 void update();
110
121 bool resized() const { return _resized; }
122
127 bool minimized() const { return _minimized; }
128
133 glm::ivec2 size() const { return glm::ivec2(_width, _height); }
134
139 bool controllerAvailable() const { return _activeController >= 0; }
140
146
152
159
165 bool pressed(const Key & keyboardKey) const;
166
173 bool triggered(const Key & keyboardKey, bool absorb = false);
174
181 bool released(const Key & keyboardKey, bool absorb = false);
182
188 bool pressed(const Mouse & mouseButton) const;
189
196 bool triggered(const Mouse & mouseButton, bool absorb = false);
197
204 bool released(const Mouse & mouseButton, bool absorb = false);
205
210 glm::vec2 mouse() const;
211
218 glm::vec2 moved(const Mouse & mouseButton) const;
219
225 glm::vec2 scroll() const;
226
230 float density() const;
231
235 bool interacted() const;
236
239private:
240 // State.
241
242 // Resize state.
243 unsigned int _width = 1;
244 unsigned int _height = 1;
245 bool _resized = false;
246 bool _minimized = false;
247 float _density = 1.0f;
248
249 // Joystick state.
251 std::unique_ptr<Controller> _controllers[16];
253 bool _joystickConnected = false;
255
257 struct MouseButton {
258 double x0 = 0.0;
259 double y0 = 0.0;
260 double x1 = 0.0;
261 double y1 = 0.0;
262 bool pressed = false;
263 bool first = false;
264 bool last = false;
265 };
266 MouseButton _mouseButtons[uint(Mouse::Count)];
267
269 struct MouseCursor {
270 double x = 0.0;
271 double y = 0.0;
272 glm::vec2 scroll = glm::vec2(0.0);
273 };
275
277 struct KeyboardKey {
278 bool pressed = false;
279 bool first = false;
280 bool last = false;
281 };
282 KeyboardKey _keys[uint(Key::Count)];
283
284 bool _mouseInteracted = false;
285 bool _keyInteracted = false;
286 bool _windowInteracted = false;
287
288 // Singleton management.
289
290public:
295 static Input & manager();
296
300 Input & operator=(const Input &) = delete;
301
303 Input(const Input &) = delete;
304
308 Input & operator=(Input &&) = delete;
309
311 Input(Input &&) = delete;
312
313private:
315 Input();
316
318 ~Input() = default;
319};
Represents a joystick or any additional controller.
Definition: Controller.hpp:9
The input manager is responsible for updating the internal input states (keyboard,...
Definition: Input.hpp:10
bool _resized
Denote if the window was resized at the current frame.
Definition: Input.hpp:245
unsigned int _height
Internal window height in pixels.
Definition: Input.hpp:244
std::unique_ptr< Controller > _controllers[16]
States of all possible controllers.
Definition: Input.hpp:251
void minimizedEvent(bool minimized)
Definition: Input.cpp:278
void keyPressedEvent(int key, int action)
Definition: Input.cpp:41
bool minimized() const
Definition: Input.hpp:127
void mouseScrolledEvent(double xoffset, double yoffset)
Definition: Input.cpp:264
Input & operator=(Input &&)=delete
bool _preferRawControllers
Should controller use the gamepad mappings or raw values.
Definition: Input.hpp:252
int _activeController
The active joystick ID, or -1 if no controller active.
Definition: Input.hpp:250
bool resized() const
Definition: Input.hpp:121
Key
Keys codes.
Definition: Input.hpp:13
Mouse
Mouse buttons codes.
Definition: Input.hpp:30
void mouseMovedEvent(double x, double y)
Definition: Input.cpp:258
bool _windowInteracted
Did the user interact with the window (minimize, resize,...).
Definition: Input.hpp:286
void mousePressedEvent(int button, int action)
Definition: Input.cpp:228
bool triggered(const Key &keyboardKey, bool absorb=false)
Definition: Input.cpp:322
glm::vec2 moved(const Mouse &mouseButton) const
Definition: Input.cpp:362
void densityEvent(float density)
Definition: Input.cpp:283
Input & operator=(const Input &)=delete
Input(Input &&)=delete
bool _minimized
Is the window minimized and thus hidden.
Definition: Input.hpp:246
bool released(const Key &keyboardKey, bool absorb=false)
Definition: Input.cpp:330
void resizeEvent(int width, int height)
Definition: Input.cpp:270
bool _joystickDisconnected
Has a joystick just been disconnected.
Definition: Input.hpp:254
Controller * controller() const
Definition: Input.hpp:158
MouseButton _mouseButtons[uint(Mouse::Count)]
States of all possible mouse buttons.
Definition: Input.hpp:266
void preferRawControllers(bool prefer)
Definition: Input.cpp:28
Input(const Input &)=delete
bool interacted() const
Definition: Input.cpp:378
bool _joystickConnected
Has a joystick just been connected.
Definition: Input.hpp:253
bool controllerConnected() const
Definition: Input.hpp:145
unsigned int _width
Internal window width in pixels.
Definition: Input.hpp:243
MouseCursor _mouse
State of the mouse cursor.
Definition: Input.hpp:274
void update()
Definition: Input.cpp:287
glm::vec2 scroll() const
Definition: Input.cpp:370
bool controllerDisconnected() const
Definition: Input.hpp:151
KeyboardKey _keys[uint(Key::Count)]
States of all possible keyboard keys.
Definition: Input.hpp:282
void joystickEvent(int joy, int event)
Definition: Input.cpp:185
Input()
Constructor (disabled).
Definition: Input.cpp:14
bool controllerAvailable() const
Definition: Input.hpp:139
static Input & manager()
Definition: Input.cpp:9
glm::vec2 mouse() const
Definition: Input.cpp:358
bool _mouseInteracted
Did the user interact with the mouse.
Definition: Input.hpp:284
~Input()=default
Destructor (disabled).
bool _keyInteracted
Did the user interact with the keyboard.
Definition: Input.hpp:285
bool pressed(const Key &keyboardKey) const
Definition: Input.cpp:318
float _density
The screen density.
Definition: Input.hpp:247
glm::ivec2 size() const
Definition: Input.hpp:133
float density() const
Definition: Input.cpp:374
Keyboard state.
Definition: Input.hpp:277
bool pressed
Is the key currently held.
Definition: Input.hpp:278
bool last
Is is the first since frame it was released.
Definition: Input.hpp:280
bool first
Is it the first frame it is held.
Definition: Input.hpp:279
Mouse state.
Definition: Input.hpp:257
double y0
Vertical coordinate at the beginning of the last press.
Definition: Input.hpp:259
double x1
Horizontal coordinate at the end of the last press.
Definition: Input.hpp:260
bool first
Is it the first frame it is held.
Definition: Input.hpp:263
double y1
Vertical coordinate at the end of the last press.
Definition: Input.hpp:261
double x0
Horizontal coordinate at the beginning of the last press.
Definition: Input.hpp:258
bool last
Is is the first frame since it was released.
Definition: Input.hpp:264
bool pressed
Is the button currently held.
Definition: Input.hpp:262
Mouse cursor state.
Definition: Input.hpp:269
double y
Current cursor vertical position.
Definition: Input.hpp:271
glm::vec2 scroll
Current amount of scroll.
Definition: Input.hpp:272
double x
Current cursor horizontal position.
Definition: Input.hpp:270