2022-12-11 22:08:41 +09:00
|
|
|
|
using System.Collections.Generic;
|
2022-12-12 18:19:50 +09:00
|
|
|
|
using System.Collections.Immutable;
|
2022-12-11 22:08:41 +09:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.Components;
|
|
|
|
|
using VRC.SDK3.Avatars.ScriptableObjects;
|
|
|
|
|
using static VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionsMenu.Control;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor {
|
|
|
|
|
public class MenuTree {
|
2022-12-12 22:27:04 +09:00
|
|
|
|
public struct ChildElement {
|
|
|
|
|
public string menuName;
|
|
|
|
|
public VRCExpressionsMenu menu;
|
|
|
|
|
public ModularAvatarMenuInstaller installer;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-11 22:08:41 +09:00
|
|
|
|
private readonly VRCExpressionsMenu _rootMenu;
|
|
|
|
|
private readonly HashSet<VRCExpressionsMenu> _included;
|
2022-12-12 22:27:04 +09:00
|
|
|
|
private readonly Dictionary<VRCExpressionsMenu, List<ChildElement>> _childrenMap;
|
2022-12-11 22:08:41 +09:00
|
|
|
|
|
2022-12-12 18:19:50 +09:00
|
|
|
|
private readonly Dictionary<VRCExpressionsMenu, ImmutableArray<VRCExpressionsMenu>> _flashedChildrenMap;
|
|
|
|
|
|
2022-12-11 22:08:41 +09:00
|
|
|
|
public MenuTree(VRCAvatarDescriptor descriptor) {
|
|
|
|
|
this._rootMenu = descriptor.expressionsMenu;
|
|
|
|
|
this._included = new HashSet<VRCExpressionsMenu>();
|
2022-12-12 22:27:04 +09:00
|
|
|
|
this._childrenMap = new Dictionary<VRCExpressionsMenu, List<ChildElement>>();
|
2022-12-11 22:08:41 +09:00
|
|
|
|
|
|
|
|
|
if (this._rootMenu == null) {
|
|
|
|
|
this._rootMenu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._included.Add(this._rootMenu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AvatarsMenuMapping() {
|
|
|
|
|
if (this._rootMenu == null) return;
|
|
|
|
|
this.MappingMenu(this._rootMenu);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 19:19:55 +09:00
|
|
|
|
public void MappingMenuInstaller(ModularAvatarMenuInstaller installer) {
|
2022-12-14 19:27:19 +09:00
|
|
|
|
if (!installer.enabled) return;
|
2022-12-11 22:08:41 +09:00
|
|
|
|
if (installer.menuToAppend == null) return;
|
2022-12-12 22:27:04 +09:00
|
|
|
|
this.MappingMenu(installer.menuToAppend, installer);
|
2022-12-11 22:08:41 +09:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 22:27:04 +09:00
|
|
|
|
public IEnumerable<ChildElement> GetChildren(VRCExpressionsMenu parent) {
|
2022-12-12 18:19:50 +09:00
|
|
|
|
// TODO: ライブラリとするのであれば、複製したリスト or ImmutableArray,を返すのが好ましい
|
|
|
|
|
if (parent == null) parent = this._rootMenu;
|
2022-12-12 22:27:04 +09:00
|
|
|
|
return this._childrenMap.TryGetValue(parent, out List<ChildElement> children)
|
2022-12-12 19:19:55 +09:00
|
|
|
|
? children
|
2022-12-12 22:27:04 +09:00
|
|
|
|
: Enumerable.Empty<ChildElement>();
|
2022-12-12 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 22:27:04 +09:00
|
|
|
|
private void MappingMenu(VRCExpressionsMenu root, ModularAvatarMenuInstaller installer = null) {
|
2022-12-11 22:08:41 +09:00
|
|
|
|
Queue<VRCExpressionsMenu> queue = new Queue<VRCExpressionsMenu>();
|
|
|
|
|
queue.Enqueue(root);
|
2022-12-12 22:27:04 +09:00
|
|
|
|
bool first = true;
|
2022-12-11 22:08:41 +09:00
|
|
|
|
|
|
|
|
|
while (queue.Count > 0) {
|
|
|
|
|
VRCExpressionsMenu parent = queue.Dequeue();
|
2022-12-12 22:27:04 +09:00
|
|
|
|
IEnumerable<KeyValuePair<string, VRCExpressionsMenu>> childMenus = GetChildMenus(parent);
|
|
|
|
|
|
|
|
|
|
if (first && installer != null) {
|
|
|
|
|
parent = installer.installTargetMenu != null ? installer.installTargetMenu : _rootMenu;
|
|
|
|
|
|
2022-12-11 22:08:41 +09:00
|
|
|
|
}
|
2022-12-12 19:19:55 +09:00
|
|
|
|
|
2022-12-12 22:27:04 +09:00
|
|
|
|
foreach (KeyValuePair<string, VRCExpressionsMenu> childMenu in childMenus) {
|
|
|
|
|
if (!this._childrenMap.TryGetValue(parent, out List<ChildElement> children)) {
|
|
|
|
|
children = new List<ChildElement>();
|
2022-12-11 22:08:41 +09:00
|
|
|
|
this._childrenMap[parent] = children;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 22:27:04 +09:00
|
|
|
|
ChildElement childElement = new ChildElement { menuName = childMenu.Key, menu = childMenu.Value, installer = installer };
|
|
|
|
|
children.Add(childElement);
|
|
|
|
|
if (this._included.Contains(childElement.menu)) continue;
|
|
|
|
|
queue.Enqueue(childElement.menu);
|
|
|
|
|
this._included.Add(childElement.menu);
|
2022-12-11 22:08:41 +09:00
|
|
|
|
}
|
2022-12-12 22:27:04 +09:00
|
|
|
|
|
|
|
|
|
first = false;
|
2022-12-11 22:08:41 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 22:27:04 +09:00
|
|
|
|
private static IEnumerable<KeyValuePair<string, VRCExpressionsMenu>> GetChildMenus(VRCExpressionsMenu expressionsMenu) {
|
2022-12-11 22:08:41 +09:00
|
|
|
|
return expressionsMenu.controls
|
|
|
|
|
.Where(control => control.type == ControlType.SubMenu && control.subMenu != null)
|
2022-12-12 19:19:55 +09:00
|
|
|
|
.Select(control => new KeyValuePair<string, VRCExpressionsMenu>(control.name, control.subMenu));
|
2022-12-11 22:08:41 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|