82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.InputSystem;
|
|||
|
using UnityEngine.EventSystems;
|
|||
|
using NeonTea.Quakeball.Player;
|
|||
|
using TMPro;
|
|||
|
|
|||
|
namespace NeonTea.Quakeball.Interface {
|
|||
|
public class Terminal : MonoBehaviour {
|
|||
|
|
|||
|
public RectTransform TerminalPanel;
|
|||
|
public TMP_InputField InputField;
|
|||
|
public TMP_Text TextField;
|
|||
|
public LocalPlayer Player;
|
|||
|
|
|||
|
private InputAction ToggleTerminalAction;
|
|||
|
private InputAction SubmitTerminal;
|
|||
|
private float DesiredTerminalPos;
|
|||
|
private bool IsOpen = false;
|
|||
|
|
|||
|
private List<string> Messages = new List<string>();
|
|||
|
private string Text = "";
|
|||
|
|
|||
|
public static string INFO_COLOR = "#FE8";
|
|||
|
|
|||
|
void Start() {
|
|||
|
ToggleTerminalAction = new InputAction("Toggle Terminal", binding: "<Keyboard>/backquote");
|
|||
|
ToggleTerminalAction.Enable();
|
|||
|
ToggleTerminalAction.performed += ToggleTerminal;
|
|||
|
SubmitTerminal = new InputAction("Terminal Submit", binding: "<Keyboard>/enter");
|
|||
|
SubmitTerminal.Enable();
|
|||
|
SubmitTerminal.performed += Submit;
|
|||
|
|
|||
|
DesiredTerminalPos = (TerminalPanel.rect.height / 2) * (IsOpen ? -1 : 1);
|
|||
|
|
|||
|
InputField.restoreOriginalTextOnEscape = false;
|
|||
|
|
|||
|
AddMessage($"<color={INFO_COLOR}>Welcome to Quakeball!</color>");
|
|||
|
}
|
|||
|
|
|||
|
public void ToggleTerminal(InputAction.CallbackContext context) {
|
|||
|
IsOpen = !IsOpen;
|
|||
|
DesiredTerminalPos = (TerminalPanel.rect.height / 2) * (IsOpen ? -1 : 1);
|
|||
|
if (IsOpen) {
|
|||
|
InputField.text = "";
|
|||
|
}
|
|||
|
InputField.readOnly = true;
|
|||
|
if (Player != null) {
|
|||
|
Player.DisableInput = IsOpen;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void Update() {
|
|||
|
Vector3 pos = TerminalPanel.anchoredPosition;
|
|||
|
pos.y = Mathf.Lerp(pos.y, DesiredTerminalPos, Time.deltaTime * 10);
|
|||
|
TerminalPanel.anchoredPosition = pos;
|
|||
|
if (IsOpened() && EventSystem.current.currentSelectedGameObject != InputField) {
|
|||
|
InputField.Select();
|
|||
|
InputField.ActivateInputField();
|
|||
|
InputField.readOnly = false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void AddMessage(string message) {
|
|||
|
Messages.Add(message);
|
|||
|
Text = String.Join("\n", Messages.ToArray());
|
|||
|
TextField.text = Text;
|
|||
|
}
|
|||
|
|
|||
|
private void Submit(InputAction.CallbackContext context) {
|
|||
|
if (IsOpened()) {
|
|||
|
AddMessage(InputField.text);
|
|||
|
InputField.text = "";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private bool IsOpened() {
|
|||
|
return IsOpen && (TerminalPanel.anchoredPosition.y + TerminalPanel.rect.height / 2) < 10;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|