7
7
8
8
namespace Toolbox . Editor . Hierarchy
9
9
{
10
- //TODO: refactor
10
+ //TODO: refactor: replace labels with drawers (similar approach to the Inspector), possibility to define drawers and implement them using a dedicated base class
11
11
12
12
/// <summary>
13
13
/// Base class for all custom, Hierarchy-related labels based on targeted <see cref="GameObject"/>.
@@ -16,16 +16,6 @@ public abstract class HierarchyPropertyLabel
16
16
{
17
17
protected GameObject target ;
18
18
19
- /// <summary>
20
- /// Does this label draw over the whole item?
21
- /// </summary>
22
- public virtual bool UsesWholeItemRect { get ; } = false ;
23
-
24
- /// <summary>
25
- /// Should this label draw for headers too?
26
- /// </summary>
27
- public virtual bool DrawForHeaders { get ; } = false ;
28
-
29
19
public virtual bool Prepare ( GameObject target , Rect availableRect )
30
20
{
31
21
return this . target = target ;
@@ -52,7 +42,6 @@ public virtual float GetWidth()
52
42
53
43
public abstract void OnGui ( Rect rect ) ;
54
44
55
-
56
45
/// <summary>
57
46
/// Returns built-in label class associated to provided <see cref="HierarchyItemDataType"/>.
58
47
/// </summary>
@@ -77,31 +66,44 @@ public static HierarchyPropertyLabel GetPropertyLabel(HierarchyItemDataType data
77
66
return null ;
78
67
}
79
68
69
+ /// <summary>
70
+ /// Does this label draw over the whole item?
71
+ /// </summary>
72
+ public virtual bool UsesWholeItemRect { get ; } = false ;
73
+
74
+ /// <summary>
75
+ /// Should this label draw for headers too?
76
+ /// </summary>
77
+ public virtual bool DrawForHeaders { get ; } = false ;
78
+
80
79
#region Classes: Internal
81
80
82
81
private class HierarchyIconLabel : HierarchyPropertyLabel
83
82
{
84
83
public override void OnGui ( Rect rect )
85
84
{
86
85
var content = EditorGuiUtility . GetObjectContent ( target , typeof ( GameObject ) ) ;
87
- if ( content . image )
86
+ if ( content . image == null )
88
87
{
89
- GUI . Label ( rect , content . image ) ;
88
+ return ;
90
89
}
90
+
91
+ GUI . Label ( rect , content . image ) ;
91
92
}
92
93
}
93
94
94
95
private class HierarchyToggleLabel : HierarchyPropertyLabel
95
96
{
97
+ private readonly GUIContent label = new GUIContent ( string . Empty , "Enable/disable GameObject" ) ;
98
+
96
99
public override void OnGui ( Rect rect )
97
100
{
98
- var content = new GUIContent ( string . Empty , "Enable/disable GameObject" ) ;
99
101
//NOTE: using EditorGUI.Toggle will cause bug and deselect all hierarchy toggles when you will pick a multi-selected property in the Inspector
100
102
var result = GUI . Toggle ( new Rect ( rect . x + EditorGUIUtility . standardVerticalSpacing ,
101
103
rect . y ,
102
104
rect . width ,
103
105
rect . height ) ,
104
- target . activeSelf , content ) ;
106
+ target . activeSelf , label ) ;
105
107
106
108
if ( rect . Contains ( Event . current . mousePosition ) )
107
109
{
@@ -116,14 +118,16 @@ public override void OnGui(Rect rect)
116
118
117
119
private class HierarchyTagLabel : HierarchyPropertyLabel
118
120
{
121
+ private const string untaggedTag = "Untagged" ;
122
+
119
123
public override float GetWidth ( )
120
124
{
121
125
return Style . maxWidth ;
122
126
}
123
127
124
128
public override void OnGui ( Rect rect )
125
129
{
126
- var content = new GUIContent ( target . CompareTag ( "Untagged" ) ? string . Empty : target . tag , target . tag ) ;
130
+ var content = new GUIContent ( target . CompareTag ( untaggedTag ) ? string . Empty : target . tag , target . tag ) ;
127
131
EditorGUI . LabelField ( rect , content , Style . defaultAlignTextStyle ) ;
128
132
}
129
133
}
@@ -165,18 +169,19 @@ private class HierarchyScriptLabel : HierarchyPropertyLabel
165
169
/// </summary>
166
170
private List < Component > cachedComponents ;
167
171
168
-
169
172
private void CacheComponents ( GameObject target )
170
173
{
171
174
var components = target . GetComponents < Component > ( ) ;
172
175
cachedComponents = new List < Component > ( components . Length ) ;
173
176
//cache only valid (non-null) components
174
177
foreach ( var component in components )
175
178
{
176
- if ( component )
179
+ if ( component == null )
177
180
{
178
- cachedComponents . Add ( component ) ;
181
+ continue ;
179
182
}
183
+
184
+ cachedComponents . Add ( component ) ;
180
185
}
181
186
}
182
187
@@ -212,7 +217,6 @@ private GUIContent GetContent(Component component)
212
217
return content ;
213
218
}
214
219
215
-
216
220
public override bool Prepare ( GameObject target , Rect availableRect )
217
221
{
218
222
var isValid = base . Prepare ( target , availableRect ) ;
@@ -302,27 +306,27 @@ private class HierarchyTreeLinesLabel : HierarchyPropertyLabel, IDisposable
302
306
private const float columnSize = 14.0f ;
303
307
304
308
private readonly List < TreeLineLevelRenderer > levelRenderers = new List < TreeLineLevelRenderer > ( ) ;
309
+
305
310
private int itemRenderCount = 0 ;
306
311
307
312
public HierarchyTreeLinesLabel ( )
308
313
{
309
- EditorApplication . update += ResetItemRenderCount ;
314
+ EditorApplication . update += ResetItemRenderCount ;
310
315
}
311
316
312
317
public void Dispose ( )
313
318
{
314
319
EditorApplication . update -= ResetItemRenderCount ;
315
320
}
316
321
317
- public override sealed void OnGui ( Rect rect )
322
+ public sealed override void OnGui ( Rect rect )
318
323
{
319
324
if ( Event . current . type != EventType . Repaint )
320
325
{
321
326
return ;
322
327
}
323
328
324
329
var levels = ( int ) ( ( rect . x + firstElementXOffset ) / columnSize ) ;
325
-
326
330
if ( levels <= 0 )
327
331
{
328
332
return ;
@@ -354,18 +358,18 @@ public override sealed void OnGui(Rect rect)
354
358
355
359
x -- ;
356
360
357
- Transform transformBuffer = targetTransform ;
361
+ var transformBuffer = targetTransform ;
358
362
for ( ; x >= startIndex ; x -- )
359
363
{
360
364
levelRenderers [ x ] . Initialize ( transformBuffer ) ;
361
365
transformBuffer = transformBuffer . parent ;
362
366
}
363
367
}
364
368
365
- Color colorCache = GUI . color ;
369
+ var colorCache = GUI . color ;
366
370
GUI . color = Color . gray ;
367
371
368
- int i = 0 ;
372
+ var i = 0 ;
369
373
for ( ; i < ( levels - 1 ) ; i ++ )
370
374
{
371
375
levelRenderers [ i ] . OnGUI ( rect , target , siblingIndex , false ) ;
@@ -382,9 +386,9 @@ private void ResetItemRenderCount()
382
386
itemRenderCount = 0 ;
383
387
}
384
388
385
- public override sealed bool UsesWholeItemRect => true ;
389
+ public sealed override bool UsesWholeItemRect => true ;
386
390
387
- public override sealed bool DrawForHeaders => true ;
391
+ public sealed override bool DrawForHeaders => true ;
388
392
389
393
private bool IsFirstRenderedElement => itemRenderCount == 0 ;
390
394
@@ -463,6 +467,8 @@ protected static class Style
463
467
internal static readonly GUIContent elementCross ;
464
468
internal static readonly GUIContent elementPass ;
465
469
470
+ internal static readonly Color characterColor ;
471
+
466
472
static Style ( )
467
473
{
468
474
elementLast = new GUIContent ( "└" ) ;
@@ -501,6 +507,11 @@ static Style()
501
507
{
502
508
fontSize = 18 ,
503
509
} ;
510
+
511
+ if ( ! EditorGUIUtility . isProSkin )
512
+ {
513
+ centreAlignTreeLineStyle . normal . textColor = Color . white ;
514
+ }
504
515
}
505
516
}
506
517
}
0 commit comments