AlicizaX/Client/Packages/com.alicizax.uxtool/Editor/UXGUI/SceneView/ControllerEdit/UXControllerEditWindow.cs

137 lines
5.1 KiB
C#
Raw Normal View History

2025-12-01 16:46:28 +08:00
using System;
using System.Drawing;
using AlicizaX.UI;
using AlicizaX.UI.Runtime;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using FontStyle = UnityEngine.FontStyle;
namespace AlicizaX.UXTool
2025-12-01 16:46:28 +08:00
{
public class UXControllerEditWindow : EditorWindow
{
private UXController _ux;
private int _index;
private TextField _nameField;
private IntegerField _lengthField;
public static void ShowWindow(UXController ux, int index, string currentName, int currentLength)
{
var w = CreateInstance<UXControllerEditWindow>();
w.titleContent = new GUIContent("编辑控制器");
w._ux = ux;
w._index = index;
w.position = new Rect(Screen.width / 2f - 150f, Screen.height / 2f - 60f, 320, 120);
w.ShowUtility();
}
private void CreateGUI()
{
var root = rootVisualElement;
root.style.paddingLeft = 8;
root.style.paddingTop = 8;
root.style.paddingRight = 8;
root.style.paddingBottom = 8;
var title = new Label($"编辑控制器 [{_index}]");
title.style.unityFontStyleAndWeight = FontStyle.Bold;
title.style.marginBottom = 6;
root.Add(title);
_nameField = new TextField("Name");
if (_ux != null && _index >= 0 && _index < _ux.Controllers.Count) _nameField.value = _ux.Controllers[_index].Name;
root.Add(_nameField);
_lengthField = new IntegerField("Length");
if (_ux != null && _index >= 0 && _index < _ux.Controllers.Count)
{
_lengthField.value = _ux.Controllers[_index].Length;
}
_lengthField.value = Mathf.Max(1, _lengthField.value);
root.Add(_lengthField);
var row = new VisualElement();
row.style.flexDirection = FlexDirection.Row;
row.style.justifyContent = Justify.FlexEnd;
row.style.marginTop = 8;
var save = new Button(() =>
{
if (_ux != null && _index >= 0 && !string.IsNullOrEmpty(_nameField.value))
{
var so = new SerializedObject(_ux);
var controllersProp = so.FindProperty("_controllers");
if (controllersProp != null)
{
Undo.RecordObject(_ux, "Edit Controller");
var el = controllersProp.GetArrayElementAtIndex(_index);
var nameProp = el.FindPropertyRelative("Name");
string oldValue = nameProp.stringValue;
var lengthProp = el.FindPropertyRelative("Length");
nameProp.stringValue = _nameField.value;
lengthProp.intValue = Mathf.Max(1, _lengthField.value);
so.ApplyModifiedProperties();
var recorders = so.FindProperty("_recorders");
for (int i = 0; i < recorders.arraySize; i++)
{
var recorderProp = recorders.GetArrayElementAtIndex(i);
var recorderSo = new SerializedObject(recorderProp.objectReferenceValue);
var stateEntriesProp = recorderSo.FindProperty("_stateEntries");
for (int j = 0; j < stateEntriesProp.arraySize; j++)
{
var entry = stateEntriesProp.GetArrayElementAtIndex(j);
var controllerNameProp = entry.FindPropertyRelative("ControllerName");
if (controllerNameProp.stringValue.Equals(oldValue))
{
controllerNameProp.stringValue = _nameField.value;
}
}
recorderSo.ApplyModifiedProperties();
}
EditorUtility.SetDirty(_ux);
}
}
Close();
});
save.text = "保存";
row.Add(save);
var delete = new Button(() =>
{
if (_ux != null && _index >= 0)
{
var so = new SerializedObject(_ux);
var controllersProp = so.FindProperty("_controllers");
if (controllersProp != null)
{
Undo.RecordObject(_ux, "Edit Controller");
controllersProp.DeleteArrayElementAtIndex(_index);
so.ApplyModifiedProperties();
EditorUtility.SetDirty(_ux);
}
}
Close();
});
delete.text = "删除";
row.Add(delete);
var cancel = new Button(() => Close());
cancel.text = "取消";
cancel.style.marginLeft = 8;
row.Add(cancel);
root.Add(row);
}
}
}