Skip to content

Commit

Permalink
Merge pull request #1095 from voltairianman/joystickStateFix
Browse files Browse the repository at this point in the history
start of JoystickState button and connectivity fix
  • Loading branch information
jvbsl committed Jul 21, 2020
2 parents b42f47d + 403cb5a commit 060ccad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/OpenToolkit.Windowing.Common/Input/JoystickState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public struct JoystickState : IEquatable<JoystickState>
/// </summary>
public string Name { get; }

/// <summary>
/// Gets a value indicating whether the joystick is active and connected.
/// </summary>
public bool IsConnected { get; }

/// <summary>
/// Initializes a new instance of the <see cref="JoystickState"/> struct.
/// </summary>
Expand All @@ -43,9 +48,10 @@ public JoystickState(int hatCount, int axesCount, int buttonCount, int id, strin
{
_hats = new Hat[hatCount];
_axes = new float[axesCount];
_buttons = new byte[buttonCount / 8];
_buttons = new byte[(buttonCount + 7) / 8];
Id = id;
Name = name;
IsConnected = true;
}

/// <summary>
Expand All @@ -62,8 +68,9 @@ public JoystickState(Hat[] hats, float[] axes, bool[] buttons, int id, string na
_axes = axes;
Id = id;
Name = name;
IsConnected = true;

_buttons = new byte[buttons.Length / 8];
_buttons = new byte[(buttons.Length + 7) / 8];
for (int i = 0; i < buttons.Length; i++)
{
SetButtonDown(i, buttons[i]);
Expand Down
14 changes: 14 additions & 0 deletions src/OpenToolkit.Windowing.Desktop/NativeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,20 @@ private void RegisterWindowCallbacks()
};
GLFW.SetJoystickCallback(_joystickCallback);

// Check for Joysticks that are connected at application launch
for (int i = 0; i < JoystickStates.Length; i++)
{
if (GLFW.JoystickPresent(i))
{
GLFW.GetJoystickHatsRaw(i, out var hatCount);
GLFW.GetJoystickAxesRaw(i, out var axisCount);
GLFW.GetJoystickButtonsRaw(i, out var buttonCount);
var name = GLFW.GetJoystickName(i);

JoystickStates[i] = new JoystickState(hatCount, axisCount, buttonCount, i, name);
}
}

_monitorCallback = (monitor, eventCode) =>
{
OnMonitorConnected(new MonitorEventArgs(new Monitor((IntPtr)monitor), eventCode == ConnectedState.Connected));
Expand Down

0 comments on commit 060ccad

Please sign in to comment.