-
Notifications
You must be signed in to change notification settings - Fork 41
[input] Add gamepad support #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Keyboard and Mouse Input | ||
======================== | ||
|
||
We use `glfw3 <https://www.glfw.org/>`__ for window and input management with `callbacks <https://www.glfw.org/docs/3.3/input_guide.html#input_keyboard>`__ instead of `manual polling <https://www.glfw.org/docs/3.3/group__input.html#ga67ddd1b7dcbbaff03e4a76c0ea67103a>`__. This way, we ensure we are not missing a key event. For more information check out `glfw's input guide <https://www.glfw.org/docs/3.3/input_guide.html>`__. We use a wrapper class for input named ``Input``. Currently, this supports keyboard, mouse, and one gamepad. | ||
|
||
.. note:: | ||
We redirect input events to class methods which handle it. Because glfw is a C-style API, it is not possible to use class methods directly as callbacks for keyboard and mouse input data. To fix this, we set the glfw window user pointer to the class instance which contains the input callback methods. Then, we use a lambda to set up the class method as callback. All setups are done in ``Application::setup_window_and_input_callbacks``. For more information about this workaround, check out this `Stackoverflow issue <https://stackoverflow.com/questions/7676971/pointing-to-a-function-that-is-a-class-member-glfw-setkeycallback>`__. | ||
|
||
.. note:: | ||
It's not possible handle glfw input data in a thread which is separate from the thread which created the corresponding window. For more information, check out this `glfw forum post <https://discourse.glfw.org/t/multithreading-glfw/573>`__. | ||
|
||
Keyboard and Mouse Input | ||
------------------------ | ||
|
||
- Keyboard and mouse input data is managed in the ``KeyboardMouseInputData`` class. | ||
|
||
Gamepads | ||
-------- | ||
|
||
- Gamepad input data is managed in the ``GamepadInputData`` class. | ||
|
||
.. note:: | ||
We currently support only one gamepad. Refactoring of this is required. See `issue 612 <https://github.com/inexorgame/vulkan-renderer/issues/612>`__. | ||
|
||
Joysticks | ||
--------- | ||
|
||
.. note:: | ||
We do not yet support `joysticks <https://www.glfw.org/docs/3.3/input_guide.html#joystick>`__, but glfw allows us to support them in the future. See `issue 612 <https://github.com/inexorgame/vulkan-renderer/issues/612>`__. |
39 changes: 0 additions & 39 deletions
39
documentation/source/engine-overview/misc/keyboard-mouse-input.rst
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#include <GLFW/glfw3.h> | ||
#include <glm/detail/qualifier.hpp> | ||
#include <glm/glm.hpp> | ||
|
||
#include <array> | ||
#include <shared_mutex> | ||
#include <vector> | ||
|
||
namespace inexor::vulkan_renderer::input { | ||
/// @brief A wrapper for gamepad input data | ||
class GamepadInputData { | ||
private: | ||
std::array<glm::vec2, 2> m_current_joystick_axes{}; | ||
std::array<glm::vec2, 2> m_previous_joystick_axes{}; | ||
std::array<std::array<bool, GLFW_JOYSTICK_LAST + 1>, GLFW_GAMEPAD_BUTTON_LAST> m_button_states{}; | ||
bool m_joysticks_updated{false}; | ||
bool m_buttons_updated{false}; | ||
mutable std::shared_mutex m_input_mutex; | ||
|
||
public: | ||
GamepadInputData() = default; | ||
|
||
void press_button(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); | ||
|
||
void release_button(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); | ||
|
||
[[nodiscard]] bool is_button_pressed(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); | ||
|
||
[[nodiscard]] bool was_button_pressed_once(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); | ||
|
||
void set_joystick_axis(std::int32_t axis, float state, std::int32_t joystick = GLFW_JOYSTICK_1); | ||
|
||
[[nodiscard]] glm::vec2 current_joystick_axes(std::int32_t joystick); | ||
|
||
[[nodiscard]] glm::vec2 calculate_joystick_axes_delta(std::int32_t joystick); | ||
}; | ||
} // namespace inexor::vulkan_renderer::input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include "inexor/vulkan-renderer/input/gamepad_data.hpp" | ||
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | ||
|
||
#include <GLFW/glfw3.h> | ||
|
||
namespace inexor::vulkan_renderer::input { | ||
class Input { | ||
private: | ||
GamepadInputData m_gamepad_data; | ||
KeyboardMouseInputData m_kbm_data; | ||
|
||
public: | ||
Input() = default; | ||
|
||
GamepadInputData &gamepad_data() { | ||
return m_gamepad_data; | ||
} | ||
KeyboardMouseInputData &kbm_data() { | ||
return m_kbm_data; | ||
} | ||
|
||
/// @brief Call glfwSetKeyCallback. | ||
/// @param window The window that received the event. | ||
/// @param key The keyboard key that was pressed or released. | ||
/// @param scancode The system-specific scancode of the key. | ||
/// @param action GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT. | ||
/// @param mods Bit field describing which modifier keys were held down. | ||
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); | ||
|
||
/// @brief Call glfwSetCursorPosCallback. | ||
/// @param window The window that received the event. | ||
/// @param x_pos The new x-coordinate, in screen coordinates, of the cursor. | ||
/// @param y_pos The new y-coordinate, in screen coordinates, of the cursor. | ||
void cursor_position_callback(GLFWwindow *window, double x_pos, double y_pos); | ||
|
||
/// @brief Call glfwSetMouseButtonCallback. | ||
/// @param window The window that received the event. | ||
/// @param button The mouse button that was pressed or released. | ||
/// @param action One of GLFW_PRESS or GLFW_RELEASE. | ||
/// @param mods Bit field describing which modifier keys were held down. | ||
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods); | ||
|
||
/// @brief Call camera's process_mouse_scroll method. | ||
/// @param window The window that received the event. | ||
/// @param x_offset The change of x-offset of the mouse wheel. | ||
/// @param y_offset The change of y-offset of the mouse wheel. | ||
void mouse_scroll_callback(GLFWwindow *window, double x_offset, double y_offset); | ||
|
||
void update_gamepad_data(); | ||
|
||
void update(); | ||
}; | ||
} // namespace inexor::vulkan_renderer::input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.