Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/Hierarchy/HierarchyTab.cs

247 lines
8.9 KiB
C#
Raw Normal View History

2025-02-09 23:47:42 -05:00
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
2025-06-14 20:47:45 +08:00
using DG.Tweening;
2025-02-09 23:47:42 -05:00
using Ichni.RhythmGame;
2025-02-19 19:01:21 -05:00
using Michsky.MUIP;
2025-04-13 14:04:17 +08:00
using Sirenix.Utilities;
2025-02-09 23:47:42 -05:00
using TMPro;
using Unity.VisualScripting;
2025-02-09 23:47:42 -05:00
using UnityEngine;
2025-04-02 18:18:25 -04:00
using UnityEngine.InputSystem;
2025-02-19 19:01:21 -05:00
using UnityEngine.Serialization;
2025-02-09 23:47:42 -05:00
using UnityEngine.UI;
namespace Ichni.Editor
{
public partial class HierarchyTab : MonoBehaviour
{
public GameObject indentationLinePrefab;
2025-02-11 22:58:56 -05:00
2025-02-09 23:47:42 -05:00
public GameElement connectedGameElement;
public HierarchyTab parentTab;
public List<HierarchyTab> childTabList;
2025-02-11 22:58:56 -05:00
2025-02-09 23:47:42 -05:00
public int tabLayer;
public bool isSelected;
public bool isExpanded;
public Image BgImage;
2025-02-09 23:47:42 -05:00
public RectTransform tabRect;
public LayoutElement layoutElement;
public RectTransform tabMainRect;
public Button tabButton;
public Button expandButton;
2025-02-19 19:01:21 -05:00
public DoubleCheckButton deleteButton;
2025-02-09 23:47:42 -05:00
public TMP_Text tabButtonText;
public void SetTab(GameElement targetElement, GameElement parentElement)
{
transform.localScale = Vector3.one;
transform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutCirc).From(new Vector3(1, 0, 1));
2025-02-11 22:58:56 -05:00
connectedGameElement = targetElement;
2025-02-09 23:47:42 -05:00
tabButtonText.text = targetElement.elementName;
targetElement.connectedTab = this;
this.isExpanded = false;
this.isSelected = false;
this.childTabList = new List<HierarchyTab>();
if (parentElement == null)
{
this.tabLayer = 0;
this.parentTab = null;
this.transform.SetAsLastSibling();
2025-02-11 22:58:56 -05:00
}
else
{
2025-02-09 23:47:42 -05:00
this.parentTab = parentElement.connectedTab;
parentElement.connectedTab.childTabList.Add(this);
2025-02-09 23:47:42 -05:00
this.tabLayer = this.parentTab.tabLayer + 1;
this.transform.SetSiblingIndex(this.parentTab.transform.GetSiblingIndex() + GetAllChildrenCount(this.parentTab));
2025-02-09 23:47:42 -05:00
if (!this.parentTab.isExpanded)
{
this.isExpanded = false;
SetExpansion(false);
}
for (int i = 1; i <= this.tabLayer; i++)
{
2025-02-19 19:01:21 -05:00
float lineX = 30 * i - 15;
2025-03-16 01:45:49 +08:00
var d = Instantiate(indentationLinePrefab, tabRect);
d.GetComponent<RectTransform>().anchoredPosition = new Vector2(lineX, 0);
2025-02-09 23:47:42 -05:00
}
2025-04-13 14:04:17 +08:00
parentTab.SetStatus();
2025-02-09 23:47:42 -05:00
}
2025-02-11 22:58:56 -05:00
2025-02-09 23:47:42 -05:00
2025-02-19 19:01:21 -05:00
float posX = (30 * tabLayer);
2025-02-09 23:47:42 -05:00
tabMainRect.anchoredPosition = new Vector2(posX, tabMainRect.anchoredPosition.y);
2025-02-11 22:58:56 -05:00
tabButton.onClick.AddListener(SelectGameElement);
2025-02-09 23:47:42 -05:00
expandButton.onClick.AddListener(ExpandOrFold);
2025-02-19 19:06:26 -05:00
deleteButton.onConfirm = () => EditorManager.instance.operationManager.CopyPasteDeleteModule.DeleteElement(connectedGameElement);
2025-04-13 14:04:17 +08:00
SetStatus();
2025-04-13 14:04:17 +08:00
}
private void OnDestroy()
{
transform.DOKill();
}
2025-04-13 14:04:17 +08:00
public void SetStatus()
{
2025-06-30 16:37:34 +08:00
expandButton.gameObject.SetActive(!connectedGameElement.childElementList.IsNullOrEmpty());
2025-06-14 20:47:45 +08:00
2025-02-09 23:47:42 -05:00
}
}
public partial class HierarchyTab
{
private int GetAllChildrenCount(HierarchyTab tab)
{
int c = tab.childTabList.Count;
for (int i = 0; i < tab.childTabList.Count; i++)
{
c += GetAllChildrenCount(tab.childTabList[i]);
}
return c;
}
public void ExecuteOrSelect()
{
if (EditorManager.instance.uiManager.hierarchy.NeedExecute)
{
EditorManager.instance.uiManager.hierarchy.upLoadElement = connectedGameElement;
}
else
{
SelectGameElement();
}
}
2025-02-11 22:58:56 -05:00
public void SelectGameElement()
2025-02-11 22:58:56 -05:00
{
2025-04-02 18:18:25 -04:00
if (Keyboard.current.leftCtrlKey.isPressed)
{
if (!isSelected)
{
EditorManager.instance.operationManager.AddSelectElement(connectedGameElement);
}
else
{
EditorManager.instance.operationManager.RemoveSelectElement(connectedGameElement);
}
}
else
{
EditorManager.instance.operationManager.ClearSelectedElements();
EditorManager.instance.operationManager.AddSelectElement(connectedGameElement);
}
if (EditorManager.instance.useQuickMove)
{
if (connectedGameElement is IHaveTransformSubmodule haveTransformSubmodule)
{
QuickMover quickMover = Instantiate(EditorManager.instance.basePrefabs.QuickMoveObj).GetComponent<QuickMover>();
quickMover.Initialize(haveTransformSubmodule);
}
else if (connectedGameElement is NoteBase noteBase && noteBase.noteVisual != null)
{
QuickMover quickMover = Instantiate(EditorManager.instance.basePrefabs.QuickMoveObj).GetComponent<QuickMover>();
quickMover.Initialize(noteBase.noteVisual);
}
else if (QuickMover.instance != null)
{
Destroy(QuickMover.instance.gameObject);
QuickMover.instance = null;
}
}
2025-04-13 14:04:17 +08:00
2025-02-17 14:46:14 -05:00
EditorManager.instance.uiManager.inspector.SetInspector(connectedGameElement);
2025-02-11 22:58:56 -05:00
}
2025-06-14 20:47:45 +08:00
public void ExpandOrFold()
{
ExpandOrFold(false);
}
public void ExpandOrFold(bool forceAllExPand = false)
2025-02-09 23:47:42 -05:00
{
this.childTabList.RemoveAll(s => s == null);
2025-02-11 22:58:56 -05:00
isExpanded = !isExpanded;
2025-06-14 20:47:45 +08:00
ExpandAnim();
2025-02-11 22:58:56 -05:00
if (isExpanded)
{
connectedGameElement.ScanAndAddEnableTypes();
List<GameElement> FixedList = !forceAllExPand ? connectedGameElement.GetChildrenByTypes() : connectedGameElement.childElementList;
2025-06-22 14:11:13 +08:00
// float startTime = Time.realtimeSinceStartup;
// connectedGameElement.childElementList.Sort();//TODO: 后续可以让玩家手动快速排序
Debug.Log(FixedList.Count);
ExpandAsync(FixedList);
2025-04-13 14:04:17 +08:00
2025-02-11 22:58:56 -05:00
}
else
{
2025-03-09 10:56:58 +08:00
//expandButton.transform.Rotate(new Vector3(0, 0, 180));
2025-02-11 22:58:56 -05:00
for (int i = childTabList.Count - 1; i >= 0; i--)
{
childTabList[i].SetExpansion(isExpanded);
}
2025-02-09 23:47:42 -05:00
}
}
private void SetExpansion(bool expand)
{
2025-02-11 22:58:56 -05:00
if (!expand && isExpanded)
2025-02-09 23:47:42 -05:00
{
for (int i = childTabList.Count; i > 0; i--)
2025-02-09 23:47:42 -05:00
{
childTabList[i - 1].SetExpansion(expand); //false
2025-02-09 23:47:42 -05:00
}
}
if (!expand)
2025-02-09 23:47:42 -05:00
{
parentTab.childTabList.Remove(this);
2025-02-11 22:58:56 -05:00
Destroy(gameObject);
EditorManager.instance.uiManager.hierarchy.tabList.Remove(this);
2025-02-09 23:47:42 -05:00
}
}
2025-06-14 20:47:45 +08:00
private void ExpandAnim()
{
expandButton.transform.DORotate(new Vector3(0, 0, !isExpanded ? 0f : 180f), 0.2f);
}
public bool isExpandDone = true;
private void ExpandImmediately(List<GameElement> FixedList)
2025-03-09 10:56:58 +08:00
{
2025-06-29 21:28:49 +08:00
float StrandTimeWhileStartUp = EditorManager.instance.CurrentFrameRate;
float frameTime = 1f / StrandTimeWhileStartUp * 3f;
for (var index = 0; index < FixedList.Count; index++)
2025-03-09 10:56:58 +08:00
{
var childElement = FixedList[index];
2025-06-14 20:47:45 +08:00
EditorManager.instance.uiManager.hierarchy.GenerateTab(childElement, connectedGameElement);
Debug.Log($"生成子Tab{childElement.elementName},索引:{index},总数:{FixedList.Count}");
// 如果处理时间超过帧时间限制,直接继续执行而不等待
// 移除了原来的 yield return null 等待逻辑
2025-03-09 10:56:58 +08:00
}
}
async void ExpandAsync(List<GameElement> FixedList)
{
isExpandDone = false;
for (var index = 0; index < FixedList.Count; index++)
{
var childElement = FixedList[index];
await EditorManager.instance.uiManager.hierarchy.GenerateTabAsync(childElement, connectedGameElement);
Debug.Log($"生成子Tab{childElement.elementName},索引:{index},总数:{FixedList.Count}");
if (!isExpanded) break;
}
isExpandDone = true;
}
2025-02-09 23:47:42 -05:00
}
}