Hide grouping box if all children properties is invisible

This commit is contained in:
VladV 2023-06-24 16:55:43 +04:00
parent bd1fc4935b
commit fcc2c39447
5 changed files with 45 additions and 2 deletions

View File

@ -16,6 +16,7 @@ namespace TriInspector.GroupDrawers
titleMode = attribute.HideTitle
? TriBoxGroupElement.TitleMode.Hidden
: TriBoxGroupElement.TitleMode.Normal,
hideIfChildrenInvisible = true,
});
}
}

View File

@ -15,6 +15,7 @@ namespace TriInspector.GroupDrawers
title = attribute.Title,
titleMode = TriBoxGroupElement.TitleMode.Foldout,
expandedByDefault = attribute.Expanded,
hideIfChildrenInvisible = true,
});
}
}

View File

@ -15,6 +15,7 @@ namespace TriInspector.GroupDrawers
title = attribute.Title,
titleMode = TriBoxGroupElement.TitleMode.Toggle,
expandedByDefault = attribute.Collapsible,
hideIfChildrenInvisible = true,
});
}
}

View File

@ -23,9 +23,13 @@ namespace TriInspector.Elements
public string title;
public TitleMode titleMode;
public bool expandedByDefault;
public bool hideIfChildrenInvisible;
}
public TriBoxGroupElement(Props props = default)
public TriBoxGroupElement(Props props = default) : base(new TriHeaderGroupBaseElement.Props
{
hideIfChildrenInvisible = props.hideIfChildrenInvisible,
})
{
_props = props;
_expanded = _props.expandedByDefault;

View File

@ -1,4 +1,7 @@
using TriInspector.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
@ -6,11 +9,34 @@ namespace TriInspector.Elements
{
public abstract class TriHeaderGroupBaseElement : TriPropertyCollectionBaseElement
{
private readonly Props _props;
private const float InsetTop = 4;
private const float InsetBottom = 4;
private const float InsetLeft = 18;
private const float InsetRight = 4;
private readonly List<TriProperty> _properties = new List<TriProperty>();
private bool IsAnyPropertyVisible => _properties.Any(it => it.IsVisible);
[Serializable]
public struct Props
{
public bool hideIfChildrenInvisible;
}
protected TriHeaderGroupBaseElement(Props props = default)
{
_props = props;
}
protected override void AddPropertyChild(TriElement element, TriProperty property)
{
_properties.Add(property);
base.AddPropertyChild(element, property);
}
protected virtual float GetHeaderHeight(float width)
{
return 22;
@ -32,6 +58,11 @@ namespace TriInspector.Elements
public sealed override float GetHeight(float width)
{
if (_props.hideIfChildrenInvisible && !IsAnyPropertyVisible)
{
return -EditorGUIUtility.standardVerticalSpacing;
}
var headerHeight = GetHeaderHeight(width);
var contentHeight = GetContentHeight(width);
@ -47,6 +78,11 @@ namespace TriInspector.Elements
public sealed override void OnGUI(Rect position)
{
if (_props.hideIfChildrenInvisible && !IsAnyPropertyVisible)
{
return;
}
var headerHeight = GetHeaderHeight(position.width);
var contentHeight = GetContentHeight(position.width);