DocumentViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.DataHolders;
  3. using PixiEditor.Models.Dialogs;
  4. using PixiEditor.Models.Enums;
  5. using SkiaSharp;
  6. using System;
  7. using System.Linq;
  8. namespace PixiEditor.ViewModels.SubViewModels.Main
  9. {
  10. public class DocumentViewModel : SubViewModel<ViewModelMain>
  11. {
  12. public const string ConfirmationDialogTitle = "Unsaved changes";
  13. public const string ConfirmationDialogMessage = "The document has been modified. Do you want to save changes?";
  14. public RelayCommand CenterContentCommand { get; set; }
  15. public RelayCommand ClipCanvasCommand { get; set; }
  16. public RelayCommand DeletePixelsCommand { get; set; }
  17. public RelayCommand OpenResizePopupCommand { get; set; }
  18. public RelayCommand RotateToRightCommand { get; set; }
  19. public RelayCommand FlipCommand { get; set; }
  20. public DocumentViewModel(ViewModelMain owner)
  21. : base(owner)
  22. {
  23. CenterContentCommand = new RelayCommand(CenterContent, Owner.DocumentIsNotNull);
  24. ClipCanvasCommand = new RelayCommand(ClipCanvas, Owner.DocumentIsNotNull);
  25. DeletePixelsCommand = new RelayCommand(DeletePixels, Owner.SelectionSubViewModel.SelectionIsNotEmpty);
  26. OpenResizePopupCommand = new RelayCommand(OpenResizePopup, Owner.DocumentIsNotNull);
  27. RotateToRightCommand = new RelayCommand(RotateDocument, Owner.DocumentIsNotNull);
  28. FlipCommand = new RelayCommand(FlipDocument, Owner.DocumentIsNotNull);
  29. }
  30. public void FlipDocument(object parameter)
  31. {
  32. if (parameter is "Horizontal")
  33. {
  34. Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
  35. }
  36. else if (parameter is "Vertical")
  37. {
  38. Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
  39. }
  40. }
  41. public void RotateDocument(object parameter)
  42. {
  43. if (parameter is double angle)
  44. {
  45. Owner.BitmapManager.ActiveDocument?.RotateActiveDocument((float)angle);
  46. }
  47. }
  48. public void ClipCanvas(object parameter)
  49. {
  50. Owner.BitmapManager.ActiveDocument?.ClipCanvas();
  51. }
  52. public void RequestCloseDocument(Document document)
  53. {
  54. if (!document.ChangesSaved)
  55. {
  56. ConfirmationType result = ConfirmationDialog.Show(ConfirmationDialogMessage, ConfirmationDialogTitle);
  57. if (result == ConfirmationType.Yes)
  58. {
  59. Owner.FileSubViewModel.SaveDocument(false);
  60. if (!document.ChangesSaved)
  61. return;
  62. }
  63. else if (result == ConfirmationType.Canceled)
  64. {
  65. return;
  66. }
  67. }
  68. Owner.BitmapManager.CloseDocument(document);
  69. }
  70. private void DeletePixels(object parameter)
  71. {
  72. var doc = Owner.BitmapManager.ActiveDocument;
  73. Owner.BitmapManager.BitmapOperations.DeletePixels(
  74. doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
  75. doc.ActiveSelection.SelectedPoints.ToArray());
  76. }
  77. private void OpenResizePopup(object parameter)
  78. {
  79. bool isCanvasDialog = (string)parameter == "canvas";
  80. ResizeDocumentDialog dialog = new ResizeDocumentDialog(
  81. Owner.BitmapManager.ActiveDocument.Width,
  82. Owner.BitmapManager.ActiveDocument.Height,
  83. isCanvasDialog);
  84. if (dialog.ShowDialog())
  85. {
  86. if (isCanvasDialog)
  87. {
  88. Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
  89. }
  90. else
  91. {
  92. Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
  93. }
  94. }
  95. }
  96. private void CenterContent(object property)
  97. {
  98. Owner.BitmapManager.ActiveDocument.CenterContent();
  99. }
  100. }
  101. }