Skip to content

Commit

Permalink
Change namespace style to file scoped.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Dec 13, 2023
1 parent 57d8ee0 commit ca49aa4
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 202 deletions.
129 changes: 64 additions & 65 deletions Toolbelt.Blazor.Gamepad/Gamepad.cs
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);
}
}
}
31 changes: 15 additions & 16 deletions Toolbelt.Blazor.Gamepad/GamepadButton.cs
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()
{
}
}
45 changes: 22 additions & 23 deletions Toolbelt.Blazor.Gamepad/GamepadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
using Microsoft.JSInterop;
using Toolbelt.Blazor.Gamepad;

namespace Toolbelt.Blazor.Extensions.DependencyInjection
namespace Toolbelt.Blazor.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for adding GamepadList service.
/// </summary>
public static class GamepadExtensions
{
/// <summary>
/// Extension methods for adding GamepadList service.
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
public static class GamepadExtensions
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
public static IServiceCollection AddGamepadList(this IServiceCollection services)
{
/// <summary>
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
public static IServiceCollection AddGamepadList(this IServiceCollection services)
{
return services.AddGamepadList(configure: null);
}
return services.AddGamepadList(configure: null);
}

/// <summary>
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
/// <param name="configure"></param>
public static IServiceCollection AddGamepadList(this IServiceCollection services, Action<GamepadOptions> configure)
{
var options = new GamepadOptions();
configure?.Invoke(options);
services.AddScoped(serviceProvider => new GamepadList(serviceProvider.GetService<IJSRuntime>(), options));
return services;
}
/// <summary>
/// Adds a gamepad list service to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
/// </summary>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
/// <param name="configure"></param>
public static IServiceCollection AddGamepadList(this IServiceCollection services, Action<GamepadOptions> configure)
{
var options = new GamepadOptions();
configure?.Invoke(options);
services.AddScoped(serviceProvider => new GamepadList(serviceProvider.GetService<IJSRuntime>(), options));
return services;
}
}
Loading

0 comments on commit ca49aa4

Please sign in to comment.