Add batching undo actions for Surface editing to prevent undo actions spam during a single edit

This commit is contained in:
Wojtek Figat
2022-07-19 23:07:18 +02:00
parent fd4af3964d
commit f90808749e
5 changed files with 32 additions and 9 deletions

View File

@@ -1389,7 +1389,7 @@ namespace FlaxEditor.Surface.Archetypes
var state = (StateMachineState)other;
var action = new AddRemoveTransitionAction(this, state);
Surface?.Undo.AddAction(action);
Surface?.AddBatchedUndoAction(action);
action.Do();
Surface?.OnNodesConnected(this, other);
}
@@ -1718,7 +1718,7 @@ namespace FlaxEditor.Surface.Archetypes
public void Delete()
{
var action = new StateMachineState.AddRemoveTransitionAction(this);
SourceState.Surface?.Undo.AddAction(action);
SourceState.Surface?.AddBatchedUndoAction(action);
action.Do();
}

View File

@@ -437,7 +437,7 @@ namespace FlaxEditor.Surface.Elements
var action = new EditNodeConnections(ParentNode.Context, ParentNode);
RemoveConnections(1);
action.End();
Surface.Undo.AddAction(action);
Surface.AddBatchedUndoAction(action);
}
else
{
@@ -629,7 +629,7 @@ namespace FlaxEditor.Surface.Elements
var action = new ConnectBoxesAction((InputBox)this, (OutputBox)connectedBox, false);
BreakConnection(connectedBox);
action.End();
Surface.Undo.AddAction(action);
Surface.AddBatchedUndoAction(action);
Surface.MarkAsEdited();
}
else
@@ -678,7 +678,7 @@ namespace FlaxEditor.Surface.Elements
var action = new EditNodeConnections(ParentNode.Context, ParentNode);
RemoveConnections();
action.End();
Surface.Undo.AddAction(action);
Surface.AddBatchedUndoAction(action);
}
else
{
@@ -856,7 +856,7 @@ namespace FlaxEditor.Surface.Elements
var action = new ConnectBoxesAction(iB, oB, false);
start.BreakConnection(end);
action.End();
Surface.Undo.AddAction(action);
Surface.AddBatchedUndoAction(action);
}
else
{
@@ -892,7 +892,7 @@ namespace FlaxEditor.Surface.Elements
var action = new ConnectBoxesAction(iB, oB, true);
iB.CreateConnection(oB);
action.End();
Surface.Undo?.AddAction(action);
Surface.AddBatchedUndoAction(action);
}
else
{

View File

@@ -926,7 +926,8 @@ namespace FlaxEditor.Surface
OnValuesChanged();
Surface?.MarkAsEdited(graphEdited);
Surface?.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited));
if (Surface?.Undo != null)
Surface.AddBatchedUndoAction(new EditNodeValuesAction(this, before, graphEdited));
_isDuringValuesEditing = false;
}
@@ -952,7 +953,8 @@ namespace FlaxEditor.Surface
OnValuesChanged();
Surface.MarkAsEdited(graphEdited);
Surface.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited));
if (Surface?.Undo != null)
Surface.AddBatchedUndoAction(new EditNodeValuesAction(this, before, graphEdited));
_isDuringValuesEditing = false;
}

View File

@@ -49,6 +49,13 @@ namespace FlaxEditor.Surface
_moveViewWithMouseDragSpeed = isMovingWithMouse ? Mathf.Clamp(_moveViewWithMouseDragSpeed + deltaTime * 20.0f, 1.0f, 8.0f) : 1.0f;
base.Update(deltaTime);
// Batch undo actions
if (_batchedUndoActions != null && _batchedUndoActions.Count != 0)
{
Undo.AddAction(_batchedUndoActions.Count == 1 ? _batchedUndoActions[0] : new MultiUndoAction(_batchedUndoActions));
_batchedUndoActions.Clear();
}
}
/// <summary>

View File

@@ -38,6 +38,7 @@ namespace FlaxEditor.Surface
private VisjectCM _activeVisjectCM;
private GroupArchetype _customNodesGroup;
private List<NodeArchetype> _customNodes;
private List<IUndoAction> _batchedUndoActions;
private Action _onSave;
private int _selectedConnectionIndex;
@@ -901,6 +902,19 @@ namespace FlaxEditor.Surface
{
return _context.FindNode(id);
}
/// <summary>
/// Adds the undo action to be batched (eg. if multiple undo actions is performed in a sequence during single update).
/// </summary>
/// <param name="action">The action.</param>
public void AddBatchedUndoAction(IUndoAction action)
{
if (Undo == null || !Undo.Enabled)
return;
if (_batchedUndoActions == null)
_batchedUndoActions = new List<IUndoAction>();
_batchedUndoActions.Add(action);
}
/// <summary>
/// Called when node gets spawned in the surface (via UI).