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

Prepare SDL2ControllerBindings class for SDL3 #6232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions osu.Framework/Platform/SDL2/SDL2ControllerBindings.cs
Expand Up @@ -13,7 +13,7 @@ namespace osu.Framework.Platform.SDL2
/// Maintain a copy of the SDL-provided bindings for the given controller.
/// Used to determine whether a given event's joystick button or axis is unmapped.
/// </summary>
public class SDL2ControllerBindings
internal class SDL2ControllerBindings
{
public readonly IntPtr JoystickHandle;
public readonly IntPtr ControllerHandle;
Expand Down Expand Up @@ -54,26 +54,26 @@ public void PopulateBindings()
.Select(i => SDL.SDL_GameControllerGetBindForAxis(ControllerHandle, (SDL.SDL_GameControllerAxis)i)).ToArray();
}

public SDL.SDL_GameControllerButton GetButtonForIndex(byte index)
public bool IsJoystickButtonBound(byte buttonIndex)
{
for (int i = 0; i < ButtonBindings.Length; i++)
{
if (ButtonBindings[i].bindType != SDL.SDL_GameControllerBindType.SDL_CONTROLLER_BINDTYPE_NONE && ButtonBindings[i].value.button == index)
return (SDL.SDL_GameControllerButton)i;
if (ButtonBindings[i].bindType != SDL.SDL_GameControllerBindType.SDL_CONTROLLER_BINDTYPE_NONE && ButtonBindings[i].value.button == buttonIndex)
return true;
}

return SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID;
return false;
}

public SDL.SDL_GameControllerAxis GetAxisForIndex(byte index)
public bool IsJoystickAxisBound(byte axisIndex)
{
for (int i = 0; i < AxisBindings.Length; i++)
{
if (AxisBindings[i].bindType != SDL.SDL_GameControllerBindType.SDL_CONTROLLER_BINDTYPE_NONE && AxisBindings[i].value.button == index)
return (SDL.SDL_GameControllerAxis)i;
if (AxisBindings[i].bindType != SDL.SDL_GameControllerBindType.SDL_CONTROLLER_BINDTYPE_NONE && AxisBindings[i].value.axis == axisIndex)
return true;
}

return SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_INVALID;
return false;
}
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Platform/SDL2Window_Input.cs
Expand Up @@ -357,7 +357,7 @@ private void handleJoyDeviceEvent(SDL.SDL_JoyDeviceEvent evtJdevice)
private void handleJoyButtonEvent(SDL.SDL_JoyButtonEvent evtJbutton)
{
// if this button exists in the controller bindings, skip it
if (controllers.TryGetValue(evtJbutton.which, out var state) && state.GetButtonForIndex(evtJbutton.button) != SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_INVALID)
if (controllers.TryGetValue(evtJbutton.which, out var state) && state.IsJoystickButtonBound(evtJbutton.button))
return;

var button = JoystickButton.FirstButton + evtJbutton.button;
Expand Down Expand Up @@ -387,7 +387,7 @@ private void handleJoyBallEvent(SDL.SDL_JoyBallEvent evtJball)
private void handleJoyAxisEvent(SDL.SDL_JoyAxisEvent evtJaxis)
{
// if this axis exists in the controller bindings, skip it
if (controllers.TryGetValue(evtJaxis.which, out var state) && state.GetAxisForIndex(evtJaxis.axis) != SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_INVALID)
if (controllers.TryGetValue(evtJaxis.which, out var state) && state.IsJoystickAxisBound(evtJaxis.axis))
return;

enqueueJoystickAxisInput(JoystickAxisSource.Axis1 + evtJaxis.axis, evtJaxis.axisValue);
Expand Down