Fix preserving actors hierarchy order when performing undo of actor removal

#1751
This commit is contained in:
2024-03-05 16:30:50 +01:00
parent f0c2e65b5c
commit 28da656ed1
5 changed files with 43 additions and 17 deletions
@@ -160,7 +160,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
var actions = new List<IUndoAction>();
foreach (var body in bodies)
{
var action = new Actions.DeleteActorsAction(new List<SceneGraphNode> { SceneGraphFactory.FindNode(body.ID) });
var action = new Actions.DeleteActorsAction(body);
action.Do();
actions.Add(action);
}
@@ -185,7 +185,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
var body = bodies.FirstOrDefault(x => x.Name == name);
if (body != null)
{
var action = new Actions.DeleteActorsAction(new List<SceneGraphNode> { SceneGraphFactory.FindNode(body.ID) });
var action = new Actions.DeleteActorsAction(body);
action.Do();
Presenter.Undo?.AddAction(action);
}
@@ -224,7 +224,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
else
{
// Remove joint that will no longer be valid
var action = new Actions.DeleteActorsAction(new List<SceneGraphNode> { SceneGraphFactory.FindNode(joint.ID) });
var action = new Actions.DeleteActorsAction(joint);
action.Do();
Presenter.Undo?.AddAction(action);
}
@@ -233,7 +233,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Remove body
{
var action = new Actions.DeleteActorsAction(new List<SceneGraphNode> { SceneGraphFactory.FindNode(body.ID) });
var action = new Actions.DeleteActorsAction(body);
action.Do();
Presenter.Undo?.AddAction(action);
}
+1 -1
View File
@@ -333,7 +333,7 @@ namespace FlaxEditor.Modules
actorNode.PostSpawn();
// Create undo action
IUndoAction action = new DeleteActorsAction(new List<SceneGraphNode>(1) { actorNode }, true);
IUndoAction action = new DeleteActorsAction(actorNode, true);
if (autoSelect)
{
var before = Selection.ToArray();
@@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using FlaxEditor.SceneGraph;
using FlaxEditor.Scripting;
using FlaxEngine;
using FlaxEngine.Utilities;
@@ -25,6 +24,9 @@ namespace FlaxEditor.Actions
[Serialize]
private Guid[] _nodeParentsIDs;
[Serialize]
private int[] _nodeParentsOrders;
[Serialize]
private Guid[] _prefabIds;
@@ -43,12 +45,35 @@ namespace FlaxEditor.Actions
[Serialize]
protected List<SceneGraphNode> _nodeParents;
/// <summary>
/// Initializes a new instance of the <see cref="DeleteActorsAction"/> class.
/// </summary>
/// <param name="actor">The actor.</param>
/// <param name="isInverted">If set to <c>true</c> action will be inverted - instead of delete it will create actors.</param>
/// <param name="preserveOrder">If set to <c>true</c> action will be preserve actors order when performing undo.</param>
internal DeleteActorsAction(Actor actor, bool isInverted = false, bool preserveOrder = true)
: this(new List<SceneGraphNode>(1) { SceneGraphFactory.FindNode(actor.ID) }, isInverted, preserveOrder)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DeleteActorsAction"/> class.
/// </summary>
/// <param name="node">The object.</param>
/// <param name="isInverted">If set to <c>true</c> action will be inverted - instead of delete it will create actors.</param>
/// <param name="preserveOrder">If set to <c>true</c> action will be preserve actors order when performing undo.</param>
internal DeleteActorsAction(SceneGraphNode node, bool isInverted = false, bool preserveOrder = true)
: this(new List<SceneGraphNode>(1) { node }, isInverted, preserveOrder)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DeleteActorsAction"/> class.
/// </summary>
/// <param name="nodes">The objects.</param>
/// <param name="isInverted">If set to <c>true</c> action will be inverted - instead of delete it will be create actors.</param>
internal DeleteActorsAction(List<SceneGraphNode> nodes, bool isInverted = false)
/// <param name="isInverted">If set to <c>true</c> action will be inverted - instead of delete it will create actors.</param>
/// <param name="preserveOrder">If set to <c>true</c> action will be preserve actors order when performing undo.</param>
internal DeleteActorsAction(List<SceneGraphNode> nodes, bool isInverted = false, bool preserveOrder = true)
{
_isInverted = isInverted;
@@ -82,6 +107,12 @@ namespace FlaxEditor.Actions
_nodeParentsIDs = new Guid[_nodeParents.Count];
for (int i = 0; i < _nodeParentsIDs.Length; i++)
_nodeParentsIDs[i] = _nodeParents[i].ID;
if (preserveOrder)
{
_nodeParentsOrders = new int[_nodeParents.Count];
for (int i = 0; i < _nodeParentsOrders.Length; i++)
_nodeParentsOrders[i] = _nodeParents[i].OrderInParent;
}
// Serialize actors
_actorsData = Actor.ToBytes(actors.ToArray());
@@ -122,6 +153,7 @@ namespace FlaxEditor.Actions
{
_actorsData = null;
_nodeParentsIDs = null;
_nodeParentsOrders = null;
_prefabIds = null;
_prefabObjectIds = null;
_nodeParents.Clear();
@@ -211,6 +243,8 @@ namespace FlaxEditor.Actions
if (foundNode is ActorNode node)
{
nodes.Add(node);
if (_nodeParentsOrders != null)
node.Actor.OrderInParent = _nodeParentsOrders[i];
}
}
nodes.BuildNodesParents(_nodeParents);
@@ -165,7 +165,7 @@ namespace FlaxEditor.Actions
var child = children[j];
if (child != actor && child.Name == name)
{
string newName = Utilities.Utils.IncrementNameNumber(name, x => IsNameValid(x));
string newName = Utilities.Utils.IncrementNameNumber(name, IsNameValid);
foundNamesResults[newName] = true;
actor.Name = newName;
// Multiple actors may have the same name, continue
-8
View File
@@ -1764,14 +1764,6 @@ bool Actor::FromBytes(const Span<byte>& data, Array<Actor*>& output, ISerializeM
}
Scripting::ObjectsLookupIdMapping.Set(nullptr);
// Link objects
//for (int32 i = 0; i < objectsCount; i++)
{
//SceneObject* obj = sceneObjects->At(i);
// TODO: post load or post spawn?
//obj->PostLoad();
}
// Update objects order
//for (int32 i = 0; i < objectsCount; i++)
{