Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ActionStackViewModel.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 using System.Linq;
5 
6 using SiliconStudio.ActionStack;
7 using SiliconStudio.Core.Extensions;
8 using SiliconStudio.Presentation.Collections;
9 using SiliconStudio.Presentation.Extensions;
10 
11 namespace SiliconStudio.Presentation.ViewModel.ActionStack
12 {
13  /// <summary>
14  /// This class is a view model for the <see cref="ViewModelTransactionalActionStack"/> class. It inherits from the <see cref="DispatcherViewModel"/> class.
15  /// </summary>
16  public class ActionStackViewModel : DispatcherViewModel, IDisposable
17  {
18  private readonly ViewModelTransactionalActionStack actionStack;
19  private readonly ObservableList<ActionItemViewModel> actionItems = new ObservableList<ActionItemViewModel>();
20  private SavePoint savePoint;
21 
22  /// <summary>
23  /// Initializes a new instance of the <see cref="ActionStackViewModel"/>.
24  /// </summary>
25  /// <param name="actionStack">The action stack. Cannot be null.</param>
27  : base(actionStack.SafeArgument("actionStack").ServiceProvider)
28  {
29  this.actionStack = actionStack;
30 
31  actionStack.ActionItemsAdded += ActionItemsAdded;
32  actionStack.ActionItemsCleared += ActionItemsCleared;
33  actionStack.ActionItemsDiscarded += ActionItemsDiscarded;
34  actionStack.Undone += ActionItemModified;
35  actionStack.Redone += ActionItemModified;
36  }
37 
38  /// <summary>
39  /// Gets the action stack linked to this view model.
40  /// </summary>
41  public ViewModelTransactionalActionStack ActionStack { get { return actionStack; } }
42 
43  /// <summary>
44  /// Gets the collection of action item view models currently contained in this view model.
45  /// </summary>
46  public IReadOnlyObservableCollection<ActionItemViewModel> ActionItems { get { return actionItems; } }
47 
48  /// <summary>
49  /// Gets whether it is currently possible to perform an undo operation.
50  /// </summary>
51  public bool CanUndo { get { return ActionItems.Count > 0 && ActionItems.First().IsDone; } }
52 
53  /// <summary>
54  /// Gets whether it is currently possible to perform a redo operation.
55  /// </summary>
56  public bool CanRedo { get { return ActionItems.Count > 0 && !ActionItems.Last().IsDone; } }
57 
58  /// <inheritdoc/>
59  public void Dispose()
60  {
61  actionStack.ActionItemsAdded -= ActionItemsAdded;
62  actionStack.ActionItemsCleared -= ActionItemsCleared;
63  actionStack.ActionItemsDiscarded -= ActionItemsDiscarded;
64  Dispatcher.Invoke(actionItems.Clear);
65  }
66 
67  /// <summary>
68  /// Notify that everything has been saved and create a save point in the action stack.
69  /// </summary>
70  public void NotifySave()
71  {
72  savePoint = actionStack.CreateSavePoint(true);
73  actionItems.ForEach(x => x.IsSavePoint = x.ActionItem.Identifier == savePoint.ActionItemIdentifier);
74  }
75 
76  private void ActionItemModified(object sender, ActionItemsEventArgs<IActionItem> e)
77  {
78  foreach (var actionItem in ActionItems)
79  actionItem.Refresh();
80  }
81 
82  private void ActionItemsDiscarded(object sender, DiscardedActionItemsEventArgs<IActionItem> e)
83  {
84  var actionsToRemove = e.ActionItems.Select(x => actionItems.FirstOrDefault(y => y.ActionItem.Identifier == x.Identifier)).Where(y => y != null).ToList();
85  actionsToRemove.ForEach(x => actionItems.Remove(x));
86  }
87 
88  private void ActionItemsCleared(object sender, EventArgs e)
89  {
90  actionStack.Clear();
91  }
92 
93  private void ActionItemsAdded(object sender, ActionItemsEventArgs<IActionItem> e)
94  {
95  foreach (IActionItem actionItem in e.ActionItems)
96  actionItems.Add(new ActionItemViewModel(actionStack.ServiceProvider, actionItem));
97  }
98  }
99 }
Represents a save point marker in the undo/redo action items stack.
Definition: SavePoint.cs:11
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
This class is a view model for the ViewModelTransactionalActionStack class. It inherits from the Disp...
This abstract class is an implementation of ViewModelBase that uses a dispatcher to invoke OnProperty...
ActionStackViewModel(ViewModelTransactionalActionStack actionStack)
Initializes a new instance of the ActionStackViewModel.
Base interface for action items.
Definition: IActionItem.cs:10
void NotifySave()
Notify that everything has been saved and create a save point in the action stack.
This class represents a thread-safe stack of action items that can be undone/redone.
Definition: ActionStack.cs:12