2023-11-15 23:54:54 +08:00
|
|
|
using System;
|
2023-06-08 14:09:50 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
using BITKit;
|
|
|
|
using BITKit.UX;
|
|
|
|
using System.Linq;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using System.Text;
|
|
|
|
using System.IO;
|
2023-11-15 23:54:54 +08:00
|
|
|
using UnityEngine.InputSystem.Interactions;
|
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
namespace BITKit.Console
|
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
public class BITConsole : MonoBehaviour
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
[BITCommand]
|
2023-11-15 23:54:54 +08:00
|
|
|
public static async void Clear()
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
await BITApp.SwitchToMainThread();
|
2023-06-08 14:09:50 +08:00
|
|
|
singleton.outputString.Clear();
|
|
|
|
singleton.text.text = string.Empty;
|
|
|
|
}
|
|
|
|
static BITConsole singleton;
|
|
|
|
const string textFieldName = "TextField";
|
|
|
|
const string commandListViewName = "commands-listview";
|
|
|
|
const string textName = "Text";
|
|
|
|
const string scrollViewName = "context-scrollview";
|
2023-11-15 23:54:54 +08:00
|
|
|
[SerializeField] private UIDocument document;
|
|
|
|
[SerializeReference] private InputActionReference toggleAction;
|
|
|
|
[SerializeReference] public InputActionReference nextOrPreviousAction;
|
|
|
|
|
|
|
|
private readonly InputActionGroup _inputActionGroup=new()
|
|
|
|
{
|
|
|
|
allowGlobalActivation = false
|
|
|
|
};
|
2023-06-08 14:09:50 +08:00
|
|
|
public int logLineLimit = 64;
|
2023-11-15 23:54:54 +08:00
|
|
|
private ListView commandListView;
|
2023-08-12 01:43:24 +08:00
|
|
|
private TextField textField;
|
|
|
|
private Label text;
|
|
|
|
private ScrollView scrollView;
|
|
|
|
private bool isActived;
|
|
|
|
private List<string> outputString = new();
|
|
|
|
private void Awake()
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
Application.logMessageReceivedThreaded += LogCallback;
|
|
|
|
}
|
|
|
|
private void Start()
|
|
|
|
{
|
2023-06-08 14:09:50 +08:00
|
|
|
singleton = this;
|
|
|
|
var visualElement = document.rootVisualElement;
|
|
|
|
textField = visualElement.Q<TextField>(textFieldName);
|
|
|
|
commandListView = visualElement.Q<ListView>(commandListViewName);
|
|
|
|
text = visualElement.Q<Label>(textName);
|
|
|
|
scrollView = visualElement.Q<ScrollView>(scrollViewName);
|
|
|
|
|
|
|
|
textField.RegisterValueChangedCallback(OnTextFieldValieChanged);
|
|
|
|
textField.RegisterCallback<KeyDownEvent>(OnKeyDown);
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
commandListView.selectionChanged += OnSelectionChange;
|
2023-06-08 14:09:50 +08:00
|
|
|
|
|
|
|
nextOrPreviousAction.action.performed += OnNextCommand;
|
|
|
|
|
|
|
|
text.text = string.Empty;
|
|
|
|
|
2023-11-15 23:54:54 +08:00
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
|
|
|
|
BIT4Log.OnNextLine += () =>
|
|
|
|
{
|
|
|
|
if (outputString.Count is not 0 && outputString.Last() != string.Empty)
|
|
|
|
outputString.Add(string.Empty);
|
|
|
|
};
|
|
|
|
|
|
|
|
commandListView.SetActive(false);
|
2023-11-15 23:54:54 +08:00
|
|
|
|
|
|
|
_inputActionGroup.RegisterCallback(toggleAction,Toggle);
|
|
|
|
_inputActionGroup.RegisterCallback(nextOrPreviousAction, OnNextCommand);
|
|
|
|
|
|
|
|
_inputActionGroup.allowInput.AddElement(this);
|
|
|
|
|
|
|
|
Toggle(false);
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
|
|
|
private void OnDestroy()
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
_inputActionGroup.allowInput.RemoveElement(this);
|
|
|
|
Application.logMessageReceivedThreaded -= LogCallback;
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
2023-11-15 23:54:54 +08:00
|
|
|
public async void Toggle(bool active)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
document.rootVisualElement.SetActive(active);
|
2023-06-08 14:09:50 +08:00
|
|
|
isActived = active;
|
2023-11-15 23:54:54 +08:00
|
|
|
|
|
|
|
BITAppForUnity.AllowCursor.SetElements(this,active);
|
|
|
|
BITInputSystem.AllowInput.SetDisableElements(this,active);
|
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
await UniTask.WaitForEndOfFrame(this);
|
|
|
|
if (active)
|
|
|
|
{
|
|
|
|
textField.SetValueWithoutNotify(string.Empty);
|
|
|
|
|
|
|
|
textField.Focus();
|
|
|
|
|
|
|
|
scrollView.ScrollToBottom();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text.Blur();
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 23:54:54 +08:00
|
|
|
private void OnTextFieldValieChanged(ChangeEvent<string> callback)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
if (callback.newValue.IsValid())
|
|
|
|
{
|
2023-08-12 01:43:24 +08:00
|
|
|
var commands = BITCommands.GetMethodInfos(callback.newValue).Select(x => x.Name).ToList();
|
2023-06-08 14:09:50 +08:00
|
|
|
if (commands.Count is 0)
|
|
|
|
{
|
|
|
|
commandListView.SetActive(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandListView.itemsSource = commands;
|
|
|
|
commandListView.SetActive(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandListView.SetActive(false);
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
private void OnDropdownValueChanged(ChangeEvent<string> callback)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
textField.SetValueWithoutNotify(callback.newValue);
|
|
|
|
OnTextFieldValieChanged(callback);
|
|
|
|
textField.Focus();
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
private void OnNextCommand(InputAction.CallbackContext context)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
if (context.started)
|
|
|
|
{
|
|
|
|
var index = context.ReadValue<Vector2>().y;
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
private void OnKeyDown(KeyDownEvent keyDownEvent)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
switch (keyDownEvent.keyCode)
|
|
|
|
{
|
|
|
|
case KeyCode.Return:
|
|
|
|
var cmd = textField.text;
|
|
|
|
|
|
|
|
LogCallback($">{cmd}", string.Empty, LogType.Log);
|
|
|
|
|
|
|
|
textField.SetValueWithoutNotify(string.Empty);
|
|
|
|
textField.Focus();
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
BITCommands.Excute(cmd);
|
2023-06-08 14:09:50 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
case KeyCode.Tab:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:54:54 +08:00
|
|
|
private void Toggle(InputAction.CallbackContext context)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
switch (context)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
case { interaction: PressInteraction, performed: true }:
|
|
|
|
Toggle(!isActived);
|
2023-06-08 14:09:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2023-11-15 23:54:54 +08:00
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
|
2023-11-15 23:54:54 +08:00
|
|
|
private async void LogCallback(string condition, string stackTrace, LogType type)
|
|
|
|
{
|
2023-06-08 14:09:50 +08:00
|
|
|
try
|
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case LogType.Error:
|
|
|
|
outputString.Add($"<color=red>{condition}</color>");
|
|
|
|
break;
|
|
|
|
case LogType.Warning:
|
|
|
|
outputString.Add($"<color=yellow>{condition}</color>");
|
|
|
|
break;
|
|
|
|
case LogType.Exception:
|
|
|
|
outputString.Add($"<color=red>{condition}</color>");
|
|
|
|
outputString.Add($"<color=red>{stackTrace}</color>");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
outputString.Add(condition);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var length = outputString.Count;
|
|
|
|
if (length > logLineLimit)
|
|
|
|
{
|
|
|
|
outputString = outputString.GetRange(length - logLineLimit, logLineLimit);
|
|
|
|
}
|
|
|
|
StringBuilder stringBuilder = new();
|
|
|
|
outputString.ForEach(x => stringBuilder.AppendLine(x));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await BITApp.SwitchToMainThread();
|
|
|
|
scrollView.ScrollToBottomAutomatic();
|
|
|
|
text.text = stringBuilder.ToString();
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
2023-11-15 23:54:54 +08:00
|
|
|
catch (Exception e)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
Debug.LogException(e);
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
private void Update()
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
var pos = textField.worldTransform.GetPosition();
|
|
|
|
var size = textField.layout.size;
|
|
|
|
|
|
|
|
commandListView.style.left = 0;
|
|
|
|
commandListView.style.top = 0;
|
|
|
|
|
|
|
|
pos.y += size.y;
|
|
|
|
commandListView.transform.position = pos;
|
|
|
|
|
|
|
|
commandListView.style.width = size.x;
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:54:54 +08:00
|
|
|
private void OnSelectionChange(IEnumerable<object> selected)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
|
|
|
var _selected = selected.First() as string;
|
|
|
|
textField.SetValueWithoutNotify(_selected);
|
|
|
|
commandListView.itemsSource?.Clear();
|
|
|
|
commandListView.SetActive(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|