From fcc2c394472c4bf67b73b25ceb34823a57093d14 Mon Sep 17 00:00:00 2001 From: VladV Date: Sat, 24 Jun 2023 16:55:43 +0400 Subject: [PATCH] Hide grouping box if all children properties is invisible --- .../GroupDrawers/TriBoxGroupDrawer.cs | 1 + .../GroupDrawers/TriFoldoutGroupDrawer.cs | 1 + .../GroupDrawers/TriToggleGroupDrawer.cs | 1 + Editor/Elements/TriBoxGroupElement.cs | 6 ++- Editor/Elements/TriHeaderGroupBaseElement.cs | 38 ++++++++++++++++++- 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Editor.Extras/GroupDrawers/TriBoxGroupDrawer.cs b/Editor.Extras/GroupDrawers/TriBoxGroupDrawer.cs index bcb44d6..a1afbec 100644 --- a/Editor.Extras/GroupDrawers/TriBoxGroupDrawer.cs +++ b/Editor.Extras/GroupDrawers/TriBoxGroupDrawer.cs @@ -16,6 +16,7 @@ namespace TriInspector.GroupDrawers titleMode = attribute.HideTitle ? TriBoxGroupElement.TitleMode.Hidden : TriBoxGroupElement.TitleMode.Normal, + hideIfChildrenInvisible = true, }); } } diff --git a/Editor.Extras/GroupDrawers/TriFoldoutGroupDrawer.cs b/Editor.Extras/GroupDrawers/TriFoldoutGroupDrawer.cs index 2b16310..0ca815c 100644 --- a/Editor.Extras/GroupDrawers/TriFoldoutGroupDrawer.cs +++ b/Editor.Extras/GroupDrawers/TriFoldoutGroupDrawer.cs @@ -15,6 +15,7 @@ namespace TriInspector.GroupDrawers title = attribute.Title, titleMode = TriBoxGroupElement.TitleMode.Foldout, expandedByDefault = attribute.Expanded, + hideIfChildrenInvisible = true, }); } } diff --git a/Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs b/Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs index 3774460..e78e22c 100644 --- a/Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs +++ b/Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs @@ -15,6 +15,7 @@ namespace TriInspector.GroupDrawers title = attribute.Title, titleMode = TriBoxGroupElement.TitleMode.Toggle, expandedByDefault = attribute.Collapsible, + hideIfChildrenInvisible = true, }); } } diff --git a/Editor/Elements/TriBoxGroupElement.cs b/Editor/Elements/TriBoxGroupElement.cs index 7a0e594..537352a 100644 --- a/Editor/Elements/TriBoxGroupElement.cs +++ b/Editor/Elements/TriBoxGroupElement.cs @@ -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; diff --git a/Editor/Elements/TriHeaderGroupBaseElement.cs b/Editor/Elements/TriHeaderGroupBaseElement.cs index 264f0bc..bc6ed18 100644 --- a/Editor/Elements/TriHeaderGroupBaseElement.cs +++ b/Editor/Elements/TriHeaderGroupBaseElement.cs @@ -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 _properties = new List(); + + 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);