UndoViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using PixiEditor.Helpers;
  5. using PixiEditor.Models.Controllers;
  6. using PixiEditor.Models.DataHolders;
  7. using PixiEditor.Models.Tools;
  8. using PixiEditor.Models.Undo;
  9. namespace PixiEditor.ViewModels.SubViewModels.Main
  10. {
  11. public class UndoViewModel : SubViewModel<ViewModelMain>
  12. {
  13. public RelayCommand UndoCommand { get; set; }
  14. public RelayCommand RedoCommand { get; set; }
  15. private LayerChange[] undoChanges;
  16. public LayerChange[] UndoChanges // This acts like UndoManager process, but it was implemented before process system, so it can be transformed into it
  17. {
  18. get => undoChanges;
  19. set
  20. {
  21. undoChanges = value;
  22. for (int i = 0; i < value.Length; i++)
  23. {
  24. Owner.BitmapManager.ActiveDocument.Layers.First(x => x.LayerGuid == value[i].LayerGuid).SetPixels(value[i].PixelChanges);
  25. }
  26. }
  27. }
  28. public UndoViewModel(ViewModelMain owner)
  29. : base(owner)
  30. {
  31. UndoCommand = new RelayCommand(Undo, CanUndo);
  32. RedoCommand = new RelayCommand(Redo, CanRedo);
  33. if (!Directory.Exists(StorageBasedChange.DefaultUndoChangeLocation))
  34. {
  35. Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);
  36. }
  37. ClearUndoTempDirectory();
  38. }
  39. public void TriggerNewUndoChange(Tool toolUsed)
  40. {
  41. if (BitmapManager.IsOperationTool(toolUsed)
  42. && ((BitmapOperationTool)toolUsed).UseDefaultUndoMethod)
  43. {
  44. Tuple<LayerChange, LayerChange>[] changes = Owner.ChangesController.PopChanges();
  45. if (changes != null && changes.Length > 0)
  46. {
  47. LayerChange[] newValues = changes.Select(x => x.Item1).ToArray();
  48. LayerChange[] oldValues = changes.Select(x => x.Item2).ToArray();
  49. Owner.BitmapManager.ActiveDocument.UndoManager.AddUndoChange(
  50. new Change("UndoChanges", oldValues, newValues, root: this));
  51. toolUsed.AfterAddedUndo(Owner.BitmapManager.ActiveDocument.UndoManager);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Redo last action.
  57. /// </summary>
  58. /// <param name="parameter">CommandProperty.</param>
  59. public void Redo(object parameter)
  60. {
  61. Owner.BitmapManager.ActiveDocument.UndoManager.Redo();
  62. }
  63. /// <summary>
  64. /// Undo last action.
  65. /// </summary>
  66. /// <param name="parameter">CommandParameter.</param>
  67. public void Undo(object parameter)
  68. {
  69. Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
  70. }
  71. /// <summary>
  72. /// Removes all files from %tmp%/PixiEditor/UndoStack/.
  73. /// </summary>
  74. public void ClearUndoTempDirectory()
  75. {
  76. DirectoryInfo dirInfo = new DirectoryInfo(StorageBasedChange.DefaultUndoChangeLocation);
  77. foreach (FileInfo file in dirInfo.GetFiles())
  78. {
  79. file.Delete();
  80. }
  81. }
  82. /// <summary>
  83. /// Returns true if undo can be done.
  84. /// </summary>
  85. /// <param name="property">CommandParameter.</param>
  86. /// <returns>True if can undo.</returns>
  87. private bool CanUndo(object property)
  88. {
  89. return Owner.BitmapManager.ActiveDocument?.UndoManager.CanUndo ?? false;
  90. }
  91. /// <summary>
  92. /// Returns true if redo can be done.
  93. /// </summary>
  94. /// <param name="property">CommandProperty.</param>
  95. /// <returns>True if can redo.</returns>
  96. private bool CanRedo(object property)
  97. {
  98. return Owner.BitmapManager.ActiveDocument?.UndoManager.CanRedo ?? false;
  99. }
  100. }
  101. }