UndoViewModel.cs 3.0 KB

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