ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
InputState.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <cstdint> // for uint32_t for ActionId
10#include <vector> // for storing ActionStates
11#include <unordered_map> // for storing string to action map
12#include <string> // for action names
13#include <glm/vec2.hpp> // for directional returns
14
15namespace Imago::Engine
16{
18 using ActionId = uint32_t;
19
21 inline constexpr ActionId INVALID_ACTION = UINT32_MAX;
22
29 class InputState {
30 private:
31
32 // friend classes, the InputHandler needs to write actions and the scheduler needs to tell the state which phase is running
33 friend class InputHandler;
34 friend class SystemScheduler;
35
36 friend class InputStateTestAccess; //TODO only for test access, remove in release
37
39 enum class Phase { Frame, Fixed };
40
42 struct ActionState {
43 float strength = 0.0f;
44 bool held = false;
45 bool beganFrame = false;
46 bool endedFrame = false;
47 bool beganFixed = false;
48 bool endedFixed = false;
49 };
50
51 // --- input storage data ---
52
53 std::vector<ActionState> _actions;
54 std::unordered_map<std::string, ActionId> _ids;
55 Phase _phase = Phase::Frame;
56
57 // --- private methods ---
58
64 ActionId add_action(const std::string& name);
65
72 void set_state(ActionId action, float strength, bool held);
73
79 void clear_edges();
80
85 const ActionState* get(ActionId action) const;
86
93 ActionState* get(ActionId action);
94
100 ActionId resolve(const std::string& name) const;
101
102 // --- scheduler hooks, called from SystemScheduler::tick() ---
103
105 void begin_frame_phase();
106
108 void end_frame_phase();
109
111 void begin_fixed_step();
112
114 void end_fixed_step();
115
116 public:
117
119 InputState() = default;
120
121 // explicity disallow copy and move behavior
122 InputState(const InputState&) = delete;
123 InputState& operator=(const InputState&) = delete;
124 InputState(InputState&&) = delete;
125 InputState& operator=(InputState&&) = delete;
126
131 ActionId action_id(const std::string& name) const;
132
133 // --- user api methods ---
134
136 bool held(ActionId action) const;
137 bool held(const std::string& action) const;
138
140 bool began(ActionId action) const;
141 bool began(const std::string& action) const;
142
144 bool ended(ActionId action) const;
145 bool ended(const std::string& action) const;
146
152 float strength(ActionId action) const;
153 float strength(const std::string& action) const;
154
160 float axis(ActionId negative, ActionId positive) const;
161 float axis(const std::string& negative, const std::string& positive) const;
162
168 glm::vec2 direction(ActionId left, ActionId right, ActionId up, ActionId down) const;
169 glm::vec2 direction(const std::string& left, const std::string& right, const std::string& up, const std::string& down) const;
170 };
171}
uint32_t ActionId
Id of a registered action, resolve a name once with action_id() and cache it for the fast path.
Definition InputState.hpp:18
constexpr ActionId INVALID_ACTION
The bits that should be returned by action_id() when a name is not registered.
Definition InputState.hpp:21
float strength(ActionId action) const
returns how much the action is engaged, from 0 to 1. Overloaded so it can be called with an action's ...
Definition InputState.cpp:228
InputState()=default
Construct a new InputState object.
bool held(ActionId action) const
returns true while the action is being held down. Overloaded so it can be called with an action's nam...
Definition InputState.cpp:168
bool ended(ActionId action) const
returns true when the action stopped being held. Overloaded so it can be called with an action's name...
Definition InputState.cpp:208
glm::vec2 direction(ActionId left, ActionId right, ActionId up, ActionId down) const
Returns result of a direction in 2D from four lines of input. Overloaded so it can be called with an ...
Definition InputState.cpp:264
ActionId action_id(const std::string &name) const
Returns an action id from its name. Returns INVALID_ACTION for unregisterd names. Used to cache ids.
Definition InputState.cpp:119
float axis(ActionId negative, ActionId positive) const
Returns result of a direction from one line of input, opposite inputs of the same strength cancel out...
Definition InputState.cpp:248
bool began(ActionId action) const
returns true when the action started being held. Overloaded so it can be called with an action's name...
Definition InputState.cpp:188