modular-avatar/Editor/Inspector/AvatarObjectReferenceDrawer.cs
kaikoga ae7103cf82
chore: use ndmf Avatar Root api where applicable (#482)
* expose ndmf AvatarRoot APIs through ma.RuntimeUtil

* refactor: prefer RuntimeUtil helpers for checking avatar root

* refactor: ndmf.BuildContext represents Avatar (log)

* refactor: ndmf.BuildContext represents Avatar (WorldFixed)

* refactor: ndmf.BuildContext represents Avatar (FirstPersonVisible)

* refactor: ndmf.BuildContext represents Avatar (BlendShapeSync)

* refactor: prefer FindAvatarTransformInParents() (ErrorReportUI)

* refactor: prefer FindAvatarTransformInParents() (Runtime)

* refactor: prefer FindAvatarTransformInParents() (Editor)

* delegate more ndmf AvatarRoot APIs through ma.RuntimeUtil
2023-10-15 18:44:53 +09:00

150 lines
6.0 KiB
C#

using UnityEditor;
using UnityEngine;
namespace nadena.dev.modular_avatar.core.editor
{
[CustomPropertyDrawer(typeof(AvatarObjectReference))]
internal class AvatarObjectReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (CustomGUI(position, property, label)) return;
var xButtonSize = EditorStyles.miniButtonRight.CalcSize(new GUIContent("x"));
var xButtonRect = new Rect(position.xMax - xButtonSize.x, position.y, xButtonSize.x, position.height);
position = new Rect(position.x, position.y, position.width - xButtonSize.x, position.height);
property = property.FindPropertyRelative(nameof(AvatarObjectReference.referencePath));
position = EditorGUI.PrefixLabel(position, label);
using (var scope = new ZeroIndentScope())
{
EditorGUI.LabelField(position,
string.IsNullOrEmpty(property.stringValue) ? "(null)" : property.stringValue);
}
}
private bool CustomGUI(Rect position, SerializedProperty property, GUIContent label)
{
var color = GUI.contentColor;
property = property.FindPropertyRelative(nameof(AvatarObjectReference.referencePath));
try
{
var avatarTransform = findContainingAvatarTransform(property);
if (avatarTransform == null) return false;
bool isRoot = property.stringValue == AvatarObjectReference.AVATAR_ROOT;
bool isNull = string.IsNullOrEmpty(property.stringValue);
Transform target;
if (isNull) target = null;
else if (isRoot) target = avatarTransform;
else target = avatarTransform.Find(property.stringValue);
var labelRect = position;
position = EditorGUI.PrefixLabel(position, label);
labelRect.width = position.x - labelRect.x;
var nullContent = GUIContent.none;
using (var scope = new ZeroIndentScope())
{
if (target != null || isNull)
{
EditorGUI.BeginChangeCheck();
var newTarget = EditorGUI.ObjectField(position, nullContent, target, typeof(Transform), true);
if (EditorGUI.EndChangeCheck())
{
if (newTarget == null)
{
property.stringValue = "";
}
else if (newTarget == avatarTransform)
{
property.stringValue = AvatarObjectReference.AVATAR_ROOT;
}
else
{
var relPath =
RuntimeUtil.RelativePath(avatarTransform.gameObject, ((Transform) newTarget).gameObject);
if (relPath == null) return true;
property.stringValue = relPath;
}
}
}
else
{
// For some reason, this color change retroactively affects the prefix label above, so draw our own
// label as well (we still want the prefix label for highlights, etc).
EditorGUI.LabelField(labelRect, label);
GUI.contentColor = new Color(0, 0, 0, 0);
EditorGUI.BeginChangeCheck();
var newTarget = EditorGUI.ObjectField(position, nullContent, target, typeof(Transform), true);
GUI.contentColor = color;
if (EditorGUI.EndChangeCheck())
{
if (newTarget == null)
{
property.stringValue = "";
}
else if (newTarget == avatarTransform)
{
property.stringValue = AvatarObjectReference.AVATAR_ROOT;
}
else
{
var relPath =
RuntimeUtil.RelativePath(avatarTransform.gameObject, ((Transform) newTarget).gameObject);
if (relPath == null) return true;
property.stringValue = relPath;
}
}
else
{
GUI.contentColor = Color.red;
EditorGUI.LabelField(position, property.stringValue);
}
}
return true;
}
}
finally
{
GUI.contentColor = color;
}
}
private static Transform findContainingAvatarTransform(SerializedProperty property)
{
// Find containing object, and from that the avatar
if (property.serializedObject == null) return null;
Transform commonAvatar = null;
var targets = property.serializedObject.targetObjects;
for (int i = 0; i < targets.Length; i++)
{
var obj = targets[i] as Component;
if (obj == null) return null;
var transform = obj.transform;
var avatar = RuntimeUtil.FindAvatarTransformInParents(transform);
if (i == 0)
{
if (avatar == null) return null;
commonAvatar = avatar;
}
else if (commonAvatar != avatar) return null;
}
return commonAvatar;
}
}
}