Skip to main content

GLFWInputAdapter Class

InputAdapter implementation for a GLFWWindow. More...

Declaration

class helios::ext::glfw::input::GLFWInputAdapter { ... }

Base class

classInputAdapter

Abstract interface for platform-specific input adapters. More...

Public Constructors Index

GLFWInputAdapter (std::unique_ptr< helios::input::gamepad::DeadzoneStrategy > deadzoneStrategy)

Constructs a GLFWInputAdapter with the specified deadzone strategy. More...

Public Member Functions Index

boolisKeyPressed (helios::input::types::Key key, const helios::window::Window &win) const noexcept override

Checks if a specific key is currently pressed for the given window. If the specified window is not of type GLFWWindow, this method always returns false. More...

boolisKeyReleased (helios::input::types::Key key, const helios::window::Window &win) const noexcept override

Checks if a specific key is currently released for the given window. More...

boolisKeyPressed (helios::input::types::Key key, const helios::ext::glfw::window::GLFWWindow &win) const noexcept

Checks if a specific key is currently pressed for the given GLFWWindow. More...

boolisKeyReleased (helios::input::types::Key key, const helios::ext::glfw::window::GLFWWindow &win) const noexcept

Checks if a specific key is currently released for the given GLFWWindow. More...

const helios::input::gamepad::GamepadState &gamepadState (helios::input::types::Gamepad gamepadId) const noexcept override

Returns a const ref to the GamepadState-object for the specified gamepadId. More...

voidupdateGamepadState (unsigned int gamepadMask) noexcept override

Updates the GamepadState objects with the values queried from the underlying Gamepad identified by the specified mask. More...

boolisConnected (helios::input::types::Gamepad gamepadId) const noexcept override

Returns a boolean value indicating the availability (i.e., connect state) of the gamepad identified by the specified gamepadId. More...

Private Member Attributes Index

std::array< helios::input::gamepad::GamepadState, std::to_underlying(helios::input::types::Gamepad::size_)>gamepadStates_ = {}

Array storing the current state of each connected gamepad. More...

Description

InputAdapter implementation for a GLFWWindow.

This class translates generic input queries into GLFW-specific API calls. Input queries are always executed against a GLFWWindow instance, which is to be specified by the caller.

The adapter applies the configured `DeadzoneStrategy` and `GamepadSettings` during gamepad state updates to normalize analog stick input and handle axis inversion.

See Also

InputAdapter for the abstract interface.

See Also

GamepadSettings for per-controller configuration.

See Also

DeadzoneStrategy for input normalization strategies.

Definition at line 46 of file GLFWInputAdapter.ixx.

Public Constructors

GLFWInputAdapter()

helios::ext::glfw::input::GLFWInputAdapter::GLFWInputAdapter (std::unique_ptr< helios::input::gamepad::DeadzoneStrategy > deadzoneStrategy)
inline

Constructs a GLFWInputAdapter with the specified deadzone strategy.

Parameters
deadzoneStrategy

The strategy used for analog stick normalization. Ownership is transferred to the base InputAdapter.

Definition at line 67 of file GLFWInputAdapter.ixx.

67 GLFWInputAdapter(std::unique_ptr<helios::input::gamepad::DeadzoneStrategy> deadzoneStrategy) :
68 helios::input::InputAdapter(std::move(deadzoneStrategy))
69 {}

Reference helios::input::InputAdapter::InputAdapter.

Public Member Functions

gamepadState()

const helios::input::gamepad::GamepadState & helios::ext::glfw::input::GLFWInputAdapter::gamepadState (helios::input::types::Gamepad gamepadId)
inline nodiscard noexcept virtual

Returns a const ref to the GamepadState-object for the specified gamepadId.

Implementing APIs can call this method in each frame to retrieve the current state for the gamepad, as computed by updateGamepadState(). This method guarantees that for a given gamepadId, a single GamepadState object is reused across calls, avoiding re-instantiation overhead.

Parameters
gamepadId

The id of the gamepad to query

Returns

Returns a const ref to the GamepadState. If the GamepadState was never queried before, or if a recent call to updateGamepadState did not succeed, the values of the GamepadState will not hold representative values. Implementing APIs should verify the availability of the Gamepad by calling isConnected()

See Also

updateGamepadState

See Also

isConnected

Definition at line 143 of file GLFWInputAdapter.ixx.

144 helios::input::types::Gamepad gamepadId) const noexcept override {
145 return gamepadStates_[
147 ];
148 }

Reference helios::ext::glfw::input::GLFWGamepadLookup::toArrayIndex.

isConnected()

