11
11
namespace Lachee . Utilities . Editor
12
12
{
13
13
/// <summary>Default styling used for the random list</summary>
14
- public sealed class RandomListEditorStyle
14
+ public class RandomListDrawerStyle
15
15
{
16
- public static readonly Color [ ] Colors =
16
+ /// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
17
+ public Dictionary < Type , IRistBoxLabelDrawer > Drawers = new Dictionary < Type , IRistBoxLabelDrawer > ( )
18
+ {
19
+ { typeof ( void ) , new BasicRistDrawer ( ) } ,
20
+ { typeof ( Color ) , new ColorRistDrawer ( ) } ,
21
+ { typeof ( AnimationCurve ) , new AnimationCurveRistDrawer ( ) }
22
+ } ;
23
+
24
+ public readonly Color [ ] Colors =
17
25
{
18
26
new Color ( 0.4831376f , 0.6211768f , 0.0219608f , 1.0f ) ,
19
27
new Color ( 0.3827448f , 0.2886272f , 0.5239216f , 1.0f ) ,
@@ -39,25 +47,22 @@ public sealed class RandomListEditorStyle
39
47
/// <summary>Interface used to draw labels on weight sliders</summary>
40
48
public interface IRistBoxLabelDrawer
41
49
{
42
- public void DrawLabel ( Rect position , RandomListEditorStyle style , SerializedProperty property , float weight , bool isMini ) ;
50
+ public void DrawLabel ( Rect position , RandomListDrawerStyle style , SerializedProperty property , float weight , bool isMini ) ;
43
51
}
44
52
45
-
46
53
/// <summary>
47
54
/// Handles drawing the Random List and its sliders sliders
48
55
/// </summary>
49
56
#pragma warning disable CS0618 // Type or member is obsolete
50
57
[ CustomPropertyDrawer ( typeof ( RandomList < > ) ) ]
51
58
[ CustomPropertyDrawer ( typeof ( Rist < > ) ) ]
52
59
#pragma warning restore CS0618 // Type or member is obsolete
53
- public sealed class RandomListDrawer : UnityEditor . PropertyDrawer , IRistBoxLabelDrawer
60
+ public sealed class RandomListDrawer : UnityEditor . PropertyDrawer
54
61
{
55
- /// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
56
- public static Dictionary < System . Type , IRistBoxLabelDrawer > SliderLabelDrawers = new Dictionary < Type , IRistBoxLabelDrawer > ( )
57
- {
58
- { typeof ( Color ) , new ColorRistDrawer ( ) } ,
59
- { typeof ( AnimationCurve ) , new AnimationCurveRistDrawer ( ) }
60
- } ;
62
+
63
+ private static RandomListDrawerStyle _style ;
64
+ public static RandomListDrawerStyle style => _style ;
65
+
61
66
62
67
private const int WEIGHT_HEIGHT = 32 ;
63
68
private const int WEIGHT_RANGE_PADDING = 2 ;
@@ -67,16 +72,6 @@ public sealed class RandomListDrawer : UnityEditor.PropertyDrawer, IRistBoxLabel
67
72
private int _selectedWeight = - 1 ;
68
73
private ReorderableList _list ;
69
74
70
- private static RandomListEditorStyle _style ;
71
- private static RandomListEditorStyle Style
72
- {
73
- get
74
- {
75
- if ( _style == null )
76
- _style = new RandomListEditorStyle ( ) ;
77
- return _style ;
78
- }
79
- }
80
75
81
76
private bool ? _isFoldOut ;
82
77
private bool isListFoldout
@@ -124,12 +119,12 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
124
119
var previous = GUI . backgroundColor ;
125
120
126
121
if ( index >= 0 )
127
- GUI . backgroundColor = RandomListEditorStyle . Colors [ index % RandomListEditorStyle . Colors . Length ] ;
122
+ GUI . backgroundColor = style . Colors [ index % style . Colors . Length ] ;
128
123
129
124
if ( ! isActive )
130
125
GUI . backgroundColor *= 0.6f ;
131
126
132
- Style . sliderRange . Draw ( rect , GUIContent . none , false , false , false , false ) ;
127
+ style . sliderRange . Draw ( rect , GUIContent . none , false , false , false , false ) ;
133
128
GUI . backgroundColor = previous ;
134
129
} ;
135
130
@@ -172,8 +167,11 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
172
167
173
168
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
174
169
{
175
- isListFoldout = EditorGUI . Foldout ( new Rect ( position . x , position . y , position . width , EditorGUIUtility . singleLineHeight ) , isListFoldout , label ) ;
170
+ // Initialize the style the first chance we get
171
+ if ( _style == null )
172
+ _style = new RandomListDrawerStyle ( ) ;
176
173
174
+ isListFoldout = EditorGUI . Foldout ( new Rect ( position . x , position . y , position . width , EditorGUIUtility . singleLineHeight ) , isListFoldout , label ) ;
177
175
EditorGUI . indentLevel ++ ;
178
176
if ( isListFoldout )
179
177
{
@@ -298,16 +296,14 @@ private void OnSliderBoxGUI(Rect groupRect, SerializedProperty property, List<We
298
296
}
299
297
private void DrawSliderBox ( Rect area , List < WeightSlider > weights , int activeWeight , bool isCompact )
300
298
{
301
- Style . sliderBG . Draw ( area , GUIContent . none , false , false , false , false ) ;
299
+ style . sliderBG . Draw ( area , GUIContent . none , false , false , false , false ) ;
302
300
var tempColor = GUI . backgroundColor ;
303
301
for ( int i = 0 ; i < weights . Count ; i ++ )
304
302
{
305
303
// Get the weight and update any missing drawers
306
304
WeightSlider weight = weights [ i ] ;
307
- if ( weight . labelDrawer == null )
308
- weight . labelDrawer = this ;
305
+ Color color = style . Colors [ i % style . Colors . Length ] ;
309
306
310
- Color color = RandomListEditorStyle . Colors [ i % RandomListEditorStyle . Colors . Length ] ;
311
307
// The can no longer be activated
312
308
// if (activeWeight == i)
313
309
// {
@@ -326,8 +322,9 @@ private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeig
326
322
{
327
323
//DrawLODRAnge
328
324
GUI . backgroundColor = color * 0.6f ;
329
- Style . sliderRange . Draw ( weight . rangePosition , GUIContent . none , false , false , false , false ) ;
330
- weight . labelDrawer . DrawLabel ( weight . rangePosition , Style , weight . itemProperty , weight . weightPercentage , isCompact ) ;
325
+ style . sliderRange . Draw ( weight . rangePosition , GUIContent . none , false , false , false , false ) ;
326
+ if ( weight . labelDrawer != null )
327
+ weight . labelDrawer . DrawLabel ( weight . rangePosition , style , weight . itemProperty , weight . weightPercentage , isCompact ) ;
331
328
}
332
329
333
330
//DrawLODButton
@@ -341,16 +338,6 @@ private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeig
341
338
GUI . backgroundColor = tempColor ;
342
339
}
343
340
344
- void IRistBoxLabelDrawer . DrawLabel ( Rect position , RandomListEditorStyle style , SerializedProperty property , float weight , bool isMini )
345
- {
346
- var name = property . hasVisibleChildren ? property . displayName : property . GetValueName ( ) ;
347
- var label = isMini
348
- ? name
349
- : string . Format ( "{0}\n {1:0}%" , name , weight * 100 ) ;
350
-
351
- style . sliderText . Draw ( position , label , false , false , false , false ) ;
352
- }
353
-
354
341
public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
355
342
{
356
343
if ( ! isListFoldout )
@@ -411,6 +398,12 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
411
398
SerializedProperty itemListProperty = property . FindPropertyRelative ( "_list" ) ;
412
399
SerializedProperty weightListProperty = property . FindPropertyRelative ( "_weights" ) ;
413
400
401
+ var baseType = property . GetSerializedType ( ) ;
402
+ var elmType = baseType . GetGenericArguments ( ) [ 0 ] ;
403
+ IRistBoxLabelDrawer drawer ;
404
+ if ( style . Drawers == null || elmType == null || ! style . Drawers . TryGetValue ( elmType , out drawer ) )
405
+ drawer = style . Drawers [ typeof ( void ) ] ;
406
+
414
407
float weightOffset = 0 ;
415
408
for ( int i = 0 ; i < itemListProperty . arraySize ; i ++ )
416
409
{
@@ -426,27 +419,39 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
426
419
startWeight = weightOffset ,
427
420
endWeight = weightOffset + weightProperty . floatValue ,
428
421
sumWeight = totalWeightProperty . floatValue ,
422
+ labelDrawer = drawer ,
429
423
} ;
430
424
431
425
// Calculate the rect
432
426
float buttonSize = 10 ;
433
427
info . rangePosition = new Rect ( sliderRect . x + info . startPercentage * sliderRect . width , sliderRect . y , info . weightPercentage * sliderRect . width , sliderRect . height ) ;
434
428
info . buttonPosition = new Rect ( info . rangePosition . x - ( buttonSize / 2 ) , info . rangePosition . y , buttonSize , info . rangePosition . height ) ;
429
+
435
430
infos . Add ( info ) ;
436
-
437
- var type = itemProperty . GetSerializedType ( ) ;
438
- SliderLabelDrawers . TryGetValue ( type , out info . labelDrawer ) ;
439
-
440
431
weightOffset += info . weight ;
441
432
}
442
433
434
+
443
435
return infos ;
444
436
}
445
437
}
446
438
447
- internal class ColorRistDrawer : IRistBoxLabelDrawer
439
+ internal sealed class BasicRistDrawer : IRistBoxLabelDrawer
440
+ {
441
+ public void DrawLabel ( Rect position , RandomListDrawerStyle style , SerializedProperty property , float weight , bool isMini )
442
+ {
443
+ var name = property . hasVisibleChildren ? property . displayName : property . GetValueName ( ) ;
444
+ var label = isMini
445
+ ? name
446
+ : string . Format ( "{0}\n {1:0}%" , name , weight * 100 ) ;
447
+
448
+ style . sliderText . Draw ( position , label , false , false , false , false ) ;
449
+ }
450
+ }
451
+
452
+ internal sealed class ColorRistDrawer : IRistBoxLabelDrawer
448
453
{
449
- public void DrawLabel ( Rect position , RandomListEditorStyle style , SerializedProperty property , float weight , bool isMini )
454
+ public void DrawLabel ( Rect position , RandomListDrawerStyle style , SerializedProperty property , float weight , bool isMini )
450
455
{
451
456
var label = isMini
452
457
? property . GetValueName ( )
@@ -463,11 +468,11 @@ public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProp
463
468
}
464
469
}
465
470
466
- internal class AnimationCurveRistDrawer : IRistBoxLabelDrawer
471
+ internal sealed class AnimationCurveRistDrawer : IRistBoxLabelDrawer
467
472
{
468
473
const float PADDING = 2 ;
469
474
470
- public void DrawLabel ( Rect position , RandomListEditorStyle style , SerializedProperty property , float weight , bool isMini )
475
+ public void DrawLabel ( Rect position , RandomListDrawerStyle style , SerializedProperty property , float weight , bool isMini )
471
476
{
472
477
var label = isMini
473
478
? ""
0 commit comments