mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add dynamic titles for groups
This commit is contained in:
parent
38bc03651e
commit
2eb9e30e0e
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using TriInspector.Resolvers;
|
||||
using TriInspector.Utilities;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
@ -8,7 +10,11 @@ namespace TriInspector.Elements
|
||||
public class TriBoxGroupElement : TriHeaderGroupBaseElement
|
||||
{
|
||||
private readonly Props _props;
|
||||
private readonly GUIContent _headerLabel;
|
||||
private readonly string _headerText;
|
||||
|
||||
[CanBeNull] private ValueResolver<string> _headerResolver;
|
||||
[CanBeNull] private TriProperty _firstProperty;
|
||||
|
||||
private bool _expanded;
|
||||
|
||||
[Serializable]
|
||||
@ -21,13 +27,28 @@ namespace TriInspector.Elements
|
||||
public TriBoxGroupElement(string title, Props props = default)
|
||||
{
|
||||
_props = props;
|
||||
_headerLabel = new GUIContent(title ?? "");
|
||||
_headerText = title;
|
||||
_expanded = _props.expandedByDefault;
|
||||
}
|
||||
|
||||
protected override void AddPropertyChild(TriElement element, TriProperty property)
|
||||
{
|
||||
_firstProperty = property;
|
||||
_headerResolver = string.IsNullOrEmpty(_headerText)
|
||||
? null
|
||||
: ValueResolver.ResolveString(property.Definition, _headerText);
|
||||
|
||||
if (_headerResolver != null && _headerResolver.TryGetErrorString(out var error))
|
||||
{
|
||||
AddChild(new TriInfoBoxElement(error, TriMessageType.Error));
|
||||
}
|
||||
|
||||
base.AddPropertyChild(element, property);
|
||||
}
|
||||
|
||||
protected override float GetHeaderHeight(float width)
|
||||
{
|
||||
if (!_props.foldout && string.IsNullOrEmpty(_headerLabel.text))
|
||||
if (!_props.foldout && _headerResolver == null)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
@ -57,14 +78,16 @@ namespace TriInspector.Elements
|
||||
yMax = position.yMax - 2,
|
||||
};
|
||||
|
||||
var headerContent = _headerResolver?.GetValue(_firstProperty, _headerText);
|
||||
|
||||
if (_props.foldout)
|
||||
{
|
||||
headerLabelRect.x += 10;
|
||||
_expanded = EditorGUI.Foldout(headerLabelRect, _expanded, _headerLabel, true);
|
||||
_expanded = EditorGUI.Foldout(headerLabelRect, _expanded, headerContent, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.LabelField(headerLabelRect, _headerLabel);
|
||||
EditorGUI.LabelField(headerLabelRect, headerContent);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using TriInspector.Resolvers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Elements
|
||||
@ -7,47 +8,57 @@ namespace TriInspector.Elements
|
||||
{
|
||||
private const string DefaultTabName = "Main";
|
||||
|
||||
private readonly List<string> _tabNames;
|
||||
private readonly List<TabInfo> _tabs;
|
||||
private readonly Dictionary<string, TriElement> _tabElements;
|
||||
|
||||
private string _activeTabName;
|
||||
|
||||
private struct TabInfo
|
||||
{
|
||||
public string name;
|
||||
public ValueResolver<string> titleResolver;
|
||||
public TriProperty property;
|
||||
}
|
||||
|
||||
public TriTabGroupElement()
|
||||
{
|
||||
_tabNames = new List<string>();
|
||||
_tabs = new List<TabInfo>();
|
||||
_tabElements = new Dictionary<string, TriElement>();
|
||||
_activeTabName = null;
|
||||
}
|
||||
|
||||
protected override void DrawHeader(Rect position)
|
||||
{
|
||||
if (_tabNames.Count == 0)
|
||||
if (_tabs.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var tabRect = new Rect(position)
|
||||
{
|
||||
width = position.width / _tabNames.Count,
|
||||
width = position.width / _tabs.Count,
|
||||
};
|
||||
|
||||
if (_tabNames.Count == 1)
|
||||
if (_tabs.Count == 1)
|
||||
{
|
||||
GUI.Toggle(tabRect, true, _tabNames[0], TriEditorStyles.TabOnlyOne);
|
||||
var tab = _tabs[0];
|
||||
var content = tab.titleResolver.GetValue(tab.property);
|
||||
GUI.Toggle(tabRect, true, content, TriEditorStyles.TabOnlyOne);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int index = 0, tabCount = _tabNames.Count; index < tabCount; index++)
|
||||
for (int index = 0, tabCount = _tabs.Count; index < tabCount; index++)
|
||||
{
|
||||
var tabName = _tabNames[index];
|
||||
var tab = _tabs[index];
|
||||
var content = tab.titleResolver.GetValue(tab.property);
|
||||
var tabStyle = index == 0 ? TriEditorStyles.TabFirst
|
||||
: index == tabCount - 1 ? TriEditorStyles.TabLast
|
||||
: TriEditorStyles.TabMiddle;
|
||||
|
||||
var isTabActive = GUI.Toggle(tabRect, _activeTabName == tabName, tabName, tabStyle);
|
||||
if (isTabActive && _activeTabName != tabName)
|
||||
var isTabActive = GUI.Toggle(tabRect, _activeTabName == tab.name, content, tabStyle);
|
||||
if (isTabActive && _activeTabName != tab.name)
|
||||
{
|
||||
SetActiveTab(tabName);
|
||||
SetActiveTab(tab.name);
|
||||
}
|
||||
|
||||
tabRect.x += tabRect.width;
|
||||
@ -68,8 +79,20 @@ namespace TriInspector.Elements
|
||||
{
|
||||
tabElement = new TriElement();
|
||||
|
||||
var info = new TabInfo
|
||||
{
|
||||
name = tabName,
|
||||
titleResolver = ValueResolver.ResolveString(property.Definition, tabName),
|
||||
property = property,
|
||||
};
|
||||
|
||||
_tabElements[tabName] = tabElement;
|
||||
_tabNames.Add(tabName);
|
||||
_tabs.Add(info);
|
||||
|
||||
if (info.titleResolver.TryGetErrorString(out var error))
|
||||
{
|
||||
tabElement.AddChild(new TriInfoBoxElement(error, TriMessageType.Error));
|
||||
}
|
||||
|
||||
if (_activeTabName == null)
|
||||
{
|
||||
|
@ -70,6 +70,8 @@ namespace TriInspector
|
||||
PropertyType = GetPropertyType(this);
|
||||
}
|
||||
|
||||
internal TriPropertyDefinition Definition => _definition;
|
||||
|
||||
[PublicAPI]
|
||||
public TriPropertyType PropertyType { get; }
|
||||
|
||||
|
@ -616,11 +616,13 @@ public class BoxGroupSample : ScriptableObject
|
||||
![FoldoutGroup](https://user-images.githubusercontent.com/26966368/201517886-4138ee55-33c2-4a1a-93bc-a3cda7745a4c.png)
|
||||
|
||||
```csharp
|
||||
[DeclareFoldoutGroup("foldout", Title = "My Foldout")]
|
||||
[DeclareFoldoutGroup("foldout", Title = "$" + nameof(DynamicTitle))]
|
||||
public class FoldoutGroupSample : ScriptableObject
|
||||
{
|
||||
[Group("foldout")] public int a;
|
||||
[Group("foldout")] public bool b;
|
||||
|
||||
public string DynamicTitle => "My Foldout";
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user