Skip to content

Add initial support for mouse and gamepad input#29

Merged
Nostritius merged 24 commits intoOpenAWE-Project:masterfrom
maaxxaam:mouse-handling
Jul 9, 2023
Merged

Add initial support for mouse and gamepad input#29
Nostritius merged 24 commits intoOpenAWE-Project:masterfrom
maaxxaam:mouse-handling

Conversation

@maaxxaam
Copy link
Contributor

@maaxxaam maaxxaam commented Mar 28, 2023

Adds support for capturing and adding bindings to mouse movement and button clicks. Also adds an option to lock mouse inside game's window. Includes a modified ControlledFreeCamera that uses mouse movement for camera rotation.

Related to #13

@maaxxaam maaxxaam marked this pull request as ready for review March 28, 2023 13:49
@maaxxaam
Copy link
Contributor Author

maaxxaam commented Mar 29, 2023

Noticed something weird: I cannot use my laptop's trackpad together with the keyboard - looks like mouse movement events get suppresed. External mouse works okay though.

Edit: after some research, it appears that this behaviour comes from OS settings and has nothing to do with the engine itself.

Use vertical mouse scroll for Y movement to test 1D mouse bindings
Copy link
Member

@Nostritius Nostritius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my first review for this one. Apart from the things, already pointed out, I would like to see one Controlled freecamera instead of two separate, ideally in a modular way, so you have methods like "setMouseInputDisabled".
I have not yet tested, the functionality of the camera in practice, I will probably follow up with comments when I have done that.


typedef std::variant<
AxisEvent<float>,
AxisEvent<double>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you use this one somewhere? If not, it should be deleted

maaxxaam added 4 commits April 4, 2023 14:36
That removes getMouseLastPosiiton() as a public method since its used
only within Window class now.
Also renames lock/unlock mouse functions for clarity.
InputManager is used to expand upon GLFW callbacks, adding button hold
state and some stub code for future gamepad support
@maaxxaam
Copy link
Contributor Author

maaxxaam commented Apr 5, 2023

Commit a520a7e changes the keyboard layout:
WASD, R/F and T/G have not changed. Camera rotation uses Arrow keys (both horizontal and vertical rotation), and camera rotation sensitivity can be changed separately now with Y/H. Q allows switching between arrow keys and mouse movement for camera rotation control.

