@@ -11,19 +11,30 @@ namespace GitHub.Unity
1111 [ Serializable ]
1212 public class TreeNodeDictionary : SerializableDictionary < string , TreeNode > { }
1313
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+
1425 [ Serializable ]
15- public abstract class Tree
26+ public abstract class Tree : ITree
1627 {
1728 public static float ItemHeight { get { return EditorGUIUtility . singleLineHeight ; } }
1829 public static float ItemSpacing { get { return EditorGUIUtility . standardVerticalSpacing ; } }
1930
2031 [ SerializeField ] public Rect Margin = new Rect ( ) ;
2132 [ SerializeField ] public Rect Padding = new Rect ( ) ;
2233
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 ;
2738 [ SerializeField ] public GUIStyle FolderStyle ;
2839 [ SerializeField ] public GUIStyle TreeNodeStyle ;
2940 [ SerializeField ] public GUIStyle ActiveTreeNodeStyle ;
@@ -52,70 +63,49 @@ private set
5263 }
5364 }
5465
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 ; } }
5670
57- public void Load ( IEnumerable < ITreeData > data , string title )
71+ public static void Load ( ITree tree , IEnumerable < ITreeData > data , string title )
5872 {
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 ( ) ;
6174
62- folders . Clear ( ) ;
63- nodes . Clear ( ) ;
75+ tree . Clear ( ) ;
6476
65- var displayRootLevel = DisplayRootNode ? 1 : 0 ;
77+ var displayRootLevel = tree . DisplayRootNode ? 1 : 0 ;
6678
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 ) ;
7780
7881 var hideChildren = false ;
7982 var hideChildrenBelowLevel = 0 ;
8083
84+ var folders = new HashSet < string > ( ) ;
85+
8186 foreach ( var d in data )
8287 {
8388 var path = d . Path ;
84- if ( PathIgnoreRoot != null )
89+ if ( tree . PathIgnoreRoot != null )
8590 {
86- var indexOf = path . IndexOf ( PathIgnoreRoot ) ;
91+ var indexOf = path . IndexOf ( tree . PathIgnoreRoot ) ;
8792 if ( indexOf != - 1 )
8893 {
89- path = path . Substring ( indexOf + PathIgnoreRoot . Length ) ;
94+ path = path . Substring ( indexOf + tree . PathIgnoreRoot . Length ) ;
9095 }
9196 }
9297
93- var parts = path . Split ( new [ ] { PathSeparator } , StringSplitOptions . None ) ;
98+ var parts = path . Split ( new [ ] { tree . PathSeparator } , StringSplitOptions . None ) ;
9499 for ( int i = 0 ; i < parts . Length ; i ++ )
95100 {
96101 var label = parts [ i ] ;
97102 var level = i + 1 ;
98- var nodePath = String . Join ( PathSeparator , parts , 0 , level ) ;
103+ var nodePath = String . Join ( tree . PathSeparator , parts , 0 , level ) ;
99104 var isFolder = i < parts . Length - 1 ;
100- var alreadyExists = folders . ContainsKey ( nodePath ) ;
105+ var alreadyExists = folders . Contains ( nodePath ) ;
101106 if ( ! alreadyExists )
102107 {
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 ;
119109 if ( hideChildren )
120110 {
121111 if ( level <= hideChildrenBelowLevel )
@@ -124,18 +114,18 @@ public void Load(IEnumerable<ITreeData> data, string title)
124114 }
125115 else
126116 {
127- node . IsHidden = true ;
117+ nodeIsHidden = true ;
128118 }
129119 }
130120
131- SetNodeIcon ( node ) ;
132-
133- nodes . Add ( node ) ;
121+ var nodeIsCollapsed = false ;
134122 if ( isFolder )
135123 {
124+ folders . Add ( nodePath ) ;
125+
136126 if ( collapsedFolders . Contains ( nodePath ) )
137127 {
138- node . IsCollapsed = true ;
128+ nodeIsCollapsed = true ;
139129
140130 if ( ! hideChildren )
141131 {
@@ -144,13 +134,55 @@ public void Load(IEnumerable<ITreeData> data, string title)
144134 }
145135 }
146136
147- folders . Add ( nodePath , node ) ;
148137 }
138+
139+ tree . AddNode ( fullPath : d . FullPath , path : nodePath , label : label , level : i + displayRootLevel , isFolder : isFolder , isActive : d . IsActive , isHidden : nodeIsHidden , isCollapsed : nodeIsCollapsed ) ;
149140 }
150141 }
151142 }
152143 }
153144
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+
154186 public Rect Render ( Rect rect , Vector2 scroll , Action < TreeNode > singleClick = null , Action < TreeNode > doubleClick = null , Action < TreeNode > rightClick = null )
155187 {
156188 RequiresRepaint = false ;
@@ -451,7 +483,7 @@ public class TreeNode
451483 public bool IsHidden ;
452484 public bool IsActive ;
453485 public GUIContent content ;
454- public bool Checkable ;
486+ public bool TreeIsCheckable ;
455487 public CheckState CheckState ;
456488
457489 [ NonSerialized ] public Texture2D Icon ;
@@ -469,9 +501,9 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
469501 return renderResult ;
470502
471503 var fillRect = rect ;
472- var nodeStartX = Level * indentation * ( Checkable ? 2 : 1 ) ;
504+ var nodeStartX = Level * indentation * ( TreeIsCheckable ? 2 : 1 ) ;
473505
474- if ( Checkable && Level > 0 )
506+ if ( TreeIsCheckable && Level > 0 )
475507 {
476508 nodeStartX += 2 * Level ;
477509 }
@@ -511,7 +543,7 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
511543 }
512544 }
513545
514- if ( Checkable )
546+ if ( TreeIsCheckable )
515547 {
516548 data += string . Format ( "SelectStart: {0} " , nodeStartX ) ;
517549
0 commit comments