Skip to content

Commit

Permalink
Attempt to rework and fix horizontal group behaviours; minor refactor…
Browse files Browse the repository at this point in the history
… changes
  • Loading branch information
arimger committed Apr 1, 2024
1 parent 2100a33 commit 72a72a0
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ namespace Toolbox.Editor.Drawers
{
public class BeginHorizontalAttributeDrawer : ToolboxDecoratorDrawer<BeginHorizontalAttribute>
{
private static float lastFetchedWidth = 0.0f;

protected override void OnGuiBeginSafe(BeginHorizontalAttribute attribute)
{
var width = EditorGUIUtility.currentViewWidth;
//set a new width value for label/field controls
EditorGUIUtility.labelWidth = width * attribute.LabelToWidthRatio;
EditorGUIUtility.fieldWidth = width * attribute.FieldToWidthRatio;
if (GuiLayoutUtility.TryGetLayoutWidth(out var layoutWidth))
{
lastFetchedWidth = layoutWidth;
}

EditorGUIUtility.labelWidth = attribute.LabelWidth;
if (attribute.ControlFieldWidth && attribute.ElementsInLayout > 0)
{
var width = lastFetchedWidth;
width -= attribute.WidthOffset;
width -= (attribute.LabelWidth + attribute.WidthOffsetPerElement + EditorGUIUtility.standardVerticalSpacing * 4) * attribute.ElementsInLayout;
width /= attribute.ElementsInLayout;
EditorGUIUtility.fieldWidth = width;
}

//begin horizontal group using internal utility
ToolboxLayoutHandler.BeginHorizontal();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static BeginHorizontalGroupAttributeDrawer()
/// </summary>
private static readonly ControlDataStorage<Vector2> storage;

private static float lastFetchedWidth = 0.0f;

private void HandleScrollView(float fixedHeight)
{
Expand All @@ -29,25 +30,33 @@ private void HandleScrollView(float fixedHeight)
storage.AppendItem(controlId, newScroll);
}


protected override void OnGuiBeginSafe(BeginHorizontalGroupAttribute attribute)
{
var fixedWidth = EditorGUIUtility.currentViewWidth;
var fixedHeight = attribute.Height;
EditorGUIUtility.labelWidth = fixedWidth * attribute.LabelToWidthRatio;
EditorGUIUtility.fieldWidth = fixedWidth * attribute.FieldToWidthRatio;
if (GuiLayoutUtility.TryGetLayoutWidth(out var layoutWidth))
{
lastFetchedWidth = layoutWidth;
}

ToolboxLayoutHandler.BeginVertical(Style.groupBackgroundStyle);
if (attribute.HasLabel)
{
GUILayout.Label(attribute.Label, EditorStyles.boldLabel);
}

HandleScrollView(fixedHeight);
EditorGUIUtility.labelWidth = attribute.LabelWidth;
if (attribute.ControlFieldWidth && attribute.ElementsInLayout > 0)
{
var width = lastFetchedWidth;
width -= attribute.WidthOffset;
width -= (attribute.LabelWidth + attribute.WidthOffsetPerElement + EditorGUIUtility.standardVerticalSpacing * 4) * attribute.ElementsInLayout;
width /= attribute.ElementsInLayout;
EditorGUIUtility.fieldWidth = width;
}

HandleScrollView(attribute.Height);
ToolboxLayoutHandler.BeginHorizontal();
}


private static class Style
{
internal static readonly GUIStyle groupBackgroundStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ static ReorderableListExposedAttributeDrawer()

private static readonly PropertyDataStorage<ReorderableListBase, ReorderableListExposedAttribute> storage;


private static void ConnectCallbacks(ReorderableListBase list, ReorderableListExposedAttribute attribute)
{
var listTarget = list.SerializedObject;
Expand Down Expand Up @@ -80,7 +79,6 @@ private static MethodInfo FindMethod(SerializedObject target, string methodName,
return methodInfo;
}


protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReorderableListExposedAttribute attribute)
{
storage.ReturnItem(property, attribute).DoList(label);
Expand Down
2 changes: 0 additions & 2 deletions Assets/Editor Toolbox/Editor/Utilities/DraggingUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ static DraggingUtility()
BindingFlags.NonPublic | BindingFlags.Instance);
}


private static readonly MethodInfo validateAssignmentMethod;
private static readonly MethodInfo appendFoldoutValueMethod;

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


public static Object ValidateAssignment(Object[] references, SerializedProperty property, Type type, bool exactType)
{
#if UNITY_2017_1_OR_NEWER
Expand Down
2 changes: 0 additions & 2 deletions Assets/Editor Toolbox/Editor/Utilities/EditorGuiUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal static class EditorGuiUtility

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


public static Texture2D CreateColorTexture()
{
return CreateColorTexture(Color.clear);
Expand Down Expand Up @@ -100,7 +99,6 @@ public static Texture GetHelpIcon(MessageType messageType)
return null;
}


public static float FoldoutSize { get; internal set; } = 15.0f;
public static float SpacingSize => EditorGUIUtility.standardVerticalSpacing;
public static float HeightSize => EditorGUIUtility.singleLineHeight;
Expand Down
23 changes: 21 additions & 2 deletions Assets/Editor Toolbox/Editor/Utilities/GuiLayoutUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static class GuiLayoutUtility
/// </summary>
public static readonly float layoutPadding = -2 * EditorGUIUtility.standardVerticalSpacing;


public static void BeginStrechedVertical()
{
BeginFixedVertical(GUIStyle.none);
Expand Down Expand Up @@ -54,6 +53,26 @@ public static void CreateSpace(float space)
GUILayout.Space(space);
}

//TODO: add more helper methods
/// <summary>
/// A bit hacky way to retrieve the width of currently active layout.
/// Width is properly calculated only when the <see cref="Event.type"/> equals to <see cref="EventType.Repaint"/>.
/// </summary>
public static bool TryGetLayoutWidth(out float width)
{
using (var scope = new EditorGUILayout.HorizontalScope())
{
if (Event.current.type == EventType.Repaint)
{
var scopeRect = scope.rect;
width = scopeRect.width;
return true;
}
else
{
width = 0.0f;
return false;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,46 @@ namespace UnityEngine
[Conditional("UNITY_EDITOR")]
public class BeginHorizontalAttribute : ToolboxDecoratorAttribute
{
public BeginHorizontalAttribute()
{ }

[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
public BeginHorizontalAttribute(float labelToWidthRatio = 0.0f, float fieldToWidthRatio = 0.0f)
{
LabelToWidthRatio = labelToWidthRatio;
FieldToWidthRatio = fieldToWidthRatio;
}

public float LabelToWidthRatio { get; private set; }
/// <summary>
/// Indicates whether layout elements should be sized automatically or using associated properties.
/// Associated properties: <see cref="ElementsInLayout"/>, <see cref="WidthOffset"/>, <see cref="WidthOffsetPerElement"/>.
/// </summary>
public bool ControlFieldWidth { get; set; }
/// <summary>
/// Indicates how many elements are placed in the layout.
/// Used to specify how big should be the field width for each element.
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
/// </summary>
public int ElementsInLayout { get; set; } = -1;
/// <summary>
/// Value substracted from the available space when calculating field width for each element.
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
/// </summary>
public float WidthOffset { get; set; }
/// <summary>
/// Value substracted from the available space when calculating field width for each element.
/// Used only when the <see cref="ControlFieldWidth"/> is set to <see langword="true"/>.
/// </summary>
public float WidthOffsetPerElement { get; set; }
/// <summary>
/// Overrides label width within the layout for each element.
/// Set to 0 to keep the default width.
/// </summary>
public float LabelWidth { get; set; } = 100.0f;

[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
public float LabelToWidthRatio { get; private set; }
[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
public float FieldToWidthRatio { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ namespace UnityEngine
[Conditional("UNITY_EDITOR")]
public class BeginHorizontalGroupAttribute : BeginHorizontalAttribute
{
public BeginHorizontalGroupAttribute() : base()
{
WidthOffset = 32.0f;
}

[Obsolete("Ratios are no longer valid, use ControlFieldWidth and linked properties to specify width of layout elements.")]
public BeginHorizontalGroupAttribute(float labelToWidthRatio = 0.0f, float fieldToWidthRatio = 0.0f, string label = null) : base(labelToWidthRatio, fieldToWidthRatio)
{
Label = label;
}

public string Label { get; private set; }
public string Label { get; set; }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public ReorderableListAttribute(ListStyle style = ListStyle.Round, string elemen
ElementLabel = elementLabel;
}

public bool Draggable { get; private set; }
public bool FixedSize { get; private set; }
public ListStyle ListStyle { get; private set; }
public string ElementLabel { get; private set; }
public bool Draggable { get; set; }
public bool FixedSize { get; set; }
public ListStyle ListStyle { get; set; }
public string ElementLabel { get; set; }

/// <summary>
/// Indicates whether list should be allowed to fold in and out.
Expand Down
26 changes: 23 additions & 3 deletions Assets/Examples/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1517,8 +1517,11 @@ MonoBehaviour:
var31: 0
gameObjects:
- {fileID: 0}
- {fileID: 0}
floats: []
floats:
- 0
- 0
- 0
- 0
var2: 0
var3: 0
var4: 0
Expand Down Expand Up @@ -1555,7 +1558,7 @@ GameObject:
m_Layer: 0
m_Name: Special&Others[Sample]
m_TagString: Untagged
m_Icon: {fileID: 0}
m_Icon: {fileID: 2800000, guid: b105bf1fb7fe62e4baba0ffd7b433e7b, type: 3}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
Expand All @@ -1571,6 +1574,23 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c48a231d0fb97494d948757fb057b3ea, type: 3}
m_Name:
m_EditorClassIdentifier:
gos1:
- {fileID: 0}
gos2:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
go1: {fileID: 1670253091}
go2: {fileID: 0}
go3: 0
go4: 0
var1: 25.4
var2: {fileID: 977748988}
var3: 0
Expand Down
4 changes: 2 additions & 2 deletions Assets/Examples/Scripts/SampleBehaviour4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ private static void TestStaticMethod()

[Label("Horizontal Layout", skinStyle: SkinStyle.Box)]

[BeginHorizontal(labelToWidthRatio: 0.1f)]
[BeginHorizontal(ControlFieldWidth = true, ElementsInLayout = 3)]
public int var29;
public int var30;
[EndHorizontal]
public int var31;

[Label("Horizontal Layout (Group)", skinStyle: SkinStyle.Box)]

[BeginHorizontalGroup(label: "Horizontal Group")]
[BeginHorizontalGroup(Label = "Horizontal Group", ControlFieldWidth = true, ElementsInLayout = 2)]
[ReorderableList(Foldable = true), InLineEditor]
public GameObject[] gameObjects;
[SpaceArea]
Expand Down

0 comments on commit 72a72a0

Please sign in to comment.