using System; using System.Runtime.Serialization; using Microsoft.Xna.Framework.Input; 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. /// Note that this type is serializable using . /// [DataContract] public readonly struct GenericInput { /// /// The of this generic input's current . /// [DataMember] public readonly InputType Type; [DataMember] private readonly int value; private GenericInput(InputType type, int value) { this.Type = type; this.value = value; } /// public override string ToString() { var ret = this.Type.ToString(); switch (this.Type) { case InputType.Mouse: ret += ((MouseButton) this).ToString(); break; case InputType.Keyboard: ret += ((Keys) this).ToString(); break; case InputType.Gamepad: ret += ((Buttons) this).ToString(); break; } return ret; } /// public override bool Equals(object obj) { return obj is GenericInput o && this.Type == o.Type && this.value == o.value; } /// public override int GetHashCode() { return ((int) this.Type * 397) ^ this.value; } /// /// Compares the two generic input instances for equality using /// /// The left input /// The right input /// Whether the two generic inputs are equal public static bool operator ==(GenericInput left, GenericInput right) { return left.Equals(right); } /// /// Compares the two generic input instances for inequality using /// /// The left input /// The right input /// Whether the two generic inputs are not equal public static bool operator !=(GenericInput left, GenericInput right) { return !left.Equals(right); } /// /// Converts a to a generic input. /// /// The keys to convert /// The resulting generic input public static implicit operator GenericInput(Keys keys) { return new GenericInput(InputType.Keyboard, (int) keys); } /// /// Converts a to a generic input. /// /// The button to convert /// The resulting generic input public static implicit operator GenericInput(MouseButton button) { return new GenericInput(InputType.Mouse, (int) button); } /// /// Converts a to a generic input. /// /// The buttons to convert /// The resulting generic input public static implicit operator GenericInput(Buttons buttons) { return new GenericInput(InputType.Gamepad, (int) buttons); } /// /// Converts a generic input to a . /// /// The input to convert /// The resulting keys /// 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(); } /// /// Converts a generic input to a . /// /// The input to convert /// The resulting button /// 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(); } /// /// Converts a generic input to a . /// /// The input to convert /// The resulting buttons /// 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(); } /// /// A type of input button. /// [DataContract] public enum InputType { /// /// A type representing no value /// [EnumMember] None, /// /// A type representing /// [EnumMember] Mouse, /// /// A type representing /// [EnumMember] Keyboard, /// /// A type representing /// [EnumMember] Gamepad } } }