VirtualFramework/Assets/Scripts/UI/UIBody3DMenuTree.cs

407 lines
15 KiB
C#
Raw Normal View History

2025-02-17 17:03:48 +08:00
using UnityEngine;
using UnityEngine.UI;
using XMLTool;
using System.Collections.Generic;
2025-02-17 17:32:59 +08:00
using TMPro;
2025-02-19 09:48:26 +08:00
using UnityEditor.Hardware;
2025-02-19 14:01:21 +08:00
using DG.Tweening;
2025-02-17 17:03:48 +08:00
namespace QFramework.Example
{
public class UIBody3DMenuTreeData : UIPanelData
{
public Body3D.Body body;
}
public partial class UIBody3DMenuTree : UIPanel
{
2025-02-18 17:26:16 +08:00
class ObjBtnData
{
public Button btn;
public Body3D.Body body;
public ObjToggleStatus status;
public Dictionary<Body3D.Body, ObjBtnData> objBtnDataMap = new Dictionary<Body3D.Body, ObjBtnData>();
public ObjBtnData(Dictionary<Body3D.Body, ObjBtnData> objBtnDataMap, Button btn, Body3D.Body body)
{
this.objBtnDataMap = objBtnDataMap;
this.btn = btn;
this.body = body;
btn.onClick.AddListener(() =>
{
switch (status)
{
case ObjToggleStatus.None:
case ObjToggleStatus.Half:
SetStatus(ObjToggleStatus.Full);
break;
case ObjToggleStatus.Full:
SetStatus(ObjToggleStatus.None);
break;
}
});
}
public void SetStatus(ObjToggleStatus status)
{
this.status = status;
RefreshStatus();
}
public void RefreshStatus()
{
switch (status)
{
case ObjToggleStatus.None:
2025-02-19 09:48:26 +08:00
CheckActive();
2025-02-18 17:26:16 +08:00
foreach (var sub in body.subBody)
{
if (objBtnDataMap.ContainsKey(sub.Value))
{
objBtnDataMap[sub.Value].SetStatus(status);
}
}
2025-02-19 09:48:26 +08:00
SetUpStatus(body, true);
2025-02-18 17:26:16 +08:00
break;
case ObjToggleStatus.Half:
2025-02-19 09:48:26 +08:00
CheckActive();
SetUpStatus(body, false);
2025-02-18 17:26:16 +08:00
break;
case ObjToggleStatus.Full:
2025-02-19 09:48:26 +08:00
CheckActive();
2025-02-18 17:26:16 +08:00
foreach (var sub in body.subBody)
{
if (objBtnDataMap.ContainsKey(sub.Value))
{
objBtnDataMap[sub.Value].SetStatus(status);
}
}
2025-02-19 09:48:26 +08:00
SetUpStatus(body, true);
2025-02-18 17:26:16 +08:00
break;
}
2025-02-19 09:48:26 +08:00
RefreshUI();
}
2025-02-18 17:26:16 +08:00
2025-02-19 09:48:26 +08:00
public void CheckActive()
{
switch (status)
{
case ObjToggleStatus.None:
Utility.FindObj(body.Path).SetActive(false);
break;
case ObjToggleStatus.Half:
case ObjToggleStatus.Full:
Utility.FindObj(body.Path).SetActive(true);
break;
}
}
2025-02-18 17:26:16 +08:00
2025-02-19 09:48:26 +08:00
public void RefreshParent(ObjToggleStatus status)
{
this.status = status;
CheckActive();
2025-02-18 17:26:16 +08:00
RefreshUI();
}
2025-02-19 09:48:26 +08:00
public void SetUpStatus(Body3D.Body body, bool needCheck)
2025-02-18 17:26:16 +08:00
{
2025-02-19 09:48:26 +08:00
var parent = body.parent;
if (parent != null && objBtnDataMap.ContainsKey(parent))
{
if (needCheck)
{
bool allOn = true;
bool allNone = true;
// 遍历 body 的子元素 subBody
foreach (var sub in parent.subBody)
{
// 检查 objBtnDataMap 中是否包含当前子元素的值
if (objBtnDataMap.ContainsKey(sub.Value))
{
// 获取当前子元素对应的按钮数据的状态
var status = objBtnDataMap[sub.Value].status;
// 如果当前子元素的状态不是 Full说明不是所有子元素都是 Full 状态
if (status != ObjToggleStatus.Full)
{
allOn = false;
}
// 如果当前子元素的状态不是 None说明不是所有子元素都是 None 状态
if (status != ObjToggleStatus.None)
{
allNone = false;
}
}
else
{
// 如果 objBtnDataMap 中不包含当前子元素的值,说明不是所有状态都符合要求
allOn = false;
allNone = false;
break;
}
}
// 如果所有子元素的状态都是 Full
if (allOn)
{
// 将当前 body 的状态设置为 Full
objBtnDataMap[parent].RefreshParent(ObjToggleStatus.Full);
}
// 如果所有子元素的状态都是 None
else if (allNone)
{
// 将当前 body 的状态设置为 None
objBtnDataMap[parent].RefreshParent(ObjToggleStatus.None);
}
else
{
objBtnDataMap[parent].RefreshParent(ObjToggleStatus.Half);
}
SetUpStatus(parent, true);
}
else
{
if (objBtnDataMap.ContainsKey(parent))
{
objBtnDataMap[parent].SetStatus(ObjToggleStatus.Half);
SetUpStatus(parent, false);
}
}
}
2025-02-18 17:26:16 +08:00
}
public void RefreshUI()
{
switch (status)
{
case ObjToggleStatus.None:
btn.transform.Find("Full").gameObject.SetActive(false);
btn.transform.Find("Half").gameObject.SetActive(false);
break;
case ObjToggleStatus.Half:
btn.transform.Find("Full").gameObject.SetActive(false);
btn.transform.Find("Half").gameObject.SetActive(true);
break;
case ObjToggleStatus.Full:
btn.transform.Find("Full").gameObject.SetActive(true);
btn.transform.Find("Half").gameObject.SetActive(false);
break;
}
}
}
2025-02-17 17:32:59 +08:00
2025-02-18 17:31:01 +08:00
2025-02-18 13:30:35 +08:00
Dictionary<GameObject, GameObject> searchItemMap = new Dictionary<GameObject, GameObject>();
2025-02-18 17:26:16 +08:00
Dictionary<Body3D.Body, ObjBtnData> objBtnDataMap = new Dictionary<Body3D.Body, ObjBtnData>();
enum ObjToggleStatus
{
None,
Half,
Full
}
2025-02-19 14:01:21 +08:00
DOTweenAnimation contentAnim;
2025-02-17 17:03:48 +08:00
protected override void OnInit(IUIData uiData = null)
{
2025-02-19 14:01:21 +08:00
contentAnim = RootContent.GetComponent<DOTweenAnimation>();
2025-02-17 17:03:48 +08:00
// please add init code here
Close.onClick.AddListener(() =>
{
Hide();
2025-02-19 14:01:21 +08:00
StringEventSystem.Global.Send("UIBody3DMenuTreeClose");
2025-02-17 17:03:48 +08:00
});
2025-02-17 17:32:59 +08:00
Input.onSelect.AddListener(str =>
{
SearchContent.gameObject.SetActive(true);
Content.gameObject.SetActive(false);
});
Input.onEndEdit.AddListener(str =>
{
if (string.IsNullOrEmpty(str))
{
SearchContent.gameObject.SetActive(false);
Content.gameObject.SetActive(true);
SearchContent.RemoveAllChildren();
}
else
{
SearchContent.gameObject.SetActive(true);
Content.gameObject.SetActive(false);
RefreshSearchContent(str);
}
});
2025-02-17 17:03:48 +08:00
}
2025-02-17 17:32:59 +08:00
2025-02-17 17:03:48 +08:00
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UIBody3DMenuTreeData ?? new UIBody3DMenuTreeData();
2025-02-19 14:01:21 +08:00
contentAnim.DORestart();
2025-02-17 17:03:48 +08:00
Content.RemoveAllChildren();
2025-02-19 13:07:05 +08:00
objBtnDataMap.Clear();
2025-02-17 17:03:48 +08:00
BuildTreeUI(mData.body);
}
private void BuildTreeUI(Body3D.Body data, int depth = 0, Transform parent = null)
{
foreach (var bodyPair in data.subBody)
{
Body3D.Body body = bodyPair.Value;
Transform targetParent = parent != null ? parent : Content;
GameObject toggleObj = GameObject.Instantiate(Item.gameObject, targetParent);
toggleObj.name = body.Name;
Toggle uiToggle = toggleObj.transform.Find("ToggleContent/UI").GetComponent<Toggle>();
2025-02-18 17:26:16 +08:00
Button objBtn = toggleObj.transform.Find("ToggleContent/Obj").GetComponent<Button>();
2025-02-17 17:32:59 +08:00
TextMeshProUGUI label = toggleObj.transform.Find("ToggleContent/Label").GetComponentInChildren<TextMeshProUGUI>();
2025-02-18 17:26:16 +08:00
label.text = body.Name;
2025-02-17 17:03:48 +08:00
toggleObj.transform.Find("ToggleContent").GetComponent<HorizontalLayoutGroup>().padding = new RectOffset(depth * 15, 5, 2, 2);
if (depth != 0)
{
toggleObj.SetActive(false);
}
Transform subContent = toggleObj.transform.Find("SubContent");
uiToggle.onValueChanged.AddListener((isOn) =>
{
for (int i = 0; i < subContent.childCount; i++)
{
subContent.GetChild(i).gameObject.SetActive(isOn);
}
});
2025-02-18 17:26:16 +08:00
objBtnDataMap.Add(body, new ObjBtnData(objBtnDataMap, objBtn, body));
2025-02-17 17:03:48 +08:00
if (body.subBody.Count > 0)
{
uiToggle.gameObject.SetActive(true);
BuildTreeUI(body, depth + 1, subContent);
}
else
{
uiToggle.gameObject.SetActive(false);
}
}
}
2025-02-18 17:26:16 +08:00
private void SetSubObjActive(Body3D.Body body, bool isOn, Transform subContent)
2025-02-17 17:03:48 +08:00
{
2025-02-18 17:26:16 +08:00
// 先设置当前对象
2025-02-17 17:03:48 +08:00
GameObject targetObj = Utility.FindObj(body.Path);
if (targetObj != null)
{
targetObj.SetActive(isOn);
}
2025-02-18 17:26:16 +08:00
// 再递归设置子对象
2025-02-17 17:03:48 +08:00
int index = 0;
foreach (var subBodyPair in body.subBody)
{
Body3D.Body subBody = subBodyPair.Value;
if (subContent.childCount > index)
{
Transform childToggleObj = subContent.GetChild(index);
Toggle childObjToggle = childToggleObj.transform.Find("ToggleContent/Obj").GetComponent<Toggle>();
2025-02-18 17:26:16 +08:00
childObjToggle.isOn = isOn;
2025-02-17 17:03:48 +08:00
Transform childSubContent = childToggleObj.transform.Find("SubContent");
2025-02-18 17:26:16 +08:00
SetSubObjActive(subBody, isOn, childSubContent);
2025-02-17 17:03:48 +08:00
}
index++;
}
}
2025-02-17 17:32:59 +08:00
2025-02-18 17:26:16 +08:00
2025-02-17 17:32:59 +08:00
private void RefreshSearchContent(string str)
{
2025-02-18 13:30:35 +08:00
searchItemMap.Clear();
2025-02-17 17:32:59 +08:00
SearchContent.RemoveAllChildren();
2025-02-18 10:49:11 +08:00
CheckChildren(Content, str);
2025-02-17 17:32:59 +08:00
}
private void CheckChildren(Transform parent, string str)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
2025-02-18 10:49:11 +08:00
Transform labelTransform = child.Find("ToggleContent/Label");
if (labelTransform != null)
2025-02-17 17:32:59 +08:00
{
2025-02-18 10:49:11 +08:00
TextMeshProUGUI textComponent = labelTransform.GetComponent<TextMeshProUGUI>();
if (textComponent != null)
{
string name = textComponent.text;
if (name.Contains(str))
{
GameObject clone = GameObject.Instantiate(SearchItem.gameObject, SearchContent);
2025-02-18 13:30:35 +08:00
searchItemMap.Add(clone, child.gameObject);
2025-02-18 10:49:11 +08:00
Transform subContent = child.Find("SubContent");
if (subContent.childCount > 0)
{
2025-02-18 13:30:35 +08:00
Button btn = clone.transform.Find("Button").GetComponent<Button>();
btn.onClick.AddListener(() =>
{
child.Find("ToggleContent/Obj").GetComponent<Toggle>().isOn = true;
});
Transform buttonLabel = btn.transform.Find("Label");
2025-02-18 10:49:11 +08:00
if (buttonLabel != null)
{
TextMeshProUGUI buttonText = buttonLabel.GetComponent<TextMeshProUGUI>();
if (buttonText != null)
{
2025-02-18 17:31:01 +08:00
buttonText.text = "组" + name + ">";
2025-02-18 10:49:11 +08:00
}
}
CheckChildren(subContent, str);
}
else
{
Transform buttonLabel = clone.transform.Find("Button/Label");
if (buttonLabel != null)
{
TextMeshProUGUI buttonText = buttonLabel.GetComponent<TextMeshProUGUI>();
if (buttonText != null)
{
2025-02-18 13:30:35 +08:00
buttonText.text = name + ">";
2025-02-18 10:49:11 +08:00
}
}
}
}
}
2025-02-17 17:32:59 +08:00
}
}
}
2025-02-17 17:03:48 +08:00
protected override void OnShow()
{
}
2025-02-19 14:01:21 +08:00
public override void Hide()
{
if (contentAnim.onComplete == null)
{
contentAnim.onComplete = new UnityEngine.Events.UnityEvent();
}
contentAnim.onComplete.RemoveAllListeners();
contentAnim.DOPlayBackwards();
contentAnim.onComplete.AddListener(() =>
{
base.Hide();
});
}
2025-02-17 17:03:48 +08:00
protected override void OnHide()
{
2025-02-19 14:01:21 +08:00
2025-02-17 17:03:48 +08:00
}
protected override void OnClose()
{
}
}
}