Files
ichni_Creator_Studio/Assets/Scripts/Console/EditorConsole.cs
2025-05-04 16:41:23 +08:00

252 lines
9.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DynamicExpresso;
using UnityEngine.UI;
using Dreamteck;
using System.Text.RegularExpressions;
using DynamicExpresso.Exceptions;
using UnityEngine.UIElements;
using System.Linq;
using Unity.VisualScripting;
using Ichni.RhythmGame;
using Sirenix.Utilities;
using UnityEngine.InputSystem;
using TMPro;
using MoreMountains.Tools;
using System.Reflection;
using System.Linq.Expressions;
//又在写大粪 ——神币
namespace Ichni.Editor
{
public partial class EditorConsole : MonoBehaviour
{
public Canvas[] scaleParts;
public Interpreter functionInterpreter;
public TMP_InputField InputCommand;
private Dictionary<int, string> historyCommand = new Dictionary<int, string>();
private int historycount = 0;
public GameObject ConsoleUI;
bool isHide = true;
public void GetChange(string change)
{
}
public void GetCommand(string Command)//当提交命令时
{
try
{
functionInterpreter.Eval(Command);
}
catch (Exception e)
{
Debug.LogWarning("WTF Command! " + e);
LogWindow.Log("Unknow Command!", Color.red);
}
}
private void Update()
{
UIscale();
if (InputCommand.isFocused) InputDect();
}
private void UIscale()
{
if (Keyboard.current.backquoteKey.wasPressedThisFrame)
{
ConsoleUI.SetActive(isHide);
isHide = !isHide;
if (!isHide) StartCoroutine(WindowAnim.ShowPanelOnScale(InputCommand.gameObject));
}
if (Keyboard.current.leftCtrlKey.isPressed && Keyboard.current.upArrowKey.wasPressedThisFrame)
{
foreach (Canvas i in scaleParts)
{
var canvasScaler = i.GetComponent<CanvasScaler>();
canvasScaler.referenceResolution = new Vector2(canvasScaler.referenceResolution.x + 100, canvasScaler.referenceResolution.y);
}
}
else
if (Keyboard.current.leftCtrlKey.isPressed && Keyboard.current.downArrowKey.wasPressedThisFrame)
{
foreach (Canvas i in scaleParts)
{
var canvasScaler = i.GetComponent<CanvasScaler>();
canvasScaler.referenceResolution = new Vector2(canvasScaler.referenceResolution.x - 100, canvasScaler.referenceResolution.y);
}
}
}
//这是史,不要看
private void InputDect()
{
if (Keyboard.current.downArrowKey.wasPressedThisFrame)
{
if (historyCommand.Count - 1 > historycount)
{
historycount++;
InputCommand.text = historyCommand[historycount];
}
else
{
InputCommand.text = "";
historycount = historyCommand.Count;
}
}
if (Keyboard.current.upArrowKey.wasPressedThisFrame && historycount != 0)
{
historycount--;
InputCommand.text = historyCommand[historycount];
}
if (Keyboard.current.enterKey.wasPressedThisFrame)
{
string[] strings = InputCommand.text.Split(' ');
string ExpoCommand = "";
foreach (string i in strings)
{
if (!i.IsNullOrWhitespace())
{
if (ExpoCommand.IsNullOrWhitespace()) ExpoCommand = i + "(";
else ExpoCommand += i + ",";
}
}
ExpoCommand = ExpoCommand.RemoveExtraSpaces().Substring(0, ExpoCommand.Length - 1);
if (!ExpoCommand.Contains('(')) ExpoCommand += "(";
ExpoCommand += ")";
print(ExpoCommand);
GetCommand(ExpoCommand);
if (historyCommand.ContainsKey(historycount)) historyCommand[historycount] = InputCommand.text;
else historyCommand.Add(historycount, InputCommand.text);
historycount++;
InputCommand.text = "";
}
}
private void Start()
{
SetUpFunctions();
//Test
// functionInterpreter.Eval("print(\"Hello World!\")");
// functionInterpreter.Eval("log(\"Hello World but debug!\")");
}
}
public partial class EditorConsole
{
public Inspector inspector => EditorManager.instance.uiManager.inspector;
public Hierarchy hierarchy => EditorManager.instance.uiManager.hierarchy;
public LogWindow logWindow => EditorManager.instance.uiManager.mainPage.logWindow;
public List<string> commandList = new List<string>();
public void SetUpFunctions()
{
functionInterpreter = new Interpreter();//这是AI给的东西
foreach (MethodInfo i in typeof(EditorConsoleMethods).GetMethods().
ToList().Where(i => i.IsStatic && i.IsPublic && i.ReturnType == typeof(void)))
{
var parameters = i.GetParameters().Select(p => p.ParameterType).ToArray();
var delegateType = Expression.GetDelegateType(parameters.Concat(new[] { i.ReturnType }).ToArray());
functionInterpreter.SetFunction(i.Name, i.CreateDelegate(delegateType));
commandList.Add(i.Name);
}
// functionInterpreter.SetFunction("test", (Action)Test);
// functionInterpreter.SetFunction("kill", (Action)Kill);
// functionInterpreter.SetFunction("lgp", (Action<int, float, float, float, float, float, float>)LGenPathNodes);
// functionInterpreter.SetFunction("print", (Action<object>)print);
// functionInterpreter.SetFunction("log", (Action<object>)Debug.Log);
// functionInterpreter.SetFunction("tp", (Action<float, float, float>)Tp);
// functionInterpreter.SetFunction("tp", (Action)Tp);
}
}
public static class EditorConsoleMethods
{
public static Inspector inspector => EditorManager.instance.uiManager.inspector;
public static Hierarchy hierarchy => EditorManager.instance.uiManager.hierarchy;
public static LogWindow logWindow => EditorManager.instance.uiManager.mainPage.logWindow;
public static void kill()
{
if (inspector.connectedGameElement == null)
{
LogWindow.Log("Please select a GameElement first!");
return;
}
for (int i = inspector.connectedGameElement.childElementList.Count - 1; i <= 0; i--)
{
EditorManager.instance.operationManager.CopyPasteDeleteModule.DeleteElement(
inspector.connectedGameElement.childElementList[i]
);
inspector.connectedGameElement.childElementList.RemoveAt(i);
}
}
public static void test()
{
var f0 = ElementFolder.GenerateElement("Folder", Guid.NewGuid(), new List<string>(), true, null);
}
public static void tp(float x, float y, float z)
{
if (EditorManager.instance.cameraManager.isSceneCameraActive)
{
EditorManager.instance.cameraManager.sceneCamera.sceneCamera.transform.position =
new Vector3(x, y, z);
}
}
public static void tp()
{
if (EditorManager.instance.cameraManager.isSceneCameraActive)
{
EditorManager.instance.cameraManager.sceneCamera.sceneCamera.transform.position =
inspector.connectedGameElement.transform.position;
}
}
public static void lgp(int loop, float xs, float xe, float ys, float ye, float zs, float ze)
{
if (inspector.connectedGameElement == null || inspector.connectedGameElement.GetType() != typeof(Track))
{
LogWindow.Log("Please select a Track first!");
return;
}
Track track = (Track)inspector.connectedGameElement;
for (int i = 0; i < loop; i++)
{
float x = xs + (xe - xs) / loop * i;
float y = ys + (ye - ys) / loop * i;
float z = zs + (ze - zs) / loop * i;
PathNode j = PathNode.GenerateElement("PathNode" + i.ToString(), Guid.NewGuid(), new List<string>(), true, track, true);
j.transformSubmodule.originalPosition = new Vector3(x, y, z);
}
}
}
}