FileViewModel.cs 6.0 KB

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