@@ -11,19 +11,30 @@ namespace GitHub.Unity
11
11
[ Serializable ]
12
12
public class TreeNodeDictionary : SerializableDictionary < string , TreeNode > { }
13
13
14
+ public interface ITree
15
+ {
16
+ void AddNode ( string fullPath , string path , string label , int level , bool isFolder , bool isActive , bool isHidden , bool isCollapsed ) ;
17
+ void Clear ( ) ;
18
+ HashSet < string > GetCollapsedFolders ( ) ;
19
+ bool DisplayRootNode { get ; }
20
+ bool IsCheckable { get ; }
21
+ string PathSeparator { get ; }
22
+ string PathIgnoreRoot { get ; }
23
+ }
24
+
14
25
[ Serializable ]
15
- public abstract class Tree
26
+ public abstract class Tree : ITree
16
27
{
17
28
public static float ItemHeight { get { return EditorGUIUtility . singleLineHeight ; } }
18
29
public static float ItemSpacing { get { return EditorGUIUtility . standardVerticalSpacing ; } }
19
30
20
31
[ SerializeField ] public Rect Margin = new Rect ( ) ;
21
32
[ SerializeField ] public Rect Padding = new Rect ( ) ;
22
33
23
- [ SerializeField ] public string PathIgnoreRoot ;
24
- [ SerializeField ] public string PathSeparator = "/" ;
25
- [ SerializeField ] public bool DisplayRootNode = true ;
26
- [ SerializeField ] public bool Checkable = false ;
34
+ [ SerializeField ] public string pathIgnoreRoot ;
35
+ [ SerializeField ] public string pathSeparator = "/" ;
36
+ [ SerializeField ] public bool displayRootNode = true ;
37
+ [ SerializeField ] public bool isCheckable = false ;
27
38
[ SerializeField ] public GUIStyle FolderStyle ;
28
39
[ SerializeField ] public GUIStyle TreeNodeStyle ;
29
40
[ SerializeField ] public GUIStyle ActiveTreeNodeStyle ;
@@ -52,70 +63,49 @@ private set
52
63
}
53
64
}
54
65
55
- public TreeNode ActiveNode { get { return activeNode ; } }
66
+ public bool DisplayRootNode { get { return displayRootNode ; } }
67
+ public bool IsCheckable { get { return isCheckable ; } }
68
+ public string PathSeparator { get { return pathSeparator ; } }
69
+ public string PathIgnoreRoot { get { return pathIgnoreRoot ; } }
56
70
57
- public void Load ( IEnumerable < ITreeData > data , string title )
71
+ public static void Load ( ITree tree , IEnumerable < ITreeData > data , string title )
58
72
{
59
- var collapsedFoldersEnumerable = folders . Where ( pair => pair . Value . IsCollapsed ) . Select ( pair => pair . Key ) ;
60
- var collapsedFolders = new HashSet < string > ( collapsedFoldersEnumerable ) ;
73
+ var collapsedFolders = tree . GetCollapsedFolders ( ) ;
61
74
62
- folders . Clear ( ) ;
63
- nodes . Clear ( ) ;
75
+ tree . Clear ( ) ;
64
76
65
- var displayRootLevel = DisplayRootNode ? 1 : 0 ;
77
+ var displayRootLevel = tree . DisplayRootNode ? 1 : 0 ;
66
78
67
- var titleNode = new TreeNode ( )
68
- {
69
- Path = title ,
70
- Label = title ,
71
- Level = - 1 + displayRootLevel ,
72
- IsFolder = true ,
73
- Checkable = Checkable
74
- } ;
75
- SetNodeIcon ( titleNode ) ;
76
- nodes . Add ( titleNode ) ;
79
+ tree . AddNode ( fullPath : title , path : title , label : title , level : - 1 + displayRootLevel , isFolder : true , isActive : false , isHidden : false , isCollapsed : false ) ;
77
80
78
81
var hideChildren = false ;
79
82
var hideChildrenBelowLevel = 0 ;
80
83
84
+ var folders = new HashSet < string > ( ) ;
85
+
81
86
foreach ( var d in data )
82
87
{
83
88
var path = d . Path ;
84
- if ( PathIgnoreRoot != null )
89
+ if ( tree . PathIgnoreRoot != null )
85
90
{
86
- var indexOf = path . IndexOf ( PathIgnoreRoot ) ;
91
+ var indexOf = path . IndexOf ( tree . PathIgnoreRoot ) ;
87
92
if ( indexOf != - 1 )
88
93
{
89
- path = path . Substring ( indexOf + PathIgnoreRoot . Length ) ;
94
+ path = path . Substring ( indexOf + tree . PathIgnoreRoot . Length ) ;
90
95
}
91
96
}
92
97
93
- var parts = path . Split ( new [ ] { PathSeparator } , StringSplitOptions . None ) ;
98
+ var parts = path . Split ( new [ ] { tree . PathSeparator } , StringSplitOptions . None ) ;
94
99
for ( int i = 0 ; i < parts . Length ; i ++ )
95
100
{
96
101
var label = parts [ i ] ;
97
102
var level = i + 1 ;
98
- var nodePath = String . Join ( PathSeparator , parts , 0 , level ) ;
103
+ var nodePath = String . Join ( tree . PathSeparator , parts , 0 , level ) ;
99
104
var isFolder = i < parts . Length - 1 ;
100
- var alreadyExists = folders . ContainsKey ( nodePath ) ;
105
+ var alreadyExists = folders . Contains ( nodePath ) ;
101
106
if ( ! alreadyExists )
102
107
{
103
- var node = new TreeNode
104
- {
105
- FullPath = d . FullPath ,
106
- Path = nodePath ,
107
- IsActive = d . IsActive ,
108
- Label = label ,
109
- Level = i + displayRootLevel ,
110
- IsFolder = isFolder ,
111
- Checkable = Checkable
112
- } ;
113
-
114
- if ( node . IsActive )
115
- {
116
- activeNode = node ;
117
- }
118
-
108
+ var nodeIsHidden = false ;
119
109
if ( hideChildren )
120
110
{
121
111
if ( level <= hideChildrenBelowLevel )
@@ -124,18 +114,18 @@ public void Load(IEnumerable<ITreeData> data, string title)
124
114
}
125
115
else
126
116
{
127
- node . IsHidden = true ;
117
+ nodeIsHidden = true ;
128
118
}
129
119
}
130
120
131
- SetNodeIcon ( node ) ;
132
-
133
- nodes . Add ( node ) ;
121
+ var nodeIsCollapsed = false ;
134
122
if ( isFolder )
135
123
{
124
+ folders . Add ( nodePath ) ;
125
+
136
126
if ( collapsedFolders . Contains ( nodePath ) )
137
127
{
138
- node . IsCollapsed = true ;
128
+ nodeIsCollapsed = true ;
139
129
140
130
if ( ! hideChildren )
141
131
{
@@ -144,13 +134,55 @@ public void Load(IEnumerable<ITreeData> data, string title)
144
134
}
145
135
}
146
136
147
- folders . Add ( nodePath , node ) ;
148
137
}
138
+
139
+ tree . AddNode ( fullPath : d . FullPath , path : nodePath , label : label , level : i + displayRootLevel , isFolder : isFolder , isActive : d . IsActive , isHidden : nodeIsHidden , isCollapsed : nodeIsCollapsed ) ;
149
140
}
150
141
}
151
142
}
152
143
}
153
144
145
+ public void AddNode ( string fullPath , string path , string label , int level , bool isFolder , bool isActive , bool isHidden , bool isCollapsed )
146
+ {
147
+ var node = new TreeNode
148
+ {
149
+ FullPath = fullPath ,
150
+ Path = path ,
151
+ Label = label ,
152
+ Level = level ,
153
+ IsFolder = isFolder ,
154
+ IsActive = isActive ,
155
+ IsHidden = isHidden ,
156
+ IsCollapsed = isCollapsed ,
157
+ TreeIsCheckable = IsCheckable
158
+ } ;
159
+
160
+ SetNodeIcon ( node ) ;
161
+ nodes . Add ( node ) ;
162
+
163
+ if ( isActive )
164
+ {
165
+ activeNode = node ;
166
+ }
167
+
168
+ if ( isFolder )
169
+ {
170
+ folders . Add ( node . Path , node ) ;
171
+ }
172
+ }
173
+
174
+ public void Clear ( )
175
+ {
176
+ folders . Clear ( ) ;
177
+ nodes . Clear ( ) ;
178
+ }
179
+
180
+ public HashSet < string > GetCollapsedFolders ( )
181
+ {
182
+ var collapsedFoldersEnumerable = folders . Where ( pair => pair . Value . IsCollapsed ) . Select ( pair => pair . Key ) ;
183
+ return new HashSet < string > ( collapsedFoldersEnumerable ) ;
184
+ }
185
+
154
186
public Rect Render ( Rect rect , Vector2 scroll , Action < TreeNode > singleClick = null , Action < TreeNode > doubleClick = null , Action < TreeNode > rightClick = null )
155
187
{
156
188
RequiresRepaint = false ;
@@ -451,7 +483,7 @@ public class TreeNode
451
483
public bool IsHidden ;
452
484
public bool IsActive ;
453
485
public GUIContent content ;
454
- public bool Checkable ;
486
+ public bool TreeIsCheckable ;
455
487
public CheckState CheckState ;
456
488
457
489
[ NonSerialized ] public Texture2D Icon ;
@@ -469,9 +501,9 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
469
501
return renderResult ;
470
502
471
503
var fillRect = rect ;
472
- var nodeStartX = Level * indentation * ( Checkable ? 2 : 1 ) ;
504
+ var nodeStartX = Level * indentation * ( TreeIsCheckable ? 2 : 1 ) ;
473
505
474
- if ( Checkable && Level > 0 )
506
+ if ( TreeIsCheckable && Level > 0 )
475
507
{
476
508
nodeStartX += 2 * Level ;
477
509
}
@@ -511,7 +543,7 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
511
543
}
512
544
}
513
545
514
- if ( Checkable )
546
+ if ( TreeIsCheckable )
515
547
{
516
548
data += string . Format ( "SelectStart: {0} " , nodeStartX ) ;
517
549
0 commit comments