quakeball/Assets/Scripts/Util/PressedActionDisplayer.cs

60 lines
2.2 KiB
C#
Raw Normal View History

2020-08-07 00:43:30 +02:00
using UnityEngine;
using UnityEngine.InputSystem;
using NeonTea.Quakeball.Interface;
2020-08-07 00:43:30 +02:00
namespace NeonTea.Quakeball.Util {
public class PressedActionDisplayer : MonoBehaviour {
private InputAction Initializer;
private InputAction AnyAction;
private InputActionRebindingExtensions.RebindingOperation Rebinding;
private bool InitializedFromTerminal = false;
2020-08-07 00:43:30 +02:00
private void Awake() {
AnyAction = new InputAction("InputAction displayer", binding: "*/*");
Initializer = new InputAction("Display the next InputAction", binding: "<Keyboard>/f1");
Initializer.Enable();
Initializer.performed += _ => {
InitializedFromTerminal = false;
StartWaiting();
2020-08-07 00:43:30 +02:00
};
Terminal.Singleton.RegisterCommand("read", args => {
if (args.Length == 0) {
InitializedFromTerminal = true;
StartWaiting();
return true;
} else {
2020-08-07 01:42:59 +02:00
Terminal.Singleton.Println($"<color={Terminal.ERROR_COLOR}>The 'read' command takes no arguments.</color>");
return false;
}
2020-08-07 01:42:59 +02:00
}, "read - Waits for an input action, and then prints it.");
}
private void StartWaiting() {
if (!InitializedFromTerminal) {
Debug.Log("Waiting for InputAction to display...");
} else {
2020-08-07 01:42:59 +02:00
Terminal.Singleton.Println("Waiting for InputAction to display...");
}
Rebinding = AnyAction.PerformInteractiveRebinding().Start();
2020-08-07 00:43:30 +02:00
}
private void Update() {
if (Rebinding != null && Rebinding.completed) {
string Binding = Rebinding.action.ToString();
Rebinding.Dispose();
2020-08-07 00:43:30 +02:00
int Index = Binding.IndexOf("/") + 1;
Binding = Binding.Substring(Index, Binding.Length - Index - 1);
if (!InitializedFromTerminal) {
Debug.Log("Binding: " + Binding);
} else {
2020-08-07 01:42:59 +02:00
Terminal.Singleton.Println("Binding: " + Binding);
}
2020-08-07 00:43:30 +02:00
Rebinding = null;
}
}
}
}