From bdc4127e3c8f5fcee72f6a96e0fc295b196c4d16 Mon Sep 17 00:00:00 2001 From: VladV Date: Sun, 30 Jan 2022 18:44:16 +0300 Subject: [PATCH] Add TriPropertyOverrideContext for override child properties from parent darwer --- Editor.Extras/Drawers/TableListDrawer.cs | 14 +++++- Editor/Elements/TriPropertyElement.cs | 5 +- Editor/TriProperty.cs | 5 ++ Editor/TriPropertyOverrideContext.cs | 59 +++++++++++++++++++++++ Editor/TriPropertyOverrideContext.cs.meta | 3 ++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 Editor/TriPropertyOverrideContext.cs create mode 100644 Editor/TriPropertyOverrideContext.cs.meta diff --git a/Editor.Extras/Drawers/TableListDrawer.cs b/Editor.Extras/Drawers/TableListDrawer.cs index e1808c2..0ead138 100644 --- a/Editor.Extras/Drawers/TableListDrawer.cs +++ b/Editor.Extras/Drawers/TableListDrawer.cs @@ -57,6 +57,7 @@ namespace TriInspector.Drawers private readonly TriElement _cellElementContainer; private readonly Dictionary _cellIndexByName; private readonly Dictionary _cellElements; + private readonly TableListPropertyOverrideContext _propertyOverrideContext; public TableMultiColumnTreeView(TriProperty property, TriElement container) : base(new TreeViewState(), BuildHeader(property)) @@ -67,6 +68,8 @@ namespace TriInspector.Drawers _cellIndexByName = new Dictionary(); _cellElements = new Dictionary(); + _propertyOverrideContext = new TableListPropertyOverrideContext(); + rowHeight = 20; showAlternatingRowBackgrounds = true; showBorder = true; @@ -127,7 +130,8 @@ namespace TriInspector.Drawers _cellElementContainer.AddChild(cellElement); } - using (TriGuiHelper.PushLabelWidth(1f)) // todo fix this + using (TriPropertyOverrideContext.Override(_propertyOverrideContext)) + //using (TriGuiHelper.PushLabelWidth(1f)) // todo fix this { _cellElements[cellValueProperty].OnGUI(cellRect); } @@ -180,5 +184,13 @@ namespace TriInspector.Drawers public TriProperty Property { get; } } + + private class TableListPropertyOverrideContext : TriPropertyOverrideContext + { + public override GUIContent GetDisplayName(TriProperty property) + { + return GUIContent.none; + } + } } } \ No newline at end of file diff --git a/Editor/Elements/TriPropertyElement.cs b/Editor/Elements/TriPropertyElement.cs index 9b01de3..dcab837 100644 --- a/Editor/Elements/TriPropertyElement.cs +++ b/Editor/Elements/TriPropertyElement.cs @@ -66,7 +66,10 @@ namespace TriInspector.Elements GUI.enabled &= _property.IsEnabled; EditorGUI.showMixedValue = _property.IsValueMixed; - base.OnGUI(position); + using (TriPropertyOverrideContext.BeginProperty()) + { + base.OnGUI(position); + } EditorGUI.showMixedValue = oldShowMixedValue; GUI.enabled = oldEnabled; diff --git a/Editor/TriProperty.cs b/Editor/TriProperty.cs index 0749521..746dc83 100644 --- a/Editor/TriProperty.cs +++ b/Editor/TriProperty.cs @@ -55,6 +55,11 @@ namespace TriInspector { get { + if (TriPropertyOverrideContext.Current != null) + { + return TriPropertyOverrideContext.Current.GetDisplayName(this); + } + if (_displayNameBackingField == null) { if (TryGetAttribute(out HideLabelAttribute _)) diff --git a/Editor/TriPropertyOverrideContext.cs b/Editor/TriPropertyOverrideContext.cs new file mode 100644 index 0000000..d578bfa --- /dev/null +++ b/Editor/TriPropertyOverrideContext.cs @@ -0,0 +1,59 @@ +using System; +using UnityEngine; + +namespace TriInspector +{ + public abstract class TriPropertyOverrideContext + { + private static TriPropertyOverrideContext Override { get; set; } + public static TriPropertyOverrideContext Current { get; private set; } + + public abstract GUIContent GetDisplayName(TriProperty property); + + public static EnterPropertyScope BeginProperty() + { + return new EnterPropertyScope().Init(); + } + + public static OverrideScope BeginOverride(TriPropertyOverrideContext overrideContext) + { + return new OverrideScope(overrideContext); + } + + public struct EnterPropertyScope : IDisposable + { + private TriPropertyOverrideContext _previousContext; + + public EnterPropertyScope Init() + { + _previousContext = Current; + Current = Override; + return this; + } + + public void Dispose() + { + Override = Current; + Current = _previousContext; + } + } + + public readonly struct OverrideScope : IDisposable + { + public OverrideScope(TriPropertyOverrideContext context) + { + if (Override != null) + { + Debug.LogError($"TriPropertyContext already overriden with {Override.GetType()}"); + } + + Override = context; + } + + public void Dispose() + { + Override = null; + } + } + } +} \ No newline at end of file diff --git a/Editor/TriPropertyOverrideContext.cs.meta b/Editor/TriPropertyOverrideContext.cs.meta new file mode 100644 index 0000000..a9c1fbd --- /dev/null +++ b/Editor/TriPropertyOverrideContext.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 009410b38db449a1a47369c87a015b0d +timeCreated: 1643555608 \ No newline at end of file