Skip to content

Commit 72a72a0

Browse files
committed
Attempt to rework and fix horizontal group behaviours; minor refactor changes
1 parent 2100a33 commit 72a72a0

File tree

11 files changed

+122
-31
lines changed

11 files changed

+122
-31
lines changed

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/BeginHorizontalAttributeDrawer.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@ namespace Toolbox.Editor.Drawers
55
{
66
public class BeginHorizontalAttributeDrawer : ToolboxDecoratorDrawer<BeginHorizontalAttribute>
77
{
8+
private static float lastFetchedWidth = 0.0f;
9+
810
protected override void OnGuiBeginSafe(BeginHorizontalAttribute attribute)
911
{
10-
var width = EditorGUIUtility.currentViewWidth;
11-
//set a new width value for label/field controls
12-
EditorGUIUtility.labelWidth = width * attribute.LabelToWidthRatio;
13-
EditorGUIUtility.fieldWidth = width * attribute.FieldToWidthRatio;
12+
if (GuiLayoutUtility.TryGetLayoutWidth(out var layoutWidth))
13+
{
14+
lastFetchedWidth = layoutWidth;
15+
}
16+
17+
EditorGUIUtility.labelWidth = attribute.LabelWidth;
18+
if (attribute.ControlFieldWidth && attribute.ElementsInLayout > 0)
19+
{
20+
var width = lastFetchedWidth;
21+
width -= attribute.WidthOffset;
22+
width -= (attribute.LabelWidth + attribute.WidthOffsetPerElement + EditorGUIUtility.standardVerticalSpacing * 4) * attribute.ElementsInLayout;
23+
width /= attribute.ElementsInLayout;
24+
EditorGUIUtility.fieldWidth = width;
25+
}
1426

15-
//begin horizontal group using internal utility
1627
ToolboxLayoutHandler.BeginHorizontal();
1728
}
1829
}

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/BeginHorizontalGroupAttributeDrawer.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static BeginHorizontalGroupAttributeDrawer()
1818
/// </summary>
1919
private static readonly ControlDataStorage<Vector2> storage;
2020

21+
private static float lastFetchedWidth = 0.0f;
2122

2223
private void HandleScrollView(float fixedHeight)
2324
{
@@ -29,25 +30,33 @@ private void HandleScrollView(float fixedHeight)
2930
storage.AppendItem(controlId, newScroll);
3031
}
3132

32-
3333
protected override void OnGuiBeginSafe(BeginHorizontalGroupAttribute attribute)
3434
{
35-
var fixedWidth = EditorGUIUtility.currentViewWidth;
36-
var fixedHeight = attribute.Height;
37-
EditorGUIUtility.labelWidth = fixedWidth * attribute.LabelToWidthRatio;
38-
EditorGUIUtility.fieldWidth = fixedWidth * attribute.FieldToWidthRatio;
35+
if (GuiLayoutUtility.TryGetLayoutWidth(out var layoutWidth))
36+
{
37+
lastFetchedWidth = layoutWidth;
38+
}
3939

4040
ToolboxLayoutHandler.BeginVertical(Style.groupBackgroundStyle);
4141
if (attribute.HasLabel)
4242
{
4343
GUILayout.Label(attribute.Label, EditorStyles.boldLabel);
4444
}
4545

46-
HandleScrollView(fixedHeight);
46+
EditorGUIUtility.labelWidth = attribute.LabelWidth;
47+
if (attribute.ControlFieldWidth && attribute.ElementsInLayout > 0)
48+
{
49+
var width = lastFetchedWidth;
50+
width -= attribute.WidthOffset;
51+
width -= (attribute.LabelWidth + attribute.WidthOffsetPerElement + EditorGUIUtility.standardVerticalSpacing * 4) * attribute.ElementsInLayout;
52+
width /= attribute.ElementsInLayout;
53+
EditorGUIUtility.fieldWidth = width;
54+
}
55+
56+
HandleScrollView(attribute.Height);
4757
ToolboxLayoutHandler.BeginHorizontal();
4858
}
4959

50-
5160
private static class Style
5261
{
5362
internal static readonly GUIStyle groupBackgroundStyle;

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertyList/ReorderableListExposedAttributeDrawer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ static ReorderableListExposedAttributeDrawer()
3131

3232
private static readonly PropertyDataStorage<ReorderableListBase, ReorderableListExposedAttribute> storage;
3333

34-
3534
private static void ConnectCallbacks(ReorderableListBase list, ReorderableListExposedAttribute attribute)
3635
{
3736
var listTarget = list.SerializedObject;
@@ -80,7 +79,6 @@ private static MethodInfo FindMethod(SerializedObject target, string methodName,
8079
return methodInfo;
8180
}
8281

83-
8482
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReorderableListExposedAttribute attribute)
8583
{
8684
storage.ReturnItem(property, attribute).DoList(label);

Assets/Editor Toolbox/Editor/Utilities/DraggingUtility.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ static DraggingUtility()
1818
BindingFlags.NonPublic | BindingFlags.Instance);
1919
}
2020

21-
2221
private static readonly MethodInfo validateAssignmentMethod;
2322
private static readonly MethodInfo appendFoldoutValueMethod;
2423

2524
private static readonly int dragAndDropHash = "customDragAndDrop".GetHashCode();
2625

27-
2826
public static Object ValidateAssignment(Object[] references, SerializedProperty property, Type type, bool exactType)
2927
{
3028
#if UNITY_2017_1_OR_NEWER

Assets/Editor Toolbox/Editor/Utilities/EditorGuiUtility.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal static class EditorGuiUtility
1616

1717
private static readonly Dictionary<string, Texture2D> loadedTextures = new Dictionary<string, Texture2D>();
1818

19-
2019
public static Texture2D CreateColorTexture()
2120
{
2221
return CreateColorTexture(Color.clear);
@@ -100,7 +99,6 @@ public static Texture GetHelpIcon(MessageType messageType)
10099
return null;
101100
}
102101

103-
104102
public static float FoldoutSize { get; internal set; } = 15.0f;
105103
public static float SpacingSize => EditorGUIUtility.standardVerticalSpacing;
106104
public static float HeightSize => EditorGUIUtility.singleLineHeight;

Assets/Editor Toolbox/Editor/Utilities/GuiLayoutUtility.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public static class GuiLayoutUtility
1313
/// </summary>
1414
public static readonly float layoutPadding = -2 * EditorGUIUtility.standardVerticalSpacing;
1515

16-
1716
public static void BeginStrechedVertical()
1817
{
1918
BeginFixedVertical(GUIStyle.none);
@@ -54,6 +53,26 @@ public static void CreateSpace(float space)
5453
GUILayout.Space(space);
5554
}
5655

57-
//TODO: add more helper methods
56+
/// <summary>
57+
/// A bit hacky way to retrieve the width of currently active layout.
58+
/// Width is properly calculated only when the <see cref="Event.type"/> equals to <see cref="EventType.Repaint"/>.
59+
/// </summary>
60+
public static bool TryGetLayoutWidth(out float width)
61+
{
62+
using (var scope = new EditorGUILayout.HorizontalScope())
63+
{
64+
if (Event.current.type == EventType.Repaint)
65+
{
66+
var scopeRect = scope.rect;
67+
width = scopeRect.width;
68+
return true;
69+
}
70+
else
71+
{
72+
width = 0.0f;
73+
return false;
74+
}
75+
}
76+
}
5877
}
5978
}

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/DecoratorAttributes/BeginHorizontalAttribute.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,46 @@ namespace UnityEngine
1010
[Conditional("UNITY_EDITOR")]
1111
public class BeginHorizontalAttribute : ToolboxDecoratorAttribute
1212
{
13+
public BeginHorizontalAttribute()
14+
{ }
15+
16+
[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
1317
public BeginHorizontalAttribute(float labelToWidthRatio = 0.0f, float fieldToWidthRatio = 0.0f)
1418
{
1519
LabelToWidthRatio = labelToWidthRatio;
1620
FieldToWidthRatio = fieldToWidthRatio;
1721
}
1822

19-
public float LabelToWidthRatio { get; private set; }
23+
/// <summary>
24+
/// Indicates whether layout elements should be sized automatically or using associated properties.
25+
/// Associated properties: <see cref="ElementsInLayout"/>, <see cref="WidthOffset"/>, <see cref="WidthOffsetPerElement"/>.
26+
/// </summary>
27+
public bool ControlFieldWidth { get; set; }
28+
/// <summary>
29+
/// Indicates how many elements are placed in the layout.
30+
/// Used to specify how big should be the field width for each element.
31+
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
32+
/// </summary>
33+
public int ElementsInLayout { get; set; } = -1;
34+
/// <summary>
35+
/// Value substracted from the available space when calculating field width for each element.
36+
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
37+
/// </summary>
38+
public float WidthOffset { get; set; }
39+
/// <summary>
40+
/// Value substracted from the available space when calculating field width for each element.
41+
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
42+
/// </summary>
43+
public float WidthOffsetPerElement { get; set; }
44+
/// <summary>
45+
/// Overrides label width within the layout for each element.
46+
/// Set to 0 to keep the default width.
47+
/// </summary>
48+
public float LabelWidth { get; set; } = 100.0f;
2049

50+
[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
51+
public float LabelToWidthRatio { get; private set; }
52+
[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
2153
public float FieldToWidthRatio { get; private set; }
2254
}
2355
}

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/DecoratorAttributes/BeginHorizontalGroupAttribute.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ namespace UnityEngine
1212
[Conditional("UNITY_EDITOR")]
1313
public class BeginHorizontalGroupAttribute : BeginHorizontalAttribute
1414
{
15+
public BeginHorizontalGroupAttribute() : base()
16+
{
17+
WidthOffset = 32.0f;
18+
}
19+
20+
[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
1521
public BeginHorizontalGroupAttribute(float labelToWidthRatio = 0.0f, float fieldToWidthRatio = 0.0f, string label = null) : base(labelToWidthRatio, fieldToWidthRatio)
1622
{
1723
Label = label;
1824
}
1925

20-
public string Label { get; private set; }
26+
public string Label { get; set; }
2127

2228
public bool HasLabel => !string.IsNullOrEmpty(Label);
2329

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/PropertyListAttributes/ReorderableListAttribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public ReorderableListAttribute(ListStyle style = ListStyle.Round, string elemen
2020
ElementLabel = elementLabel;
2121
}
2222

23-
public bool Draggable { get; private set; }
24-
public bool FixedSize { get; private set; }
25-
public ListStyle ListStyle { get; private set; }
26-
public string ElementLabel { get; private set; }
23+
public bool Draggable { get; set; }
24+
public bool FixedSize { get; set; }
25+
public ListStyle ListStyle { get; set; }
26+
public string ElementLabel { get; set; }
2727

2828
/// <summary>
2929
/// Indicates whether list should be allowed to fold in and out.

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,8 +1517,11 @@ MonoBehaviour:
15171517
var31: 0
15181518
gameObjects:
15191519
- {fileID: 0}
1520-
- {fileID: 0}
1521-
floats: []
1520+
floats:
1521+
- 0
1522+
- 0
1523+
- 0
1524+
- 0
15221525
var2: 0
15231526
var3: 0
15241527
var4: 0
@@ -1555,7 +1558,7 @@ GameObject:
15551558
m_Layer: 0
15561559
m_Name: Special&Others[Sample]
15571560
m_TagString: Untagged
1558-
m_Icon: {fileID: 0}
1561+
m_Icon: {fileID: 2800000, guid: b105bf1fb7fe62e4baba0ffd7b433e7b, type: 3}
15591562
m_NavMeshLayer: 0
15601563
m_StaticEditorFlags: 0
15611564
m_IsActive: 1
@@ -1571,6 +1574,23 @@ MonoBehaviour:
15711574
m_Script: {fileID: 11500000, guid: c48a231d0fb97494d948757fb057b3ea, type: 3}
15721575
m_Name:
15731576
m_EditorClassIdentifier:
1577+
gos1:
1578+
- {fileID: 0}
1579+
gos2:
1580+
- {fileID: 0}
1581+
- {fileID: 0}
1582+
- {fileID: 0}
1583+
- {fileID: 0}
1584+
- {fileID: 0}
1585+
- {fileID: 0}
1586+
- {fileID: 0}
1587+
- {fileID: 0}
1588+
- {fileID: 0}
1589+
- {fileID: 0}
1590+
go1: {fileID: 1670253091}
1591+
go2: {fileID: 0}
1592+
go3: 0
1593+
go4: 0
15741594
var1: 25.4
15751595
var2: {fileID: 977748988}
15761596
var3: 0

0 commit comments

Comments
 (0)