using UnityEngine;
namespace CnControls
{
///
/// Virtual button class
///
public class VirtualButton
{
///
/// Name of the button for which this virtual button has to be registered
///
public string Name { get; set; }
///
/// Is this button currently pressed?
///
public bool IsPressed { get; private set; }
///
/// The last frame this button was pressed
///
private int _lastPressedFrame = -1;
///
/// The last frame this butto was released
///
private int _lastReleasedFrame = -1;
public VirtualButton(string name)
{
Name = name;
}
///
/// Press logic sets the current state of the button to "IsPressed" untill the Release() method is called
///
public void Press()
{
if (IsPressed)
{
return;
}
IsPressed = true;
_lastPressedFrame = Time.frameCount;
}
///
/// Release logic frees the button from its "IsPressed" state
///
public void Release()
{
IsPressed = false;
_lastReleasedFrame = Time.frameCount;
}
///
/// Is this button currently pressed?
///
public bool GetButton
{
get { return IsPressed; }
}
///
/// Check whether this button has just been pressed
///
public bool GetButtonDown
{
get
{
return _lastPressedFrame != -1 && _lastPressedFrame - Time.frameCount == -1;
}
}
///
/// Check whether this button has just been released
///
public bool GetButtonUp
{
get
{
return _lastReleasedFrame != -1 && _lastReleasedFrame == Time.frameCount - 1;
}
}
}
}