using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using AX.MessageSystem;
using UnityEngine.EventSystems;
using System.IO;
using System.Collections;
///
/// 树形菜单元素
///
public class TreeViewItem__ : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
///
/// 树形菜单控制器
///
public TreeViewControl__ Controler;
///
/// 当前元素的子元素是否展开(展开时可见)
///
public bool IsExpanding = false;
//当前元素的ID
public int ID;
//当前元素的parentID
public int parentID;
//当前元素的名称
public string Name;
//当前元素在树形图中所属的层级
private int _hierarchy = 0;
//当前元素指向的父元素
private TreeViewItem__ _parent;
//当前元素的所有子元素
private List _children;
public int maxHierarchy = 5;
private Button TreeViewButtonDelete;
private Button TreeViewButtonAdd;
private Button TreeViewButtonEdit;
void Start()
{
Init();
}
private void Init()
{
rectViewPort = transform.parent.parent.parent.GetComponent();
canvas = GameObject.Find("Canvas").GetComponent();
TreeViewButtonDelete = TransformHelper.FindChild(transform, "TreeViewButtonDelete").GetComponent();
TreeViewButtonAdd = TransformHelper.FindChild(transform, "TreeViewButtonAdd").GetComponent();
TreeViewButtonEdit = TransformHelper.FindChild(transform, "TreeViewButtonEdit").GetComponent();
if (_hierarchy == 0)
{
TreeViewButtonDelete.gameObject.SetActive(false);
TreeViewButtonAdd.onClick.AddListener(
GetComponent().TreeViewButtonAddClick);
if (transform.Find("TreeViewText").GetComponent().text.Contains("周边水源") ||
transform.Find("TreeViewText").GetComponent().text.Contains("行车路线"))
{
TreeViewButtonAdd.gameObject.SetActive(false);
}
}
else if (_hierarchy > maxHierarchy - 1)
{
TreeViewButtonAdd.gameObject.SetActive(false);
TreeViewButtonDelete.gameObject.SetActive(true);
TreeViewButtonDelete.onClick.AddListener(
GetComponent().TreeViewButtonDeleteClick);
}
else
{
TreeViewButtonAdd.gameObject.SetActive(true);
TreeViewButtonDelete.gameObject.SetActive(true);
TreeViewButtonAdd.onClick.AddListener(
GetComponent().TreeViewButtonAddClick);
TreeViewButtonDelete.onClick.AddListener(
GetComponent().TreeViewButtonDeleteClick);
}
//上下文按钮点击回调
transform.Find("ContextButton").GetComponent().onClick.AddListener(ContextButtonClick);
transform.Find("TreeViewToggle").GetComponent().onValueChanged.AddListener(ValueChange);
transform.Find("ViewButton").GetComponent().onClick.AddListener(
GetComponent().ChangeViewModeClick);
TreeViewButtonEdit.onClick.AddListener(
GetComponent().TreeViewButtonEditClick);
EventTriggerListener.Get(transform.Find("ViewButton").gameObject).onEnter = (PointerEventData eventData) => { TreeViewControl__.Instance.ShowTip(true); };
EventTriggerListener.Get(transform.Find("ViewButton").gameObject).onExit = (PointerEventData eventData) => { TreeViewControl__.Instance.ShowTip(false); };
if (Application.platform== RuntimePlatform.Android)
{
Set_Android_TreeView();
}
if (ExamInfoHelpClass.loadSceneMode == ExamInfoHelpClass.LoadSceneMode.PrepareModeLook)
{
TreeViewButtonAdd.gameObject.SetActive(false);
TreeViewButtonDelete.gameObject.SetActive(false);
TreeViewButtonEdit.gameObject.SetActive(false);
}
}
///
/// 在安卓端重新布局按钮
///
void Set_Android_TreeView()
{//这个脚本是为了设置二三级按钮的位置以及toggle复选框的大小,这是因为安卓端的toggle复选框太小了
if (!transform.Find("TreeViewToggle").gameObject.activeInHierarchy)
{//toggle按钮没激活,就不用重新布置
return;
}
RectTransform mark_Trans;
mark_Trans = transform.Find("TreeViewToggle/Background/Checkmark").GetComponent();
mark_Trans.anchorMin = new Vector2(0.5f, 0.5f);
mark_Trans.anchorMax = new Vector2(0.5f, 0.5f);
mark_Trans.anchoredPosition = new Vector2(-0.7f,0.7f);
mark_Trans.sizeDelta = new Vector2(20f, 20f);
transform.Find("TreeViewToggle").GetComponent().localPosition = new Vector2(15, 0);
transform.Find("ViewButton").GetComponent().localPosition = new Vector2(34, 0);
Text text;
text= transform.Find("TreeViewText").GetComponent();
text.rectTransform.localPosition = new Vector2(80, 0);
transform.Find("TreeViewText").GetComponent().alignment = TextAnchor.MiddleLeft;
text.transform.gameObject.AddComponent();
text.transform.GetComponent().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
}
private void ValueChange(bool flag)
{
if (flag)
{
TempDataManager.Instance.CurNodeID = ID;
TreeViewControl__.Instance.SetAllToggleFalse(ID);
var filepath = ExamInfoHelpClass.savexmlWorkPath + Name + ".xml";
if (File.Exists(filepath))
{
NodeSet.Instance.JiaZaiBtn.gameObject.SetActive(true);
}
}
else {
NodeSet.Instance.JiaZaiBtn.gameObject.SetActive(false);
TempDataManager.Instance.CurNodeID = -1;
}
MessageDispatcher.SendMessage(flag, "ShowHideLayerObjs", (object)ID);
}
public void ShowChildren()
{
transform.Find("ContextButton").GetComponent().localRotation = Quaternion.Euler(0, 0, 0.0001f);
IsExpanding = true;
ChangeChildren(this, true);
Controler.RefreshTreeView();
}
///
/// 点击上下文菜单按钮,元素的子元素改变显示状态
///
void ContextButtonClick()
{
if (IsExpanding)
{
transform.Find("ContextButton").GetComponent().localRotation = Quaternion.Euler(0, 0, 89.99f);
IsExpanding = false;
ChangeChildren(this, false);
}
else
{
transform.Find("ContextButton").GetComponent().localRotation = Quaternion.Euler(0, 0, 0.0001f);
IsExpanding = true;
ChangeChildren(this, true);
}
//刷新树形菜单
Controler.RefreshTreeView();
}
private bool flag = true;
private void GetParentExpanding(TreeViewItem__ tvi)
{
if (tvi.GetParent())
{
if (tvi.GetParent().IsExpanding)
{
GetParentExpanding(tvi.GetParent());
}
else
{
flag = false;
return;
}
}
else
{
flag = true;
}
}
///
/// 改变某一元素所有子元素的显示状态
///
void ChangeChildren(TreeViewItem__ tvi, bool value)
{
for (int i = 0; i < tvi.GetChildrenNumber(); i++)
{
if (value)
{
if (tvi.IsExpanding )
{
//if (tvi.GetHierarchy() > 0)
//{
// if (tvi.GetParent().IsExpanding)
// {
// tvi.GetChildrenByIndex(i).gameObject.SetActive(value);
// }
//}
//else
//{
// tvi.GetChildrenByIndex(i).gameObject.SetActive(value);
//}
GetParentExpanding(tvi);
if (flag)
{
tvi.GetChildrenByIndex(i).gameObject.SetActive(value);
}
}
}
else
{
tvi.GetChildrenByIndex(i).gameObject.SetActive(value);
}
ChangeChildren(tvi.GetChildrenByIndex(i), value);
}
}
#region 拖拽功能
Canvas canvas;
GameObject objTemp;
RectTransform rectViewPort;
public void OnBeginDrag(PointerEventData eventData)
{
if (parentID == -1) return;
//canvas = GameObject.Find("Canvas").GetComponent();
//rectViewPort = transform.parent.parent.parent.GetComponent();
objTemp = Instantiate(gameObject, transform.parent);
objTemp.GetComponent().SetHierarchy(_hierarchy);//初始要設置層級,否則會使刪除按鈕在Start中被隱藏掉
}
public void OnDrag(PointerEventData eventData)
{
if (parentID == -1) return;
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectViewPort,
Input.mousePosition, canvas.worldCamera, out pos))
{
objTemp.transform.localPosition = pos;
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (parentID == -1) return;
TreeViewControl__.Instance.DragTreeViewItem(objTemp.GetComponent(), GetComponent());
}
#endregion
#region 属性访问
public int GetHierarchy()
{
return _hierarchy;
}
public void SetHierarchy(int hierarchy)
{
_hierarchy = hierarchy;
}
public TreeViewItem__ GetParent()
{
return _parent;
}
public void SetID(TreeViewData data)
{
ID = data.ID;
parentID = data.ParentID;
Name = data.Name;
}
///
/// 拖拽时,为新的TreeViewItem赋值
///
///
public void SetID( TreeViewItem__ oldViewItem)
{
ID = oldViewItem.ID ;
parentID = oldViewItem.parentID;
Name = oldViewItem.Name ;
}
public void SetParent(TreeViewItem__ parent)
{
_parent = parent;
}
public void AddChildren(TreeViewItem__ children)
{
if (_children == null)
{
_children = new List();
}
_children.Add(children);
}
public void InsertChildren(TreeViewItem__ children, int num)
{
if (_children == null)
{
_children = new List();
}
_children.Insert(num, children);
}
public void RemoveChildren(TreeViewItem__ children)
{
if (_children == null)
{
return;
}
_children.Remove(children);
}
public void RemoveChildren(int index)
{
if (_children == null || index < 0 || index >= _children.Count)
{
return;
}
_children.RemoveAt(index);
}
public int GetChildrenNumber()
{
if (_children == null)
{
return 0;
}
return _children.Count;
}
public TreeViewItem__ GetChildrenByIndex(int index)
{
if (index >= _children.Count)
{
return null;
}
return _children[index];
}
public List GetChildrenList()
{
return _children;
}
public void SetChildrenList(List oldChildrenList)
{
_children = oldChildrenList;
}
#endregion
}