FileViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Microsoft.Win32;
  2. using PixiEditor.Helpers;
  3. using PixiEditor.Models.DataHolders;
  4. using PixiEditor.Models.Dialogs;
  5. using PixiEditor.Models.Enums;
  6. using PixiEditor.Models.IO;
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Media.Imaging;
  12. namespace PixiEditor.ViewModels.SubViewModels.Main
  13. {
  14. public class FileViewModel : SubViewModel<ViewModelMain>
  15. {
  16. public RelayCommand OpenNewFilePopupCommand { get; set; }
  17. public RelayCommand SaveDocumentCommand { get; set; }
  18. public RelayCommand OpenFileCommand { get; set; }
  19. public RelayCommand ExportFileCommand { get; set; } //Command that is used to save file
  20. public FileViewModel(ViewModelMain owner) : base(owner)
  21. {
  22. OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
  23. SaveDocumentCommand = new RelayCommand(SaveDocument, Owner.DocumentIsNotNull);
  24. OpenFileCommand = new RelayCommand(Open);
  25. ExportFileCommand = new RelayCommand(ExportFile, CanSave);
  26. Owner.OnStartupEvent += Owner_OnStartupEvent;
  27. }
  28. private void Owner_OnStartupEvent(object sender, System.EventArgs e)
  29. {
  30. var lastArg = Environment.GetCommandLineArgs().Last();
  31. if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
  32. {
  33. Open(lastArg);
  34. }
  35. else
  36. {
  37. OpenNewFilePopup(null);
  38. }
  39. }
  40. /// <summary>
  41. /// Generates new Layer and sets it as active one
  42. /// </summary>
  43. /// <param name="parameter"></param>
  44. public void OpenNewFilePopup(object parameter)
  45. {
  46. NewFileDialog newFile = new NewFileDialog();
  47. if (newFile.ShowDialog())
  48. {
  49. NewDocument(newFile.Width, newFile.Height);
  50. }
  51. }
  52. public void NewDocument(int width, int height, bool addBaseLayer = true)
  53. {
  54. Owner.BitmapManager.ActiveDocument = new Document(width, height);
  55. if (addBaseLayer)
  56. {
  57. Owner.BitmapManager.AddNewLayer("Base Layer");
  58. }
  59. Owner.ResetProgramStateValues();
  60. }
  61. /// <summary>
  62. /// Opens file from path.
  63. /// </summary>
  64. /// <param name="path"></param>
  65. public void OpenFile(string path)
  66. {
  67. ImportFileDialog dialog = new ImportFileDialog();
  68. if (path != null && File.Exists(path))
  69. dialog.FilePath = path;
  70. if (dialog.ShowDialog())
  71. {
  72. NewDocument(dialog.FileWidth, dialog.FileHeight, false);
  73. Owner.BitmapManager.AddNewLayer("Image", Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
  74. }
  75. }
  76. private void Open(string path)
  77. {
  78. if (Owner.DocumentSubViewModel.UnsavedDocumentModified)
  79. {
  80. var result = ConfirmationDialog.Show(DocumentViewModel.ConfirmationDialogMessage);
  81. if (result == ConfirmationType.Yes)
  82. {
  83. SaveDocument(null);
  84. }
  85. else if (result == ConfirmationType.Canceled)
  86. {
  87. return;
  88. }
  89. }
  90. Owner.ResetProgramStateValues();
  91. if (path.EndsWith(".pixi"))
  92. OpenDocument(path);
  93. else
  94. OpenFile(path);
  95. }
  96. private void Open(object property)
  97. {
  98. OpenFileDialog dialog = new OpenFileDialog
  99. {
  100. Filter = "All Files|*.*|PixiEditor Files | *.pixi|PNG Files|*.png",
  101. DefaultExt = "pixi"
  102. };
  103. if ((bool)dialog.ShowDialog())
  104. {
  105. if (Importer.IsSupportedFile(dialog.FileName))
  106. Open(dialog.FileName);
  107. Owner.ViewportSubViewModel.CenterViewport();
  108. }
  109. }
  110. private void OpenDocument(string path)
  111. {
  112. Owner.BitmapManager.ActiveDocument = Importer.ImportDocument(path);
  113. Exporter.SaveDocumentPath = path;
  114. Owner.DocumentSubViewModel.UnsavedDocumentModified = false;
  115. }
  116. public void SaveDocument(bool asNew)
  117. {
  118. SaveDocument(parameter: asNew ? "asnew" : null);
  119. }
  120. private void SaveDocument(object parameter)
  121. {
  122. bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
  123. if (paramIsAsNew || Exporter.SaveDocumentPath == null)
  124. {
  125. var saved = Exporter.SaveAsEditableFileWithDialog(Owner.BitmapManager.ActiveDocument, !paramIsAsNew);
  126. Owner.DocumentSubViewModel.UnsavedDocumentModified = Owner.DocumentSubViewModel.UnsavedDocumentModified && !saved;
  127. }
  128. else
  129. {
  130. Exporter.SaveAsEditableFile(Owner.BitmapManager.ActiveDocument, Exporter.SaveDocumentPath);
  131. Owner.DocumentSubViewModel.UnsavedDocumentModified = false;
  132. }
  133. }
  134. /// <summary>
  135. /// Generates export dialog or saves directly if save data is known.
  136. /// </summary>
  137. /// <param name="parameter"></param>
  138. private void ExportFile(object parameter)
  139. {
  140. WriteableBitmap bitmap = Owner.BitmapManager.GetCombinedLayersBitmap();
  141. Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  142. }
  143. /// <summary>
  144. /// Returns true if file save is possible.
  145. /// </summary>
  146. /// <param name="property"></param>
  147. /// <returns></returns>
  148. private bool CanSave(object property)
  149. {
  150. return Owner.BitmapManager.ActiveDocument != null;
  151. }
  152. }
  153. }