bool helios::ext::glfw::input::GLFWInputAdapter::isConnected (helios::input::types::Gamepad gamepadId)
inline nodiscard noexcept virtual

Returns a boolean value indicating the availability (i.e., connect state) of the gamepad identified by the specified gamepadId.

Parameters
gamepadId

The gamepadId to query for availability.

Returns

true if the Gamepad identified by gamepadId is connected, otherwise false.

Definition at line 238 of file GLFWInputAdapter.ixx.

238 [[nodiscard]] bool isConnected(
239 helios::input::types::Gamepad gamepadId) const noexcept override {
240 const int glfwGamepadId = helios::ext::glfw::input::GLFWGamepadLookup::from(gamepadId);
241 return glfwJoystickPresent(glfwGamepadId) != 0;
242 }

Reference helios::ext::glfw::input::GLFWGamepadLookup::from.

isKeyPressed()

bool helios::ext::glfw::input::GLFWInputAdapter::isKeyPressed (helios::input::types::Key key, const helios::window::Window & win)
inline nodiscard noexcept virtual

Checks if a specific key is currently pressed for the given window. If the specified window is not of type GLFWWindow, this method always returns false.

Returns true if the key is pressed, otherwise false.

Parameters
key

The key to query for the `pressed` state.

win

The window instance from which the state should be queried.

Returns

True if the key is pressed, otherwise false.

Definition at line 77 of file GLFWInputAdapter.ixx.

77 [[nodiscard]] bool isKeyPressed(helios::input::types::Key key,
78 const helios::window::Window& win) const noexcept override {
79 auto const* win_ptr = dynamic_cast<helios::ext::glfw::window::GLFWWindow const*>(&win);
80
81 if (!win_ptr) {
82 logger_.warn("GLFWInput requires GLFWWindow");
83 return false;
84 }
85
86 return isKeyPressed(key, *win_ptr);
87 }

References isKeyPressed and helios::input::InputAdapter::logger_.

Referenced by isKeyPressed.

isKeyPressed()

bool helios::ext::glfw::input::GLFWInputAdapter::isKeyPressed (helios::input::types::Key key, const helios::ext::glfw::window::GLFWWindow & win)
inline nodiscard noexcept

Checks if a specific key is currently pressed for the given GLFWWindow.

Parameters
key

The helios key to check.

win

The `GLFWWindow` instance to query for the key-press.

Returns

true if the key is pressed, otherwise false.

Definition at line 116 of file GLFWInputAdapter.ixx.

117 const helios::ext::glfw::window::GLFWWindow& win) const noexcept {
118 return glfwGetKey(
119 win.nativeHandle(),
121 ) == GLFW_PRESS;
122 }

Reference helios::ext::glfw::input::GLFWKeyLookup::from.

isKeyReleased()

bool helios::ext::glfw::input::GLFWInputAdapter::isKeyReleased (helios::input::types::Key key, const helios::window::Window & win)
inline nodiscard noexcept virtual

Checks if a specific key is currently released for the given window.

If the specified window is not of type GLFWWindow, this method always returns false.

Returns true if the key is released, otherwise false.

Parameters
key

The key to query for the `released` state.

win

The window instance from which the state should be queried.

Returns

True if the key is released, otherwise false.

Definition at line 96 of file GLFWInputAdapter.ixx.

97 const helios::window::Window& win) const noexcept override {
98 auto const* win_ptr = dynamic_cast<helios::ext::glfw::window::GLFWWindow const*>(&win);
99
100 if (!win_ptr) {
101 logger_.warn("GLFWInput requires GLFWWindow");
102 return false;
103 }
104
105 return isKeyReleased(key, *win_ptr);
106 }

References isKeyReleased and helios::input::InputAdapter::logger_.

Referenced by isKeyReleased.

isKeyReleased()

bool helios::ext::glfw::input::GLFWInputAdapter::isKeyReleased (helios::input::types::Key key, const helios::ext::glfw::window::GLFWWindow & win)
inline nodiscard noexcept

Checks if a specific key is currently released for the given GLFWWindow.

Parameters
key

The helios key to check.

win

The `GLFWWindow` instance to query for the key-release.

Returns

true if the key is released, otherwise false.

Definition at line 132 of file GLFWInputAdapter.ixx.

133 const helios::ext::glfw::window::GLFWWindow& win) const noexcept {
134 return glfwGetKey(
135 win.nativeHandle(),
137 ) == GLFW_RELEASE;
138 }

Reference helios::ext::glfw::input::GLFWKeyLookup::from.

updateGamepadState()

void helios::ext::glfw::input::GLFWInputAdapter::updateGamepadState (unsigned int gamepadMask)
inline noexcept virtual

