2020-06-12 17:09:35 +02:00
using System ;
using System.Runtime.Serialization ;
using Microsoft.Xna.Framework.Input ;
namespace MLEM.Input {
/// <summary>
/// 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.
/// For creating and extracting inputs from a generic input, the implicit operators and <see cref="Type"/> can be used.
/// Note that this type is serializable using <see cref="DataContractAttribute"/>.
/// </summary>
[DataContract]
2020-06-18 17:24:35 +02:00
public readonly struct GenericInput {
2020-06-12 17:09:35 +02:00
/// <summary>
/// The <see cref="InputType"/> of this generic input's current <see cref="value"/>.
/// </summary>
[DataMember]
public readonly InputType Type ;
[DataMember]
private readonly int value ;
private GenericInput ( InputType type , int value ) {
this . Type = type ;
this . value = value ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Returns this generic input, converted to a string.</summary>
/// <returns>This generic input, converted to a string.</returns>
2021-06-20 23:05:02 +02:00
public override string ToString ( ) {
switch ( this . Type ) {
case InputType . Mouse :
2021-07-13 15:41:42 +02:00
return $"Mouse{(MouseButton) this}" ;
2021-06-20 23:05:02 +02:00
case InputType . Keyboard :
2021-07-13 15:41:42 +02:00
return ( ( Keys ) this ) . ToString ( ) ;
2021-06-20 23:05:02 +02:00
case InputType . Gamepad :
2021-07-13 15:41:42 +02:00
return $"Gamepad{(Buttons) this}" ;
default :
return this . Type . ToString ( ) ;
2021-06-20 23:05:02 +02:00
}
}
2021-11-22 19:25:18 +01:00
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, <see langword="false" />.</returns>
2021-06-20 23:17:39 +02:00
public override bool Equals ( object obj ) {
return obj is GenericInput o & & this . Type = = o . Type & & this . value = = o . value ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
2021-06-20 23:17:39 +02:00
public override int GetHashCode ( ) {
return ( ( int ) this . Type * 397 ) ^ this . value ;
}
/// <summary>
/// Compares the two generic input instances for equality using <see cref="Equals"/>
/// </summary>
/// <param name="left">The left input</param>
/// <param name="right">The right input</param>
/// <returns>Whether the two generic inputs are equal</returns>
public static bool operator = = ( GenericInput left , GenericInput right ) {
return left . Equals ( right ) ;
}
/// <summary>
/// Compares the two generic input instances for inequality using <see cref="Equals"/>
/// </summary>
/// <param name="left">The left input</param>
/// <param name="right">The right input</param>
/// <returns>Whether the two generic inputs are not equal</returns>
public static bool operator ! = ( GenericInput left , GenericInput right ) {
return ! left . Equals ( right ) ;
}
2020-06-12 17:09:35 +02:00
/// <summary>
/// Converts a <see cref="Keys"/> to a generic input.
/// </summary>
/// <param name="keys">The keys to convert</param>
/// <returns>The resulting generic input</returns>
public static implicit operator GenericInput ( Keys keys ) {
return new GenericInput ( InputType . Keyboard , ( int ) keys ) ;
}
/// <summary>
/// Converts a <see cref="MouseButton"/> to a generic input.
/// </summary>
/// <param name="button">The button to convert</param>
/// <returns>The resulting generic input</returns>
public static implicit operator GenericInput ( MouseButton button ) {
return new GenericInput ( InputType . Mouse , ( int ) button ) ;
}
/// <summary>
/// Converts a <see cref="Buttons"/> to a generic input.
/// </summary>
/// <param name="buttons">The buttons to convert</param>
/// <returns>The resulting generic input</returns>
public static implicit operator GenericInput ( Buttons buttons ) {
return new GenericInput ( InputType . Gamepad , ( int ) buttons ) ;
}
/// <summary>
/// Converts a generic input to a <see cref="Keys"/>.
/// </summary>
/// <param name="input">The input to convert</param>
/// <returns>The resulting keys</returns>
2021-06-21 00:49:09 +02:00
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Keyboard"/> or <see cref="InputType.None"/></exception>
2020-06-12 17:09:35 +02:00
public static implicit operator Keys ( GenericInput input ) {
2021-06-21 00:49:09 +02:00
if ( input . Type = = InputType . None )
return Keys . None ;
return input . Type = = InputType . Keyboard ? ( Keys ) input . value : throw new ArgumentException ( ) ;
2020-06-12 17:09:35 +02:00
}
/// <summary>
/// Converts a generic input to a <see cref="MouseButton"/>.
/// </summary>
/// <param name="input">The input to convert</param>
/// <returns>The resulting button</returns>
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Mouse"/></exception>
public static implicit operator MouseButton ( GenericInput input ) {
2021-06-21 00:49:09 +02:00
return input . Type = = InputType . Mouse ? ( MouseButton ) input . value : throw new ArgumentException ( ) ;
2020-06-12 17:09:35 +02:00
}
/// <summary>
/// Converts a generic input to a <see cref="Buttons"/>.
/// </summary>
/// <param name="input">The input to convert</param>
/// <returns>The resulting buttons</returns>
/// <exception cref="ArgumentException">If the given generic input's <see cref="Type"/> is not <see cref="InputType.Gamepad"/></exception>
public static implicit operator Buttons ( GenericInput input ) {
2021-06-21 00:49:09 +02:00
return input . Type = = InputType . Gamepad ? ( Buttons ) input . value : throw new ArgumentException ( ) ;
2020-06-12 17:09:35 +02:00
}
/// <summary>
/// A type of input button.
/// </summary>
[DataContract]
public enum InputType {
2021-06-21 00:49:09 +02:00
/// <summary>
/// A type representing no value
/// </summary>
[EnumMember]
None ,
2020-06-12 17:09:35 +02:00
/// <summary>
/// A type representing <see cref="MouseButton"/>
/// </summary>
[EnumMember]
Mouse ,
/// <summary>
/// A type representing <see cref="Keys"/>
/// </summary>
[EnumMember]
Keyboard ,
/// <summary>
/// A type representing <see cref="Buttons"/>
/// </summary>
[EnumMember]
Gamepad
}
}
}