Skip to content

Commit

Permalink
start to fix gamepad api
Browse files Browse the repository at this point in the history
  • Loading branch information
Redninja106 committed Jun 20, 2024
1 parent 62924f1 commit a34f423
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/BasicInput/BasicInputSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void OnRender(ICanvas canvas)

Mouse.SetCursor(colors, Alignment.CenterLeft);
}

if (Keyboard.IsKeyPressed(Key.R))
{
if (!Window.IsMaximized)
Expand Down
70 changes: 58 additions & 12 deletions src/SimulationFramework.Desktop/DesktopGamepadProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
namespace SimulationFramework.Desktop;
internal class DesktopGamepadProvider : IGamepadProvider
{
private readonly IGamepad gamepad;
private IGamepad? gamepad;

public Vector2 LeftJoystick => new(gamepad.Thumbsticks[0].X, gamepad.Thumbsticks[0].X);
public Vector2 RightJoystick => new(gamepad.Thumbsticks[1].X, gamepad.Thumbsticks[1].X);
public float LeftTrigger => gamepad.Triggers[0].Position;
public float RightTrigger => gamepad.Triggers[1].Position;
public Vector2 LeftJoystick => gamepad is null ? Vector2.Zero : new(gamepad.Thumbsticks[0].X, gamepad.Thumbsticks[0].Y);
public Vector2 RightJoystick => gamepad is null ? Vector2.Zero : new(gamepad.Thumbsticks[1].X, gamepad.Thumbsticks[1].Y);
public float LeftTrigger => gamepad is null ? 0 : gamepad.Triggers[0].Position;
public float RightTrigger => gamepad is null ? 0 : gamepad.Triggers[1].Position;
public IEnumerable<GamepadButton> HeldButtons => heldButtons;
public IEnumerable<GamepadButton> PressedButtons => pressedButtons;
public IEnumerable<GamepadButton> ReleasedButtons => releasedButtons;
Expand All @@ -20,10 +20,13 @@ public float VibrationStrength
get => vibrationStrength;
set
{
vibrationStrength = value;
foreach (var motor in gamepad.VibrationMotors)
if (gamepad is not null)
{
motor.Speed = vibrationStrength;
vibrationStrength = value;
foreach (var motor in gamepad.VibrationMotors)
{
motor.Speed = vibrationStrength;
}
}
}
}
Expand All @@ -35,13 +38,56 @@ public float VibrationStrength
private readonly List<GamepadButton> pressedButtons = new();
private readonly List<GamepadButton> releasedButtons = new();
private float vibrationStrength;
private IInputContext context;

public DesktopGamepadProvider(IGamepad gamepad)
public DesktopGamepadProvider(IInputContext context)
{
this.gamepad = gamepad;
this.context = context;
context.ConnectionChanged += Context_ConnectionChanged;

gamepad.ButtonDown += Gamepad_ButtonDown;
gamepad.ButtonUp += Gamepad_ButtonUp;
var gp = context.Gamepads.FirstOrDefault();
if (gp != null)
{
OnGamepadAdded(gp);
}
}

private void Context_ConnectionChanged(IInputDevice device, bool added)
{
if (device is IGamepad gamepad)
{
if (added)
{
OnGamepadAdded(gamepad);
}
else
{
OnGamepadRemoved(gamepad);
}
}
}

private void OnGamepadAdded(IGamepad gamepad)
{
if (this.gamepad is null)
{
this.gamepad = gamepad;
this.gamepad.ButtonDown += Gamepad_ButtonDown;
this.gamepad.ButtonUp += Gamepad_ButtonUp;
}
}

private void OnGamepadRemoved(IGamepad gamepad)
{
if (gamepad == this.gamepad)
{
this.gamepad = null;
}

if (context.Gamepads.Count > 0)
{
OnGamepadAdded(context.Gamepads.First());
}
}

private void Gamepad_ButtonUp(IGamepad arg1, Button arg2)
Expand Down
7 changes: 1 addition & 6 deletions src/SimulationFramework.Desktop/DesktopPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ protected virtual void RegisterInputProviders()
Application.RegisterComponent(new DesktopKeyboardProvider(keyboard));
}

var gamepad = inputContext.Gamepads.FirstOrDefault();

if (gamepad is not null)
{
Application.RegisterComponent(new DesktopGamepadProvider(gamepad));
}
Application.RegisterComponent(new DesktopGamepadProvider(inputContext));
}
}

0 comments on commit a34f423

Please sign in to comment.