// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using FlaxEngine;
namespace FlaxEditor
{
///
/// Implementation of that contains one or more child actions performed at once. Allows to merge different actions.
///
///
[Serializable]
[HideInEditor]
public class MultiUndoAction : IUndoAction
{
///
/// The child actions.
///
[Serialize]
public readonly IUndoAction[] Actions;
///
/// Initializes a new instance of the class.
///
/// The actions to include within this multi action.
public MultiUndoAction(params IUndoAction[] actions)
{
Actions = actions?.ToArray() ?? throw new ArgumentNullException();
if (Actions.Length == 0)
throw new ArgumentException("Empty actions collection.");
ActionString = Actions[0].ActionString;
}
///
/// Initializes a new instance of the class.
///
/// The actions to include within this multi action.
/// The action string.
public MultiUndoAction(IEnumerable actions, string actionString = null)
{
Actions = actions?.ToArray() ?? throw new ArgumentNullException();
if (Actions.Length == 0)
throw new ArgumentException("Empty actions collection.");
ActionString = actionString ?? Actions[0].ActionString;
}
///
public string ActionString { get; }
///
public void Do()
{
for (int i = 0; i < Actions.Length; i++)
{
Actions[i].Do();
}
}
///
public void Undo()
{
for (int i = Actions.Length - 1; i >= 0; i--)
{
Actions[i].Undo();
}
}
///
public void Dispose()
{
for (int i = 0; i < Actions.Length; i++)
{
Actions[i].Dispose();
}
}
}
}