Tri-Inspector/Editor/Elements/TriPropertyElement.cs

152 lines
4.5 KiB
C#
Raw Normal View History

2021-12-07 10:20:36 -05:00
using TriInspector.Utilities;
2022-01-08 10:36:08 -05:00
using UnityEditor;
2021-12-07 10:20:36 -05:00
using UnityEngine;
namespace TriInspector.Elements
{
2022-01-21 12:12:46 -05:00
public class TriPropertyElement : TriElement
2021-12-07 10:20:36 -05:00
{
private readonly TriProperty _property;
public TriPropertyElement(TriProperty property, bool inline = false)
{
_property = property;
var element = CreateElement(property, inline);
var drawers = property.AllDrawers;
for (var index = drawers.Count - 1; index >= 0; index--)
{
var drawer = drawers[index];
2022-01-20 05:06:04 -05:00
if (_property.IsArrayElement && !drawer.ApplyOnArrayElement ||
_property.IsArray && drawer.ApplyOnArrayElement)
2021-12-07 10:20:36 -05:00
{
continue;
}
var canDrawMessage = drawer.CanDraw(_property);
if (!string.IsNullOrEmpty(canDrawMessage))
{
AddChild(new TriInfoBoxElement(canDrawMessage, TriMessageType.Error));
continue;
}
element = drawer.CreateElementInternal(property, element);
2021-12-07 10:20:36 -05:00
}
2022-01-15 11:25:12 -05:00
if (property.HasValidators)
{
AddChild(new TriPropertyValidationResultElement(property));
}
2021-12-07 10:20:36 -05:00
AddChild(element);
}
public override float GetHeight(float width)
{
if (!_property.IsVisible)
{
return 0f;
}
return base.GetHeight(width);
}
public override void OnGUI(Rect position)
{
if (!_property.IsVisible)
{
return;
}
2022-01-08 10:36:08 -05:00
var oldShowMixedValue = EditorGUI.showMixedValue;
2021-12-07 10:20:36 -05:00
var oldEnabled = GUI.enabled;
GUI.enabled &= _property.IsEnabled;
2022-01-08 10:36:08 -05:00
EditorGUI.showMixedValue = _property.IsValueMixed;
using (TriPropertyOverrideContext.BeginProperty())
{
base.OnGUI(position);
}
2022-01-08 10:36:08 -05:00
EditorGUI.showMixedValue = oldShowMixedValue;
2021-12-07 10:20:36 -05:00
GUI.enabled = oldEnabled;
}
private static TriElement CreateElement(TriProperty property, bool inline = false)
{
var isSerializedProperty = property.TryGetSerializedProperty(out var serializedProperty);
var handler = isSerializedProperty
? ScriptAttributeUtilityProxy.GetHandler(serializedProperty)
: default(PropertyHandlerProxy?);
if (!handler.HasValue || !handler.Value.hasPropertyDrawer)
{
var propertyType = property.PropertyType;
switch (propertyType)
{
case TriPropertyType.Array:
{
return CreateArrayElement(property);
}
case TriPropertyType.Reference:
{
return CreateReferenceElement(property, inline);
}
case TriPropertyType.Generic:
{
return CreateGenericElement(property, inline);
}
}
}
if (isSerializedProperty)
{
return new TriBuiltInPropertyElement(property, serializedProperty, handler.Value);
}
return new TriNoDrawerElement(property);
}
private static TriElement CreateArrayElement(TriProperty property)
{
return new TriListElement(property);
}
private static TriElement CreateReferenceElement(TriProperty property, bool inline)
{
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);
}
private static TriElement CreateGenericElement(TriProperty property, bool inline)
{
if (inline)
{
return new TriInlineGenericElement(property);
}
if (property.TryGetAttribute(out InlinePropertyAttribute inlineAttribute))
{
return new TriInlineGenericElement(property, true, inlineAttribute.LabelWidth);
}
return new TriFoldoutElement(property);
}
}
}