Tri-Inspector/Editor.Extras/Drawers/InlineEditorDrawer.cs

86 lines
2.9 KiB
C#
Raw Normal View History

2022-01-08 12:45:11 -05:00
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
2023-04-05 13:32:32 -04:00
[assembly: RegisterTriAttributeDrawer(typeof(InlineEditorDrawer), TriDrawerOrder.Decorator,
2022-01-09 11:24:45 -05:00
ApplyOnArrayElement = true)]
2022-01-08 12:45:11 -05:00
namespace TriInspector.Drawers
{
public class InlineEditorDrawer : TriAttributeDrawer<InlineEditorAttribute>
{
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
2022-01-08 12:45:11 -05:00
{
2022-06-03 08:16:23 -04:00
if (!typeof(Object).IsAssignableFrom(propertyDefinition.FieldType))
2022-01-08 12:45:11 -05:00
{
return "[InlineEditor] valid only on Object fields";
2022-01-08 12:45:11 -05:00
}
return TriExtensionInitializationResult.Ok;
}
public override TriElement CreateElement(TriProperty property, TriElement next)
{
var element = new TriBoxGroupElement(new TriBoxGroupElement.Props
{
titleMode = TriBoxGroupElement.TitleMode.Hidden,
});
2022-01-08 12:45:11 -05:00
element.AddChild(new ObjectReferenceFoldoutDrawerElement(property));
2024-04-06 05:38:17 -04:00
element.AddChild(new InlineEditorElement(property, new InlineEditorElement.Props
{
mode = Attribute.Mode,
previewHeight = Attribute.PreviewHeight,
previewWidth = Attribute.PreviewWidth,
}));
2022-01-08 12:45:11 -05:00
return element;
}
private class ObjectReferenceFoldoutDrawerElement : TriElement
{
private readonly TriProperty _property;
public ObjectReferenceFoldoutDrawerElement(TriProperty property)
{
_property = property;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var prefixRect = new Rect(position)
{
height = EditorGUIUtility.singleLineHeight,
xMax = position.xMin + EditorGUIUtility.labelWidth,
};
var pickerRect = new Rect(position)
{
height = EditorGUIUtility.singleLineHeight,
xMin = prefixRect.xMax,
};
2022-08-28 03:42:45 -04:00
TriEditorGUI.Foldout(prefixRect, _property);
2022-01-08 12:45:11 -05:00
EditorGUI.BeginChangeCheck();
2022-12-07 02:38:25 -05:00
var allowSceneObjects = _property.PropertyTree.TargetIsPersistent == false;
2022-01-08 12:45:11 -05:00
var value = (Object) _property.Value;
value = EditorGUI.ObjectField(pickerRect, GUIContent.none, value,
_property.FieldType, allowSceneObjects);
if (EditorGUI.EndChangeCheck())
{
_property.SetValue(value);
}
}
}
}
}