-
Notifications
You must be signed in to change notification settings - Fork 34
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
[input] Added controller support #518
base: main
Are you sure you want to change the base?
Conversation
Nice work! I think I will buy myself a controller just to check if it's working. Will review later |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
[[nodiscard]] glm::ivec2 get_cursor_pos() const; | ||
|
||
/// @brief Calculate the change in x- and y-position of the cursor. | ||
/// @return a std::array of size 2 which contains the change in x-position in index 0 and the change in y-position | ||
/// in index 1 | ||
[[nodiscard]] std::array<double, 2> calculate_cursor_position_delta(); | ||
[[nodiscard]] glm::dvec2 calculate_cursor_position_delta(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use glm types now everywhere or do we abstract them away to keep independent?
@IAmNotHanni @yeetari
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using glm here is ok.
@@ -0,0 +1,212 @@ | |||
[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file shouldn't be here.
#include "glm/detail/qualifier.hpp" | ||
#include <GLFW/glfw3.h> | ||
#include <vector> | ||
|
||
#define GLM_PRECISION_HIGHP_DOUBLE | ||
#define GLM_PRECISION_HIGHP_INT | ||
#include <glm/glm.hpp> | ||
|
||
#include <array> | ||
#include <shared_mutex> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "glm/detail/qualifier.hpp" | |
#include <GLFW/glfw3.h> | |
#include <vector> | |
#define GLM_PRECISION_HIGHP_DOUBLE | |
#define GLM_PRECISION_HIGHP_INT | |
#include <glm/glm.hpp> | |
#include <array> | |
#include <shared_mutex> | |
#include <glm/detail/qualifier.hpp> | |
#include <GLFW/glfw3.h> | |
#define GLM_PRECISION_HIGHP_DOUBLE | |
#define GLM_PRECISION_HIGHP_INT | |
#include <glm/glm.hpp> | |
#include <array> | |
#include <shared_mutex> | |
#include <vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes: We have rules for ordering include directives.
#include <GLFW/glfw3.h> | ||
|
||
#include "inexor/vulkan-renderer/input/gamepad_data.hpp" | ||
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <GLFW/glfw3.h> | |
#include "inexor/vulkan-renderer/input/gamepad_data.hpp" | |
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | |
#include "inexor/vulkan-renderer/input/gamepad_data.hpp" | |
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | |
#include <GLFW/glfw3.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vulkan-renderer/input/keyboard_mouse_data.cpp | ||
vulkan-renderer/input/gamepad_data.cpp | ||
vulkan-renderer/input/input.cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vulkan-renderer/input/keyboard_mouse_data.cpp | |
vulkan-renderer/input/gamepad_data.cpp | |
vulkan-renderer/input/input.cpp | |
vulkan-renderer/input/gamepad_data.cpp | |
vulkan-renderer/input/input.cpp | |
vulkan-renderer/input/keyboard_mouse_data.cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: keep in alphabetical order
#include <inexor/vulkan-renderer/input/gamepad_data.hpp> | ||
#include <mutex> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <inexor/vulkan-renderer/input/gamepad_data.hpp> | |
#include <mutex> | |
#include "inexor/vulkan-renderer/input/gamepad_data.hpp" | |
#include <mutex> |
#include "GLFW/glfw3.h" | ||
#include "spdlog/spdlog.h" | ||
#include <inexor/vulkan-renderer/input/input.hpp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "GLFW/glfw3.h" | |
#include "spdlog/spdlog.h" | |
#include <inexor/vulkan-renderer/input/input.hpp> | |
#include "inexor/vulkan-renderer/input/input.hpp" | |
#include <GLFW/glfw3.h> | |
#include <spdlog/spdlog.h> |
#include <inexor/vulkan-renderer/input/input.hpp> | ||
|
||
namespace inexor::vulkan_renderer::input { | ||
void Input::key_callback(GLFWwindow * /*window*/, int key, int, int action, int /*mods*/) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void Input::key_callback(GLFWwindow * /*window*/, int key, int, int action, int /*mods*/) { | |
void Input::key_callback(GLFWwindow * /*window*/, int key, int /*scancode*/, int action, int /*mods*/) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes: That's the standard way of saying "there once was a parameter" and also to mention the name of the parameter.
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | ||
#include "GLFW/glfw3.h" | ||
#include "glm/fwd.hpp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | |
#include "GLFW/glfw3.h" | |
#include "glm/fwd.hpp" | |
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" | |
#include <GLFW/glfw3.h> | |
#include <glm/fwd.hpp> |
#include "inexor/vulkan-renderer/wrapper/window.hpp" | ||
#include "GLFW/glfw3.h" | ||
|
||
#include <spdlog/spdlog.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include "inexor/vulkan-renderer/wrapper/window.hpp" | |
#include "GLFW/glfw3.h" | |
#include <spdlog/spdlog.h> | |
#include "inexor/vulkan-renderer/wrapper/window.hpp" | |
#include <GLFW/glfw3.h> | |
#include <spdlog/spdlog.h> |
Other things: This will only support one Gamepad right? not multiple? |
@IceflowRE About multiple gamepads/joysticks: I just ordered several online. We should do it in another pull request pls. |
https://inexor-vulkan-renderer.readthedocs.io/en/latest/development/reference/keyboard-mouse-input.html
That's no longer true. Also, the ref pages needs to be renamed to "keyboard, mouse and joystick input" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, code looks good, please could you squash the commits though.
#define GLM_PRECISION_HIGHP_DOUBLE | ||
#define GLM_PRECISION_HIGHP_INT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define GLM_PRECISION_HIGHP_DOUBLE | |
#define GLM_PRECISION_HIGHP_INT |
These don't do anything.
|
||
if (m_camera->type() == CameraType::LOOK_AT && m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT)) { | ||
auto deadzone_lambda = [](const float state) { | ||
if (state > -0.2f && state < 0.2f) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How were these constants chosen? Also it might be easier written as glm::abs(state) < 0.2f
(at least for me it looks pretty confusing at first)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the constants should be named as static constexpr WHAT_IS_MY_NAME{0.2f};
m_camera->set_movement_state(CameraMovement::LEFT, m_input_data->is_key_pressed(GLFW_KEY_A)); | ||
m_camera->set_movement_state(CameraMovement::BACKWARD, m_input_data->is_key_pressed(GLFW_KEY_S)); | ||
m_camera->set_movement_state(CameraMovement::RIGHT, m_input_data->is_key_pressed(GLFW_KEY_D)); | ||
if (m_camera->type() == CameraType::LOOK_AT) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works for now, though in future the Input
class should abstract away whether the input event came from kb&m or controller, with a mapping system (so both a controller and mouse could be mapped to a "vertical" and "horizontal" axis that the application can query from m_input
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah in the future I want to refactor the camera code anyways (see #474)
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_GAMEPAD_BUTTON_LAST + 1>, GLFW_GAMEPAD_BUTTON_LAST> m_button_states{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the outer array size should be GLFW_JOYSTICK_LAST + 1
?
} | ||
|
||
void Input::update_gamepad_data() { | ||
if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1) == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could early return here to make it a little easier to read.
Added controller support.
Abstracted input by adding the Input class, overarching both KeyboardMouseInputData and the also newly added GamepadInputData.