Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ActionItem.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 namespace SiliconStudio.ActionStack
6 {
7  /// <summary>
8  /// Base class for action items.
9  /// </summary>
10  /// <remarks>
11  /// An ActionItem represents an action that can be undone or redone. Action items are usually stacked in an <see cref="IActionStack"/>.
12  /// </remarks>
13  public abstract class ActionItem : IActionItem
14  {
15  private readonly Guid identifier = Guid.NewGuid();
16  private bool undoRedoInProgress;
17  private bool isDone = true;
18 
19  /// <summary>
20  /// Initializes a new instance of the <see cref="ActionItem"/> class with the given name.
21  /// </summary>
22  /// <param name="name">The name of this action item.</param>
23  protected ActionItem(string name)
24  {
25  Name = name;
26  }
27 
28  /// <summary>
29  /// Initializes a new instance of the <see cref="ActionItem"/> class.
30  /// </summary>
31  protected ActionItem()
32  {
33  }
34 
35  /// <inheritdoc/>
36  public Guid Identifier { get { return identifier; } }
37 
38  /// <inheritdoc/>
39  public string Name { get; set; }
40 
41  /// <inheritdoc/>
42  public virtual bool IsSaved { get; set; }
43 
44  /// <inheritdoc/>
45  public virtual bool IsDone { get { return isDone; } protected set { isDone = value; } }
46 
47  /// <inheritdoc/>
48  public bool IsFrozen { get; private set; }
49 
50  /// <inheritdoc/>
51  public void Freeze()
52  {
53  if (!IsFrozen)
54  {
55  FreezeMembers();
56  IsFrozen = true;
57  }
58  }
59 
60  /// <inheritdoc/>
61  public void Undo()
62  {
63  if (undoRedoInProgress) throw new InvalidOperationException(string.Format(Properties.ExceptionMessages.InvokingUndoRedoWhileAlreadyInProgress, "Undo"));
64  if (IsFrozen) throw new InvalidOperationException(string.Format(Properties.ExceptionMessages.UndoRedoOnFrozenItem, "Undo"));
65  undoRedoInProgress = true;
66  UndoAction();
67  IsDone = false;
68  undoRedoInProgress = false;
69  }
70 
71  /// <inheritdoc/>
72  public void Redo()
73  {
74  if (undoRedoInProgress) throw new InvalidOperationException(string.Format(Properties.ExceptionMessages.UndoRedoOnFrozenItem, "Redo"));
75  if (IsFrozen) throw new InvalidOperationException("Unable to invoke Undo on a frozen ActionItem.");
76  undoRedoInProgress = true;
77  RedoAction();
78  IsDone = true;
79  undoRedoInProgress = false;
80  }
81 
82  /// <summary>
83  /// Invoked by <see cref="Freeze"/> before setting <see cref="IsFrozen"/> to true.
84  /// </summary>
85  /// <remarks>This method will not be invoked if <see cref="IsFrozen"/> is already true.</remarks>
86  protected abstract void FreezeMembers();
87 
88  /// <summary>
89  /// Invoked by <see cref="Undo"/> after setting <see cref="IsDone"/> to true.
90  /// </summary>
91  protected abstract void UndoAction();
92 
93  /// <summary>
94  /// Invoked by <see cref="Redo"/> after setting <see cref="IsDone"/> to true.
95  /// </summary>
96  protected abstract void RedoAction();
97  }
98 }
void Undo()
Undo the action.
Definition: ActionItem.cs:61
ActionItem()
Initializes a new instance of the ActionItem class.
Definition: ActionItem.cs:31
Base class for action items.
Definition: ActionItem.cs:13
void Freeze()
Freezes this ActionItem. A frozen action item can't be undone anymore and should have freed the resou...
Definition: ActionItem.cs:51
Base interface for action items.
Definition: IActionItem.cs:10
ActionItem(string name)
Initializes a new instance of the ActionItem class with the given name.
Definition: ActionItem.cs:23
void Redo()
Redo the action.
Definition: ActionItem.cs:72
This class represents a thread-safe stack of action items that can be undone/redone.
Definition: ActionStack.cs:12