-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change namespace style to file scoped.
- Loading branch information
Showing
6 changed files
with
196 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,93 @@ | ||
using System.ComponentModel; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Toolbelt.Blazor.Gamepad | ||
namespace Toolbelt.Blazor.Gamepad; | ||
|
||
/// <summary> | ||
/// An individual gamepad or other controller device. | ||
/// </summary> | ||
public class Gamepad | ||
{ | ||
private readonly JSInvoker JSInvoker; | ||
|
||
/// <summary> | ||
/// An individual gamepad or other controller device. | ||
/// A string containing identifying information about the controller. | ||
/// </summary> | ||
public class Gamepad | ||
{ | ||
private readonly JSInvoker JSInvoker; | ||
public string Id { get; } | ||
|
||
/// <summary> | ||
/// A string containing identifying information about the controller. | ||
/// </summary> | ||
public string Id { get; } | ||
/// <summary> | ||
/// An integer that is auto-incremented to be unique for each device currently connected to the system. | ||
/// </summary> | ||
public int Index { get; } | ||
|
||
/// <summary> | ||
/// An integer that is auto-incremented to be unique for each device currently connected to the system. | ||
/// </summary> | ||
public int Index { get; } | ||
internal bool _Connected; | ||
|
||
internal bool _Connected; | ||
/// <summary> | ||
/// A boolean indicating whether the gamepad is still connected to the system. | ||
/// </summary> | ||
public bool Connected => this.Refresh()._Connected; | ||
|
||
/// <summary> | ||
/// A boolean indicating whether the gamepad is still connected to the system. | ||
/// </summary> | ||
public bool Connected => this.Refresh()._Connected; | ||
private double[] _Axes = new double[0]; | ||
|
||
private double[] _Axes = new double[0]; | ||
/// <summary> | ||
/// A list representing the controls with axes present on the device (e.g. analog thumb sticks). | ||
/// </summary> | ||
public IReadOnlyList<double> Axes => this.Refresh()._Axes; | ||
|
||
/// <summary> | ||
/// A list representing the controls with axes present on the device (e.g. analog thumb sticks). | ||
/// </summary> | ||
public IReadOnlyList<double> Axes => this.Refresh()._Axes; | ||
private readonly List<GamepadButton> _Buttons = new List<GamepadButton>(); | ||
|
||
private readonly List<GamepadButton> _Buttons = new List<GamepadButton>(); | ||
/// <summary> | ||
/// A list of GamepadButton objects representing the buttons present on the device. | ||
/// </summary> | ||
public IReadOnlyList<GamepadButton> Buttons => this.Refresh()._Buttons; | ||
|
||
/// <summary> | ||
/// A list of GamepadButton objects representing the buttons present on the device. | ||
/// </summary> | ||
public IReadOnlyList<GamepadButton> Buttons => this.Refresh()._Buttons; | ||
private ValueTask<object> LastRefreshTask = default; | ||
|
||
private ValueTask<object> LastRefreshTask = default; | ||
private DotNetObjectReference<Gamepad> _ObjectRefOfThis; | ||
|
||
private DotNetObjectReference<Gamepad> _ObjectRefOfThis; | ||
internal Gamepad(JSInvoker jsInvoker, string id, int index, bool connected) | ||
{ | ||
this.JSInvoker = jsInvoker; | ||
this.Id = id; | ||
this.Index = index; | ||
this._Connected = connected; | ||
} | ||
|
||
internal Gamepad(JSInvoker jsInvoker, string id, int index, bool connected) | ||
private Gamepad Refresh() | ||
{ | ||
if (this.LastRefreshTask.IsCompleted) | ||
{ | ||
this.JSInvoker = jsInvoker; | ||
this.Id = id; | ||
this.Index = index; | ||
this._Connected = connected; | ||
if (this._ObjectRefOfThis == null) this._ObjectRefOfThis = DotNetObjectReference.Create(this); | ||
this.LastRefreshTask = this.JSInvoker.InvokeAsync<object>("Toolbelt.Blazor.Gamepad.refresh", this._ObjectRefOfThis, this.Id, this.Index); | ||
} | ||
return this; | ||
} | ||
|
||
private Gamepad Refresh() | ||
{ | ||
if (this.LastRefreshTask.IsCompleted) | ||
{ | ||
if (this._ObjectRefOfThis == null) this._ObjectRefOfThis = DotNetObjectReference.Create(this); | ||
this.LastRefreshTask = this.JSInvoker.InvokeAsync<object>("Toolbelt.Blazor.Gamepad.refresh", this._ObjectRefOfThis, this.Id, this.Index); | ||
} | ||
return this; | ||
} | ||
[JSInvokable(nameof(UpdateStatus)), EditorBrowsable(EditorBrowsableState.Never)] | ||
public void UpdateStatus(bool connected, double[] axes, bool[] buttonsPressed, double[] buttonsValue) | ||
{ | ||
this._Connected = connected; | ||
this._Axes = axes; | ||
|
||
[JSInvokable(nameof(UpdateStatus)), EditorBrowsable(EditorBrowsableState.Never)] | ||
public void UpdateStatus(bool connected, double[] axes, bool[] buttonsPressed, double[] buttonsValue) | ||
var buttonsCount = this._Buttons.Count; | ||
for (var i = 0; i < buttonsPressed.Length; i++) | ||
{ | ||
this._Connected = connected; | ||
this._Axes = axes; | ||
|
||
var buttonsCount = this._Buttons.Count; | ||
for (var i = 0; i < buttonsPressed.Length; i++) | ||
var button = default(GamepadButton); | ||
if (i < buttonsCount) | ||
{ | ||
var button = default(GamepadButton); | ||
if (i < buttonsCount) | ||
{ | ||
button = this._Buttons[i]; | ||
} | ||
else | ||
{ | ||
button = new GamepadButton(); | ||
this._Buttons.Add(button); | ||
} | ||
button.Pressed = buttonsPressed[i]; | ||
button.Value = buttonsValue[i]; | ||
button = this._Buttons[i]; | ||
} | ||
if (buttonsPressed.Length < buttonsCount) | ||
else | ||
{ | ||
this._Buttons.RemoveRange(buttonsPressed.Length, buttonsCount - buttonsPressed.Length); | ||
button = new GamepadButton(); | ||
this._Buttons.Add(button); | ||
} | ||
button.Pressed = buttonsPressed[i]; | ||
button.Value = buttonsValue[i]; | ||
} | ||
if (buttonsPressed.Length < buttonsCount) | ||
{ | ||
this._Buttons.RemoveRange(buttonsPressed.Length, buttonsCount - buttonsPressed.Length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
namespace Toolbelt.Blazor.Gamepad | ||
namespace Toolbelt.Blazor.Gamepad; | ||
|
||
/// <summary> | ||
/// An individual button of a gamepad or other controller. | ||
/// </summary> | ||
public class GamepadButton | ||
{ | ||
/// <summary> | ||
/// An individual button of a gamepad or other controller. | ||
/// A boolean value indicating whether the button is currently pressed (true) or unpressed (false). | ||
/// </summary> | ||
public class GamepadButton | ||
{ | ||
/// <summary> | ||
/// A boolean value indicating whether the button is currently pressed (true) or unpressed (false). | ||
/// </summary> | ||
public bool Pressed { get; internal set; } | ||
public bool Pressed { get; internal set; } | ||
|
||
/// <summary> | ||
/// A double value used to represent the current state of analog buttons, such as the triggers on many modern gamepads. | ||
/// <para>The values are normalized to the range 0.0 —1.0, with 0.0 representing a button that is not pressed, and 1.0 representing a button that is fully pressed.</para> | ||
/// </summary> | ||
public double Value { get; internal set; } | ||
/// <summary> | ||
/// A double value used to represent the current state of analog buttons, such as the triggers on many modern gamepads. | ||
/// <para>The values are normalized to the range 0.0 —1.0, with 0.0 representing a button that is not pressed, and 1.0 representing a button that is fully pressed.</para> | ||
/// </summary> | ||
public double Value { get; internal set; } | ||
|
||
internal GamepadButton() | ||
{ | ||
} | ||
internal GamepadButton() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.