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

View File

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

View File

@@ -926,7 +926,8 @@ namespace FlaxEditor.Surface
OnValuesChanged(); OnValuesChanged();
Surface?.MarkAsEdited(graphEdited); Surface?.MarkAsEdited(graphEdited);
Surface?.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited)); if (Surface?.Undo != null)
Surface.AddBatchedUndoAction(new EditNodeValuesAction(this, before, graphEdited));
_isDuringValuesEditing = false; _isDuringValuesEditing = false;
} }
@@ -952,7 +953,8 @@ namespace FlaxEditor.Surface
OnValuesChanged(); OnValuesChanged();
Surface.MarkAsEdited(graphEdited); Surface.MarkAsEdited(graphEdited);
Surface.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited)); if (Surface?.Undo != null)
Surface.AddBatchedUndoAction(new EditNodeValuesAction(this, before, graphEdited));
_isDuringValuesEditing = false; _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; _moveViewWithMouseDragSpeed = isMovingWithMouse ? Mathf.Clamp(_moveViewWithMouseDragSpeed + deltaTime * 20.0f, 1.0f, 8.0f) : 1.0f;
base.Update(deltaTime); 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> /// <summary>

View File

@@ -38,6 +38,7 @@ namespace FlaxEditor.Surface
private VisjectCM _activeVisjectCM; private VisjectCM _activeVisjectCM;
private GroupArchetype _customNodesGroup; private GroupArchetype _customNodesGroup;
private List<NodeArchetype> _customNodes; private List<NodeArchetype> _customNodes;
private List<IUndoAction> _batchedUndoActions;
private Action _onSave; private Action _onSave;
private int _selectedConnectionIndex; private int _selectedConnectionIndex;
@@ -901,6 +902,19 @@ namespace FlaxEditor.Surface
{ {
return _context.FindNode(id); 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> /// <summary>
/// Called when node gets spawned in the surface (via UI). /// Called when node gets spawned in the surface (via UI).