You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
380 lines
12 KiB
380 lines
12 KiB
using UnityEngine; |
|
using UnityEngine.UI; |
|
using System.Collections.Generic; |
|
using System; |
|
using AX.MessageSystem; |
|
using UnityEngine.EventSystems; |
|
using System.IO; |
|
using System.Collections; |
|
/// <summary> |
|
/// 树形菜单元素 |
|
/// </summary> |
|
public class TreeViewItem__ : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler |
|
{ |
|
/// <summary> |
|
/// 树形菜单控制器 |
|
/// </summary> |
|
public TreeViewControl__ Controler; |
|
/// <summary> |
|
/// 当前元素的子元素是否展开(展开时可见) |
|
/// </summary> |
|
public bool IsExpanding = false; |
|
//当前元素的ID |
|
public int ID; |
|
//当前元素的parentID |
|
public int parentID; |
|
//当前元素的名称 |
|
public string Name; |
|
//当前元素在树形图中所属的层级 |
|
private int _hierarchy = 0; |
|
//当前元素指向的父元素 |
|
private TreeViewItem__ _parent; |
|
//当前元素的所有子元素 |
|
private List<TreeViewItem__> _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<RectTransform>(); |
|
canvas = GameObject.Find("Canvas").GetComponent<Canvas>(); |
|
|
|
TreeViewButtonDelete = TransformHelper.FindChild(transform, "TreeViewButtonDelete").GetComponent<Button>(); |
|
TreeViewButtonAdd = TransformHelper.FindChild(transform, "TreeViewButtonAdd").GetComponent<Button>(); |
|
TreeViewButtonEdit = TransformHelper.FindChild(transform, "TreeViewButtonEdit").GetComponent<Button>(); |
|
|
|
if (_hierarchy == 0) |
|
{ |
|
TreeViewButtonDelete.gameObject.SetActive(false); |
|
TreeViewButtonAdd.onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().TreeViewButtonAddClick); |
|
if (transform.Find("TreeViewText").GetComponent<Text>().text.Contains("周边水源") || |
|
transform.Find("TreeViewText").GetComponent<Text>().text.Contains("行车路线")) |
|
{ |
|
TreeViewButtonAdd.gameObject.SetActive(false); |
|
} |
|
} |
|
else if (_hierarchy > maxHierarchy - 1) |
|
{ |
|
TreeViewButtonAdd.gameObject.SetActive(false); |
|
TreeViewButtonDelete.gameObject.SetActive(true); |
|
TreeViewButtonDelete.onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().TreeViewButtonDeleteClick); |
|
} |
|
else |
|
{ |
|
TreeViewButtonAdd.gameObject.SetActive(true); |
|
TreeViewButtonDelete.gameObject.SetActive(true); |
|
TreeViewButtonAdd.onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().TreeViewButtonAddClick); |
|
TreeViewButtonDelete.onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().TreeViewButtonDeleteClick); |
|
} |
|
//上下文按钮点击回调 |
|
transform.Find("ContextButton").GetComponent<Button>().onClick.AddListener(ContextButtonClick); |
|
transform.Find("TreeViewToggle").GetComponent<Toggle>().onValueChanged.AddListener(ValueChange); |
|
|
|
transform.Find("ViewButton").GetComponent<Button>().onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().ChangeViewModeClick); |
|
|
|
TreeViewButtonEdit.onClick.AddListener( |
|
GetComponent<TreeViewItemButtonClick>().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); |
|
} |
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
/// 在安卓端重新布局按钮 |
|
/// </summary> |
|
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<RectTransform>(); |
|
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<RectTransform>().localPosition = new Vector2(15, 0); |
|
transform.Find("ViewButton").GetComponent<RectTransform>().localPosition = new Vector2(34, 0); |
|
Text text; |
|
text= transform.Find("TreeViewText").GetComponent<Text>(); |
|
text.rectTransform.localPosition = new Vector2(80, 0); |
|
transform.Find("TreeViewText").GetComponent<Text>().alignment = TextAnchor.MiddleLeft; |
|
|
|
text.transform.gameObject.AddComponent<ContentSizeFitter>(); |
|
text.transform.GetComponent<ContentSizeFitter>().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<RectTransform>().localRotation = Quaternion.Euler(0, 0, 0.0001f); |
|
IsExpanding = true; |
|
ChangeChildren(this, true); |
|
Controler.RefreshTreeView(); |
|
} |
|
|
|
/// <summary> |
|
/// 点击上下文菜单按钮,元素的子元素改变显示状态 |
|
/// </summary> |
|
void ContextButtonClick() |
|
{ |
|
if (IsExpanding) |
|
{ |
|
transform.Find("ContextButton").GetComponent<RectTransform>().localRotation = Quaternion.Euler(0, 0, 89.99f); |
|
IsExpanding = false; |
|
ChangeChildren(this, false); |
|
} |
|
else |
|
{ |
|
transform.Find("ContextButton").GetComponent<RectTransform>().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; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 改变某一元素所有子元素的显示状态 |
|
/// </summary> |
|
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<Canvas>(); |
|
//rectViewPort = transform.parent.parent.parent.GetComponent<RectTransform>(); |
|
objTemp = Instantiate(gameObject, transform.parent); |
|
objTemp.GetComponent<TreeViewItem__>().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<TreeViewItem__>(), GetComponent<TreeViewItem__>()); |
|
} |
|
#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; |
|
} |
|
/// <summary> |
|
/// 拖拽时,为新的TreeViewItem赋值 |
|
/// </summary> |
|
/// <param name="newViewItem"></param> |
|
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<TreeViewItem__>(); |
|
} |
|
_children.Add(children); |
|
} |
|
|
|
public void InsertChildren(TreeViewItem__ children, int num) |
|
{ |
|
if (_children == null) |
|
{ |
|
_children = new List<TreeViewItem__>(); |
|
} |
|
_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<TreeViewItem__> GetChildrenList() |
|
{ |
|
return _children; |
|
} |
|
|
|
public void SetChildrenList(List<TreeViewItem__> oldChildrenList) |
|
{ |
|
_children = oldChildrenList; |
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
}
|
|
|