From ae6ce6e7d5dea792ea7c300c3fb94a2feb75cd12 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 26 Mar 2022 12:51:14 +0100 Subject: [PATCH] Added properties and constructors for existing operator overloads to GenericInput --- CHANGELOG.md | 1 + MLEM/Input/GenericInput.cs | 70 +++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3240ebc..6ec39d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Improvements - Allow better control over the order and layout of a Keybind's combinations - Allow setting a gamepad button deadzone in InputHandler - Trigger InputHandler key and gamepad repeats for the most recently pressed input +- Added properties and constructors for existing operator overloads to GenericInput Fixes - **Fixed a formatting Code only knowing about the last Token that it is applied in** diff --git a/MLEM/Input/GenericInput.cs b/MLEM/Input/GenericInput.cs index 871b651..4432454 100644 --- a/MLEM/Input/GenericInput.cs +++ b/MLEM/Input/GenericInput.cs @@ -6,7 +6,7 @@ namespace MLEM.Input { /// /// A generic input represents any kind of input key. /// This includes for keyboard keys, for mouse buttons and for gamepad buttons. - /// For creating and extracting inputs from a generic input, the implicit operators and can be used. + /// For creating and extracting inputs from a generic input, the implicit operators and can additionally be used. /// Note that this type is serializable using . /// [DataContract] @@ -20,6 +20,46 @@ namespace MLEM.Input { [DataMember] private readonly int value; + /// + /// Returns this generic input's . + /// + /// If this generic input's is not or . + public Keys Key { + get { + if (this.Type == InputType.None) + return Keys.None; + return this.Type == InputType.Keyboard ? (Keys) this.value : throw new InvalidOperationException(); + } + } + /// + /// Returns this generic input's . + /// + /// If this generic input's is not . + public MouseButton MouseButton => this.Type == InputType.Mouse ? (MouseButton) this.value : throw new InvalidOperationException(); + /// + /// Returns this generic input's . + /// + /// If this generic input's is not . + public Buttons Button => this.Type == InputType.Gamepad ? (Buttons) this.value : throw new InvalidOperationException(); + + /// + /// Creates a new generic input from the given keyboard . + /// + /// The key to convert. + public GenericInput(Keys key) : this(InputType.Keyboard, (int) key) {} + + /// + /// Creates a new generic input from the given . + /// + /// The button to convert. + public GenericInput(MouseButton button) : this(InputType.Mouse, (int) button) {} + + /// + /// Creates a new generic input from the given gamepad . + /// + /// The button to convert. + public GenericInput(Buttons button) : this(InputType.Gamepad, (int) button) {} + private GenericInput(InputType type, int value) { this.Type = type; this.value = value; @@ -83,10 +123,10 @@ namespace MLEM.Input { /// /// Converts a to a generic input. /// - /// The keys to convert + /// The keys to convert /// The resulting generic input - public static implicit operator GenericInput(Keys keys) { - return new GenericInput(InputType.Keyboard, (int) keys); + public static implicit operator GenericInput(Keys key) { + return new GenericInput(key); } /// @@ -95,16 +135,16 @@ namespace MLEM.Input { /// The button to convert /// The resulting generic input public static implicit operator GenericInput(MouseButton button) { - return new GenericInput(InputType.Mouse, (int) button); + return new GenericInput(button); } /// /// Converts a to a generic input. /// - /// The buttons to convert + /// The buttons to convert /// The resulting generic input - public static implicit operator GenericInput(Buttons buttons) { - return new GenericInput(InputType.Gamepad, (int) buttons); + public static implicit operator GenericInput(Buttons button) { + return new GenericInput(button); } /// @@ -112,11 +152,9 @@ namespace MLEM.Input { /// /// The input to convert /// The resulting keys - /// If the given generic input's is not or + /// If the given generic input's is not or public static implicit operator Keys(GenericInput input) { - if (input.Type == InputType.None) - return Keys.None; - return input.Type == InputType.Keyboard ? (Keys) input.value : throw new ArgumentException(); + return input.Key; } /// @@ -124,9 +162,9 @@ namespace MLEM.Input { /// /// The input to convert /// The resulting button - /// If the given generic input's is not + /// If the given generic input's is not public static implicit operator MouseButton(GenericInput input) { - return input.Type == InputType.Mouse ? (MouseButton) input.value : throw new ArgumentException(); + return input.MouseButton; } /// @@ -134,9 +172,9 @@ namespace MLEM.Input { /// /// The input to convert /// The resulting buttons - /// If the given generic input's is not + /// If the given generic input's is not public static implicit operator Buttons(GenericInput input) { - return input.Type == InputType.Gamepad ? (Buttons) input.value : throw new ArgumentException(); + return input.Button; } ///