Skip to content
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

Implement keyboard rebinding support #1301

Closed
wants to merge 27 commits into from

Conversation

Fabxx
Copy link
Contributor

@Fabxx Fabxx commented Dec 1, 2022

Special thanks to:

toxicshadow, dracc and antangelo for suggestions.
SSUPI for correcting some errors in the SDL_Event handling.

Partially fixes #136 for keyboard rebinding. Controller rebinding will come in another PR

-Allows you to rebind keyboard controls

-Controls are stored in the .toml file configuration for next boots (no need to remap)

-Checks if a key has already been bound to any previous key to avoid conflicts.

-Uses same window for the controller, no extra UI windows.

-A white text under the toggles tells to the user what key for xemu he is binding for each key.

-Button press, rumble, analog movement and trigger animations during remapping are disabled to avoid confusing visual conflicts with already mapped keys before changing them.

-When remapping, a "Abort rebinding" toggle will appear, so the user can manually stop the binding process. This will not alter the already done bindings just in case the user presses the toggle by mistake.

-The auto-bind option has been removed, instead there's a "Reset controls to default" option, so the user can reset his controls while running xemu, and doesn't need to reboot to get the controls updated at runtime. The .toml file is updated on reboot with defaults if a remapping is not done after this option.

-While remapping, the "Reset controls to default" toggle will disappear.

-As soon the user by mistake changes window or closes the menu, the remapping process is aborted and we restore the default keys, since SDL still continues to catch events if outside of that window, meaning that the user will involountary map the keys even if it doesn't want to.

-The ESC button will NOT make the user exit the window while remapping. The button can be mapped as well.

Showcase (updated at 6/12/2022 14:56pm):

8mb.video-DjK-X4K1DlWm.mp4

Copy link
Contributor

@antangelo antangelo left a comment

Choose a reason for hiding this comment

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

Left a few comments. Another thing I noticed while using it:

  • While remapping keys, it's possible for the keyboard inputs to be used to navigate the UI. I think this should be disabled while remapping.
  • The need to disable auto-bind is rather unintuitive. If there is some bug where the config is overwritten by auto-bind being on, then I think it should be fixed instead of requiring it to be disabled.

ui/xemu-input.c Outdated Show resolved Hide resolved
ui/xui/gl-helpers.cc Show resolved Hide resolved
ui/xui/gl-helpers.cc Outdated Show resolved Hide resolved
ui/xui/main-menu.cc Outdated Show resolved Hide resolved
ui/xui/main-menu.cc Outdated Show resolved Hide resolved
ui/xui/main-menu.cc Outdated Show resolved Hide resolved
ui/xemu-input.c Outdated Show resolved Hide resolved
ui/xemu-input.c Outdated Show resolved Hide resolved
ui/xemu-input.c Outdated Show resolved Hide resolved
@Fabxx Fabxx marked this pull request as draft December 4, 2022 22:02
@Fabxx Fabxx marked this pull request as ready for review December 6, 2022 13:57
@Fabxx
Copy link
Contributor Author

Fabxx commented Dec 6, 2022

The modifications are tested and complete, feel free to test it and report if you agree with the choices applied to the input management and code design

@ElTioRata
Copy link

ElTioRata commented Dec 10, 2022

Does this includes rebinding buttons to mouse?

@Fabxx
Copy link
Contributor Author

Fabxx commented Dec 12, 2022

Does this includes rebinding buttons to mouse?

No, of course it can be implemented by just catching the SDL event for the mouse press and bind it to that scancode, but the right mouse button is used to access the UI. Besides i think it's more proper to implement the mouse binding only on analog axis movement since that's the most useful thing when playing with both keyboard and mouse. If the devs agree with this thinkering i might implement it in this PR already. UPDATE: If we want to make sure that the user can use the ESC Button and the mouse buttons/movement we should:

-Disable the esc button for the UI permanently, i already have an idea for it.
-Disable the right click mouse button for the UI permanently

This is needed if we have to guarantee the usage of all keys for the user while playing and not having interferences with the UI, not to mention if i add mouse remapping as well that could be very useful.

