using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace MoreMountains.Tools
{
///
/// A class used to store MMInputExecution bindings, associating a target keycode to UnityEvents
///
[System.Serializable]
public class MMInputExecutionBinding
{
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
public Key TargetInputKey = Key.Space;
#else
/// the key the user needs to press to trigger events
public KeyCode TargetKey = KeyCode.Space;
#endif
/// the event to trigger when the key is pressed down
public UnityEvent OnKeyDown;
/// the event to trigger every frame if the key is being pressed
public UnityEvent OnKey;
/// the event to trigger when the key is released
public UnityEvent OnKeyUp;
///
/// Checks for input and invokes events if needed
///
public virtual void ProcessInput()
{
bool key = false;
bool keyDown = false;
bool keyUp = false;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
key = Keyboard.current[TargetInputKey].isPressed;
keyDown = Keyboard.current[TargetInputKey].wasPressedThisFrame;
keyUp = Keyboard.current[TargetInputKey].wasReleasedThisFrame;
#else
key = Input.GetKey(TargetKey);
keyDown = Input.GetKeyDown(TargetKey);
keyUp = Input.GetKeyUp(TargetKey);
#endif
if (OnKey != null)
{
if (key)
{
OnKey.Invoke();
}
}
if (OnKeyDown != null)
{
if (keyDown)
{
OnKeyDown.Invoke();
}
}
if (OnKeyUp != null)
{
if (keyUp)
{
OnKeyUp.Invoke();
}
}
}
}
///
/// A simple class used to bind target keys to specific events to trigger when the key is pressed or released
///
public class MMInputExecution : MonoBehaviour
{
[Header("Bindings")]
/// a list of bindings
public List Bindings;
///
/// On update we process our input
///
protected virtual void Update()
{
HandleInput();
}
///
/// Parses all bindings and asks them to trigger events if needed
///
protected virtual void HandleInput()
{
if (Bindings == null)
{
return;
}
foreach(MMInputExecutionBinding binding in Bindings)
{
binding.ProcessInput();
}
}
}
}