Added ToggleGroup attribute (#100)

This commit is contained in:
HoSHIZA 2023-04-09 19:51:22 +03:00 committed by GitHub
parent 9bb0c1611c
commit e40ad952f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 157 additions and 9 deletions

View File

@ -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,
});
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cfcf76d363a541e39de3654d978a39ac
timeCreated: 1680888338

View File

@ -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;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f1bae35e64ea4dec9c6ec1818dc21b6a
timeCreated: 1680963694

View File

@ -13,6 +13,7 @@ namespace TriInspector.Elements
private ValueResolver<string> _headerResolver;
[CanBeNull] private TriProperty _firstProperty;
[CanBeNull] private TriProperty _toggleProperty;
private bool _expanded;
@ -40,6 +41,29 @@ namespace TriInspector.Elements
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);
}
@ -55,7 +79,8 @@ namespace TriInspector.Elements
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;
}
@ -77,14 +102,34 @@ namespace TriInspector.Elements
var headerContent = _headerResolver.GetValue(_firstProperty);
if (_props.titleMode == TitleMode.Foldout)
switch (_props.titleMode)
{
headerLabelRect.x += 10;
_expanded = EditorGUI.Foldout(headerLabelRect, _expanded, headerContent, true);
}
else
{
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;
}
}
@ -95,6 +140,16 @@ namespace TriInspector.Elements
return;
}
if (_props.titleMode == TitleMode.Toggle && !_props.expandedByDefault && !_expanded)
{
EditorGUI.BeginDisabledGroup(true);
base.DrawContent(position);
EditorGUI.EndDisabledGroup();
return;
}
base.DrawContent(position);
}
@ -103,6 +158,7 @@ namespace TriInspector.Elements
Normal,
Hidden,
Foldout,
Toggle,
}
}
}

View File

@ -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)

View File

@ -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;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f93204143e294161afc85d1c6a3c6d16
timeCreated: 1680887538