Tri-Inspector/Editor/Elements/TriPropertyElement.cs

140 lines
4.2 KiB
C#
Raw Normal View History

2021-12-07 18:20:36 +03:00
using TriInspector.Utilities;
2022-01-08 18:36:08 +03:00
using UnityEditor;
2021-12-07 18:20:36 +03:00
using UnityEngine;
namespace TriInspector.Elements
{
internal class TriPropertyElement : TriElement
{
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--)
{
2022-01-08 20:25:50 +03:00
if (_property.IsArray && (drawers[index].Target & TriTargetPropertyType.Array) == 0 ||
!_property.IsArray && (drawers[index].Target & TriTargetPropertyType.Self) == 0)
2021-12-07 18:20:36 +03:00
{
continue;
}
element = drawers[index].CreateElementInternal(property, element);
}
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 18:36:08 +03:00
var oldShowMixedValue = EditorGUI.showMixedValue;
2021-12-07 18:20:36 +03:00
var oldEnabled = GUI.enabled;
GUI.enabled &= _property.IsEnabled;
2022-01-08 18:36:08 +03:00
EditorGUI.showMixedValue = _property.IsValueMixed;
2021-12-07 18:20:36 +03:00
base.OnGUI(position);
2022-01-08 18:36:08 +03:00
EditorGUI.showMixedValue = oldShowMixedValue;
2021-12-07 18:20:36 +03:00
GUI.enabled = oldEnabled;
}
private static TriElement CreateElement(TriProperty property, bool inline = false)
{
var isSerializedProperty = property.TryGetSerializedProperty(out var serializedProperty);
if (!isSerializedProperty && property.PropertyTree.TargetObjects.Length > 1)
{
return new TriMultiEditNotSupportedElement(property);
}
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);
}
}
}