1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

Added properties and constructors for existing operator overloads to GenericInput

This commit is contained in:
Ell 2022-03-26 12:51:14 +01:00
parent 54ca580dd3
commit ae6ce6e7d5
2 changed files with 55 additions and 16 deletions

View file

@ -26,6 +26,7 @@ Improvements
- Allow better control over the order and layout of a Keybind's combinations - Allow better control over the order and layout of a Keybind's combinations
- Allow setting a gamepad button deadzone in InputHandler - Allow setting a gamepad button deadzone in InputHandler
- Trigger InputHandler key and gamepad repeats for the most recently pressed input - Trigger InputHandler key and gamepad repeats for the most recently pressed input
- Added properties and constructors for existing operator overloads to GenericInput
Fixes Fixes
- **Fixed a formatting Code only knowing about the last Token that it is applied in** - **Fixed a formatting Code only knowing about the last Token that it is applied in**

View file

@ -6,7 +6,7 @@ namespace MLEM.Input {
/// <summary> /// <summary>
/// A generic input represents any kind of input key. /// A generic input represents any kind of input key.
/// This includes <see cref="Keys"/> for keyboard keys, <see cref="MouseButton"/> for mouse buttons and <see cref="Buttons"/> for gamepad buttons. /// This includes <see cref="Keys"/> for keyboard keys, <see cref="MouseButton"/> for mouse buttons and <see cref="Buttons"/> for gamepad buttons.
/// For creating and extracting inputs from a generic input, the implicit operators and <see cref="Type"/> can be used. /// For creating and extracting inputs from a generic input, the implicit operators and <see cref="Type"/> can additionally be used.
/// Note that this type is serializable using <see cref="DataContractAttribute"/>. /// Note that this type is serializable using <see cref="DataContractAttribute"/>.
/// </summary> /// </summary>
[DataContract] [DataContract]
@ -20,6 +20,46 @@ namespace MLEM.Input {
[DataMember] [DataMember]
private readonly int value; private readonly int value;
/// <summary>
/// Returns this generic input's <see cref="Keys"/>.
/// </summary>
/// <exception cref="InvalidOperationException">If this generic input's <see cref="Type"/> is not <see cref="InputType.Keyboard"/> or <see cref="InputType.None"/>.</exception>
public Keys Key {
get {
if (this.Type == InputType.None)
return Keys.None;
return this.Type == InputType.Keyboard ? (Keys) this.value : throw new InvalidOperationException();
}
}
/// <summary>
/// Returns this generic input's <see cref="MouseButton"/>.
/// </summary>
/// <exception cref="InvalidOperationException">If this generic input's <see cref="Type"/> is not <see cref="InputType.Mouse"/>.</exception>
public MouseButton MouseButton => this.Type == InputType.Mouse ? (MouseButton) this.value : throw new InvalidOperationException();
/// <summary>
/// Returns this generic input's <see cref="Buttons"/>.
/// </summary>
/// <exception cref="InvalidOperationException">If this generic input's <see cref="Type"/> is not <see cref="InputType.Gamepad"/>.</exception>
public Buttons Button => this.Type == InputType.Gamepad ? (Buttons) this.value : throw new InvalidOperationException();
/// <summary>
/// Creates a new generic input from the given keyboard <see cref="Keys"/>.
/// </summary>
/// <param name="key">The key to convert.</param>
public GenericInput(Keys key) : this(InputType.Keyboard, (int) key) {}
/// <summary>
/// Creates a new generic input from the given <see cref="MouseButton"/>.
/// </summary>
/// <param name="button">The button to convert.</param>
public GenericInput(MouseButton button) : this(InputType.Mouse, (int) button) {}
/// <summary>
/// Creates a new generic input from the given gamepad <see cref="Buttons"/>.
/// </summary>
/// <param name="button">The button to convert.</param>
public GenericInput(Buttons button) : this(InputType.Gamepad, (int) button) {}
private GenericInput(InputType type, int value) { private GenericInput(InputType type, int value) {
this.Type = type; this.Type = type;
this.value = value; this.value = value;
@ -83,10 +123,10 @@ namespace MLEM.Input {
/// <summary> /// <summary>
/// Converts a <see cref="Keys"/> to a generic input. /// Converts a <see cref="Keys"/> to a generic input.
/// </summary> /// </summary>
/// <param name="keys">The keys to convert</param> /// <param name="key">The keys to convert</param>
/// <returns>The resulting generic input</returns> /// <returns>The resulting generic input</returns>
public static implicit operator GenericInput(Keys keys) { public static implicit operator GenericInput(Keys key) {
return new GenericInput(InputType.Keyboard, (int) keys); return new GenericInput(key);
} }
/// <summary> /// <summary>
@ -95,16 +135,16 @@ namespace MLEM.Input {
/// <param name="button">The button to convert</param> /// <param name="button">The button to convert</param>
/// <returns>The resulting generic input</returns> /// <returns>The resulting generic input</returns>
public static implicit operator GenericInput(MouseButton button) { public static implicit operator GenericInput(MouseButton button) {
return new GenericInput(InputType.Mouse, (int) button); return new GenericInput(button);
} }
/// <summary> /// <summary>
/// Converts a <see cref="Buttons"/> to a generic input. /// Converts a <see cref="Buttons"/> to a generic input.
/// </summary> /// </summary>
/// <param name="buttons">The buttons to convert</param> /// <param name="button">The buttons to convert</param>
/// <returns>The resulting generic input</returns> /// <returns>The resulting generic input</returns>
public static implicit operator GenericInput(Buttons buttons) { public static implicit operator GenericInput(Buttons button) {
return new GenericInput(InputType.Gamepad, (int) buttons); return new GenericInput(button);
} }
/// <summary> /// <summary>
@ -112,11 +152,9 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="input">The input to convert</param> /// <param name="input">The input to convert</param>
/// <returns>The resulting keys</returns> /// <returns>The resulting keys</returns>
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Keyboard"/> or <see cref="InputType.None"/></exception> /// <exception cref="InvalidOperationException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Keyboard"/> or <see cref="InputType.None"/></exception>
public static implicit operator Keys(GenericInput input) { public static implicit operator Keys(GenericInput input) {
if (input.Type == InputType.None) return input.Key;
return Keys.None;
return input.Type == InputType.Keyboard ? (Keys) input.value : throw new ArgumentException();
} }
/// <summary> /// <summary>
@ -124,9 +162,9 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="input">The input to convert</param> /// <param name="input">The input to convert</param>
/// <returns>The resulting button</returns> /// <returns>The resulting button</returns>
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Mouse"/></exception> /// <exception cref="InvalidOperationException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Mouse"/></exception>
public static implicit operator MouseButton(GenericInput input) { public static implicit operator MouseButton(GenericInput input) {
return input.Type == InputType.Mouse ? (MouseButton) input.value : throw new ArgumentException(); return input.MouseButton;
} }
/// <summary> /// <summary>
@ -134,9 +172,9 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="input">The input to convert</param> /// <param name="input">The input to convert</param>
/// <returns>The resulting buttons</returns> /// <returns>The resulting buttons</returns>
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Gamepad"/></exception> /// <exception cref="InvalidOperationException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Gamepad"/></exception>
public static implicit operator Buttons(GenericInput input) { public static implicit operator Buttons(GenericInput input) {
return input.Type == InputType.Gamepad ? (Buttons) input.value : throw new ArgumentException(); return input.Button;
} }
/// <summary> /// <summary>