Updates the GamepadState objects with the values queried from the underlying Gamepad identified by the specified mask.

Updates the GamepadState objects with the current values of the underlying hardware. If querying the underlying hardware fails, this method will update the GamepadState values to a valid initial state (no movement or interaction). Implementations should use `isConnected()` to check whether the current values of a `GamepadState` object can be trusted.

Parameters
gamepadMask

A bitmask representing all GamepadState objects to update, e.g. Gamepad::ONE | Gamepad::TWO...

Definition at line 153 of file GLFWInputAdapter.ixx.

153 void updateGamepadState(unsigned int gamepadMask) noexcept override {
154 int index = 0;
155 while (gamepadMask != 0) {
156
157 unsigned int id = gamepadMask & 1;
158
159 if (id) {
160 // update gamepad
161 const auto gamepadId = static_cast<helios::input::types::Gamepad>(static_cast<unsigned int>(std::pow(2, index)));
162
163 int glfwGamepadId = helios::ext::glfw::input::GLFWGamepadLookup::from(gamepadId);
164
165 auto& gamepadSettings = InputAdapter::gamepadSettings(gamepadId);
166
167
168 if (glfwJoystickIsGamepad(glfwGamepadId)) {
169 GLFWgamepadstate state;
170 glfwGetGamepadState(glfwGamepadId, &state);
171
172 const float* axes = state.axes;
173 const unsigned char* buttons = state.buttons;
174
175 // left stick
176 float leftX = (gamepadSettings.invertLeftX() ? -1.0f : 1.0f) * axes[GLFW_GAMEPAD_AXIS_LEFT_X];
177 // invert left Y, glfw uses down:1, up:-1, we need down:-1, up:1
178 float leftY = (gamepadSettings.invertLeftY() ? -1.0f : 1.0f) * -axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
179 deadzoneStrategy_->normalize(gamepadSettings.leftStickDeadzone(), leftX, leftY);
180
181 // right stick
182 float rightX = (gamepadSettings.invertRightX() ? -1.0f : 1.0f) * axes[GLFW_GAMEPAD_AXIS_RIGHT_X];
183 // invert right Y, same logic as left Y
184 float rightY = (gamepadSettings.invertRightY() ? -1.0f : 1.0f) * -axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];
185 deadzoneStrategy_->normalize(gamepadSettings.rightStickDeadzone(), rightX, rightY);
186
187 gamepadStates_[index].updateAxes(
188 leftX,
189 leftY,
190 rightX,
191 rightY,
192 // normalize trigger values, since glfw returns them in the range
193 // [-1, 1]
194 axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER]/2 + 0.5f ,
195 axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER]/2 + 0.5f,
196
197 // button section
198 !!buttons[GLFW_GAMEPAD_BUTTON_A],
199 !!buttons[GLFW_GAMEPAD_BUTTON_B],
200 !!buttons[GLFW_GAMEPAD_BUTTON_X],
201 !!buttons[GLFW_GAMEPAD_BUTTON_Y],
202 !!buttons[GLFW_GAMEPAD_BUTTON_START],
203 !!buttons[GLFW_GAMEPAD_BUTTON_BACK],
204 !!buttons[GLFW_GAMEPAD_BUTTON_GUIDE],
205 !!buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER],
206 !!buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER],
207 !!buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB],
208 !!buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB],
209 !!buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP],
210 !!buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT],
211 !!buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN],
212 !!buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT]
213 );
214
215 } else {
216 // could also be a reset() method in GamepadState-object,
217 // but since we use it only once a manual assignment will do for now
218 gamepadStates_[index].updateAxes(
219 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
220 false, false, false, false, false,
221 false, false, false, false,
222 false, false, false, false,
223 false, false
224
225 );
226 }
227
228 } // if id
229
230 index++;
231 gamepadMask >>= 1;
232 }
233 }

References helios::input::InputAdapter::deadzoneStrategy_, helios::ext::glfw::input::GLFWGamepadLookup::from and helios::input::InputAdapter::gamepadSettings.

Private Member Attributes

gamepadStates_

std::array<helios::input::gamepad::GamepadState, std::to_underlying(helios::input::types::Gamepad::size_)> helios::ext::glfw::input::GLFWInputAdapter::gamepadStates_ = {}

Array storing the current state of each connected gamepad.

Indexed by gamepad ID, each entry holds the most recently polled button and axis states for that controller.

Definition at line 57 of file GLFWInputAdapter.ixx.

57 std::array<helios::input::gamepad::GamepadState, std::to_underlying(helios::input::types::Gamepad::size_)> gamepadStates_= {};

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.