// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
///
/// Virtual input axis binding. Helps with listening for a selected axis input.
///
public class InputAxis : IComparable, IComparable
{
///
/// The name of the axis to use. See .
///
[Tooltip("The name of the axis to use.")]
public string Name;
///
/// Gets the current axis value.
///
public float Value => Input.GetAxis(Name);
///
/// Gets the current axis raw value.
///
public float ValueRaw => Input.GetAxisRaw(Name);
///
/// Occurs when axis is changed. Called before scripts update.
///
public event Action ValueChanged;
///
/// Initializes a new instance of the class.
///
public InputAxis()
{
Input.AxisValueChanged += Handler;
}
///
/// Initializes a new instance of the class.
///
/// The axis name.
public InputAxis(string name)
{
Input.AxisValueChanged += Handler;
Name = name;
}
private void Handler(string name)
{
if (string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
ValueChanged?.Invoke();
}
///
/// Finalizes an instance of the class.
///
~InputAxis()
{
Input.AxisValueChanged -= Handler;
ValueChanged = null;
}
///
/// Releases this object.
///
public void Dispose()
{
Input.AxisValueChanged -= Handler;
ValueChanged = null;
GC.SuppressFinalize(this);
}
///
public int CompareTo(InputAxis other)
{
return string.Compare(Name, other.Name, StringComparison.Ordinal);
}
///
public int CompareTo(object obj)
{
return obj is InputAxis other ? CompareTo(other) : -1;
}
///
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
///
public override bool Equals(object obj)
{
return obj is InputAxis other && string.Equals(Name, other.Name, StringComparison.Ordinal);
}
///
public override string ToString()
{
return Name;
}
}
}