modular-avatar/Packages/net.fushizen.modular-avatar/Editor/Inspector/MenuInstallerEditor.cs

156 lines
5.3 KiB
C#
Raw Normal View History

using System;
2022-10-17 03:47:51 +08:00
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
2022-10-17 03:47:51 +08:00
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Avatars.ScriptableObjects;
2022-10-17 05:05:26 +08:00
using static net.fushizen.modular_avatar.core.editor.Localization;
namespace net.fushizen.modular_avatar.core.editor
{
[CustomEditor(typeof(ModularAvatarMenuInstaller))]
[CanEditMultipleObjects]
public class MenuInstallerEditor : Editor
{
private ModularAvatarMenuInstaller _installer;
private Editor _innerMenuEditor;
private VRCExpressionsMenu _menuToAppend;
private bool _menuFoldout;
private bool _devFoldout;
2022-10-17 03:47:51 +08:00
private HashSet<VRCExpressionsMenu> _avatarMenus;
private void OnEnable()
{
_installer = (ModularAvatarMenuInstaller) target;
2022-10-17 03:47:51 +08:00
FindMenus();
}
private void SetupMenuEditor()
{
if (targets.Length != 1)
{
_innerMenuEditor = null;
_menuToAppend = null;
}
else if (_installer.menuToAppend != _menuToAppend)
{
if (_installer.menuToAppend == null) _innerMenuEditor = null;
else
{
_innerMenuEditor = CreateEditor(_installer.menuToAppend);
}
_menuToAppend = _installer.menuToAppend;
}
}
public override void OnInspectorGUI()
{
2022-10-23 04:31:10 +08:00
LogoDisplay.DisplayLogo();
InspectorCommon.DisplayOutOfAvatarWarning(targets);
2022-10-23 04:31:10 +08:00
SetupMenuEditor();
var installTo = serializedObject.FindProperty(nameof(ModularAvatarMenuInstaller.installTargetMenu));
2022-10-17 03:47:51 +08:00
var isEnabled = targets.Length != 1 || ((ModularAvatarMenuInstaller) target).enabled;
2022-10-17 03:47:51 +08:00
if (!installTo.hasMultipleDifferentValues)
{
if (installTo.objectReferenceValue == null)
{
if (isEnabled)
{
EditorGUILayout.HelpBox(S("menuinstall.help.hint_set_menu"), MessageType.Info);
}
2022-10-17 03:47:51 +08:00
}
else if (!IsMenuReachable(RuntimeUtil.FindAvatarInParents(((Component) target).transform),
(VRCExpressionsMenu) installTo.objectReferenceValue))
{
2022-10-17 05:05:26 +08:00
EditorGUILayout.HelpBox(S("menuinstall.help.hint_bad_menu"), MessageType.Error);
2022-10-17 03:47:51 +08:00
}
}
2022-10-17 05:05:26 +08:00
EditorGUILayout.PropertyField(installTo, G("menuinstall.installto"));
var avatar = RuntimeUtil.FindAvatarInParents(_installer.transform);
2022-10-17 05:05:26 +08:00
if (avatar != null && GUILayout.Button(G("menuinstall.selectmenu")))
{
AvMenuTreeViewWindow.Show(avatar, menu =>
{
installTo.objectReferenceValue = menu;
serializedObject.ApplyModifiedProperties();
});
}
if (targets.Length == 1)
{
2022-10-17 05:05:26 +08:00
_menuFoldout = EditorGUILayout.Foldout(_menuFoldout, G("menuinstall.showcontents"));
if (_menuFoldout)
{
EditorGUI.indentLevel++;
using (var disabled = new EditorGUI.DisabledScope(true))
{
if (_innerMenuEditor != null) _innerMenuEditor.OnInspectorGUI();
2022-10-17 05:05:26 +08:00
else EditorGUILayout.HelpBox(S("menuinstall.showcontents.notselected"), MessageType.Info);
}
EditorGUI.indentLevel--;
}
}
2022-10-17 05:05:26 +08:00
_devFoldout = EditorGUILayout.Foldout(_devFoldout, G("menuinstall.devoptions"));
if (_devFoldout)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(
serializedObject.FindProperty(nameof(ModularAvatarMenuInstaller.menuToAppend)),
2022-10-17 05:05:26 +08:00
new GUIContent(G("menuinstall.srcmenu"))
);
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties();
2022-10-17 05:05:26 +08:00
Localization.ShowLanguageUI();
}
2022-10-17 03:47:51 +08:00
private void FindMenus()
{
if (targets.Length > 1)
{
_avatarMenus = null;
return;
}
_avatarMenus = new HashSet<VRCExpressionsMenu>();
var queue = new Queue<VRCExpressionsMenu>();
var avatar = RuntimeUtil.FindAvatarInParents(((Component) target).transform);
if (avatar == null || avatar.expressionsMenu == null) return;
queue.Enqueue(avatar.expressionsMenu);
while (queue.Count > 0)
{
var menu = queue.Dequeue();
if (_avatarMenus.Contains(menu)) continue;
_avatarMenus.Add(menu);
foreach (var subMenu in menu.controls)
{
if (subMenu.type == VRCExpressionsMenu.Control.ControlType.SubMenu)
{
queue.Enqueue(subMenu.subMenu);
}
}
}
}
private bool IsMenuReachable(VRCAvatarDescriptor avatar, VRCExpressionsMenu menu)
{
return _avatarMenus == null || _avatarMenus.Contains(menu);
}
}
}