FileViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Exceptions;
  8. using PixiEditor.Helpers;
  9. using PixiEditor.Models.Controllers;
  10. using PixiEditor.Models.DataHolders;
  11. using PixiEditor.Models.Dialogs;
  12. using PixiEditor.Models.Enums;
  13. using PixiEditor.Models.IO;
  14. using PixiEditor.Models.UserPreferences;
  15. using PixiEditor.Parser;
  16. namespace PixiEditor.ViewModels.SubViewModels.Main
  17. {
  18. public class FileViewModel : SubViewModel<ViewModelMain>
  19. {
  20. public RelayCommand OpenNewFilePopupCommand { get; set; }
  21. public RelayCommand SaveDocumentCommand { get; set; }
  22. public RelayCommand OpenFileCommand { get; set; }
  23. public RelayCommand ExportFileCommand { get; set; } // Command that is used to save file
  24. public FileViewModel(ViewModelMain owner)
  25. : base(owner)
  26. {
  27. OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
  28. SaveDocumentCommand = new RelayCommand(SaveDocument, Owner.DocumentIsNotNull);
  29. OpenFileCommand = new RelayCommand(Open);
  30. ExportFileCommand = new RelayCommand(ExportFile, CanSave);
  31. Owner.OnStartupEvent += Owner_OnStartupEvent;
  32. }
  33. /// <summary>
  34. /// Generates new Layer and sets it as active one.
  35. /// </summary>
  36. /// <param name="parameter">CommandParameter.</param>
  37. public void OpenNewFilePopup(object parameter)
  38. {
  39. NewFileDialog newFile = new NewFileDialog();
  40. if (newFile.ShowDialog())
  41. {
  42. NewDocument(newFile.Width, newFile.Height);
  43. }
  44. }
  45. public void NewDocument(int width, int height, bool addBaseLayer = true)
  46. {
  47. Owner.BitmapManager.Documents.Add(new Document(width, height));
  48. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents[^1];
  49. if (addBaseLayer)
  50. {
  51. Owner.BitmapManager.ActiveDocument.AddNewLayer("Base Layer");
  52. }
  53. Owner.ResetProgramStateValues();
  54. }
  55. /// <summary>
  56. /// Opens file from path.
  57. /// </summary>
  58. /// <param name="path">Path to file.</param>
  59. public void OpenFile(string path)
  60. {
  61. ImportFileDialog dialog = new ImportFileDialog();
  62. if (path != null && File.Exists(path))
  63. {
  64. dialog.FilePath = path;
  65. }
  66. if (dialog.ShowDialog())
  67. {
  68. NewDocument(dialog.FileWidth, dialog.FileHeight, false);
  69. Owner.BitmapManager.ActiveDocument.DocumentFilePath = path;
  70. Owner.BitmapManager.ActiveDocument.AddNewLayer(
  71. "Image",
  72. Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
  73. }
  74. }
  75. public void SaveDocument(bool asNew)
  76. {
  77. SaveDocument(parameter: asNew ? "asnew" : null);
  78. }
  79. private void Owner_OnStartupEvent(object sender, System.EventArgs e)
  80. {
  81. var lastArg = Environment.GetCommandLineArgs().Last();
  82. if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
  83. {
  84. Open(lastArg);
  85. }
  86. else
  87. {
  88. if (PreferencesSettings.GetPreference("ShowNewFilePopupOnStartup", true))
  89. {
  90. OpenNewFilePopup(null);
  91. }
  92. }
  93. }
  94. private void Open(string path)
  95. {
  96. try
  97. {
  98. if (path.EndsWith(".pixi"))
  99. {
  100. OpenDocument(path);
  101. }
  102. else
  103. {
  104. OpenFile(path);
  105. }
  106. Owner.ResetProgramStateValues();
  107. }
  108. catch (CorruptedFileException ex)
  109. {
  110. MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
  111. }
  112. catch (OldFileFormatException)
  113. {
  114. MessageBoxResult result = MessageBox.Show("This pixi file uses the old file format and is insecure.\nOnly continue if you trust the source of the file", "Old file format", MessageBoxButton.OKCancel);
  115. if (result == MessageBoxResult.OK)
  116. {
  117. try
  118. {
  119. OpenDocument(path, true);
  120. }
  121. catch (CorruptedFileException ex)
  122. {
  123. MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
  124. }
  125. }
  126. }
  127. }
  128. private void Open(object property)
  129. {
  130. OpenFileDialog dialog = new OpenFileDialog
  131. {
  132. Filter = "All Files|*.*|PixiEditor Files | *.pixi|PNG Files|*.png",
  133. DefaultExt = "pixi"
  134. };
  135. if ((bool)dialog.ShowDialog())
  136. {
  137. if (Importer.IsSupportedFile(dialog.FileName))
  138. {
  139. Open(dialog.FileName);
  140. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  141. }
  142. }
  143. }
  144. private void OpenDocument(string path, bool openOld = false)
  145. {
  146. Document document;
  147. if (openOld)
  148. {
  149. document = Importer.ImportOldDocument(path);
  150. }
  151. else
  152. {
  153. document = Importer.ImportDocument(path);
  154. }
  155. if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
  156. {
  157. Owner.BitmapManager.Documents.Add(document);
  158. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  159. }
  160. else
  161. {
  162. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.First(y => y.DocumentFilePath == path);
  163. }
  164. }
  165. private void SaveDocument(object parameter)
  166. {
  167. bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
  168. if (paramIsAsNew ||
  169. string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath) ||
  170. !Owner.BitmapManager.ActiveDocument.DocumentFilePath.EndsWith(".pixi"))
  171. {
  172. Owner.BitmapManager.ActiveDocument.SaveWithDialog();
  173. }
  174. else
  175. {
  176. Owner.BitmapManager.ActiveDocument.Save();
  177. }
  178. }
  179. /// <summary>
  180. /// Generates export dialog or saves directly if save data is known.
  181. /// </summary>
  182. /// <param name="parameter">CommandProperty.</param>
  183. private void ExportFile(object parameter)
  184. {
  185. WriteableBitmap bitmap = Owner.BitmapManager.GetCombinedLayersBitmap();
  186. Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  187. }
  188. /// <summary>
  189. /// Returns true if file save is possible.
  190. /// </summary>
  191. /// <param name="property">CommandProperty.</param>
  192. /// <returns>True if active document is not null.</returns>
  193. private bool CanSave(object property)
  194. {
  195. return Owner.BitmapManager.ActiveDocument != null;
  196. }
  197. }
  198. }