|
| 1 | +namespace Orbit.Input; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Represents a physical game controller that is connected to a device. |
| 5 | +/// </summary> |
| 6 | +public partial class GameController |
| 7 | +{ |
| 8 | + private readonly WeakEventManager weakEventManager = new(); |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Gets the <see cref="Stick"/> that represents the D-pad on the game controller. |
| 12 | + /// </summary> |
| 13 | + public Stick Dpad { get; } |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Gets the <see cref="Stick"/> that represents the left thumbstick on the game controller. |
| 17 | + /// </summary> |
| 18 | + public Stick LeftStick { get; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Gets the <see cref="Stick"/> that represents the right thumbstick on the game controller. |
| 22 | + /// </summary> |
| 23 | + public Stick RightStick { get; } |
| 24 | + |
| 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> |
| 29 | + public ButtonValue<bool> East { get; } |
| 30 | + |
| 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> |
| 35 | + public ButtonValue<bool> North { get; } |
| 36 | + |
| 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> |
| 41 | + public ButtonValue<bool> South { get; } |
| 42 | + |
| 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> |
| 47 | + public ButtonValue<bool> West { get; } |
| 48 | + |
| 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> |
| 53 | + public ButtonValue<bool> Pause { get; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the <see cref="Shoulder"/> that represents the left hand shoulder on the controller. |
| 57 | + /// </summary> |
| 58 | + public Shoulder LeftShoulder { get; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets the <see cref="Shoulder"/> that represents the right hand shoulder on the controller. |
| 62 | + /// </summary> |
| 63 | + public Shoulder RightShoulder { get; } |
| 64 | + |
| 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 | + |
| 83 | + internal void RaiseButtonValueChanged(ButtonValue buttonValue) |
| 84 | + { |
| 85 | + switch (buttonValue) |
| 86 | + { |
| 87 | + case ButtonValue<float> floatValue: |
| 88 | + weakEventManager.HandleEvent(this, new GameControllerValueChangedEventArgs(buttonValue.Name, floatValue.Value), nameof(ValueChanged)); |
| 89 | + break; |
| 90 | + case ButtonValue<bool> boolValue: |
| 91 | + weakEventManager.HandleEvent(this, new GameControllerButtonChangedEventArgs(buttonValue.Name, boolValue.Value), nameof(ButtonChanged)); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments