FileViewModel.cs 8.4 KB

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