Skip to content

Commit 70cc808

Browse files
committed
Extra documentation
1 parent e011603 commit 70cc808

20 files changed

+348
-117
lines changed

.editorconfig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ dotnet_naming_rule.public_fields_should_be_pascalcase.severity = suggestion
216216
dotnet_naming_rule.public_fields_should_be_pascalcase.symbols = public_fields
217217
dotnet_naming_rule.public_fields_should_be_pascalcase.style = pascalcase
218218

219-
dotnet_naming_rule.private_fields_should_be__camelcase.severity = suggestion
220-
dotnet_naming_rule.private_fields_should_be__camelcase.symbols = private_fields
221-
dotnet_naming_rule.private_fields_should_be__camelcase.style = _camelcase
219+
dotnet_naming_rule.private_fields_should_be_camelcase.severity = suggestion
220+
dotnet_naming_rule.private_fields_should_be_camelcase.symbols = private_fields
221+
dotnet_naming_rule.private_fields_should_be_camelcase.style = camelcase
222222

223-
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.severity = suggestion
224-
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.symbols = private_static_fields
225-
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelcase
223+
dotnet_naming_rule.private_static_fields_should_be_camelcase.severity = suggestion
224+
dotnet_naming_rule.private_static_fields_should_be_camelcase.symbols = private_static_fields
225+
dotnet_naming_rule.private_static_fields_should_be_camelcase.style = camelcase
226226

227227
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion
228228
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,76 @@
11
namespace Orbit.Input;
22

3-
public class ButtonValue
3+
/// <summary>
4+
/// Represents a game controller button and its associated value.
5+
/// </summary>
6+
public abstract class ButtonValue
47
{
5-
public ButtonValue(string parent, string name)
8+
/// <summary>
9+
/// Creates a new instance of <see cref="ButtonValue"/>.
10+
/// </summary>
11+
/// <param name="parent">The name of the component of a game controller that this button belongs to.</param>
12+
/// <param name="name">The name of the button.</param>
13+
protected ButtonValue(string parent, string name)
614
{
715
Name = NameHelper.GetName(parent, name);
816
}
917

18+
/// <summary>
19+
/// Gets the name of the button.
20+
/// </summary>
1021
public string Name { get; }
1122
}
1223

24+
/// <inheritdoc />
1325
public class ButtonValue<TValue> : ButtonValue where TValue : struct
1426
{
1527
private readonly GameController gameController;
1628
private readonly IComparer<TValue> comparer;
1729
private TValue buttonValue;
18-
30+
31+
/// <summary>
32+
/// Creates a new instance of <see cref="ButtonValue"/>.
33+
/// </summary>
34+
/// <param name="gameController">The <see cref="GameController"/> that this button belongs to.</param>
35+
/// <param name="parent">The name of the component of a game controller that this button belongs to.</param>
36+
/// <param name="name">The name of the button.</param>
37+
/// <param name="comparer">The <see cref="IComparer{T}"/> to use in comparisons for value changed events. Particularly useful when dealing with values like <see cref="float"/> where accuracy can be messy.</param>
1938
public ButtonValue(GameController gameController, string parent, string name, IComparer<TValue>? comparer = null)
2039
: base(parent, name)
2140
{
2241
this.gameController = gameController;
2342
this.comparer = comparer ?? Comparer<TValue>.Default;
2443
}
2544

45+
/// <summary>
46+
/// Creates a new instance of <see cref="ButtonValue"/>.
47+
/// </summary>
48+
/// <param name="gameController">The <see cref="GameController"/> that this button belongs to.</param>
49+
/// <param name="name">The name of the button.</param>
50+
/// <param name="comparer">The <see cref="IComparer{T}"/> to use in comparisons for value changed events. Particularly useful when dealing with values like <see cref="float"/> where accuracy can be messy.</param>
2651
public ButtonValue(GameController gameController, string name, IComparer<TValue>? comparer = null)
2752
: base(string.Empty, name)
2853
{
2954
this.gameController = gameController;
3055
this.comparer = comparer ?? Comparer<TValue>.Default;
3156
}
3257

58+
/// <summary>
59+
/// Gets the current value for the button.
60+
/// </summary>
3361
public TValue Value
3462
{
3563
get => buttonValue;
36-
set
64+
internal set
3765
{
38-
if (comparer.Compare(buttonValue, value) != 0)
66+
if (comparer.Compare(buttonValue, value) == 0)
3967
{
40-
buttonValue = value;
41-
42-
this.gameController.RaiseButtonValueChanged(this);
68+
return;
4369
}
70+
71+
buttonValue = value;
72+
73+
this.gameController.RaiseButtonValueChanged(this);
4474
}
4575
}
4676
}

