Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using TriInspector;
using TriInspector.Elements;
using TriInspector.GroupDrawers;

[assembly: RegisterTriGroupDrawer(typeof(TriToggleGroupDrawer))]

namespace TriInspector.GroupDrawers
{
public class TriToggleGroupDrawer : TriGroupDrawer<DeclareToggleGroupAttribute>
{
public override TriPropertyCollectionBaseElement CreateElement(DeclareToggleGroupAttribute attribute)
{
return new TriBoxGroupElement(new TriBoxGroupElement.Props
{
title = attribute.Title,
titleMode = TriBoxGroupElement.TitleMode.Toggle,
expandedByDefault = attribute.Collapsible,
});
}
}
}
3 changes: 3 additions & 0 deletions Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Editor.Samples/Groups/Groups_ToggleGroupSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using TriInspector;
using UnityEngine;

[DeclareToggleGroup("My Toggle")]
[DeclareToggleGroup("My Non Collapsible Toggle", Collapsible = false)]
[DeclareToggleGroup("boxed_toggle_struct", Title = "Toggle Struct")]
public class Groups_ToggleGroupSample : ScriptableObject
{
[Group("My Toggle")] public bool aEnabled = true;
[Group("My Toggle")] public string b;
[Group("My Toggle")] public bool c;

[Group("My Non Collapsible Toggle")] public bool dEnabled;
[Group("My Non Collapsible Toggle")] public bool e;
[Group("My Non Collapsible Toggle")] public Vector3 f;

[Group("boxed_toggle_struct"), InlineProperty, HideLabel]
public MyStruct boxedStruct;

public MyStruct defaultStruct;

[Serializable]
public struct MyStruct
{
public bool enabled;
public int a;
public float b;
}
}
3 changes: 3 additions & 0 deletions Editor.Samples/Groups/Groups_ToggleGroupSample.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 66 additions & 10 deletions Editor/Elements/TriBoxGroupElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TriBoxGroupElement : TriHeaderGroupBaseElement

private ValueResolver<string> _headerResolver;
[CanBeNull] private TriProperty _firstProperty;
[CanBeNull] private TriProperty _toggleProperty;

private bool _expanded;

Expand All @@ -34,12 +35,35 @@ protected override void AddPropertyChild(TriElement element, TriProperty propert
{
_firstProperty = property;
_headerResolver = ValueResolver.ResolveString(property.Definition, _props.title ?? "");

if (_headerResolver.TryGetErrorString(out var error))
{
AddChild(new TriInfoBoxElement(error, TriMessageType.Error));
}


if (_props.titleMode == TitleMode.Toggle)
{
if (_toggleProperty == null)
{
if (property.ValueType == typeof(bool))
{
_toggleProperty = property;

return;
}

if (property.ChildrenProperties?.Count > 0)
{
var childrenProperty = property.ChildrenProperties[0];

if (childrenProperty.ValueType == typeof(bool))
{
_toggleProperty = childrenProperty;
}
}
}
}

base.AddPropertyChild(element, property);
}

Expand All @@ -55,7 +79,8 @@ protected override float GetHeaderHeight(float width)

protected override float GetContentHeight(float width)
{
if (_props.titleMode == TitleMode.Foldout && !_expanded)
if (((_props.titleMode == TitleMode.Toggle && _props.expandedByDefault) ||
_props.titleMode == TitleMode.Foldout) && !_expanded)
{
return 0f;
}
Expand All @@ -77,14 +102,34 @@ protected override void DrawHeader(Rect position)

var headerContent = _headerResolver.GetValue(_firstProperty);

if (_props.titleMode == TitleMode.Foldout)
{
headerLabelRect.x += 10;
_expanded = EditorGUI.Foldout(headerLabelRect, _expanded, headerContent, true);
}
else
switch (_props.titleMode)
{
EditorGUI.LabelField(headerLabelRect, headerContent);
case TitleMode.Foldout:
headerLabelRect.x += 10;
_expanded = EditorGUI.Foldout(headerLabelRect, _expanded, headerContent, true);
break;
case TitleMode.Toggle:
{
if (_toggleProperty?.Value is bool cachedValue)
{
var newValue = EditorGUI.ToggleLeft(headerLabelRect, headerContent, cachedValue);

if (newValue != cachedValue)
{
_toggleProperty.SetValue(newValue);
}

_expanded = newValue;
}
else
{
EditorGUI.LabelField(headerLabelRect, $"The first property in the group must be of bool.");
}
break;
}
default:
EditorGUI.LabelField(headerLabelRect, headerContent);
break;
}
}

Expand All @@ -95,6 +140,16 @@ protected override void DrawContent(Rect position)
return;
}

if (_props.titleMode == TitleMode.Toggle && !_props.expandedByDefault && !_expanded)
{
EditorGUI.BeginDisabledGroup(true);
base.DrawContent(position);
EditorGUI.EndDisabledGroup();

return;

}

base.DrawContent(position);
}

Expand All @@ -103,6 +158,7 @@ public enum TitleMode
Normal,
Hidden,
Foldout,
Toggle,
}
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ public class FoldoutGroupSample : ScriptableObject
}
```

#### Toggle Group

```csharp
[DeclareToggleGroup("toggle", Title = "$" + nameof(DynamicTitle))]
public class ToggleGroupSample : ScriptableObject
{
[Group("toggle")] public bool enabled;
[Group("toggle")] public int a;
[Group("toggle")] public bool b;

public string DynamicTitle => "My Toggle";
}
```

#### Tab Group

![TabGroup](https://user-images.githubusercontent.com/26966368/177552003-528a4e52-e340-460b-93e6-f56c07ac063b.png)
Expand Down
18 changes: 18 additions & 0 deletions Runtime/Attributes/DeclareToggleGroupAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Diagnostics;

namespace TriInspector
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
[Conditional("UNITY_EDITOR")]
public class DeclareToggleGroupAttribute : DeclareGroupBaseAttribute
{
public DeclareToggleGroupAttribute(string path) : base(path)
{
Title = path;
}

public string Title { get; set; }
public bool Collapsible { get; set; } = true;
}
}
3 changes: 3 additions & 0 deletions Runtime/Attributes/DeclareToggleGroupAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.