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() { switch (this.Type) { case InputType.Mouse: return ((MouseButton) this).ToString(); case InputType.Keyboard: return ((Keys) this).ToString(); case InputType.Gamepad: return ((Buttons) this).ToString(); default: throw new ArgumentOutOfRangeException(nameof(this.Type)); } } /// /// 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 public static implicit operator Keys(GenericInput input) { if (input.Type != InputType.Keyboard) throw new ArgumentException(); return (Keys) input.value; } /// /// 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) { if (input.Type != InputType.Mouse) throw new ArgumentException(); return (MouseButton) input.value; } /// /// 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) { if (input.Type != InputType.Gamepad) throw new ArgumentException(); return (Buttons) input.value; } /// /// A type of input button. /// [DataContract] public enum InputType { /// /// A type representing /// [EnumMember] Mouse, /// /// A type representing /// [EnumMember] Keyboard, /// /// A type representing /// [EnumMember] Gamepad } } }