Skip to content

Commit

Permalink
Add option to set fixed sizes for horizontal group columns
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Mar 12, 2023
1 parent 3c36b7b commit 22884cc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Editor.Extras/GroupDrawers/TriHorizontalGroupDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TriHorizontalGroupDrawer : TriGroupDrawer<DeclareHorizontalGroupAtt
{
public override TriPropertyCollectionBaseElement CreateElement(DeclareHorizontalGroupAttribute attribute)
{
return new TriHorizontalGroupElement();
return new TriHorizontalGroupElement(attribute.Sizes);
}
}
}
80 changes: 72 additions & 8 deletions Editor/Elements/TriHorizontalGroupElement.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
using TriInspector.Utilities;
using System;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;

namespace TriInspector.Elements
{
public class TriHorizontalGroupElement : TriPropertyCollectionBaseElement
{
private readonly float[] _sizes;
private readonly float _totalFixedSize;

public TriHorizontalGroupElement(float[] sizes = null)
{
_sizes = sizes ?? Array.Empty<float>();
_totalFixedSize = 0f;

for (var index = 0; index < _sizes.Length; index++)
{
if (TryGetFixedSizeByIndex(index, out var fixedSize))
{
_totalFixedSize += fixedSize;
}
}
}

public override float GetHeight(float width)
{
if (ChildrenCount == 0)
{
return 0f;
}

var height = 0f;

var spacing = EditorGUIUtility.standardVerticalSpacing;
var totalWidth = width - spacing * (ChildrenCount - 1);
var childWidth = totalWidth / ChildrenCount;
var totalSpacing = spacing * (ChildrenCount - 1);
var totalDynamic = width - totalSpacing - _totalFixedSize;
var dynamicChildCount = GetDynamicChildCount();

var height = 0f;

for (var i = 0; i < ChildrenCount; i++)
{
var childWidth = GetChildWidth(i, totalDynamic, dynamicChildCount);
var child = GetChild(i);
var childHeight = child.GetHeight(childWidth);

Expand All @@ -38,24 +58,68 @@ public override void OnGUI(Rect position)
}

var spacing = EditorGUIUtility.standardVerticalSpacing;
var totalWidth = position.width - spacing * (ChildrenCount - 1);
var childWidth = totalWidth / ChildrenCount;
var totalSpacing = spacing * (ChildrenCount - 1);
var totalDynamic = position.width - totalSpacing - _totalFixedSize;
var dynamicChildCount = GetDynamicChildCount();

var xOffset = 0f;
for (var i = 0; i < ChildrenCount; i++)
{
var childWidth = GetChildWidth(i, totalDynamic, dynamicChildCount);
var child = GetChild(i);
var childRect = new Rect(position)
{
width = childWidth,
height = child.GetHeight(childWidth),
x = position.x + i * (childWidth + spacing),
x = position.xMin + xOffset,
};

using (TriGuiHelper.PushLabelWidth(EditorGUIUtility.labelWidth / ChildrenCount))
{
child.OnGUI(childRect);
}

xOffset += childWidth + spacing;
}
}

private float GetDynamicChildCount()
{
var count = 0f;

for (var i = 0; i < ChildrenCount; i++)
{
if (TryGetFixedSizeByIndex(i, out _))
{
continue;
}

count++;
}

return count;
}

private float GetChildWidth(int i, float totalDynamic, float dynamicChildCount)
{
if (TryGetFixedSizeByIndex(i, out var fixedSize))
{
return fixedSize;
}

return totalDynamic / dynamicChildCount;
}

private bool TryGetFixedSizeByIndex(int index, out float fixedSize)
{
if (index < _sizes.Length && _sizes[index] > 0f)
{
fixedSize = _sizes[index];
return true;
}

fixedSize = 0f;
return false;
}
}
}
2 changes: 2 additions & 0 deletions Runtime/Attributes/DeclareHorizontalGroupAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class DeclareHorizontalGroupAttribute : DeclareGroupBaseAttribute
public DeclareHorizontalGroupAttribute(string path) : base(path)
{
}

public float[] Sizes { get; set; }
}
}

0 comments on commit 22884cc

Please sign in to comment.