vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
input_map.h
1#pragma once
2
3#include <cstdint>
4#include <bitset>
5#include <map>
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include <glm/glm.hpp>
11#include <glm/gtc/quaternion.hpp>
12
13#include "platform/input/input_command.h"
14#include "platform/input/input_handle.h"
15
16namespace Vkm::Engine {
17
18class HostChrome;
19
25namespace InputActions {
32 inline constexpr const char* UI_CLICK = "UI/Click";
33
34} // namespace InputActions
35
41enum class InputSource : uint8_t {
42 Key,
43 MouseButton,
44};
45
52 InputSource source = InputSource::Key;
53 int code = 0;
54 float scale = 1.0f;
55};
56
64class InputMap {
65 public:
66 InputMap();
67 ~InputMap() = default;
68
69 InputMap(const InputMap& other) = delete;
70 InputMap& operator=(const InputMap& other) = delete;
71
72 InputMap(InputMap && other) = delete;
73 InputMap& operator=(InputMap && other) = delete;
74
75 public:
83 void define(std::string_view action, std::vector<InputBinding> bindings);
84
91 void addBinding(std::string_view action, InputBinding binding);
92
98 void clearBindings(std::string_view action);
99
106 const std::vector<InputBinding>& bindings(std::string_view action) const;
107
113 std::vector<std::string> actions() const;
114
127 void update(InputHandle& input, const HostChrome& chrome);
128
136 void setPointerOverUI(bool over) { m_pointerOverUI = over; }
137
145
153 void reset();
154
163 void beginTick(uint32_t tick);
164
173 void setView(const glm::quat& view) { m_view = view; }
174
184 int indexOf(std::string_view action) const;
185
195 uint64_t actionFingerprint() const;
196
205 bool held(std::string_view action) const;
206
213 bool pressed(std::string_view action) const;
214
221 bool released(std::string_view action) const;
222
229 float axis(std::string_view action) const;
230
242 bool held(const InputCommand& command, std::string_view action) const;
243
251 bool pressed(const InputCommand& command, std::string_view action) const;
252
260 bool released(const InputCommand& command, std::string_view action) const;
261
269 float axis(const InputCommand& command, std::string_view action) const;
270
278 bool pointerOverUI() const { return m_pointerOverUI; }
279
287 const glm::vec2& pointer() const { return m_pointer; }
288
296 const glm::vec2& pointerDelta() const { return m_pointerDelta; }
297
305 float wheel() const { return m_wheel; }
306
314 float uiWheel() const { return m_uiWheel; }
315
324 std::u32string_view text() const { return m_text; }
325
335 bool typed(int key) const { return key >= 0 && key <= MAX_KEY && m_typed[static_cast<size_t>(key)]; }
336
344 const InputCommand& command() const { return m_command; }
345
354 uint32_t actionCount() const { return m_nextSlot; }
355
356 private:
357 struct Action {
358 std::vector<InputBinding> bindings;
359
360 float value = 0.0f;
361 bool active = false;
362 bool wasActive = false;
363 int slot = -1;
364 };
365
369 enum class Holder : uint8_t {
370 Host,
371 UI,
372 Gameplay,
373 };
374
378 struct HeldPress {
379 InputSource source;
380 int code;
381 Holder holder;
382 };
383
389 void defineEngineActions();
390
397 const Action* find(std::string_view action) const;
398
407 Action& mutableAction(std::string_view action);
408
415 void assignSlot(Action& entry, std::string_view action);
416
431 bool heldElsewhere(const InputBinding& binding, bool down, bool hostHas, bool uiHas, bool forUI);
432
433 private:
439 std::map<std::string, Action, std::less<>> m_actions;
440
441 glm::vec2 m_pointer{0.0f};
442 glm::vec2 m_pointerDelta{0.0f};
443 float m_wheel = 0.0f;
444 float m_uiWheel = 0.0f;
445
446 std::u32string m_text;
447 std::bitset<MAX_KEY + 1> m_typed;
448
449 InputCommand m_command;
450 glm::quat m_view{1.0f, 0.0f, 0.0f, 0.0f};
451 uint32_t m_pendingPressed = 0;
452 uint32_t m_pendingReleased = 0;
453 uint32_t m_nextSequence = 1;
454 uint32_t m_nextSlot = 0;
455
456 bool m_pointerOverUI = false;
457
463 std::vector<HeldPress> m_heldPresses;
464};
465
466} // namespace Vkm::Engine
What an authoring host tells the engine about the frame it draws over.
Definition host_chrome.h:18
One frame of raw device state: keys and buttons down, cursor position and wheel turn.
Definition input_handle.h:22
bool pointerOverUI() const
Whether the pointer was over a blocking element of the game's UI.
Definition input_map.h:278
int indexOf(std::string_view action) const
The command's slot for action, or -1 when it has none.
void addBinding(std::string_view action, InputBinding binding)
Add one binding to an action, defining it if new.
bool pressed(const InputCommand &command, std::string_view action) const
Did it become active since the previous command?
float axis(const InputCommand &command, std::string_view action) const
The action's value in command, -1..1.
uint64_t actionFingerprint() const
A fingerprint of which action holds which command slot.
void discardPendingEdges()
Drop the edges latched since the last command.
uint32_t actionCount() const
How many action slots are in use.
Definition input_map.h:354
void setView(const glm::quat &view)
Say where the player is looking, for the commands that follow.
Definition input_map.h:173
std::u32string_view text() const
The characters typed this frame, in order, as UTF-32.
Definition input_map.h:324
const InputCommand & command() const
The command the current fixed step is running under.
Definition input_map.h:344
void setPointerOverUI(bool over)
Say whether the pointer is over a blocking element of the game's UI.
Definition input_map.h:136
bool released(const InputCommand &command, std::string_view action) const
Did it stop being active since the previous command?
const glm::vec2 & pointerDelta() const
How far the pointer moved since the previous frame, in window pixels.
Definition input_map.h:296
bool typed(int key) const
Whether key went down or auto-repeated this frame.
Definition input_map.h:335
void define(std::string_view action, std::vector< InputBinding > bindings)
Define (or replace) an action's bindings.
void beginTick(uint32_t tick)
Build the command for tick from everything since the last one.
float axis(std::string_view action) const
Summed, clamped value of the action's bindings, -1..1.
bool released(std::string_view action) const
Did it stop being active this frame?
float wheel() const
Wheel notches turned this frame for gameplay, positive away from the viewer.
Definition input_map.h:305
float uiWheel() const
Wheel notches turned this frame, whatever is under the pointer.
Definition input_map.h:314
bool held(const InputCommand &command, std::string_view action) const
Is the action active in command? (the tick's answer)
std::vector< std::string > actions() const
Every defined action name, in name order.
const std::vector< InputBinding > & bindings(std::string_view action) const
Bindings currently feeding action.
bool pressed(std::string_view action) const
Did it become active this frame?
const glm::vec2 & pointer() const
Where the pointer is this frame, in window pixels.
Definition input_map.h:287
void update(InputHandle &input, const HostChrome &chrome)
Sample every action from input, rolling the previous values.
void clearBindings(std::string_view action)
Drop every binding for action, keeping the action defined.
bool held(std::string_view action) const
Is the action active this frame?
void reset()
Forget every action and every latched edge, then define the engine's own.
Action names the engine itself reads.
Definition input_map.h:25
constexpr const char * UI_CLICK
Press, hold and release of the game UI's primary pointer button.
Definition input_map.h:32
One physical control contributing to an action.
Definition input_map.h:51
int code
GLFW key or mouse-button code.
Definition input_map.h:53
float scale
Contribution to axis(); sign gives direction.
Definition input_map.h:54
Everything one simulation tick was told to do, as a value.
Definition input_command.h:25