// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using FlaxEngine; namespace FlaxEditor { /// /// Helper class to record undo operations in a block with using keyword. /// /// using(new UndoBlock(undo, obj, "Rename")) /// { /// obj.Name = "super name"; /// } /// /// /// [HideInEditor] public class UndoBlock : IDisposable { private readonly Undo _undo; private readonly object _snapshotUndoInternal; private readonly IUndoAction _customActionBefore; private readonly IUndoAction _customActionAfter; /// /// Creates new undo object for recording actions with using pattern. /// /// The undo/redo object. /// Instance of an object to record. /// Name of action to be displayed in undo stack. /// Custom action to append to the undo block action before recorded modifications apply. /// Custom action to append to the undo block action after recorded modifications apply. public UndoBlock(Undo undo, object snapshotInstance, string actionString, IUndoAction customActionBefore = null, IUndoAction customActionAfter = null) { if (undo == null) return; _snapshotUndoInternal = snapshotInstance; _undo = undo; _undo.RecordBegin(_snapshotUndoInternal, actionString); _customActionBefore = customActionBefore; _customActionAfter = customActionAfter; } /// public void Dispose() { _undo?.RecordEnd(_snapshotUndoInternal, _customActionBefore, _customActionAfter); } } }