engine/Orbit.Input/GameControllers/FloatComparer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// A <see cref="float"/> based implementation of <see cref="IComparer{T}"/>.
5+
/// </summary>
36
public class FloatComparer : IComparer<float>
47
{
58
private readonly float threshold;
69

710
internal static FloatComparer Default { get; set; } = new FloatComparer(0.001f);
811

12+
/// <summary>
13+
/// Creates a new instance of <see cref="FloatComparer"/>.
14+
/// </summary>
15+
/// <param name="threshold">The threshold to use when determining whether values are equal.</param>
916
public FloatComparer(float threshold)
1017
{
1118
this.threshold = threshold;
1219
}
1320

21+
/// <inheritdoc cref="IComparer{T}.Compare"/>
1422
public int Compare(float x, float y)
1523
{
1624
if (Math.Abs(x - y) < this.threshold)

engine/Orbit.Input/GameControllers/GameController.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,94 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Represents a physical game controller that is connected to a device.
5+
/// </summary>
36
public partial class GameController
47
{
5-
public event EventHandler<GameControllerButtonChangedEventArgs>? ButtonChanged;
6-
public event EventHandler<GameControllerValueChangedEventArgs>? ValueChanged;
8+
private readonly WeakEventManager weakEventManager = new();
79

10+
/// <summary>
11+
/// Gets the <see cref="Stick"/> that represents the D-pad on the game controller.
12+
/// </summary>
813
public Stick Dpad { get; }
914

15+
/// <summary>
16+
/// Gets the <see cref="Stick"/> that represents the left thumbstick on the game controller.
17+
/// </summary>
1018
public Stick LeftStick { get; }
1119

20+
/// <summary>
21+
/// Gets the <see cref="Stick"/> that represents the right thumbstick on the game controller.
22+
/// </summary>
1223
public Stick RightStick { get; }
1324

25+
/// <summary>
26+
/// Gets the <see cref="ButtonValue{T}"/> that represents the right most button on the controller.
27+
/// Circle on Playstation and B on XBox controllers.
28+
/// </summary>
1429
public ButtonValue<bool> East { get; }
1530

31+
/// <summary>
32+
/// Gets the <see cref="ButtonValue{T}"/> that represents the top most button on the controller.
33+
/// Triangle on Playstation and Y on XBox controllers.
34+
/// </summary>
1635
public ButtonValue<bool> North { get; }
1736

37+
/// <summary>
38+
/// Gets the <see cref="ButtonValue{T}"/> that represents the bottom most button on the controller.
39+
/// X on Playstation and A on XBox controllers.
40+
/// </summary>
1841
public ButtonValue<bool> South { get; }
1942

43+
/// <summary>
44+
/// Gets the <see cref="ButtonValue{T}"/> that represents the left most button on the controller.
45+
/// Square on Playstation and X on XBox controllers.
46+
/// </summary>
2047
public ButtonValue<bool> West { get; }
2148

49+
/// <summary>
50+
/// Gets the <see cref="ButtonValue{T}"/> that represents the left most button on the controller.
51+
/// Options on Playstation and hamburger on XBox controllers.
52+
/// </summary>
2253
public ButtonValue<bool> Pause { get; }
2354

55+
/// <summary>
56+
/// Gets the <see cref="Shoulder"/> that represents the left hand shoulder on the controller.
57+
/// </summary>
2458
public Shoulder LeftShoulder { get; }
2559

60+
/// <summary>
61+
/// Gets the <see cref="Shoulder"/> that represents the right hand shoulder on the controller.
62+
/// </summary>
2663
public Shoulder RightShoulder { get; }
2764

65+
/// <summary>
66+
/// Event that is raised when a button on the game controller is detected as being pressed or released.
67+
/// </summary>
68+
public event EventHandler<GameControllerButtonChangedEventArgs> ButtonChanged
69+
{
70+
add => weakEventManager.AddEventHandler(value);
71+
remove => weakEventManager.RemoveEventHandler(value);
72+
}
73+
74+
/// <summary>
75+
/// Event that is raised when a button that supports a varying value on the game controller is detected as being pressed or released to some degree.
76+
/// </summary>
77+
public event EventHandler<GameControllerValueChangedEventArgs> ValueChanged
78+
{
79+
add => weakEventManager.AddEventHandler(value);
80+
remove => weakEventManager.RemoveEventHandler(value);
81+
}
82+
2883
internal void RaiseButtonValueChanged(ButtonValue buttonValue)
2984
{
3085
switch (buttonValue)
3186
{
3287
case ButtonValue<float> floatValue:
33-
this.ValueChanged?.Invoke(this, new GameControllerValueChangedEventArgs(buttonValue.Name, floatValue.Value));
88+
weakEventManager.HandleEvent(this, new GameControllerValueChangedEventArgs(buttonValue.Name, floatValue.Value), nameof(ValueChanged));
3489
break;
3590
case ButtonValue<bool> boolValue:
36-
this.ButtonChanged?.Invoke(this, new GameControllerButtonChangedEventArgs(buttonValue.Name, boolValue.Value));
91+
weakEventManager.HandleEvent(this, new GameControllerButtonChangedEventArgs(buttonValue.Name, boolValue.Value), nameof(ButtonChanged));
3792
break;
3893
}
3994
}

engine/Orbit.Input/GameControllers/GameControllerButtonChangedEventArgs.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Contains event data for all button pressed and released events.
5+
/// </summary>
36
public class GameControllerButtonChangedEventArgs : EventArgs
47
{
8+
/// <summary>
9+
/// Gets the name of the button.
10+
/// </summary>
511
public string ButtonName { get; }
12+
13+
/// <summary>
14+
/// Gets whether the button is pressed or released.
15+
/// </summary>
616
public bool IsPressed { get; }
717

18+
/// <summary>
19+
/// Creates a new instance of <see cref="GameControllerButtonChangedEventArgs"/>.
20+
/// </summary>
21+
/// <param name="buttonName">The name of the button.</param>
22+
/// <param name="isPressed">Whether the button is pressed or released.</param>
823
public GameControllerButtonChangedEventArgs(string buttonName, bool isPressed)
924
{
1025
ButtonName = buttonName;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Contains event data for when game controllers are detected as being connected to the device.
5+
/// </summary>
36
public class GameControllerConnectedEventArgs : EventArgs
47
{
8+
/// <summary>
9+
/// Creates a new instance of <see cref="GameControllerConnectedEventArgs"/>.
10+
/// </summary>
11+
/// <param name="gameController">The <see cref="GameController"/> that has been detected and being connected to the device.</param>
512
public GameControllerConnectedEventArgs(GameController gameController)
613
{
714
GameController = gameController;
815
}
916

17+
/// <summary>
18+
/// Gets the <see cref="GameController"/> that has been detected and being connected to the device.
19+
/// </summary>
1020
public GameController GameController { get; }
1121
}
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Provides the ability to interact with game controller support on each platform.
5+
/// </summary>
36
public partial class GameControllerManager
47
{
58
private readonly List<GameController> gameControllers = [];
9+
private readonly WeakEventManager weakEventManager = new();
610

711
private static GameControllerManager? current;
812

13+
/// <summary>
14+
/// Gets the current instance of the <see cref="GameControllerManager"/>.
15+
/// </summary>
916
public static GameControllerManager Current => current ??= new GameControllerManager();
1017

18+
/// <summary>
19+
/// Starts the controller discovery process. Make sure to subscribe to the <see cref="GameControllerConnected"/> event in order to be notified when a controller has been discovered.
20+
/// </summary>
21+
/// <returns></returns>
1122
public partial Task StartDiscovery();
1223

24+
/// <summary>
25+
/// Gets the list of <see cref="GameController"/>s that are connected to the device.
26+
/// </summary>
1327
public IReadOnlyCollection<GameController> GameControllers => gameControllers;
1428

15-
public event EventHandler<GameControllerConnectedEventArgs>? GameControllerConnected;
29+
/// <summary>
30+
/// Event that is raised when a <see cref="GameController"/> is detected as being connected to the device.
31+
/// </summary>
32+
public event EventHandler<GameControllerConnectedEventArgs> GameControllerConnected
33+
{
34+
add => weakEventManager.AddEventHandler(value);
35+
remove => weakEventManager.RemoveEventHandler(value);
36+
}
1637

1738
private void OnGameControllerConnected(GameController controller)
1839
{
1940
gameControllers.Add(controller);
20-
this.GameControllerConnected?.Invoke(this, new GameControllerConnectedEventArgs(controller));
41+
weakEventManager.HandleEvent(this, new GameControllerConnectedEventArgs(controller), nameof(GameControllerConnected));
2142
}
2243
}

engine/Orbit.Input/GameControllers/GameControllerValueChangedEventArgs.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Contains event data for all button value changed events.
5+
/// </summary>
36
public class GameControllerValueChangedEventArgs : EventArgs
47
{
8+
/// <summary>
9+
/// Gets the name of the button.
10+
/// </summary>
511
public string ButtonName { get; }
12+
13+
/// <summary>
14+
/// Gets how much the button has been pressed.
15+
/// </summary>
616
public float Value { get; }
717

18+
/// <summary>
19+
/// Creates a new instance of <see cref="GameControllerValueChangedEventArgs"/>.
20+
/// </summary>
21+
/// <param name="buttonName">The name of the button.</param>
22+
/// <param name="value">How much the button has been pressed.</param>
823
public GameControllerValueChangedEventArgs(string buttonName, float value)
924
{
1025
ButtonName = buttonName;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
namespace Orbit.Input;
22

3+
/// <summary>
4+
/// Represents a collection of buttons on ther 'shoulder' of a game controller.
5+
/// </summary>
36
public class Shoulder
47
{
8+
/// <summary>
9+
/// Creates a new instance of <see cref="Shoulder"/>.
10+
/// </summary>
11+
/// <param name="controller">The <see cref="GameController"/> that the shoulder belongs to.</param>
12+
/// <param name="name">The name of the shoulder (e.g. left or right).</param>
513
public Shoulder(GameController controller, string name)
614
{
715
Button = new ButtonValue<bool>(controller, name, nameof(Button));
816
Trigger = new ButtonValue<float>(controller, name, nameof(Trigger), FloatComparer.Default);
917
}
1018

19+
/// <summary>
20+
/// Gets the <see cref="ButtonValue{TValue}"/> that represents the top button on the shoulder.
21+
/// This is represented as a simple pressed/released button.
22+
/// </summary>
1123
public ButtonValue<bool> Button { get; }
24+
25+
/// <summary>
26+
/// Gets the <see cref="ButtonValue{TValue}"/> that represents the bottom button on the shoulder.
27+
/// This is represented as a value between 0 and 1 based on how much the button is pressed.
28+
/// </summary>
1229
public ButtonValue<float> Trigger { get; }
1330
}

0 commit comments

Comments
 (0)