更改文件架构
This commit is contained in:
232
Packages/Common~/Scripts/Console/BITConsole.cs
Normal file
232
Packages/Common~/Scripts/Console/BITConsole.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
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;
|
||||
namespace BITKit.Console
|
||||
{
|
||||
public class BITConsole : UXPanel
|
||||
{
|
||||
[BITCommand]
|
||||
public static void Clear()
|
||||
{
|
||||
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";
|
||||
public InputActionReference inputAction;
|
||||
public InputActionReference nextOrPreviousAction;
|
||||
public int logLineLimit = 64;
|
||||
ListView commandListView;
|
||||
TextField textField;
|
||||
Label text;
|
||||
ScrollView scrollView;
|
||||
bool isActived;
|
||||
List<string> outputString = new();
|
||||
void Awake()
|
||||
{
|
||||
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);
|
||||
|
||||
commandListView.onSelectionChange += OnSelectionChange;
|
||||
|
||||
inputAction.action.performed += Toggle;
|
||||
|
||||
nextOrPreviousAction.action.performed += OnNextCommand;
|
||||
|
||||
text.text = string.Empty;
|
||||
|
||||
Application.logMessageReceived += LogCallback;
|
||||
|
||||
BIT4Log.OnNextLine += () =>
|
||||
{
|
||||
if (outputString.Count is not 0 && outputString.Last() != string.Empty)
|
||||
outputString.Add(string.Empty);
|
||||
};
|
||||
|
||||
commandListView.SetActive(false);
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
|
||||
inputAction.action.performed -= Toggle;
|
||||
Application.logMessageReceived -= LogCallback;
|
||||
}
|
||||
public async override void Set(bool active)
|
||||
{
|
||||
base.Set(active);
|
||||
isActived = active;
|
||||
await UniTask.WaitForEndOfFrame(this);
|
||||
if (active)
|
||||
{
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
|
||||
textField.Focus();
|
||||
|
||||
scrollView.ScrollToBottom();
|
||||
}
|
||||
else
|
||||
{
|
||||
text.Blur();
|
||||
}
|
||||
}
|
||||
void OnTextFieldValieChanged(ChangeEvent<string> callback)
|
||||
{
|
||||
if (callback.newValue.IsValid())
|
||||
{
|
||||
var commands = BITCommand.GetMethodInfos(callback.newValue).Select(x => x.Name).ToList();
|
||||
if (commands.Count is 0)
|
||||
{
|
||||
commandListView.SetActive(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandListView.itemsSource = commands;
|
||||
commandListView.SetActive(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
commandListView.SetActive(false);
|
||||
}
|
||||
|
||||
}
|
||||
void OnDropdownValueChanged(ChangeEvent<string> callback)
|
||||
{
|
||||
textField.SetValueWithoutNotify(callback.newValue);
|
||||
OnTextFieldValieChanged(callback);
|
||||
textField.Focus();
|
||||
}
|
||||
void OnNextCommand(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.started)
|
||||
{
|
||||
var index = context.ReadValue<Vector2>().y;
|
||||
}
|
||||
}
|
||||
void OnKeyDown(KeyDownEvent keyDownEvent)
|
||||
{
|
||||
switch (keyDownEvent.keyCode)
|
||||
{
|
||||
case KeyCode.Return:
|
||||
var cmd = textField.text;
|
||||
|
||||
LogCallback($">{cmd}", string.Empty, LogType.Log);
|
||||
|
||||
textField.SetValueWithoutNotify(string.Empty);
|
||||
textField.Focus();
|
||||
|
||||
BITCommand.Excute(cmd);
|
||||
|
||||
break;
|
||||
case KeyCode.Tab:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Toggle(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.performed)
|
||||
{
|
||||
if (isActived is false)
|
||||
{
|
||||
UXFramework.Enter<BITConsole>();
|
||||
}
|
||||
else
|
||||
{
|
||||
UXFramework.Return();
|
||||
}
|
||||
}
|
||||
}
|
||||
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));
|
||||
try
|
||||
{
|
||||
if (BITApp.CancellationTokenSource is not null)
|
||||
{
|
||||
await UniTask.SwitchToMainThread(BITApp.CancellationTokenSource.Token);
|
||||
scrollView.ScrollToBottomAutomatic();
|
||||
text.text = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
catch (System.OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void OnSelectionChange(IEnumerable<object> selected)
|
||||
{
|
||||
var _selected = selected.First() as string;
|
||||
textField.SetValueWithoutNotify(_selected);
|
||||
commandListView.itemsSource?.Clear();
|
||||
commandListView.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Console/BITConsole.cs.meta
Normal file
11
Packages/Common~/Scripts/Console/BITConsole.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b23c89eee6c8e0c45afde7e671026335
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
22
Packages/Common~/Scripts/Console/BITKit.Console.asmdef
Normal file
22
Packages/Common~/Scripts/Console/BITKit.Console.asmdef
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "BITKit.Console",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:be17a8778dbfe454890ed8279279e153",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e208a1abcb6254f498b8abfb2ea03f2e
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user