Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ViewModelTransactionalActionStack.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.Collections.Generic;
5 
6 using SiliconStudio.ActionStack;
7 using SiliconStudio.Core.Extensions;
8 using SiliconStudio.Presentation.Services;
9 
10 namespace SiliconStudio.Presentation.ViewModel.ActionStack
11 {
13  {
14  private readonly List<ViewModelActionItem> discardedActionItems = new List<ViewModelActionItem>();
15 
16  public ViewModelTransactionalActionStack(int capacity, IViewModelServiceProvider serviceProvider)
17  : base(capacity)
18  {
19  if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");
20  ServiceProvider = serviceProvider;
21  Dispatcher = serviceProvider.Get<IDispatcherService>();
22  }
23 
24  public ViewModelTransactionalActionStack(int capacity, IViewModelServiceProvider serviceProvider, IEnumerable<IActionItem> initialActionsItems)
25  : base(capacity, initialActionsItems)
26  {
27  if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");
28  ServiceProvider = serviceProvider;
29  Dispatcher = serviceProvider.Get<IDispatcherService>();
30  }
31 
32  public IViewModelServiceProvider ServiceProvider { get; private set; }
33 
34  public IDispatcherService Dispatcher { get; private set; }
35 
36  public override void Add(IActionItem item)
37  {
38  Dispatcher.Invoke(() => base.Add(item));
39  }
40 
41  public override SavePoint CreateSavePoint(bool markActionsAsSaved)
42  {
43  var savePoint = base.CreateSavePoint(markActionsAsSaved);
44  if (markActionsAsSaved)
45  {
46  discardedActionItems.ForEach(x => x.Dirtiables.ForEach(y => y.DiscardActionItem(x)));
47  discardedActionItems.Clear();
48  }
49  return savePoint;
50  }
51 
52  protected override void OnActionItemsAdded(ActionItemsEventArgs<IActionItem> e)
53  {
54  base.OnActionItemsAdded(e);
55  RegisterActionItemsRecursively(e.ActionItems);
56  }
57 
58  protected override void OnActionItemsDiscarded(DiscardedActionItemsEventArgs<IActionItem> e)
59  {
60  base.OnActionItemsDiscarded(e);
61  DiscardActionItemsRecursively(e.ActionItems, e.Type);
62  }
63 
64  private static void RegisterActionItemsRecursively(IEnumerable<IActionItem> actionItems)
65  {
66  foreach (var actionItem in actionItems)
67  {
68  var viewModelActionItem = actionItem as ViewModelActionItem;
69  if (viewModelActionItem != null)
70  {
71  viewModelActionItem.Dirtiables.ForEach(x => x.RegisterActionItem(viewModelActionItem));
72  }
73  var aggregateActionItem = actionItem as IAggregateActionItem;
74  if (aggregateActionItem != null)
75  {
76  RegisterActionItemsRecursively(aggregateActionItem.ActionItems);
77  }
78  }
79  }
80 
81  private void DiscardActionItemsRecursively(IEnumerable<IActionItem> actionItems, ActionItemDiscardType discardType)
82  {
83  foreach (var actionItem in actionItems)
84  {
85  var viewModelActionItem = actionItem as ViewModelActionItem;
86  if (viewModelActionItem != null)
87  {
88  if (discardType == ActionItemDiscardType.Swallowed)
89  {
90  discardedActionItems.Add(viewModelActionItem);
91  }
92  else if (discardType != ActionItemDiscardType.UndoRedoInProgress)
93  {
94  viewModelActionItem.Dirtiables.ForEach(x => x.DiscardActionItem(viewModelActionItem));
95  }
96  }
97  var aggregateActionItem = actionItem as IAggregateActionItem;
98  if (aggregateActionItem != null)
99  {
100  DiscardActionItemsRecursively(aggregateActionItem.ActionItems, discardType);
101  }
102  }
103  }
104  }
105 }
ActionItemDiscardType
This enum describes how an action item is being discarded when the ActionStack.ActionItemsDiscarded e...
This class is an implementation of the ITransactionalActionStack interface.
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
An abstact class that inherits from ActionItem and can be used to manage actions related to an IDirti...
override void OnActionItemsAdded(ActionItemsEventArgs< IActionItem > e)
Invoked whenever action items are added to the stack.
ViewModelTransactionalActionStack(int capacity, IViewModelServiceProvider serviceProvider, IEnumerable< IActionItem > initialActionsItems)
Base interface for action items.
Definition: IActionItem.cs:10
override SavePoint CreateSavePoint(bool markActionsAsSaved)
Creates a save point at the current index of the action stack. Indicate whether to set the IActionIte...
override void Add(IActionItem item)
Adds an action item to the stack. Discards any action item that is currently undone. The action item to add to the stack.
Base interface of aggregate of action items.
override void OnActionItemsDiscarded(DiscardedActionItemsEventArgs< IActionItem > e)
Invoked whenever action items are discarded from the stack.
This class represents a thread-safe stack of action items that can be undone/redone.
Definition: ActionStack.cs:12