JustAnotherDnDGame 0.1.0
Jeu de rôle tactique au d20, vue de dessus, en C++/Qt
Loading...
Searching...
No Matches
InputState.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Valentin Eloy
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7#include <cstddef>
8#include <cstdint>
9#include <vector>
10
12
17
18namespace hmi {
19
29enum class Key : std::uint16_t {
30 Backspace = 0x08,
31 Tab = 0x09,
32 Enter = 0x0D,
33 Shift = 0x10, // Maj : action de dash (`EX-CTRL-013`) ; liaison de mécanismes (éditeur, LOT-14)
34 Control = 0x11, // Ctrl : raccourcis d'édition (annuler/refaire, éditeur, LOT-14)
35 Escape = 0x1B,
36 Space = 0x20,
37 Left = 0x25,
38 Up = 0x26,
39 Right = 0x27,
40 Down = 0x28,
41 D0 = 0x30, // « 0 » : réinitialiser la caméra de l'éditeur (LOT-15)
42 A = 0x41, // touches lettres (codes VK_*) pour les schémas ZQSD / WASD
43 C = 0x43, // Ctrl+C : copier une zone (éditeur, LOT-15)
44 D = 0x44,
45 E = 0x45, // Interagir, touche clavier par défaut (jeu, LOT-63)
46 P = 0x50, // Essai immédiat du niveau en cours d'édition (éditeur, LOT-14)
47 Q = 0x51,
48 R = 0x52, // Ctrl+R : redimensionner par saisie directe (éditeur, LOT-16)
49 S = 0x53, // Ctrl+S : enregistrer (éditeur, LOT-14)
50 T = 0x54, // Outil « Texture par instance » (éditeur, LOT-45)
51 V = 0x56, // Ctrl+V : coller une zone (éditeur, LOT-15)
52 W = 0x57,
53 Y = 0x59, // Ctrl+Y : refaire (éditeur, LOT-14)
54 Z = 0x5A, // Ctrl+Z : annuler (éditeur, LOT-14)
55 F1 = 0x70, // Aide des raccourcis (éditeur, LOT-15)
56 F2 = 0x71, // Renommer le niveau en cours d'édition (éditeur, LOT-15)
57 F10 = 0x79, // Bascule la grille de repère (éditeur, LOT-15)
58};
59
66enum class MouseButton : std::uint8_t {
67 Left = 0,
68 Right = 1,
69 Middle = 2,
70 Count = 3,
71};
72
98public:
100 InputState() = default;
101
108 void beginFrame() noexcept;
109
118 void releaseAll() noexcept;
119
121 void onKeyDown(Key key) noexcept;
122
124 void onKeyUp(Key key) noexcept;
125
132 void onGamepadKeyDown(Key key) noexcept;
133
136 void onGamepadKeyUp(Key key) noexcept;
137
146 void onGamepadButtonDown(GamepadButton button) noexcept;
147
149 void onGamepadButtonUp(GamepadButton button) noexcept;
150
156 void onMouseMove(int x, int y) noexcept;
157
159 void onMouseButtonDown(MouseButton button) noexcept;
160
162 void onMouseButtonUp(MouseButton button) noexcept;
163
169 void onMouseWheel(int delta) noexcept;
170
175 void onCharTyped(wchar_t character);
176
181 void setGamepadConnected(bool connected) noexcept;
182
184 [[nodiscard]] bool gamepadConnected() const noexcept;
185
187 [[nodiscard]] bool keyDown(Key key) const noexcept;
188
190 [[nodiscard]] bool keyPressed(Key key) const noexcept;
191
193 [[nodiscard]] bool keyReleased(Key key) const noexcept;
194
196 [[nodiscard]] bool gamepadButtonDown(GamepadButton button) const noexcept;
197
199 [[nodiscard]] bool gamepadButtonPressed(GamepadButton button) const noexcept;
200
202 [[nodiscard]] bool gamepadButtonReleased(GamepadButton button) const noexcept;
203
205 [[nodiscard]] int mouseX() const noexcept;
206
208 [[nodiscard]] int mouseY() const noexcept;
209
211 [[nodiscard]] bool mouseButtonDown(MouseButton button) const noexcept;
212
214 [[nodiscard]] bool mouseButtonPressed(MouseButton button) const noexcept;
215
217 [[nodiscard]] bool mouseButtonReleased(MouseButton button) const noexcept;
218
224 [[nodiscard]] int wheelDelta() const noexcept;
225
230 [[nodiscard]] const std::vector<wchar_t>& typedCharacters() const noexcept;
231
232private:
234 static constexpr std::size_t KEY_COUNT = 256;
235
237 static constexpr std::size_t BUTTON_COUNT = static_cast<std::size_t>(MouseButton::Count);
238
239 std::array<bool, KEY_COUNT> _keysCurrent{};
240 std::array<bool, KEY_COUNT> _keysPrevious{};
241 std::array<bool, KEY_COUNT> _gamepadCurrent{};
242 std::array<bool, KEY_COUNT> _gamepadPrevious{};
244 std::array<bool, GAMEPAD_BUTTON_COUNT> _gamepadButtonsCurrent{};
245 std::array<bool, GAMEPAD_BUTTON_COUNT> _gamepadButtonsPrevious{};
246 std::array<bool, BUTTON_COUNT> _buttonsCurrent{};
247 std::array<bool, BUTTON_COUNT> _buttonsPrevious{};
248 int _mouseX = 0;
249 int _mouseY = 0;
250 int _wheelDelta = 0;
251 std::vector<wchar_t> _typedCharacters;
252 bool _gamepadConnected = false;
253};
254
255} // namespace hmi
Bouton manette logique, indépendant de toute action de jeu ou touche clavier.
void onKeyDown(Key key) noexcept
Marque key comme enfoncée dans l'état courant (source clavier).
Definition InputState.cpp:58
void beginFrame() noexcept
Ouvre une nouvelle frame : recopie l'état courant vers l'état précédent.
Definition InputState.cpp:29
bool keyDown(Key key) const noexcept
Definition InputState.cpp:142
bool gamepadButtonReleased(GamepadButton button) const noexcept
Definition InputState.cpp:182
InputState()=default
Construit un état vide (aucune touche ni bouton enfoncé, souris à l'origine).
bool keyPressed(Key key) const noexcept
Definition InputState.cpp:148
void onMouseButtonDown(MouseButton button) noexcept
Marque button comme enfoncé dans l'état courant.
Definition InputState.cpp:104
void onMouseButtonUp(MouseButton button) noexcept
Marque button comme relâché dans l'état courant.
Definition InputState.cpp:112
std::vector< wchar_t > _typedCharacters
Vidée à chaque beginFrame().
Definition InputState.h:251
bool gamepadButtonDown(GamepadButton button) const noexcept
Definition InputState.cpp:171
void onCharTyped(wchar_t character)
Enregistre un caractère tapé pour la frame courante (WM_CHAR).
Definition InputState.cpp:126
bool gamepadButtonPressed(GamepadButton button) const noexcept
Definition InputState.cpp:176
const std::vector< wchar_t > & typedCharacters() const noexcept
Caractères tapés depuis le dernier beginFrame(), dans leur ordre de saisie.
Definition InputState.cpp:223
static constexpr std::size_t BUTTON_COUNT
Nombre de boutons de souris suivis.
Definition InputState.h:237
void onGamepadButtonUp(GamepadButton button) noexcept
Marque button comme relâché dans l'état courant (voir onGamepadButtonDown).
Definition InputState.cpp:93
bool _gamepadConnected
Definition InputState.h:252
std::array< bool, GAMEPAD_BUTTON_COUNT > _gamepadButtonsPrevious
Definition InputState.h:245
void onMouseWheel(int delta) noexcept
Accumule un incrément de molette pour la frame courante.
Definition InputState.cpp:121
void onGamepadKeyDown(Key key) noexcept
Marque key comme enfoncée dans l'état courant, source manette.
Definition InputState.cpp:74
int mouseY() const noexcept
Definition InputState.cpp:193
std::array< bool, BUTTON_COUNT > _buttonsCurrent
Definition InputState.h:246
int wheelDelta() const noexcept
Somme des incréments de molette accumulés depuis le dernier beginFrame().
Definition InputState.cpp:218
bool mouseButtonReleased(MouseButton button) const noexcept
Definition InputState.cpp:212
bool gamepadConnected() const noexcept
Definition InputState.cpp:136
int _wheelDelta
Remis à zéro à chaque beginFrame().
Definition InputState.h:250
int mouseX() const noexcept
Definition InputState.cpp:188
int _mouseY
Definition InputState.h:249
void onGamepadKeyUp(Key key) noexcept
Marque key comme relâchée dans l'état courant, source manette (voir onGamepadKeyDown).
Definition InputState.cpp:81
std::array< bool, KEY_COUNT > _gamepadPrevious
Definition InputState.h:242
std::array< bool, GAMEPAD_BUTTON_COUNT > _gamepadButtonsCurrent
Piste manette brute (GamepadButton), indépendante de la fusion Key ci-dessus.
Definition InputState.h:244
bool mouseButtonPressed(MouseButton button) const noexcept
Definition InputState.cpp:205
void onGamepadButtonDown(GamepadButton button) noexcept
Marque button comme enfoncé dans l'état courant (piste manette brute).
Definition InputState.cpp:89
void setGamepadConnected(bool connected) noexcept
Déclare l'état de connexion de la manette pour cette frame (EX-CTRL-002).
Definition InputState.cpp:131
void onMouseMove(int x, int y) noexcept
Met à jour la position de la souris.
Definition InputState.cpp:98
void releaseAll() noexcept
Relâche toutes les entrées maintenues (clavier, manette, souris), sans front.
Definition InputState.cpp:43
std::array< bool, KEY_COUNT > _keysPrevious
Definition InputState.h:240
std::array< bool, KEY_COUNT > _keysCurrent
Definition InputState.h:239
std::array< bool, KEY_COUNT > _gamepadCurrent
Source manette, distincte du clavier.
Definition InputState.h:241
void onKeyUp(Key key) noexcept
Marque key comme relâchée dans l'état courant (source clavier).
Definition InputState.cpp:65
std::array< bool, BUTTON_COUNT > _buttonsPrevious
Definition InputState.h:247
bool keyReleased(Key key) const noexcept
Definition InputState.cpp:160
static constexpr std::size_t KEY_COUNT
Nombre de codes de touches suivis (couvre l'ensemble des codes virtuels Win32).
Definition InputState.h:234
int _mouseX
Definition InputState.h:248
bool mouseButtonDown(MouseButton button) const noexcept
Definition InputState.cpp:198
Definition AudioEngine.cpp:15
MouseButton
Bouton de la souris suivi par l'InputState.
Definition InputState.h:66
@ Count
Definition InputState.h:70
GamepadButton
Bouton (ou direction) manette remappable à une action de jeu (EX-CTRL-002, LOT-30).
Definition GamepadButton.h:22
@ Down
Definition GamepadButton.h:24
@ Up
Definition GamepadButton.h:23
@ Y
Definition GamepadButton.h:30
@ A
Definition GamepadButton.h:27
Key
Touche du clavier, identifiée par son code virtuel Win32.
Definition InputState.h:29
@ Escape
Definition InputState.h:35
@ C
Definition InputState.h:43
@ D0
Definition InputState.h:41
@ Z
Definition InputState.h:54
@ E
Definition InputState.h:45
@ P
Definition InputState.h:46
@ V
Definition InputState.h:51
@ Tab
Definition InputState.h:31
@ S
Definition InputState.h:49
@ W
Definition InputState.h:52
@ Shift
Definition InputState.h:33
@ Control
Definition InputState.h:34
@ F10
Definition InputState.h:57
@ T
Definition InputState.h:50
@ Backspace
Definition InputState.h:30
@ Space
Definition InputState.h:36
@ F1
Definition InputState.h:55
@ R
Definition InputState.h:48
@ Q
Definition InputState.h:47
@ Enter
Definition InputState.h:32
@ D
Definition InputState.h:44
@ F2
Definition InputState.h:56
@ Middle
Definition TextRenderer.h:29
@ Right
Definition TextRenderer.h:26
@ Left
Definition TextRenderer.h:26