Skip to content

Commit 4705437

Browse files
fix: remove GUILayout calls from TilesetFlagsMaskDrawer
1 parent 420c964 commit 4705437

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed
Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MeshTilesets;
1+
using System.Linq;
2+
using MeshTilesets;
23
using UnityEditor;
34
using UnityEngine;
45

@@ -7,11 +8,21 @@ namespace MeshTilesetsEditor
78
[CustomPropertyDrawer(typeof(TilesetFlagsMask))]
89
public class TilesetFlagsMaskDrawer : PropertyDrawer
910
{
10-
private bool foldout = true;
11-
1211
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
1312
{
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;
1526
}
1627

1728
// TODO: implement generic property drawer
@@ -24,7 +35,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2435
{
2536
// EditorGUI.BeginProperty(position, label, property);
2637
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;
2841
if (GUI.changed) property.SetValue(mask);
2942
// EditorGUI.EndProperty();
3043
return;
@@ -33,20 +46,25 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
3346
EditorGUI.HelpBox(position, "No Tileset found", MessageType.None);
3447
}
3548

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)
3750
{
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+
4060
if (foldout)
4161
{
42-
EditorGUI.indentLevel++;
43-
DrawTilesetFlagsMask(label, mask, tileset);
44-
EditorGUI.indentLevel--;
62+
DrawTilesetFlagsMask(pos, label, mask, tileset);
4563
}
4664
// EditorGUILayout.EndFoldoutHeaderGroup();
4765
}
4866

49-
public static void DrawTilesetFlagsMask(GUIContent label, TilesetFlagsMask mask, Tileset tileset)
67+
public static void DrawTilesetFlagsMask(Rect pos, GUIContent label, TilesetFlagsMask mask, Tileset tileset)
5068
{
5169
var flags = tileset.TilesetFlags;
5270

@@ -56,13 +74,40 @@ public static void DrawTilesetFlagsMask(GUIContent label, TilesetFlagsMask mask,
5674

5775
if (flags[i].isToggle)
5876
{
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;
6078
}
6179
else
6280
{
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);
6482
}
83+
84+
pos.height = EditorGUIUtility.singleLineHeight;
85+
pos.y += pos.height + EditorGUIUtility.standardVerticalSpacing;
6586
}
6687
}
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+
}
67112
}
68113
}

0 commit comments

Comments
 (0)