1
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -9,13 +10,16 @@ using UnityEngine.InputSystem;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
|
||||
namespace BITKit.Console
|
||||
{
|
||||
public class BITConsole : UXPanel
|
||||
public class BITConsole : MonoBehaviour
|
||||
{
|
||||
[BITCommand]
|
||||
public static void Clear()
|
||||
public static async void Clear()
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
singleton.outputString.Clear();
|
||||
singleton.text.text = string.Empty;
|
||||
}
|
||||
@@ -24,21 +28,27 @@ namespace BITKit.Console
|
||||
const string commandListViewName = "commands-listview";
|
||||
const string textName = "Text";
|
||||
const string scrollViewName = "context-scrollview";
|
||||
public InputActionReference inputAction;
|
||||
public InputActionReference nextOrPreviousAction;
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeReference] private InputActionReference toggleAction;
|
||||
[SerializeReference] public InputActionReference nextOrPreviousAction;
|
||||
|
||||
private readonly InputActionGroup _inputActionGroup=new()
|
||||
{
|
||||
allowGlobalActivation = false
|
||||
};
|
||||
public int logLineLimit = 64;
|
||||
private ListView commandListView;
|
||||
private ListView commandListView;
|
||||
private TextField textField;
|
||||
private Label text;
|
||||
private ScrollView scrollView;
|
||||
private bool isActived;
|
||||
private List<string> outputString = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
inputAction.action.Enable();
|
||||
nextOrPreviousAction.action.Enable();
|
||||
|
||||
Application.logMessageReceivedThreaded += LogCallback;
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
singleton = this;
|
||||
var visualElement = document.rootVisualElement;
|
||||
textField = visualElement.Q<TextField>(textFieldName);
|
||||
@@ -51,13 +61,11 @@ namespace BITKit.Console
|
||||
|
||||
commandListView.selectionChanged += OnSelectionChange;
|
||||
|
||||
inputAction.action.performed += Toggle;
|
||||
|
||||
nextOrPreviousAction.action.performed += OnNextCommand;
|
||||
|
||||
text.text = string.Empty;
|
||||
|
||||
Application.logMessageReceived += LogCallback;
|
||||
|
||||
|
||||
BIT4Log.OnNextLine += () =>
|
||||
{
|
||||
@@ -66,18 +74,28 @@ namespace BITKit.Console
|
||||
};
|
||||
|
||||
commandListView.SetActive(false);
|
||||
|
||||
_inputActionGroup.RegisterCallback(toggleAction,Toggle);
|
||||
_inputActionGroup.RegisterCallback(nextOrPreviousAction, OnNextCommand);
|
||||
|
||||
_inputActionGroup.allowInput.AddElement(this);
|
||||
|
||||
Toggle(false);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
|
||||
inputAction.action.performed -= Toggle;
|
||||
Application.logMessageReceived -= LogCallback;
|
||||
_inputActionGroup.allowInput.RemoveElement(this);
|
||||
Application.logMessageReceivedThreaded -= LogCallback;
|
||||
}
|
||||
public override async void Set(bool active)
|
||||
public async void Toggle(bool active)
|
||||
{
|
||||
base.Set(active);
|
||||
document.rootVisualElement.SetActive(active);
|
||||
isActived = active;
|
||||
|
||||
BITAppForUnity.AllowCursor.SetElements(this,active);
|
||||
BITInputSystem.AllowInput.SetDisableElements(this,active);
|
||||
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
if (active)
|
||||
{
|
||||
@@ -92,7 +110,7 @@ namespace BITKit.Console
|
||||
text.Blur();
|
||||
}
|
||||
}
|
||||
void OnTextFieldValieChanged(ChangeEvent<string> callback)
|
||||
private void OnTextFieldValieChanged(ChangeEvent<string> callback)
|
||||
{
|
||||
if (callback.newValue.IsValid())
|
||||
{
|
||||
@@ -111,9 +129,7 @@ namespace BITKit.Console
|
||||
{
|
||||
commandListView.SetActive(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnDropdownValueChanged(ChangeEvent<string> callback)
|
||||
{
|
||||
textField.SetValueWithoutNotify(callback.newValue);
|
||||
@@ -147,60 +163,56 @@ namespace BITKit.Console
|
||||
}
|
||||
}
|
||||
|
||||
void Toggle(InputAction.CallbackContext context)
|
||||
private void Toggle(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.performed)
|
||||
switch (context)
|
||||
{
|
||||
if (isActived is false)
|
||||
{
|
||||
UXFramework.Enter<BITConsole>();
|
||||
}
|
||||
else
|
||||
{
|
||||
UXFramework.Return();
|
||||
}
|
||||
case { interaction: PressInteraction, performed: true }:
|
||||
Toggle(!isActived);
|
||||
break;
|
||||
}
|
||||
}
|
||||
async void LogCallback(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
|
||||
var debugLevel = Data.Get<int>("DebugLevel");
|
||||
switch (type)
|
||||
{
|
||||
case LogType.Error:
|
||||
outputString.Add($"<color=red>{condition}</color>");
|
||||
if (debugLevel is 2)
|
||||
outputString.Add(stackTrace);
|
||||
break;
|
||||
case LogType.Warning:
|
||||
outputString.Add($"<color=yellow>{condition}</color>");
|
||||
if (debugLevel is 2)
|
||||
outputString.Add(stackTrace);
|
||||
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));
|
||||
private async void LogCallback(string condition, string stackTrace, LogType type)
|
||||
{
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToMainThread(BITApp.CancellationToken);
|
||||
scrollView.ScrollToBottomAutomatic();
|
||||
text.text = stringBuilder.ToString();
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
catch (System.OperationCanceledException)
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +230,7 @@ namespace BITKit.Console
|
||||
commandListView.style.width = size.x;
|
||||
}
|
||||
|
||||
void OnSelectionChange(IEnumerable<object> selected)
|
||||
private void OnSelectionChange(IEnumerable<object> selected)
|
||||
{
|
||||
var _selected = selected.First() as string;
|
||||
textField.SetValueWithoutNotify(_selected);
|
||||
|
Reference in New Issue
Block a user