102 lines
2.2 KiB
C#
102 lines
2.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
public class TreeViewComponentBase<T> : ListViewItem
|
|
{
|
|
public Image Icon;
|
|
|
|
public Text Text;
|
|
|
|
public TreeNodeToggle Toggle;
|
|
|
|
public NodeToggleEvent ToggleEvent = new NodeToggleEvent();
|
|
|
|
public LayoutElement Filler;
|
|
|
|
public bool AnimateArrow;
|
|
|
|
public float PaddingPerLevel = 30f;
|
|
|
|
public bool SetNativeSize = true;
|
|
|
|
protected IEnumerator AnimationCorutine;
|
|
|
|
public TreeNode<T> Node { get; protected set; }
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
Toggle.OnClick.AddListener(ToggleNode);
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
if (Toggle != null)
|
|
{
|
|
Toggle.OnClick.RemoveListener(ToggleNode);
|
|
}
|
|
base.OnDestroy();
|
|
}
|
|
|
|
protected virtual void ToggleNode()
|
|
{
|
|
if (AnimationCorutine != null)
|
|
{
|
|
StopCoroutine(AnimationCorutine);
|
|
}
|
|
SetToggleRotation(Node.IsExpanded);
|
|
ToggleEvent.Invoke(Index);
|
|
if (AnimateArrow)
|
|
{
|
|
AnimationCorutine = ((!Node.IsExpanded) ? OpenCorutine() : CloseCorutine());
|
|
StartCoroutine(AnimationCorutine);
|
|
}
|
|
}
|
|
|
|
protected virtual IEnumerator OpenCorutine()
|
|
{
|
|
RectTransform rect = Toggle.transform as RectTransform;
|
|
yield return StartCoroutine(Animations.RotateZ(rect, 0.2f, -90f, 0f));
|
|
}
|
|
|
|
protected virtual IEnumerator CloseCorutine()
|
|
{
|
|
RectTransform rect = Toggle.transform as RectTransform;
|
|
yield return StartCoroutine(Animations.RotateZ(rect, 0.2f, 0f, -90f));
|
|
}
|
|
|
|
protected virtual void SetToggleRotation(bool isExpanded)
|
|
{
|
|
if (!(Toggle == null))
|
|
{
|
|
RectTransform rectTransform = Toggle.transform as RectTransform;
|
|
rectTransform.rotation = Quaternion.Euler(0f, 0f, isExpanded ? (-90) : 0);
|
|
}
|
|
}
|
|
|
|
public virtual void SetData(TreeNode<T> node, int depth)
|
|
{
|
|
if (node != null)
|
|
{
|
|
Node = node;
|
|
SetToggleRotation(Node.IsExpanded);
|
|
}
|
|
if (Filler != null)
|
|
{
|
|
Filler.preferredWidth = (float)depth * PaddingPerLevel;
|
|
}
|
|
if (Toggle != null && Toggle.gameObject != null)
|
|
{
|
|
bool flag = node.Nodes != null && node.Nodes.Count > 0;
|
|
if (Toggle.gameObject.activeSelf != flag)
|
|
{
|
|
Toggle.gameObject.SetActive(flag);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|