Engine
Raylib based game framework
Loading...
Searching...
No Matches
InputSystem.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "MyMath/MyVectors.hpp"
5#include "Types.hpp"
6#include <optional>
7
12
17enum class InputType : u8
18{
19 NONE,
20 KEY_PRESS,
21 KEY_PRESS_REPEAT,
22 KEY_DOWN,
23 KEY_RELEASE,
24 KEY_UP,
25 AXIS,
26 POSITION,
27 DELTA
28};
29
34enum class InputObject : u8
35{
36 KEYBOARD,
37 MOUSE,
38 GAMEPAD,
39};
40
45struct Input
46{
47 InputType type = InputType::NONE;
48 InputObject object = InputObject::KEYBOARD;
49
50 u32 key = 0;
51 u32 negativeKey = 0;
52 u32 gamepad = 0;
53
54 constexpr bool operator<=>(const Input&) const = default;
55};
56
62{
63 bool boolean = false;
64 float number = 0;
65 Vec2<float> vector;
66
67 constexpr bool operator<=>(const InputState&) const = default;
68};
69
78class InputSystem : public System
79{
80public:
81
89 void Update(const float deltaT) override;
90
98 void BindInput(const std::string& name, const Input& input);
99
104 void UnbindInput(const std::string& name);
105
111 std::optional<bool> GetBoolInput(const std::string& name);
112
118 std::optional<float> GetNumberInput(const std::string& name);
119
125 std::optional<Vec2<float>> GetVectorInput(const std::string& name);
126
132 void SetScaling(const float scale, const Vec2<float> offset);
133
134private:
135
136 static void UpdateKeyboard(Input& input, InputState& state);
137 void UpdateMouse(Input& input, InputState& state);
138 static void UpdateGamepad(Input& input, InputState& state);
139
140 std::unordered_map<std::string, std::vector<Input>> m_inputs;
141 std::unordered_map<std::string, InputState> m_states;
142
143 Vec2<float> m_scaledMousePos;
144 float m_scale = 1;
145 Vec2<float> m_offset;
146};
InputObject
Input device categories.
Definition InputSystem.hpp:35
InputType
Types of input queries.
Definition InputSystem.hpp:18
Engine lifelong systems and their managing.
Aggregates multiple physical inputs into logical actions.
Definition InputSystem.hpp:79
std::optional< float > GetNumberInput(const std::string &name)
Returns the float axis state of a logical binding.
Definition InputSystem.cpp:71
void UnbindInput(const std::string &name)
Removes a logical binding entirely.
Definition InputSystem.cpp:55
void SetScaling(const float scale, const Vec2< float > offset)
Sets mouse scaling and offset for POSITION/DELTA queries.
Definition InputSystem.cpp:285
void Update(const float deltaT) override
Updates all input states.
Definition InputSystem.cpp:5
std::optional< bool > GetBoolInput(const std::string &name)
Returns the boolean state of a logical binding.
Definition InputSystem.cpp:61
std::optional< Vec2< float > > GetVectorInput(const std::string &name)
Returns the vector (position/delta) state of a logical binding.
Definition InputSystem.cpp:81
void BindInput(const std::string &name, const Input &input)
Adds an input to a logical binding.
Definition InputSystem.cpp:44
Base class for all systems.
Definition SystemManager.hpp:27
Aggregated state for a named binding.
Definition InputSystem.hpp:62
Describes a single input binding (device, key/axis, type).
Definition InputSystem.hpp:46