Add TabGroup

This commit is contained in:
VladV 2022-01-21 14:21:57 +03:00
parent 74fba9172b
commit 87f6eeac6e
13 changed files with 253 additions and 58 deletions

View File

@ -0,0 +1,16 @@
using TriInspector;
using TriInspector.Elements;
using TriInspector.GroupDrawers;
[assembly: RegisterTriGroupDrawer(typeof(TriTabGroupDrawer))]
namespace TriInspector.GroupDrawers
{
public class TriTabGroupDrawer : TriGroupDrawer<DeclareTabGroupAttribute>
{
public override TriPropertyCollectionBaseElement CreateElement(DeclareTabGroupAttribute attribute)
{
return new TriTabGroupElement();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2d4f714ffdae47c79edb698ab906daea
timeCreated: 1642758980

View File

@ -4,14 +4,8 @@ using UnityEngine;
namespace TriInspector.Elements
{
public class TriBoxGroupElement : TriPropertyCollectionBaseElement
public class TriBoxGroupElement : TriHeaderGroupBaseElement
{
private const float HeaderWidth = 22;
private const float InsetTop = 4;
private const float InsetBottom = 4;
private const float InsetLeft = 4;
private const float InsetRight = 4;
private readonly GUIContent _headerLabel;
public TriBoxGroupElement(DeclareBoxGroupAttribute attribute)
@ -21,58 +15,29 @@ namespace TriInspector.Elements
: new GUIContent(attribute.Title);
}
public override float GetHeight(float width)
protected override float GetHeaderHeight(float width)
{
var height = base.GetHeight(width) + InsetTop + InsetBottom;
if (_headerLabel != GUIContent.none)
if (string.IsNullOrEmpty(_headerLabel.text))
{
height += HeaderWidth;
return 0f;
}
return height;
return base.GetHeaderHeight(width);
}
public override void OnGUI(Rect position)
protected override void DrawHeader(Rect position)
{
var headerBgRect = new Rect(position)
TriEditorGUI.DrawBox(position, TriEditorStyles.HeaderBox);
var headerLabelRect = new Rect(position)
{
height = _headerLabel != GUIContent.none ? HeaderWidth : 0,
};
var headerLabelRect = new Rect(headerBgRect)
{
xMin = headerBgRect.xMin + 6,
xMax = headerBgRect.xMax - 6,
yMin = headerBgRect.yMin + 2,
yMax = headerBgRect.yMax - 2,
};
var contentBgRect = new Rect(position)
{
yMin = headerBgRect.yMax,
};
var contentRect = new Rect(contentBgRect)
{
xMin = contentBgRect.xMin + InsetLeft,
xMax = contentBgRect.xMax - InsetRight,
yMin = contentBgRect.yMin + InsetTop,
yMax = contentBgRect.yMax - InsetBottom,
xMin = position.xMin + 6,
xMax = position.xMax - 6,
yMin = position.yMin + 2,
yMax = position.yMax - 2,
};
if (_headerLabel != GUIContent.none)
{
TriEditorGUI.DrawBox(headerBgRect, TriEditorStyles.HeaderBox);
EditorGUI.LabelField(headerLabelRect, _headerLabel);
TriEditorGUI.DrawBox(contentBgRect, TriEditorStyles.ContentBox);
}
else
{
TriEditorGUI.DrawBox(contentBgRect, TriEditorStyles.Box);
}
using (TriGuiHelper.PushLabelWidth(EditorGUIUtility.labelWidth - InsetLeft))
{
base.OnGUI(contentRect);
}
EditorGUI.LabelField(headerLabelRect, _headerLabel);
}
}
}

View File

@ -0,0 +1,65 @@
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
namespace TriInspector.Elements
{
public abstract class TriHeaderGroupBaseElement : TriPropertyCollectionBaseElement
{
private const float InsetTop = 4;
private const float InsetBottom = 4;
private const float InsetLeft = 4;
private const float InsetRight = 4;
protected virtual float GetHeaderHeight(float width)
{
return 22;
}
protected virtual void DrawHeader(Rect position)
{
}
public sealed override float GetHeight(float width)
{
return base.GetHeight(width) + InsetTop + InsetBottom + GetHeaderHeight(width);
}
public sealed override void OnGUI(Rect position)
{
var headerHeight = GetHeaderHeight(position.width);
var headerBgRect = new Rect(position)
{
height = headerHeight,
};
var contentBgRect = new Rect(position)
{
yMin = headerBgRect.yMax,
};
var contentRect = new Rect(contentBgRect)
{
xMin = contentBgRect.xMin + InsetLeft,
xMax = contentBgRect.xMax - InsetRight,
yMin = contentBgRect.yMin + InsetTop,
yMax = contentBgRect.yMax - InsetBottom,
};
if (headerHeight > 0f)
{
DrawHeader(headerBgRect);
TriEditorGUI.DrawBox(contentBgRect, TriEditorStyles.ContentBox);
}
else
{
TriEditorGUI.DrawBox(contentBgRect, TriEditorStyles.Box);
}
using (TriGuiHelper.PushLabelWidth(EditorGUIUtility.labelWidth - InsetLeft))
{
base.OnGUI(contentRect);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7406688f5ef349a3ae0acee7ea9ec935
timeCreated: 1642760804

View File

@ -41,20 +41,20 @@ namespace TriInspector.Elements
var remaining = path.GetEnumerator();
if (remaining.MoveNext())
{
AddGroupedChild(propertyElement, remaining.Current, remaining.Current, remaining);
AddGroupedChild(propertyElement, property, remaining.Current, remaining.Current, remaining);
}
else
{
AddChild(propertyElement);
AddPropertyChild(propertyElement, property);
}
}
else
{
AddChild(propertyElement);
AddPropertyChild(propertyElement, property);
}
}
private void AddGroupedChild(TriElement child, string currentPath, string currentName,
private void AddGroupedChild(TriElement child, TriProperty property, string currentPath, string currentName,
IEnumerator<string> remainingPath)
{
if (_groups == null)
@ -62,22 +62,23 @@ namespace TriInspector.Elements
_groups = new Dictionary<string, TriPropertyCollectionBaseElement>();
}
var groupElement = CreateSubGroup(currentPath, currentName);
var groupElement = CreateSubGroup(property, currentPath, currentName);
if (remainingPath.MoveNext())
{
var nextPath = currentPath + "/" + remainingPath.Current;
var nextName = remainingPath.Current;
groupElement.AddGroupedChild(child, nextPath, nextName, remainingPath);
groupElement.AddGroupedChild(child, property, nextPath, nextName, remainingPath);
}
else
{
groupElement.AddChild(child);
groupElement.AddPropertyChild(child, property);
}
}
private TriPropertyCollectionBaseElement CreateSubGroup(string groupPath, string groupName)
private TriPropertyCollectionBaseElement CreateSubGroup(TriProperty property,
string groupPath, string groupName)
{
if (!_groups.TryGetValue(groupName, out var groupElement))
{
@ -97,12 +98,17 @@ namespace TriInspector.Elements
_groups.Add(groupName, groupElement);
AddChild(groupElement);
AddPropertyChild(groupElement, property);
}
return groupElement;
}
protected virtual void AddPropertyChild(TriElement element, TriProperty property)
{
AddChild(element);
}
private class DefaultGroupElement : TriPropertyCollectionBaseElement
{
}

View File

@ -0,0 +1,92 @@
using System.Collections.Generic;
using UnityEngine;
namespace TriInspector.Elements
{
public class TriTabGroupElement : TriHeaderGroupBaseElement
{
private const string DefaultTabName = "Main";
private readonly List<string> _tabNames;
private readonly Dictionary<string, TriElement> _tabElements;
private string _activeTabName;
public TriTabGroupElement()
{
_tabNames = new List<string>();
_tabElements = new Dictionary<string, TriElement>();
_activeTabName = null;
}
protected override void DrawHeader(Rect position)
{
if (_tabNames.Count == 0)
{
return;
}
var tabRect = new Rect(position)
{
width = position.width / _tabNames.Count,
};
if (_tabNames.Count == 1)
{
GUI.Toggle(tabRect, true, _tabNames[0], TriEditorStyles.TabOnlyOne);
}
else
{
for (int index = 0, tabCount = _tabNames.Count; index < tabCount; index++)
{
var tabName = _tabNames[index];
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)
{
SetActiveTab(tabName);
}
tabRect.x += tabRect.width;
}
}
}
protected override void AddPropertyChild(TriElement element, TriProperty property)
{
var tabName = DefaultTabName;
if (property.TryGetAttribute(out TabAttribute tab))
{
tabName = tab.TabName ?? tabName;
}
if (!_tabElements.TryGetValue(tabName, out var tabElement))
{
tabElement = new TriElement();
_tabElements[tabName] = tabElement;
_tabNames.Add(tabName);
if (_activeTabName == null)
{
SetActiveTab(tabName);
}
}
tabElement.AddChild(element);
}
private void SetActiveTab(string tabName)
{
_activeTabName = tabName;
RemoveAllChildren();
AddChild(_tabElements[_activeTabName]);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: deb5a57d2d0a4476a3e396a49b9f44da
timeCreated: 1642759023

View File

@ -11,6 +11,11 @@ namespace TriInspector
private static GUIStyle _contentBox;
private static GUIStyle _box;
public static GUIStyle TabOnlyOne { get; } = "Tab onlyOne";
public static GUIStyle TabFirst { get; } = "Tab first";
public static GUIStyle TabMiddle { get; } = "Tab middle";
public static GUIStyle TabLast { get; } = "Tab last";
public static GUIStyle HeaderBox
{
get

View File

@ -0,0 +1,14 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
[Conditional("UNITY_EDITOR")]
public class DeclareTabGroupAttribute : DeclareGroupBaseAttribute
{
public DeclareTabGroupAttribute(string path) : base(path)
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 951a8c6083fe40a69430e6f53d5ba7a0
timeCreated: 1642758819

View File

@ -0,0 +1,17 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class TabAttribute : Attribute
{
public TabAttribute(string tab)
{
TabName = tab;
}
public string TabName { get; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 19d2d001ee784250be738bb1a51a0fc6
timeCreated: 1642758885