-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTreeIMGUI.cs
104 lines (76 loc) · 2.33 KB
/
TreeIMGUI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
/**
* TreeNode.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
namespace TreeView {
public class TreeIMGUI<T> where T : ITreeIMGUIData
{
private readonly TreeNode<T> _root;
private Rect _controlRect;
private float _drawY;
private float _height;
private TreeNode<T> _selected;
private int _controlID;
public TreeIMGUI(TreeNode<T> root)
{
_root = root;
}
public void DrawTreeLayout()
{
_height = 0;
_drawY = 0;
_root.Traverse(OnGetLayoutHeight);
_controlRect = EditorGUILayout.GetControlRect(false,_height);
_controlID = GUIUtility.GetControlID(FocusType.Passive,_controlRect);
_root.Traverse(OnDrawRow);
}
protected virtual float GetRowHeight(TreeNode<T> node)
{
return EditorGUIUtility.singleLineHeight;
}
protected virtual bool OnGetLayoutHeight(TreeNode<T> node)
{
if(node.Data==null) return true;
_height += GetRowHeight(node);
return node.Data.isExpanded;
}
protected virtual bool OnDrawRow(TreeNode<T> node)
{
if(node.Data==null) return true;
float rowIndent = 14*node.Level;
float rowHeight = GetRowHeight(node);
Rect rowRect = new Rect(0,_controlRect.y+_drawY,_controlRect.width,rowHeight);
Rect indentRect = new Rect(rowIndent,_controlRect.y+_drawY,_controlRect.width-rowIndent,rowHeight);
// render
if(_selected==node){
EditorGUI.DrawRect(rowRect,Color.gray);
}
OnDrawTreeNode(indentRect,node,_selected==node,false);
// test for events
EventType eventType = Event.current.GetTypeForControl(_controlID);
if(eventType==EventType.MouseUp && rowRect.Contains(Event.current.mousePosition)){
_selected = node;
GUI.changed = true;
Event.current.Use();
}
_drawY += rowHeight;
return node.Data.isExpanded;
}
protected virtual void OnDrawTreeNode(Rect rect, TreeNode<T> node, bool selected, bool focus)
{
GUIContent labelContent = new GUIContent(node.Data.ToString());
if(!node.IsLeaf){
node.Data.isExpanded = EditorGUI.Foldout(new Rect(rect.x-12,rect.y,12,rect.height),node.Data.isExpanded,GUIContent.none);
}
EditorGUI.LabelField(rect,labelContent,selected ? EditorStyles.whiteLabel : EditorStyles.label);
}
}
public interface ITreeIMGUIData
{
bool isExpanded { get; set; }
}
}