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
InputHandler.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <vector> // to store the input sources
10#include <string> // to store names of registered actions
11
14
15// forward declaration for sf::Event (necessary for polling)
16namespace sf { class Event; }
17
18namespace Imago::Engine
19{
20
27 private:
28
29 //TODO have different vectors for differnt entries to get rid of the InputSource variant and unecessary data for non-axis inputs.
30
32 struct SourceEntry {
33 InputSource source;
34 ActionId action;
35 bool down = false;
36 float value = 0.0f;
37 float deadZone = 0.0f;
38 float threshold = 0.5f;
39 };
40
41 InputState _state;
42 std::vector<SourceEntry> _sources;
43
44 // --- private methods ---
45
51 void set_source_value(const InputSource& source, float magnitude);
52
57 void refresh_action(ActionId action);
58
62 void release_all();
63
64 public:
65
67 InputHandler() = default;
68
69 // explicitly disallow copy and move behavior
70 InputHandler(const InputHandler&) = delete;
71 InputHandler& operator=(const InputHandler&) = delete;
72 InputHandler(InputHandler&&) = delete;
73 InputHandler& operator=(InputHandler&&) = delete;
74
81 ActionId register_action(const std::string& name);
82
84 void add_input(ActionId action, const InputSource& source, float deadZone = 0.0f, float threshold = 0.5f);
85 void add_input(const std::string& name, const InputSource& source, float deadZone = 0.0f, float threshold = 0.5f);
86 void add_input(const std::string& name, sf::Keyboard::Scancode scancode);
87 void add_input(const std::string& name, sf::Mouse::Button button);
88 void add_input(const std::string& name, GamepadButton button);
89 void add_input(const std::string& name, GamepadAxis axis, float deadZone = 0.2f, float threshold = 0.5f);
90
91 //TODO remove_input once there is a rebinding menu
92
100 void handle_event(const sf::Event& event);
101
108 void handle_gamepad_button(GamepadButton button, bool down);
109
116 void handle_gamepad_axis(GamepadAxis axis, float magnitude);
117
122
126 void clear_edges();
127
131 InputState& state();
132 };
133}
Defines the raw inputs that drive an action.
std::variant< KeySource, MouseButtonSource, GamepadButtonSource, GamepadAxisSource > InputSource
Any single raw input that can drive an action. Is a std::variant of all the InputSource types.
Definition InputSource.hpp:85
Defines InputState, the read-only view of every action that sytems query.
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
void handle_gamepad_button(GamepadButton button, bool down)
Handles a Gamepad button event. Called by Game which determines what button the event is associated w...
Definition InputHandler.cpp:186
void handle_gamepad_axis(GamepadAxis axis, float magnitude)
Handles a Gamepad axis event. Called by Game which determines which axis had motion and what the magn...
Definition InputHandler.cpp:192
void add_input(ActionId action, const InputSource &source, float deadZone=0.0f, float threshold=0.5f)
Adds a raw input to an action. Overloaded so callers can call with action name or action id and Input...
Definition InputHandler.cpp:70
void handle_event(const sf::Event &event)
Updates input and action state from one window event. Handles key and mouse button presses and releas...
Definition InputHandler.cpp:167
void clear_edges()
Clears latched edges, keeps held state. Game calls this after a scene switch.
Definition InputHandler.cpp:283
InputHandler()=default
Constructs an InputHandler object.
void release_gamepad_inputs()
Releases all Gamepad inputs, called by Game when a controller is disconnected.
Definition InputHandler.cpp:263
InputState & state()
Returns the state that SceneContext::input points to.
Definition InputHandler.cpp:290
ActionId register_action(const std::string &name)
Registers an action name and returns its id.
Definition InputHandler.cpp:53
What systems query for input (ctx.input->began("jump")). This is only for querying....
Definition InputState.hpp:29