1
- using MeshTilesets ;
1
+ using System . Linq ;
2
+ using MeshTilesets ;
2
3
using UnityEditor ;
3
4
using UnityEngine ;
4
5
@@ -7,11 +8,21 @@ namespace MeshTilesetsEditor
7
8
[ CustomPropertyDrawer ( typeof ( TilesetFlagsMask ) ) ]
8
9
public class TilesetFlagsMaskDrawer : PropertyDrawer
9
10
{
10
- private bool foldout = true ;
11
-
12
11
public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
13
12
{
14
- return 0 ;
13
+ if ( property . serializedObject . targetObject is Component c )
14
+ {
15
+ var tileset = c . gameObject . GetComponentInParent < Tileset > ( ) ;
16
+ if ( tileset != null )
17
+ {
18
+ if ( ! property . isExpanded ) return EditorGUIUtility . singleLineHeight ;
19
+
20
+ var mask = property . GetValue < TilesetFlagsMask > ( ) ;
21
+ return EditorGUIUtility . singleLineHeight + tileset . TilesetFlags . GetDisplayHeight ( ) ;
22
+ }
23
+ }
24
+
25
+ return EditorGUIUtility . singleLineHeight * 2 ;
15
26
}
16
27
17
28
// TODO: implement generic property drawer
@@ -24,7 +35,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
24
35
{
25
36
// EditorGUI.BeginProperty(position, label, property);
26
37
var mask = property . GetValue < TilesetFlagsMask > ( ) ;
27
- DrawTilesetFlagsMask ( label , mask , tileset , ref foldout ) ;
38
+ bool foldout = property . isExpanded ;
39
+ DrawTilesetFlagsMask ( position , label , mask , tileset , ref foldout ) ;
40
+ property . isExpanded = foldout ;
28
41
if ( GUI . changed ) property . SetValue ( mask ) ;
29
42
// EditorGUI.EndProperty();
30
43
return ;
@@ -33,20 +46,25 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
33
46
EditorGUI . HelpBox ( position , "No Tileset found" , MessageType . None ) ;
34
47
}
35
48
36
- public static void DrawTilesetFlagsMask ( GUIContent label , TilesetFlagsMask mask , Tileset tileset , ref bool foldout )
49
+ public static void DrawTilesetFlagsMask ( Rect pos , GUIContent label , TilesetFlagsMask mask , Tileset tileset , ref bool foldout )
37
50
{
38
- foldout = EditorGUILayout . Foldout ( foldout , label ) ;
39
-
51
+ // Foldout
52
+ pos . height = EditorGUIUtility . singleLineHeight ;
53
+ foldout = EditorGUI . Foldout ( pos , foldout , label ) ;
54
+ pos . y += pos . height + EditorGUIUtility . standardVerticalSpacing ;
55
+
56
+ // Indent
57
+ pos . x += EditorGUIUtility . singleLineHeight ;
58
+ pos . width -= EditorGUIUtility . singleLineHeight ;
59
+
40
60
if ( foldout )
41
61
{
42
- EditorGUI . indentLevel ++ ;
43
- DrawTilesetFlagsMask ( label , mask , tileset ) ;
44
- EditorGUI . indentLevel -- ;
62
+ DrawTilesetFlagsMask ( pos , label , mask , tileset ) ;
45
63
}
46
64
// EditorGUILayout.EndFoldoutHeaderGroup();
47
65
}
48
66
49
- public static void DrawTilesetFlagsMask ( GUIContent label , TilesetFlagsMask mask , Tileset tileset )
67
+ public static void DrawTilesetFlagsMask ( Rect pos , GUIContent label , TilesetFlagsMask mask , Tileset tileset )
50
68
{
51
69
var flags = tileset . TilesetFlags ;
52
70
@@ -56,13 +74,40 @@ public static void DrawTilesetFlagsMask(GUIContent label, TilesetFlagsMask mask,
56
74
57
75
if ( flags [ i ] . isToggle )
58
76
{
59
- mask [ i ] = EditorGUILayout . Toggle ( flags [ i ] . name , mask [ i ] == 1 ) ? 1 : 0 ;
77
+ mask [ i ] = EditorGUI . Toggle ( pos , flags [ i ] . name , mask [ i ] == 1 ) ? 1 : 0 ;
60
78
}
61
79
else
62
80
{
63
- mask [ i ] = EditorGUILayout . Popup ( flags [ i ] . name , mask [ i ] , flags [ i ] . OptionsWithUndefined ) ;
81
+ mask [ i ] = EditorGUI . Popup ( pos , flags [ i ] . name , mask [ i ] , flags [ i ] . OptionsWithUndefined ) ;
64
82
}
83
+
84
+ pos . height = EditorGUIUtility . singleLineHeight ;
85
+ pos . y += pos . height + EditorGUIUtility . standardVerticalSpacing ;
65
86
}
66
87
}
88
+
89
+ public static void DrawTilesetFlagsMask ( GUIContent label , TilesetFlagsMask mask , Tileset tileset , ref bool foldout )
90
+ {
91
+ float height = EditorGUIUtility . singleLineHeight + ( foldout ? tileset . TilesetFlags . GetDisplayHeight ( ) : 0 ) ;
92
+ var rect = GUILayoutUtility . GetRect ( 0 , EditorGUIUtility . currentViewWidth , height , height ) ;
93
+ DrawTilesetFlagsMask ( rect , label , mask , tileset , ref foldout ) ;
94
+ }
95
+
96
+ public static void DrawTilesetFlagsMask ( GUIContent label , TilesetFlagsMask mask , Tileset tileset )
97
+ {
98
+ float height = tileset . TilesetFlags . GetDisplayHeight ( ) ;
99
+ var rect = GUILayoutUtility . GetRect ( 0 , EditorGUIUtility . currentViewWidth , height , height ) ;
100
+ DrawTilesetFlagsMask ( rect , label , mask , tileset ) ;
101
+ }
102
+ }
103
+
104
+ public static class TilesetFlagsExtension
105
+ {
106
+ public static float GetDisplayHeight ( this TilesetFlags [ ] flags )
107
+ {
108
+ return flags . Aggregate ( 0f , ( r , f ) =>
109
+ r + ( f . IsEnabled ? EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing : 0 )
110
+ ) ;
111
+ }
67
112
}
68
113
}
0 commit comments