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

45 lines
1.4 KiB
C#
Raw Normal View History

2022-01-07 12:21:36 -05:00
using UnityEditor;
using UnityEngine;
namespace TriInspector.Drawers
{
public abstract class BuiltinDrawerBase<T> : TriValueDrawer<T>
{
public sealed override TriElement CreateElement(TriValue<T> propertyValue, TriElement next)
{
if (propertyValue.Property.TryGetSerializedProperty(out _))
{
return next;
}
return base.CreateElement(propertyValue, next);
}
public virtual int CompactModeLines => 1;
public virtual int WideModeLines => 1;
2022-01-07 12:21:36 -05:00
public sealed override float GetHeight(float width, TriValue<T> propertyValue, TriElement next)
{
var lineHeight = EditorGUIUtility.singleLineHeight;
var spacing = EditorGUIUtility.standardVerticalSpacing;
var lines = EditorGUIUtility.wideMode ? WideModeLines : CompactModeLines;
return lineHeight * lines + spacing * (lines - 1);
2022-01-07 12:21:36 -05:00
}
public sealed override void OnGUI(Rect position, TriValue<T> propertyValue, TriElement next)
{
2022-05-11 04:23:11 -04:00
var value = propertyValue.SmartValue;
2022-01-07 12:21:36 -05:00
EditorGUI.BeginChangeCheck();
value = OnValueGUI(position, propertyValue.Property.DisplayNameContent, value);
if (EditorGUI.EndChangeCheck())
{
2022-05-11 04:23:11 -04:00
propertyValue.SmartValue = value;
2022-01-07 12:21:36 -05:00
}
}
protected abstract T OnValueGUI(Rect position, GUIContent label, T value);
}
}