DocumentViewModel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Linq;
  3. using PixiEditor.Helpers;
  4. using PixiEditor.Models.DataHolders;
  5. using PixiEditor.Models.Dialogs;
  6. using PixiEditor.Models.Enums;
  7. namespace PixiEditor.ViewModels.SubViewModels.Main
  8. {
  9. public class DocumentViewModel : SubViewModel<ViewModelMain>
  10. {
  11. public const string ConfirmationDialogMessage = "Document was modified. Do you want to save changes?";
  12. public RelayCommand CenterContentCommand { get; set; }
  13. public RelayCommand ClipCanvasCommand { get; set; }
  14. public RelayCommand DeletePixelsCommand { get; set; }
  15. public RelayCommand OpenResizePopupCommand { get; set; }
  16. public DocumentViewModel(ViewModelMain owner)
  17. : base(owner)
  18. {
  19. CenterContentCommand = new RelayCommand(CenterContent, Owner.DocumentIsNotNull);
  20. ClipCanvasCommand = new RelayCommand(ClipCanvas, Owner.DocumentIsNotNull);
  21. DeletePixelsCommand = new RelayCommand(DeletePixels, Owner.SelectionSubViewModel.SelectionIsNotEmpty);
  22. OpenResizePopupCommand = new RelayCommand(OpenResizePopup, Owner.DocumentIsNotNull);
  23. }
  24. public void ClipCanvas(object parameter)
  25. {
  26. Owner.BitmapManager.ActiveDocument?.ClipCanvas();
  27. }
  28. public void RequestCloseDocument(Document document)
  29. {
  30. if (!document.ChangesSaved)
  31. {
  32. ConfirmationType result = ConfirmationDialog.Show(ConfirmationDialogMessage);
  33. if (result == ConfirmationType.Yes)
  34. {
  35. Owner.FileSubViewModel.SaveDocument(false);
  36. }
  37. else if (result == ConfirmationType.Canceled)
  38. {
  39. return;
  40. }
  41. }
  42. Owner.BitmapManager.CloseDocument(document);
  43. }
  44. private void DeletePixels(object parameter)
  45. {
  46. var doc = Owner.BitmapManager.ActiveDocument;
  47. Owner.BitmapManager.BitmapOperations.DeletePixels(
  48. doc.Layers.Where(x => x.IsActive && doc.GetFinalLayerIsVisible(x)).ToArray(),
  49. doc.ActiveSelection.SelectedPoints.ToArray());
  50. }
  51. private void OpenResizePopup(object parameter)
  52. {
  53. bool isCanvasDialog = (string)parameter == "canvas";
  54. ResizeDocumentDialog dialog = new ResizeDocumentDialog(
  55. Owner.BitmapManager.ActiveDocument.Width,
  56. Owner.BitmapManager.ActiveDocument.Height,
  57. isCanvasDialog);
  58. if (dialog.ShowDialog())
  59. {
  60. if (isCanvasDialog)
  61. {
  62. Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
  63. }
  64. else
  65. {
  66. Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
  67. }
  68. }
  69. }
  70. private void CenterContent(object property)
  71. {
  72. Owner.BitmapManager.ActiveDocument.CenterContent();
  73. }
  74. }
  75. }