// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
///
/// Virtual input action binding. Helps with listening for a selected input event.
///
public class InputEvent
{
///
/// The name of the action to use. See .
///
[Tooltip("The name of the action to use.")]
public string Name;
///
/// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use to catch events without active waiting.
///
public bool Active => Input.GetAction(Name);
///
/// Occurs when event is triggered (e.g. user pressed a key). Called before scripts update.
///
public event Action Triggered;
///
/// Initializes a new instance of the class.
///
public InputEvent()
{
Input.ActionTriggered += Handler;
}
///
/// Initializes a new instance of the class.
///
/// The action name.
public InputEvent(string name)
{
Input.ActionTriggered += Handler;
Name = name;
}
///
/// Finalizes an instance of the class.
///
~InputEvent()
{
Input.ActionTriggered -= Handler;
}
private void Handler(string name)
{
if (string.Equals(name, Name, StringComparison.OrdinalIgnoreCase))
Triggered?.Invoke();
}
///
/// Releases this object.
///
public void Dispose()
{
Input.ActionTriggered -= Handler;
GC.SuppressFinalize(this);
}
}
}