vkmEngine 1.0.15
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 <optional>
7#include <string>
8#include <string_view>
9#include <vector>
10
11#include <glm/glm.hpp>
12#include <glm/gtc/quaternion.hpp>
13#include <nlohmann/json_fwd.hpp>
14
15#include "core/reflect.h"
16
17#include "platform/input/input_command.h"
18#include "platform/input/input_handle.h"
19
20namespace Vkm::Engine {
21
22class HostChrome;
23
29namespace InputActions {
36 inline constexpr const char* UI_CLICK = "UI/Click";
37
38} // namespace InputActions
39
45enum class InputSource : uint8_t {
46 Key,
47 MouseButton,
48 Count
49};
50
57 InputSource source = InputSource::Key;
58 int code = 0;
59 float scale = 1.0f;
60};
61
70std::string bindingName(const InputBinding& binding);
71
79class InputMap {
80 public:
81 InputMap();
82 ~InputMap() = default;
83
84 InputMap(const InputMap& other) = delete;
85 InputMap& operator=(const InputMap& other) = delete;
86
87 InputMap(InputMap && other) = delete;
88 InputMap& operator=(InputMap && other) = delete;
89
90 public:
100 void define(std::string_view action, std::vector<InputBinding> bindings);
101
109 void rebind(std::string_view action, std::vector<InputBinding> bindings);
110
115
124 nlohmann::json saveBindings() const;
125
134 void loadBindings(const nlohmann::json& saved);
135
142 const std::vector<InputBinding>& bindings(std::string_view action) const;
143
149 std::vector<std::string> actions() const;
150
163 void update(InputHandle& input, const HostChrome& chrome);
164
172 void setPointerOverUI(bool over) { m_pointerOverUI = over; }
173
181
189 void reset();
190
199 void beginTick(uint32_t tick);
200
209 void setView(const glm::quat& view) { m_view = view; }
210
222 bool setPayload(const uint8_t* bytes, size_t size);
223
233 int indexOf(std::string_view action) const;
234
243 uint64_t actionFingerprint() const;
244
253 bool held(std::string_view action) const;
254
261 bool pressed(std::string_view action) const;
262
269 bool released(std::string_view action) const;
270
277 float axis(std::string_view action) const;
278
290 bool held(const InputCommand& command, std::string_view action) const;
291
299 bool pressed(const InputCommand& command, std::string_view action) const;
300
308 bool released(const InputCommand& command, std::string_view action) const;
309
317 float axis(const InputCommand& command, std::string_view action) const;
318
326 bool pointerOverUI() const { return m_pointerOverUI; }
327
335 const glm::vec2& pointer() const { return m_pointer; }
336
344 const glm::vec2& pointerDelta() const { return m_pointerDelta; }
345
353 float wheel() const { return m_wheel; }
354
362 float uiWheel() const { return m_uiWheel; }
363
372 std::u32string_view text() const { return m_text; }
373
383 bool typed(int key) const { return key >= 0 && key <= MAX_KEY && m_typed[static_cast<size_t>(key)]; }
384
393 const std::optional<InputBinding>& struck() const { return m_struck; }
394
402 const InputCommand& command() const { return m_command; }
403
412 uint32_t actionCount() const { return m_nextSlot; }
413
414 private:
415 struct Action {
416 std::vector<InputBinding> bindings;
417 std::vector<InputBinding> defaults;
418
419 float value = 0.0f;
420 bool active = false;
421 bool wasActive = false;
422 int slot = -1;
423 };
424
428 enum class Holder : uint8_t {
429 Host,
430 UI,
431 Gameplay,
432 };
433
437 struct HeldPress {
438 InputSource source;
439 int code;
440 Holder holder;
441 };
442
448 void defineEngineActions();
449
456 const Action* find(std::string_view action) const;
457
466 Action& mutableAction(std::string_view action);
467
474 void assignSlot(Action& entry, std::string_view action);
475
490 bool heldElsewhere(const InputBinding& binding, bool down, bool hostHas, bool uiHas, bool forUI);
491
492 private:
498 std::map<std::string, Action, std::less<>> m_actions;
500 std::map<std::string, std::vector<InputBinding>, std::less<>> m_saved;
501
502 glm::vec2 m_pointer{0.0f};
503 glm::vec2 m_pointerDelta{0.0f};
504 float m_wheel = 0.0f;
505 float m_uiWheel = 0.0f;
506
507 std::u32string m_text;
508 std::bitset<MAX_KEY + 1> m_typed;
509 std::optional<InputBinding> m_struck;
510
511 InputCommand m_command;
512 glm::quat m_view{1.0f, 0.0f, 0.0f, 0.0f};
513 std::array<uint8_t, MAX_COMMAND_PAYLOAD> m_payload{};
514 uint8_t m_payloadSize = 0;
515 uint32_t m_pendingPressed = 0;
516 uint32_t m_pendingReleased = 0;
517 uint32_t m_nextSequence = 1;
518 uint32_t m_nextSlot = 0;
519
520 bool m_pointerOverUI = false;
521
527 std::vector<HeldPress> m_heldPresses;
528};
529
530} // namespace Vkm::Engine
531
532VKM_ENUM_NAMES(::Vkm::Engine::InputSource, "Key", "MouseButton")
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:326
int indexOf(std::string_view action) const
The command's slot for action, or -1 when it has none.
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.
void rebind(std::string_view action, std::vector< InputBinding > bindings)
The player's own bindings for action, its defaults kept; what a controls screen sets....
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:412
void setView(const glm::quat &view)
Say where the player is looking, for the commands that follow.
Definition input_map.h:209
std::u32string_view text() const
The characters typed this frame, in order, as UTF-32.
Definition input_map.h:372
const InputCommand & command() const
The command the current fixed step is running under.
Definition input_map.h:402
void setPointerOverUI(bool over)
Say whether the pointer is over a blocking element of the game's UI.
Definition input_map.h:172
bool released(const InputCommand &command, std::string_view action) const
Did it stop being active since the previous command?
const std::optional< InputBinding > & struck() const
The key or mouse button that went down this frame, bound or not.
Definition input_map.h:393
const glm::vec2 & pointerDelta() const
How far the pointer moved since the previous frame, in window pixels.
Definition input_map.h:344
bool typed(int key) const
Whether key went down or auto-repeated this frame.
Definition input_map.h:383
bool setPayload(const uint8_t *bytes, size_t size)
Say what the game sends beside the actions, for the commands that follow.
void define(std::string_view action, std::vector< InputBinding > bindings)
Define (or replace) an action and its default 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:353
float uiWheel() const
Wheel notches turned this frame, whatever is under the pointer.
Definition input_map.h:362
void loadBindings(const nlohmann::json &saved)
Rebinds each action saved names, as saveBindings wrote it.
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:335
void update(InputHandle &input, const HostChrome &chrome)
Sample every action from input, rolling the previous values.
void resetBindings()
Every action back to its defaults, the player's own bindings forgotten.
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.
nlohmann::json saveBindings() const
The actions the player rebound, each its bindings, for a settings file.
Action names the engine itself reads.
Definition input_map.h:29
constexpr const char * UI_CLICK
Press, hold and release of the game UI's primary pointer button.
Definition input_map.h:36
One physical control contributing to an action.
Definition input_map.h:56
int code
GLFW key or mouse-button code.
Definition input_map.h:58
float scale
Contribution to axis(); sign gives direction.
Definition input_map.h:59
Everything one simulation tick was told to do, as a value.
Definition input_command.h:28