feat: serialize Move Independently grouping (#1067)

Closed: #842
This commit is contained in:
bd_ 2024-08-30 19:19:46 -07:00 committed by GitHub
parent f085ce07b6
commit 802fea09d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 4 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using nadena.dev.modular_avatar.core.ArmatureAwase;
using nadena.dev.ndmf.preview;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
@ -15,15 +16,38 @@ namespace nadena.dev.modular_avatar.core.editor
[SerializeField] private StyleSheet uss;
[SerializeField] private VisualTreeAsset uxml;
private ComputeContext _ctx;
private VisualElement _root;
private TransformChildrenNode _groupedNodesElem;
protected override void OnInnerInspectorGUI()
{
throw new System.NotImplementedException();
throw new NotImplementedException();
}
protected override VisualElement CreateInnerInspectorGUI()
{
_root = new VisualElement();
RebuildInnerGUI();
return _root;
}
private void RebuildInnerGUI()
{
_root.Clear();
_ctx = new ComputeContext("MoveIndependentlyEditor");
_root.Add(BuildInnerGUI(_ctx));
}
private VisualElement BuildInnerGUI(ComputeContext ctx)
{
if (this.target == null) return new VisualElement();
_ctx.InvokeOnInvalidate(this, editor => editor.RebuildInnerGUI());
#pragma warning disable CS0618 // Type or member is obsolete
var root = uxml.Localize();
#pragma warning restore CS0618 // Type or member is obsolete
@ -32,9 +56,14 @@ namespace nadena.dev.modular_avatar.core.editor
var container = root.Q<VisualElement>("group-container");
MAMoveIndependently target = (MAMoveIndependently) this.target;
var grouped = (target.GroupedBones ?? Array.Empty<GameObject>())
// Note: We specifically _don't_ use an ImmutableHashSet here as we want to update the previously-returned
// set in place to avoid rebuilding GUI elements after the user changes the grouping.
var grouped = ctx.Observe(target,
t => (t.GroupedBones ?? Array.Empty<GameObject>())
.Select(obj => obj.transform)
.ToImmutableHashSet();
.ToHashSet(new ObjectIdentityComparer<Transform>()),
(x, y) => x.SetEquals(y)
);
_groupedNodesElem = new TransformChildrenNode(target.transform, grouped);
_groupedNodesElem.AddToClassList("group-root");
@ -43,6 +72,8 @@ namespace nadena.dev.modular_avatar.core.editor
{
Undo.RecordObject(target, "Toggle grouped nodes");
target.GroupedBones = _groupedNodesElem.Active().Select(t => t.gameObject).ToArray();
grouped.Clear();
grouped.UnionWith(target.GroupedBones.Select(obj => obj.transform));
PrefabUtility.RecordPrefabInstancePropertyModifications(target);
};

View File

@ -17,6 +17,7 @@ namespace nadena.dev.modular_avatar.core.ArmatureAwase
{
private float EPSILON = 0.0000001f;
[SerializeField]
private GameObject[] m_groupedBones;
public GameObject[] GroupedBones