Fix bug due to which InlineProperty attribute was ignored in lists

This commit is contained in:
VladV 2022-05-04 10:15:08 +03:00
parent aa9df5a6b6
commit f2dd8848ba
5 changed files with 87 additions and 46 deletions

View File

@ -125,7 +125,10 @@ namespace TriInspector.Drawers
if (!_cellElements.ContainsKey(cellValueProperty))
{
var cellElement = new TriPropertyElement(cellValueProperty, true);
var cellElement = new TriPropertyElement(cellValueProperty, new TriPropertyElement.Props
{
forceInline = true,
});
_cellElements.Add(cellValueProperty, cellElement);
_cellElementContainer.AddChild(cellElement);
}

View File

@ -1,4 +1,5 @@
using TriInspector.Utilities;
using System;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
@ -6,16 +7,20 @@ namespace TriInspector.Elements
{
internal class TriInlineGenericElement : TriPropertyCollectionBaseElement
{
private readonly bool _drawPrefixLabel;
private readonly float _labelWidth;
private readonly Props _props;
private readonly TriProperty _property;
public TriInlineGenericElement(TriProperty property,
bool drawPrefixLabel = false, float labelWidth = 0f)
[Serializable]
public struct Props
{
public bool drawPrefixLabel;
public float labelWidth;
}
public TriInlineGenericElement(TriProperty property, Props props = default)
{
_property = property;
_drawPrefixLabel = drawPrefixLabel;
_labelWidth = labelWidth;
_props = props;
DeclareGroups(property.ValueType);
@ -27,13 +32,13 @@ namespace TriInspector.Elements
public override void OnGUI(Rect position)
{
if (_drawPrefixLabel)
if (_props.drawPrefixLabel)
{
var controlId = GUIUtility.GetControlID(FocusType.Passive);
position = EditorGUI.PrefixLabel(position, controlId, _property.DisplayNameContent);
}
using (TriGuiHelper.PushLabelWidth(_labelWidth))
using (TriGuiHelper.PushLabelWidth(_props.labelWidth))
{
base.OnGUI(position);
}

View File

@ -170,7 +170,10 @@ namespace TriInspector.Elements
while (ChildrenCount < count)
{
var property = _property.ArrayElementProperties[ChildrenCount];
AddChild(new TriPropertyElement(property, true));
AddChild(new TriPropertyElement(property, new TriPropertyElement.Props
{
forceInline = true,
}));
}
while (ChildrenCount > count)

View File

@ -1,4 +1,5 @@
using TriInspector.Utilities;
using System;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
@ -8,11 +9,17 @@ namespace TriInspector.Elements
{
private readonly TriProperty _property;
public TriPropertyElement(TriProperty property, bool inline = false)
[Serializable]
public struct Props
{
public bool forceInline;
}
public TriPropertyElement(TriProperty property, Props props = default)
{
_property = property;
var element = CreateElement(property, inline);
var element = CreateElement(property, props);
var drawers = property.AllDrawers;
for (var index = drawers.Count - 1; index >= 0; index--)
@ -75,7 +82,7 @@ namespace TriInspector.Elements
GUI.enabled = oldEnabled;
}
private static TriElement CreateElement(TriProperty property, bool inline = false)
private static TriElement CreateElement(TriProperty property, Props props)
{
var isSerializedProperty = property.TryGetSerializedProperty(out var serializedProperty);
@ -96,12 +103,12 @@ namespace TriInspector.Elements
case TriPropertyType.Reference:
{
return CreateReferenceElement(property, inline);
return CreateReferenceElement(property, props);
}
case TriPropertyType.Generic:
{
return CreateGenericElement(property, inline);
return CreateGenericElement(property, props);
}
}
}
@ -119,31 +126,51 @@ namespace TriInspector.Elements
return new TriListElement(property);
}
private static TriElement CreateReferenceElement(TriProperty property, bool inline)
private static TriElement CreateReferenceElement(TriProperty property, Props props)
{
if (inline)
{
return new TriReferenceElement(property, true);
}
if (property.TryGetAttribute(out InlinePropertyAttribute inlineAttribute))
{
return new TriReferenceElement(property, true, true, inlineAttribute.LabelWidth);
return new TriReferenceElement(property, new TriReferenceElement.Props
{
inline = true,
drawPrefixLabel = !props.forceInline,
labelWidth = inlineAttribute.LabelWidth,
});
}
return new TriReferenceElement(property);
if (props.forceInline)
{
return new TriReferenceElement(property, new TriReferenceElement.Props
{
inline = true,
drawPrefixLabel = false,
});
}
return new TriReferenceElement(property, new TriReferenceElement.Props
{
inline = false,
drawPrefixLabel = false,
});
}
private static TriElement CreateGenericElement(TriProperty property, bool inline)
private static TriElement CreateGenericElement(TriProperty property, Props props)
{
if (inline)
{
return new TriInlineGenericElement(property);
}
if (property.TryGetAttribute(out InlinePropertyAttribute inlineAttribute))
{
return new TriInlineGenericElement(property, true, inlineAttribute.LabelWidth);
return new TriInlineGenericElement(property, new TriInlineGenericElement.Props
{
drawPrefixLabel = !props.forceInline,
labelWidth = inlineAttribute.LabelWidth,
});
}
if (props.forceInline)
{
return new TriInlineGenericElement(property, new TriInlineGenericElement.Props
{
drawPrefixLabel = false,
});
}
return new TriFoldoutElement(property);

View File

@ -7,20 +7,23 @@ namespace TriInspector.Elements
{
internal class TriReferenceElement : TriPropertyCollectionBaseElement
{
private readonly bool _drawPrefixLabel;
private readonly bool _inline;
private readonly float _labelWidth;
private readonly Props _props;
private readonly TriProperty _property;
private Type _referenceType;
public TriReferenceElement(TriProperty property,
bool inline = false, bool drawPrefixLabel = false, float labelWidth = 0f)
[Serializable]
public struct Props
{
public bool inline;
public bool drawPrefixLabel;
public float labelWidth;
}
public TriReferenceElement(TriProperty property, Props props = default)
{
_property = property;
_inline = inline;
_drawPrefixLabel = drawPrefixLabel;
_labelWidth = labelWidth;
_props = props;
DeclareGroups(property.ValueType);
}
@ -29,7 +32,7 @@ namespace TriInspector.Elements
{
var dirty = false;
if (_inline || _property.IsExpanded)
if (_props.inline || _property.IsExpanded)
{
dirty |= GenerateChildren();
}
@ -47,7 +50,7 @@ namespace TriInspector.Elements
{
var height = EditorGUIUtility.singleLineHeight;
if (_inline || _property.IsExpanded)
if (_props.inline || _property.IsExpanded)
{
height += base.GetHeight(width);
}
@ -57,7 +60,7 @@ namespace TriInspector.Elements
public override void OnGUI(Rect position)
{
if (_drawPrefixLabel)
if (_props.drawPrefixLabel)
{
var controlId = GUIUtility.GetControlID(FocusType.Passive);
position = EditorGUI.PrefixLabel(position, controlId, _property.DisplayNameContent);
@ -82,11 +85,11 @@ namespace TriInspector.Elements
yMin = position.yMin + headerRect.height,
};
if (_inline)
if (_props.inline)
{
TriManagedReferenceGui.DrawTypeSelector(headerRect, _property);
using (TriGuiHelper.PushLabelWidth(_labelWidth))
using (TriGuiHelper.PushLabelWidth(_props.labelWidth))
{
base.OnGUI(contentRect);
}
@ -99,7 +102,7 @@ namespace TriInspector.Elements
if (_property.IsExpanded)
{
using (TriGuiHelper.PushIndentLevel())
using (TriGuiHelper.PushLabelWidth(_labelWidth))
using (TriGuiHelper.PushLabelWidth(_props.labelWidth))
{
base.OnGUI(contentRect);
}