FileViewModel.cs 8.9 KB

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