@maaxxaam maaxxaam requested a review from Nostritius April 5, 2023 17:13
@maaxxaam maaxxaam changed the title Add initial support for mouse input Add initial support for mouse and gamepad input Apr 8, 2023
Events::Gamepad2DAxis convertGamepadStick(int stick) {
switch (stick) {
case 0: return Events::Gamepad2DAxis::kGamepadAxisLeft;
case 1: return Events::Gamepad2DAxis::kGamepadAxisRight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the glfw nacros in these functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GLFW does not distinguish gamepad sticks and triggers by default - it treats them all as 1D axes, so library macros are weird to use here (like GLFW_GAMEPAD_AXIS_LEFT_X for the whole left stick?). I can create an enum for them, though, and switch to using macros for triggers axes.

src/game.cpp Outdated
EventMan.setActionCallback({ kLockMouse }, [&](Events::Event event){_window->hideMouseCursor();});
EventMan.setActionCallback({ kUnlockMouse }, [&](Events::Event event){_window->showMouseCursor();});
EventMan.addBinding(kLockMouse, Events::kMouseLeft);
EventMan.addBinding(kUnlockMouse, Events::kKeyEscape);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep the lock/unlock mouse mechanic on the layer of the game class, it would probably be simpler to jus put this into the callback. And please define a unified key combination to lock the camera, one which is probably never used in the game, for example alt + l.

Copy link
Member

@Nostritius Nostritius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the new freecamera today and I like it so far. I think it would be beneficial to have a README hint, which keys control the camera how.
As I have written in the last Review , I'm not really happy with adding a new event type where we have to do all the injecting by ourselves which adds a lot of code. So I would prefer a solution which would be able to work without this hold event. As I have written, would the utilizing of the update function help?

glfwSetFramebufferSizeCallback(_window, &Window::callbackFramebufferSize);
GamepadMan.setGamepadButtonCallback(_window, &Window::callbackGamepadButton);
GamepadMan.setGamepadTriggerCallback(_window, &Window::callbackGamepadTrigger);
GamepadMan.setGamepadStickCallback(_window, &Window::callbackGamepadStick);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Gamepad code should stay independent of the window, since the gamepads are in no way associated with it. The gamepad callbacks should be initialized in the game class directly over the GamepadMan singleton.

*/
enum KeyState {
kPress,
kHold,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point especially with the mouse stuff. Would it be an option if you rather implement the update(float delta) method?

maaxxaam added 3 commits May 28, 2023 22:43
FreeCamera now has flags for whether to clear direction/rotation
after update(delta) or not. This allows to handle both keyboard and
mouse/gamepad events without KeyManager. Also includes some tweaks
according to the PR's feedback.
Removes hold state from both event.h and GamepadManager,
since keyboard keys don't have hold state now.
CI failed because, apparently, fmt cannot format an Enum (see
prev. two commits). Don't know why it started to fail now
(code's been there for a while), but a simple conversion to
an int should hopefully suffice.
@maaxxaam maaxxaam requested a review from Nostritius May 28, 2023 22:13
Copy link
Member

@Nostritius Nostritius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code crashes at my laptop when executed in the gamepad manager. furthermore I have some additional comments,

void pollGamepadEvents();
void setGamepadButtonCallback(GLFWwindow *window, InputGamepadButtonCallback function);
void setGamepadTriggerCallback(GLFWwindow *window, InputGamepadTriggerCallback function);
void setGamepadStickCallback(GLFWwindow *window, InputGamepadStickCallback function);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gamepad callbacks do not need a reference to the GLFWwindow

w->_mouseButtonCallback(button, action, mods);
}

void Window::callbackGamepadButton(GLFWwindow *window, int button, int action) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you misunderstood me here, the gamepad callbacks should be moved into the GamepadMan and the Window should have absolutely nothing to do with the gamepads.

@maaxxaam maaxxaam requested a review from Nostritius June 23, 2023 11:28
Since both movement and camera rotation should have worked with them
from the beginning in this case
Copy link
Member

@Nostritius Nostritius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left three comments for the code. One thing I have to add, it would be nice if the locking of the mouse cursor is off by default, just to prevent someone inexperienced being caught in the window.

}
}

void EventManager::injectMouse1DAxisInput(Events::Mouse1DAxis axis, float position, float delta) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see in the current state, you are not using the 1D Axis inputs. Do we need them in the future? If not, I would prefer to remove them for simplifying the whole interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, they are used for separating mouse scroll inputs (that are 2D by default) into horizontal and vertical components. Here is a snippet for callback assignment from game.cpp:

_window->setMouseScrollCallback([&](glm::vec2 absolute, glm::vec2 delta){
		EventMan.injectMouse1DAxisInput(Events::kMouseScrollHorizontal, absolute.x, delta.x);
		EventMan.injectMouse1DAxisInput(Events::kMouseScrollVertical, absolute.y, delta.y);
	});

- [TG] to increase/decrease movement speed
# Camera
- [←→↑↓] to look left/right/up/down
- [YH] to increase/decrease camera speed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you use a QWERTY keyboard? Since I am using a german QWERTZ keyboard this combination is a little bit counter intuitive for me. Maybe you have an idea how to handle this better, if not we can leave it this way for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when I'm referring to keys here, I imply QWERTY layout. Also not sure yet of what can be done here for now.

Also makes mouse lock disabled by default
@maaxxaam maaxxaam requested a review from Nostritius July 3, 2023 12:37
@Nostritius Nostritius merged commit 469a23b into OpenAWE-Project:master Jul 9, 2023
@Nostritius
Copy link
Member

Merged, Thanks :)

@Nostritius
Copy link
Member

If you are interested, I have recently added support for moving Alan with very basic animations. If you want you could try to implement his Orbital Cam.

@maaxxaam maaxxaam deleted the mouse-handling branch July 9, 2023 21:32
@Nostritius Nostritius mentioned this pull request Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants