R-Type 0.1
Loading...
Searching...
No Matches
InputSystem.hpp
1#pragma once
2
3#include "inputAction/inputMapping.hpp"
4#include "systems/RenderConcept.hpp"
5#include <ISystem.hpp>
6#include <functional>
7#include <stdexcept>
8
9namespace rtk::systems {
23template <ecs::concepts::IsRenderWindow WindowType>
24class InputSystem final : public rtk::ecs::ISystem {
25public:
32 InputSystem(std::reference_wrapper<WindowType> window,
33 rtk::InputState &inputState,
34 const rtk::Input::InputMapping &mapping)
35 : _window(window.get()), _inputState(inputState), _mapping(mapping) {};
36
37 ~InputSystem() override = default;
38
42 void onStart(rtk::ecs::Registry &) override {
43 LOG_DEBUG("Load Input System");
44 }
45
49 void onStop(rtk::ecs::Registry &) override {}
50
54 void update([[maybe_unused]] rtk::ecs::Registry &reg, float) override {
55 _inputState.actionMask = 0;
56 _inputState.closeRequested = false;
57
58 if (!_window.pollEvents(_event))
59 _inputState.closeRequested = true;
60
61 const auto &bindings = _mapping.getBindings();
62
63 for (uint8_t bit = 0; bit < rtk::Input::InputMapping::MAX_ACTIONS; ++bit) {
64
65 size_t startIndex = bit * rtk::Input::InputMapping::BINDINGS_PER_ACTION;
66
67 for (size_t slot = 0;
69 const auto &b = bindings[startIndex + slot];
70
72 continue;
73
74 bool isPressed = false;
75
77 isPressed = _event.isKeyPressed(b.key);
79 isPressed = _event.isGamepadButtonPressed(0, b.button);
81 isPressed = _event.isMouseButtonPressed(b.mouse);
83 float pression = _event.getGamepadAxis(0, b.axisData.axis);
84 isPressed = (b.axisData.validationPression < 0)
85 ? (pression <= b.axisData.validationPression)
86 : (pression >= b.axisData.validationPression);
87 }
88
89 if (isPressed) {
90 _inputState.actionMask |= (1 << bit);
91 break;
92 }
93 }
94 }
95
96 if (_event.isKeyPressed(ecs::concepts::Key::Escape))
97 _inputState.closeRequested = true;
98
99 if (_event.isKeyPressed(ecs::concepts::Key::F3) &&
100 _event.isKeyReleased(ecs::concepts::Key::B))
101 _inputState.debug = !_inputState.debug;
102 }
103
104private:
106 WindowType &_window;
107 rtk::InputState &_inputState;
108 const rtk::Input::InputMapping &_mapping;
109};
110
112} // namespace rtk::systems
Table binding up to four physical controls to each of 32 actions.
Definition inputMapping.hpp:54
static constexpr uint8_t BINDINGS_PER_ACTION
Number of controls that can trigger the same action.
Definition inputMapping.hpp:59
static constexpr uint8_t MAX_ACTIONS
Number of actions, one per bit of InputState::actionMask.
Definition inputMapping.hpp:57
const std::array< InputBinding, MAX_ACTIONS *BINDINGS_PER_ACTION > & getBindings() const
Returns every binding slot.
Definition inputMapping.hpp:121
Input query interface filled by the window backend.
Definition RenderConcept.hpp:186
bool isKeyReleased(Key key) const
Tells whether a key was released this frame.
Definition RenderConcept.hpp:225
bool isKeyPressed(Key key) const
Tells whether a key is held.
Definition RenderConcept.hpp:216
bool isGamepadButtonPressed(int joystickId, GamepadButton button) const
Tells whether a gamepad button is held.
Definition RenderConcept.hpp:271
bool isMouseButtonPressed(MouseButton button) const
Tells whether a mouse button is held.
Definition RenderConcept.hpp:234
float getGamepadAxis(int joystickId, GamepadAxis axis) const
Returns the position of a gamepad axis.
Definition RenderConcept.hpp:297
Polls window events and turns bound controls into action bits.
Definition InputSystem.hpp:24
InputSystem(std::reference_wrapper< WindowType > window, rtk::InputState &inputState, const rtk::Input::InputMapping &mapping)
Creates the system.
Definition InputSystem.hpp:32
void update(rtk::ecs::Registry &reg, float) override
Rebuilds the action mask from the current controls.
Definition InputSystem.hpp:54
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition InputSystem.hpp:49
void onStart(rtk::ecs::Registry &) override
Logs that the system is loaded.
Definition InputSystem.hpp:42
Player input sampled during the current frame.
Definition InputAction.hpp:14
uint32_t actionMask
Bit n is set while action n is active.
Definition InputAction.hpp:15
bool closeRequested
True when the window must close.
Definition InputAction.hpp:16
bool debug
True while the debug overlay is enabled.
Definition InputAction.hpp:17
@ GamepadButton
Gamepad button.
@ GamepadAxis
Gamepad axis past a threshold.