Add InputAxis and InputEvent integration with stdlib features

This commit is contained in:
Wojtek Figat
2024-03-10 21:11:35 +01:00
parent fc029b018e
commit b00b5bed00
2 changed files with 68 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ namespace FlaxEngine
/// <summary>
/// Virtual input axis binding. Helps with listening for a selected axis input.
/// </summary>
public class InputAxis
public class InputAxis : IComparable, IComparable<InputAxis>
{
/// <summary>
/// The name of the axis to use. See <see cref="Input.AxisMappings"/>.
@@ -47,13 +47,13 @@ namespace FlaxEngine
Input.AxisValueChanged += Handler;
Name = name;
}
private void Handler(string name)
{
if (string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
ValueChanged?.Invoke();
}
/// <summary>
/// Finalizes an instance of the <see cref="InputAxis"/> class.
/// </summary>
@@ -61,7 +61,7 @@ namespace FlaxEngine
{
Input.AxisValueChanged -= Handler;
}
/// <summary>
/// Releases this object.
/// </summary>
@@ -70,5 +70,35 @@ namespace FlaxEngine
Input.AxisValueChanged -= Handler;
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public int CompareTo(InputAxis other)
{
return string.Compare(Name, other.Name, StringComparison.Ordinal);
}
/// <inheritdoc />
public int CompareTo(object obj)
{
return obj is InputAxis other ? CompareTo(other) : -1;
}
/// <inheritdoc />
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is InputAxis other && string.Equals(Name, other.Name, StringComparison.Ordinal);
}
/// <inheritdoc />
public override string ToString()
{
return Name;
}
}
}

View File

@@ -7,7 +7,7 @@ namespace FlaxEngine
/// <summary>
/// Virtual input action binding. Helps with listening for a selected input event.
/// </summary>
public class InputEvent
public class InputEvent : IComparable, IComparable<InputEvent>
{
/// <summary>
/// The name of the action to use. See <see cref="Input.ActionMappings"/>.
@@ -21,7 +21,7 @@ namespace FlaxEngine
public bool Active => Input.GetAction(Name);
/// <summary>
/// Returns the event state. Use Use <see cref="Pressed"/>, <see cref="Pressing"/>, <see cref="Released"/> to catch events without active waiting.
/// Returns the event state. Use <see cref="Pressed"/>, <see cref="Pressing"/>, <see cref="Released"/> to catch events without active waiting.
/// </summary>
public InputActionState State => Input.GetActionState(Name);
@@ -35,12 +35,12 @@ namespace FlaxEngine
/// Occurs when event is pressed (e.g. user pressed a key). Called before scripts update.
/// </summary>
public event Action Pressed;
/// <summary>
/// Occurs when event is being pressing (e.g. user pressing a key). Called before scripts update.
/// </summary>
public event Action Pressing;
/// <summary>
/// Occurs when event is released (e.g. user releases a key). Called before scripts update.
/// </summary>
@@ -102,5 +102,35 @@ namespace FlaxEngine
Input.ActionTriggered -= Handler;
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public int CompareTo(InputEvent other)
{
return string.Compare(Name, other.Name, StringComparison.Ordinal);
}
/// <inheritdoc />
public int CompareTo(object obj)
{
return obj is InputEvent other ? CompareTo(other) : -1;
}
/// <inheritdoc />
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is InputEvent other && string.Equals(Name, other.Name, StringComparison.Ordinal);
}
/// <inheritdoc />
public override string ToString()
{
return Name;
}
}
}