I can already do the code but i need a response to understand how i have to move. So guarantee freedom with keyboard + mouse but disable button UI interaction, and just let the user move the mouse/use the controller to access the UI, or it stays like so.

UPDATE: I guess i'll leave it as it is to avoid a mess. The code works as intended for the keyboard rebinding, mouse/controller might come in another PR later.

@ElTioRata
Copy link

ElTioRata commented Jan 4, 2023

Tried the build for a few weeks, works fine. However, I think it would be a lot more convenient if you could remap specific buttons separately instead of going through the whole remap process. It would also be nice if you could add key mapping profiles as well, since not all games are comfortable to play with the same keys.

@Fabxx
Copy link
Contributor Author

Fabxx commented Jan 4, 2023

Tried the build for a few weeks, works fine. However, I think it would be a lot more convenient if you could remap specific buttons separately instead of going through the whole remap process. It would also be nice if you could add key mapping profiles as well, since not all games are comfortable to play with the same keys.

For the profiles might be doable, for the specific buttons i have to see

@Fabxx Fabxx marked this pull request as draft January 12, 2023 17:42
@Fabxx Fabxx marked this pull request as ready for review January 12, 2023 21:38
@Fabxx
Copy link
Contributor Author

Fabxx commented Jan 12, 2023

The system works as intended, but in the future it will be made more flexible, but for now it's possible to simply bind all the keys to the combination wanted.

} else if (is_remapping_active) {
xemu_input_keyboard_rebind(event);
} else if (restore_controls) {
xemu_input_restore_defaults();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this done here instead of being called directly when the restore button is pressed?


void xemu_input_keyboard_rebind(const SDL_Event *ev)
{
//Check if the user aborts the remapping process.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: This comment adds no value

}

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Extra newline

primary_color,
(state->buttons & CONTROLLER_BUTTON_RSTICK) ? primary_color :
(state->buttons & rstick_press_anim) ? primary_color :
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

float w = tex_items[obj_lstick].w;
float h = tex_items[obj_lstick].h;
float c_x = frame_x+lstick_ctr.x;
float c_y = frame_y+lstick_ctr.y;
float lstick_x = (float)state->axis[CONTROLLER_AXIS_LSTICK_X]/32768.0;
float lstick_y = (float)state->axis[CONTROLLER_AXIS_LSTICK_Y]/32768.0;
int lstick_press_anim = CONTROLLER_BUTTON_LSTICK;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this take the remapping into account?

@@ -38,6 +38,16 @@
#include "../xemu-xbe.h"

MainMenuScene g_main_menu;
bool is_remapping_active = false;
bool duplicate_found = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

This variable is unused

}

if (!is_remapping_active) {
if (Toggle("Reset controls to default", &restore_controls,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment about being a toggle as below

@@ -38,6 +38,16 @@
#include "../xemu-xbe.h"

MainMenuScene g_main_menu;
bool is_remapping_active = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Globals should be prefixed with g_

sdl_kbd_scancode_map[23] = SDL_SCANCODE_K;
sdl_kbd_scancode_map[24] = SDL_SCANCODE_O;

g_config.input.keyboard_controller_scancode_map.a = SDL_SCANCODE_A;
Copy link
Contributor

Choose a reason for hiding this comment

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

Create a function that takes sdl_kbd_scancode_map as a parameter and sets the g_config values like you do when binding is complete, then reuse that here to clean this up.

bool is_remapping_active = false;
bool duplicate_found = false;
bool restore_controls = false;
bool abort_rebinding = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can detect the abort case if you keep a static variable in_progress in xemu_input_keyboard_rebind that tracks this:

  • Initialize to false
  • If is_remapping_active is true, and in_progress is false, set it to true
  • If is_remapping_active is false, and in_progress is true, this is the abort case. Only change you have to add is to set in_progress to false after handling
  • If both are false do nothing, same as now.
  • When finished remapping, set in_progress to false.

You can rename it to something better if you want, just a suggestion. This removes the need for an extra global.

@Fabxx
Copy link
Contributor Author

Fabxx commented Aug 7, 2023

closing this, please follow #1516 since it's in a better state.

@Fabxx Fabxx closed this Aug 7, 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.

Implement gamepad button/axis remapping
3 participants