DocumentViewModel.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Owner.BitmapManager.BitmapOperations.DeletePixels(
  47. Owner.BitmapManager.ActiveDocument.Layers.Where(x => x.IsActive && x.IsVisible).ToArray(),
  48. Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.ToArray());
  49. }
  50. private void OpenResizePopup(object parameter)
  51. {
  52. bool isCanvasDialog = (string)parameter == "canvas";
  53. ResizeDocumentDialog dialog = new ResizeDocumentDialog(
  54. Owner.BitmapManager.ActiveDocument.Width,
  55. Owner.BitmapManager.ActiveDocument.Height,
  56. isCanvasDialog);
  57. if (dialog.ShowDialog())
  58. {
  59. if (isCanvasDialog)
  60. {
  61. Owner.BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
  62. }
  63. else
  64. {
  65. Owner.BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
  66. }
  67. }
  68. }
  69. private void CenterContent(object property)
  70. {
  71. Owner.BitmapManager.ActiveDocument.CenterContent();
  72. }
  73. }
  74. }