mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 08:18:49 -05:00
Added ToggleGroup attribute (#100)
This commit is contained in:
parent
9bb0c1611c
commit
e40ad952f4
21
Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs
Normal file
21
Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
3
Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs.meta
Normal file
3
Editor.Extras/GroupDrawers/TriToggleGroupDrawer.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfcf76d363a541e39de3654d978a39ac
|
||||
timeCreated: 1680888338
|
30
Editor.Samples/Groups/Groups_ToggleGroupSample.cs
Normal file
30
Editor.Samples/Groups/Groups_ToggleGroupSample.cs
Normal 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;
|
||||
}
|
||||
}
|
3
Editor.Samples/Groups/Groups_ToggleGroupSample.cs.meta
Normal file
3
Editor.Samples/Groups/Groups_ToggleGroupSample.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1bae35e64ea4dec9c6ec1818dc21b6a
|
||||
timeCreated: 1680963694
|
@ -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)
|
||||
{
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
14
README.md
14
README.md
@ -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)
|
||||
|
18
Runtime/Attributes/DeclareToggleGroupAttribute.cs
Normal file
18
Runtime/Attributes/DeclareToggleGroupAttribute.cs
Normal 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;
|
||||
}
|
||||
}
|
3
Runtime/Attributes/DeclareToggleGroupAttribute.cs.meta
Normal file
3
Runtime/Attributes/DeclareToggleGroupAttribute.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f93204143e294161afc85d1c6a3c6d16
|
||||
timeCreated: 1680887538
|
Loading…
Reference in New Issue
Block a user