Skip to content

Commit fcc2c39

Browse files
committed
Hide grouping box if all children properties is invisible
1 parent bd1fc49 commit fcc2c39

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

Editor.Extras/GroupDrawers/TriBoxGroupDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public override TriPropertyCollectionBaseElement CreateElement(DeclareBoxGroupAt
1616
titleMode = attribute.HideTitle
1717
? TriBoxGroupElement.TitleMode.Hidden
1818
: TriBoxGroupElement.TitleMode.Normal,
19+
hideIfChildrenInvisible = true,
1920
});
2021
}
2122
}

Editor.Extras/GroupDrawers/TriFoldoutGroupDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public override TriPropertyCollectionBaseElement CreateElement(DeclareFoldoutGro
1515
title = attribute.Title,
1616
titleMode = TriBoxGroupElement.TitleMode.Foldout,
1717
expandedByDefault = attribute.Expanded,
18+
hideIfChildrenInvisible = true,
1819
});
1920
}
2021
}

Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public override TriPropertyCollectionBaseElement CreateElement(DeclareToggleGrou
1515
title = attribute.Title,
1616
titleMode = TriBoxGroupElement.TitleMode.Toggle,
1717
expandedByDefault = attribute.Collapsible,
18+
hideIfChildrenInvisible = true,
1819
});
1920
}
2021
}

Editor/Elements/TriBoxGroupElement.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ public struct Props
2323
public string title;
2424
public TitleMode titleMode;
2525
public bool expandedByDefault;
26+
public bool hideIfChildrenInvisible;
2627
}
2728

28-
public TriBoxGroupElement(Props props = default)
29+
public TriBoxGroupElement(Props props = default) : base(new TriHeaderGroupBaseElement.Props
30+
{
31+
hideIfChildrenInvisible = props.hideIfChildrenInvisible,
32+
})
2933
{
3034
_props = props;
3135
_expanded = _props.expandedByDefault;

Editor/Elements/TriHeaderGroupBaseElement.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1-
using TriInspector.Utilities;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using TriInspector.Utilities;
25
using UnityEditor;
36
using UnityEngine;
47

58
namespace TriInspector.Elements
69
{
710
public abstract class TriHeaderGroupBaseElement : TriPropertyCollectionBaseElement
811
{
12+
private readonly Props _props;
913
private const float InsetTop = 4;
1014
private const float InsetBottom = 4;
1115
private const float InsetLeft = 18;
1216
private const float InsetRight = 4;
1317

18+
private readonly List<TriProperty> _properties = new List<TriProperty>();
19+
20+
private bool IsAnyPropertyVisible => _properties.Any(it => it.IsVisible);
21+
22+
[Serializable]
23+
public struct Props
24+
{
25+
public bool hideIfChildrenInvisible;
26+
}
27+
28+
protected TriHeaderGroupBaseElement(Props props = default)
29+
{
30+
_props = props;
31+
}
32+
33+
protected override void AddPropertyChild(TriElement element, TriProperty property)
34+
{
35+
_properties.Add(property);
36+
37+
base.AddPropertyChild(element, property);
38+
}
39+
1440
protected virtual float GetHeaderHeight(float width)
1541
{
1642
return 22;
@@ -32,6 +58,11 @@ protected virtual void DrawContent(Rect position)
3258

3359
public sealed override float GetHeight(float width)
3460
{
61+
if (_props.hideIfChildrenInvisible && !IsAnyPropertyVisible)
62+
{
63+
return -EditorGUIUtility.standardVerticalSpacing;
64+
}
65+
3566
var headerHeight = GetHeaderHeight(width);
3667
var contentHeight = GetContentHeight(width);
3768

@@ -47,6 +78,11 @@ public sealed override float GetHeight(float width)
4778

4879
public sealed override void OnGUI(Rect position)
4980
{
81+
if (_props.hideIfChildrenInvisible && !IsAnyPropertyVisible)
82+
{
83+
return;
84+
}
85+
5086
var headerHeight = GetHeaderHeight(position.width);
5187
var contentHeight = GetContentHeight(position.width);
5288

0 commit comments

Comments
 (0)