Skip to main content

InputManager.ixx File

InputManager providing access to various input states. More...

Included Headers

#include <memory> #include <stdexcept> #include <cassert> #include <cmath> #include <helios.util.log.LogManager> #include <helios.util.log.Logger> #include <helios.window.Window> #include <helios.input.types.Key> #include <helios.input.InputAdapter> #include <helios.input.gamepad.GamepadState> #include <helios.input.types.Gamepad>

Namespaces Index

namespacehelios
namespaceinput

Input handling and management. More...

Classes Index

classInputManager

InputManager providing access to various input states. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::input::InputManager"

Description

InputManager providing access to various input states.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::input::InputManager"

Definition at line 22 of file InputManager.ixx.

22#define HELIOS_LOG_SCOPE "helios::input::InputManager"

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file InputManager.ixx
3 * @brief InputManager providing access to various input states.
4 */
5module;
6
7#include <memory>
8#include <stdexcept>
9#include <cassert>
10#include <cmath>
11
12export module helios.input.InputManager;
13
14import helios.input.types.Gamepad;
15import helios.input.gamepad.GamepadState;
16import helios.input.InputAdapter;
17import helios.input.types.Key;
18import helios.window.Window;
19import helios.util.log.Logger;
20import helios.util.log.LogManager;
21
22#define HELIOS_LOG_SCOPE "helios::input::InputManager"
23export namespace helios::input {
24
25 /**
26 * @brief InputManager providing access to various input states.
27 *
28 * The `InputManager` acts as an intermediary between the application and
29 * the underlying systems that provide input. Raw events are processed by
30 * InputAdapters owned by this `InputManager`.
31 *
32 * An `InputManager` allows querying `GamepadState` objects by calling
33 * `gamepadState()`. The method accepts an id that identifies the
34 * gamepad whose input state should be returned.
35 */
37
38 private:
39 /**
40 * @brief A pointer to the currently observed window, which might be nullptr.
41 */
42 const helios::window::Window* observedWin_ = nullptr;
43
44 /**
45 * @brief The InputAdapter owned by this InputManager.
46 */
47 std::unique_ptr<helios::input::InputAdapter> input_;
48
49 /**
50 * @brief A bitmask used for registering the gamepads that should be polled for inputs.
51 */
52 unsigned int gamepadMask_ = 0x00;
53
54 protected:
55 /**
56 * @brief Shared logger instance for all InputManager objects.
57 */
59
60
61 public:
62 /**
63 * @brief Creates a new InputManager with the specified InputAdapter.
64 *
65 * Ownership of the InputAdapter is transferred to this InputManager.
66 *
67 * @param input The InputAdapter used with this InputManager.
68 */
69 explicit InputManager(std::unique_ptr<helios::input::InputAdapter> input)
70 : input_(std::move(input)) {}
71
72
73 /**
74 * @brief Sets the window this InputManager will observe for input.
75 *
76 * All subsequent input queries will be performed on this window.
77 *
78 * @param win A const reference to the window to observe.
79 */
80 void observe(const helios::window::Window& win) noexcept {
81 observedWin_ = &win;
82 }
83
84
85 /**
86 * @brief Returns a non-owning pointer to the currently observed window.
87 *
88 * Returns `nullptr` if no window is currently observed.
89 *
90 * @return A non-owning pointer to the currently observed window, or `nullptr`.
91 */
92 [[nodiscard]] const helios::window::Window* observedWindow() const {
93 return observedWin_;
94 }
95
96
97 /**
98 * @brief Polls events from the currently observed window and registered gamepads.
99 *
100 * Calls the `pollEvents()` method of the observed window to process
101 * any pending window-related input events. This method should be called
102 * regularly, preferably once per frame. For updating `GamepadState`
103 * objects with their current input states, call `registerGamepads()`
104 * with a bitmask representing the gamepads to poll.
105 *
106 * @see Window::pollEvents
107 *
108 * @param deltaTime The time elapsed since the last frame, in seconds.
109 */
110 void poll(float deltaTime) noexcept {
111 if (observedWin_ != nullptr) {
112 observedWin_->pollEvents();
113 } else {
114 logger_.warn("No window to observe.");
115 }
116 input_->updateGamepadState(gamepadMask_);
117 }
118
119
120 /**
121 * @brief Returns true if the specified key is currently pressed, otherwise false.
122 *
123 * This method delegates to the underlying `InputAdapter`.
124 *
125 * @see InputAdapter::isKeyPressed
126 *
127 * @param key The key to check for the pressed state.
128 *
129 * @return True if the key is pressed; returns false if the observed window is not set.
130 */
131 [[nodiscard]] bool isKeyPressed(const helios::input::types::Key& key) const noexcept {
132 if (observedWin_ == nullptr) {
133 logger_.warn("No window to observe.");
134 return false;
135 }
136
137 return input_->isKeyPressed(key, *observedWin_);
138 }
139
140 /**
141 * @brief Returns true if the specified key is currently released, otherwise false.
142 *
143 * This method delegates to the underlying `InputAdapter`.
144 *
145 * @see InputAdapter::isKeyReleased
146 *
147 * @param key The key to check for the released state.
148 *
149 * @return True if the key is released; returns false if the observed window is not set.
150 */
151 [[nodiscard]] bool isKeyReleased(const helios::input::types::Key& key) const noexcept {
152 if (observedWin_ == nullptr) {
153 logger_.warn("No window to observe.");
154 return false;
155 }
156
157 return input_->isKeyReleased(key, *observedWin_);
158 }
159
160
161 /**
162 * @brief Explicitly tells this InputManager which gamepads to poll for input states
163 * in `poll()`.
164 *
165 * @param gamepadMask A bitmask representing the gamepad ids that should be observed by
166 * this InputManager (e.g. `registerGamepads(Gamepad::ONE | Gamepad::THREE)`).
167 *
168 * @return The bitmask this InputManager uses for polling gamepad states. If an invalid
169 * mask is provided, the method returns a mask that represents no gamepads.
170 */
171 unsigned int registerGamepads(unsigned int mask) noexcept {
172 const unsigned int maxMask = static_cast<int>(std::pow(
173 helios::input::types::Gamepad::size_,
174 2.0f
175 ) - 1);
176 assert(mask <= maxMask && "mask out of bounds");
177 if (mask > maxMask) {
178 logger_.warn("Gamepad mask out of bounds. Clamping to maximum valid value.");
179 mask = maxMask;
180 }
181
182
183 gamepadMask_ = mask;
184
185 return gamepadMask_;
186 }
187
188
189 /**
190 * @brief Returns a const reference to the `GamepadState` for the specified `gamepadId`.
191 *
192 * This method queries the `InputAdapter` owned by this `InputManager` for the
193 * `GamepadState` identified by the specified `gamepadId`. If no `GamepadState` exists
194 * for the given id, this method returns a `GamepadState` object initialized to its
195 * default values. To test whether a gamepad is available, use `isConnected()`.
196 *
197 * `GamepadState` objects are updated by calling `poll()`.
198 *
199 * @param gamepadId The id of the gamepad for which the `GamepadState` is queried.
200 *
201 * @return A const reference to the `GamepadState` for the specified gamepad.
202 *
203 * @see isConnected()
204 * @see poll()
205 */
207 return input_->gamepadState(gamepadId);
208 }
209
210 /**
211 * @brief Returns true if the specified gamepad is connected, otherwise false.
212 *
213 * @param gamepadId The id of the gamepad to test.
214 *
215 * @return True if a gamepad was found for the specified `gamepadId`, otherwise false.
216 */
217 [[nodiscard]] bool isConnected(helios::input::types::Gamepad gamepadId) const noexcept {
218 return input_->isConnected(gamepadId);
219 }
220
221
222 /**
223 * @brief Retrieves the InputAdapter associated with this InputManager.
224 *
225 * @return A reference to the InputAdapter associated with this InputManager.
226 */
227 [[nodiscard]] const helios::input::InputAdapter& inputAdapter() const noexcept {
228 return *input_;
229 }
230
231 /**
232 * @brief Retrieves the InputAdapter associated with this InputManager.
233 *
234 * This non-const overload allows modification of InputAdapter settings,
235 * such as gamepad deadzone and axis inversion configuration.
236 *
237 * @return A reference to the InputAdapter associated with this InputManager.
238 */
239 [[nodiscard]] helios::input::InputAdapter& inputAdapter() noexcept {
240 return *input_;
241 }
242
243 };
244
245
246
247} // namespace helios::input

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.