iFactory.Cutting.Unity/Assets/Artists/Scripts/UX/UXCuttingTool.cs

94 lines
2.1 KiB
C#
Raw Normal View History

2024-01-23 02:56:26 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.CommandPattern;
2024-03-04 18:45:21 +08:00
using BITKit.Mod;
2024-01-23 02:56:26 +08:00
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITFactory.Cutting
{
public class UXCuttingTool : MonoBehaviour
{
[SerializeField] private UIDocument document;
[SerializeReference,SubclassSelector] private ICuttingTool cuttingTool;
[UXBindPath("commands-container")]
private VisualElement _commandsContainer;
[UXBindPath("export-button")]
private Button exportButton;
[UXBindPath("clear-button")]
private Button clearButton;
[UXBindPath("undo-button")]
private Button undoButton;
private void Start()
{
UXUtils.Inject(this);
_commandsContainer.Clear();
cuttingTool.OnExecute += OnExecute;
cuttingTool.OnUndo += OnUndo;
cuttingTool.OnClear += OnClear;
2024-03-04 18:45:21 +08:00
cuttingTool.OnRelease += OnRelease;
2024-01-23 02:56:26 +08:00
2024-03-04 18:45:21 +08:00
exportButton.clicked += cuttingTool.Release;
2024-01-23 02:56:26 +08:00
clearButton.clicked += cuttingTool.Clear;
undoButton.clicked += cuttingTool.Undo;
}
2024-03-04 18:45:21 +08:00
private static void OnRelease(ICommand[] commands)
2024-01-23 02:56:26 +08:00
{
var stringBuilder = new System.Text.StringBuilder();
2024-03-04 18:45:21 +08:00
foreach (var x in commands.OfType<ICuttingCommand>())
2024-01-23 02:56:26 +08:00
{
stringBuilder.AppendLine(x.Name);
}
var value = stringBuilder.ToString();
Alert.Print(new BITKit.UX.Internal.Alert()
{
2024-03-04 18:45:21 +08:00
message =ModService.Mods.Length>0 ? "已安装Mod,其他程序正在处理数据" :value.Length>1024 ?"数据过长,请复制到剪贴板查看": value,
2024-01-23 02:56:26 +08:00
OnConfirm = Copy,
title = "已命令,按[确定]复制到剪切板"
});
return;
void Copy()
{
GUIUtility.systemCopyBuffer = stringBuilder.ToString();
}
}
private void OnClear(ICommand obj)
{
_commandsContainer.Clear();
}
private void OnUndo(ICommand obj)
{
throw new NotImplementedException();
}
private void OnExecute(ICommand obj)
{
if (obj is not ICuttingCommand command) return;
var button = _commandsContainer.Create<Button>();
button.text = command.Name;
button.clicked += Undo;
return;
void Undo()
{
cuttingTool.Trace(obj);
}
}
}
}