using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections;
namespace UIWidgets {
public enum NodeToggle {
Rotate = 0,
ChangeSprite = 1,
}
///
/// Node toggle event.
///
[Serializable]
public class NodeToggleEvent : UnityEvent {
}
///
/// Tree view component base.
///
public class TreeViewComponentBase : ListViewItem {
///
/// The icon.
///
public Image Icon;
///
/// The text.
///
public Text Text;
///
/// The toggle.
///
public TreeNodeToggle Toggle;
Image toggleImage;
protected Image ToggleImage {
get {
if (toggleImage==null)
{
toggleImage = Toggle.GetComponent();
}
return toggleImage;
}
}
///
/// The toggle event.
///
public NodeToggleEvent ToggleEvent = new NodeToggleEvent();
///
/// The filler.
///
public LayoutElement Filler;
public NodeToggle OnNodeExpand = NodeToggle.Rotate;
///
/// Is need animate arrow?
///
public bool AnimateArrow;
public Sprite NodeOpened;
public Sprite NodeClosed;
///
/// The node.
///
public TreeNode Node {
get;
protected set;
}
///
/// The padding per level.
///
public float PaddingPerLevel = 30;
///
/// Set icon native size.
///
public bool SetNativeSize = true;
///
/// Start this instance.
///
protected override void Start()
{
base.Start();
Toggle.OnClick.AddListener(ToggleNode);
}
///
/// This function is called when the MonoBehaviour will be destroyed.
///
protected override void OnDestroy()
{
if (Toggle!=null)
{
Toggle.OnClick.RemoveListener(ToggleNode);
}
base.OnDestroy();
}
///
/// Toggles the node.
///
protected virtual void ToggleNode()
{
if (AnimationCorutine!=null)
{
StopCoroutine(AnimationCorutine);
}
SetToggle(Node.IsExpanded);
ToggleEvent.Invoke(Index);
if (OnNodeExpand==NodeToggle.Rotate)
{
if (AnimateArrow)
{
AnimationCorutine = Node.IsExpanded ? CloseCorutine() : OpenCorutine();
StartCoroutine(AnimationCorutine);
}
}
else
{
SetToggle(Node.IsExpanded);
}
}
protected virtual void SeToggleSprite(bool isExpanded)
{
ToggleImage.sprite = isExpanded ? NodeOpened : NodeClosed;
}
///
/// The animation corutine.
///
protected IEnumerator AnimationCorutine;
///
/// Animate arrow on open.
///
/// The corutine.
protected virtual IEnumerator OpenCorutine()
{
var rect = Toggle.transform as RectTransform;
yield return StartCoroutine(Animations.RotateZ(rect, 0.2f, -90, 0));
}
///
/// Animate arrow on close.
///
/// The corutine.
protected virtual IEnumerator CloseCorutine()
{
var rect = Toggle.transform as RectTransform;
yield return StartCoroutine(Animations.RotateZ(rect, 0.2f, 0, -90));
}
///
/// Sets the toggle rotation.
///
/// If set to true is expanded.
protected virtual void SetToggleRotation(bool isExpanded)
{
if (Toggle==null)
{
return ;
}
Toggle.transform.localRotation = Quaternion.Euler(0, 0, (isExpanded) ? -90 : 0);
}
protected virtual void SetToggle(bool isExpanded)
{
if (OnNodeExpand==NodeToggle.Rotate)
{
SetToggleRotation(isExpanded);
}
else
{
SeToggleSprite(isExpanded);
}
}
///
/// Sets the data.
///
/// Node.
/// Depth.
public virtual void SetData(TreeNode node, int depth)
{
if (node!=null)
{
Node = node;
SetToggle(Node.IsExpanded);
}
if (Filler!=null)
{
Filler.preferredWidth = depth * PaddingPerLevel;
}
if ((Toggle!=null) && (Toggle.gameObject!=null))
{
var toggle_active = (node.Nodes!=null) && (node.Nodes.Count>0);
if (Toggle.gameObject.activeSelf!=toggle_active)
{
Toggle.gameObject.SetActive(toggle_active);
}
}
}
}
}