Add option to set fixed sizes for horizontal group columns

This commit is contained in:
VladV 2023-03-12 12:21:12 +04:00
parent 3c36b7b2d3
commit 22884cc5cf
3 changed files with 75 additions and 9 deletions

View File

@ -11,7 +11,7 @@ namespace TriInspector.GroupDrawers
{
public override TriPropertyCollectionBaseElement CreateElement(DeclareHorizontalGroupAttribute attribute)
{
return new TriHorizontalGroupElement();
return new TriHorizontalGroupElement(attribute.Sizes);
}
}
}

View File

@ -1,4 +1,5 @@
using TriInspector.Utilities;
using System;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
@ -6,6 +7,23 @@ 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)
@ -13,14 +31,16 @@ namespace TriInspector.Elements
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);
@ -38,24 +58,68 @@ namespace TriInspector.Elements
}
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;
}
}
}

View File

@ -10,5 +10,7 @@ namespace TriInspector
public DeclareHorizontalGroupAttribute(string path) : base(path)
{
}
public float[] Sizes { get; set; }
}
}