FileViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json.Linq;
  3. using PixiEditor.Exceptions;
  4. using PixiEditor.Helpers;
  5. using PixiEditor.Models;
  6. using PixiEditor.Models.DataHolders;
  7. using PixiEditor.Models.Dialogs;
  8. using PixiEditor.Models.IO;
  9. using PixiEditor.Models.UserPreferences;
  10. using PixiEditor.Parser;
  11. using PixiEditor.Views.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Drawing.Imaging;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Windows;
  18. using System.Windows.Media.Imaging;
  19. using PixiEditor.Models.Controllers;
  20. namespace PixiEditor.ViewModels.SubViewModels.Main
  21. {
  22. public class FileViewModel : SubViewModel<ViewModelMain>
  23. {
  24. private bool hasRecent;
  25. public RelayCommand OpenNewFilePopupCommand { get; set; }
  26. public RelayCommand SaveDocumentCommand { get; set; }
  27. public RelayCommand OpenFileCommand { get; set; }
  28. public RelayCommand ExportFileCommand { get; set; } // Command that is used to save file
  29. public RelayCommand OpenRecentCommand { get; set; }
  30. public RelayCommand RemoveRecentlyOpenedCommand { get; set; }
  31. public bool HasRecent
  32. {
  33. get => hasRecent;
  34. set
  35. {
  36. hasRecent = value;
  37. RaisePropertyChanged(nameof(HasRecent));
  38. }
  39. }
  40. public RecentlyOpenedCollection RecentlyOpened { get; set; } = new RecentlyOpenedCollection();
  41. public FileViewModel(ViewModelMain owner)
  42. : base(owner)
  43. {
  44. OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
  45. SaveDocumentCommand = new RelayCommand(SaveDocument, Owner.DocumentIsNotNull);
  46. OpenFileCommand = new RelayCommand(Open);
  47. ExportFileCommand = new RelayCommand(ExportFile, CanSave);
  48. OpenRecentCommand = new RelayCommand(OpenRecent);
  49. RemoveRecentlyOpenedCommand = new RelayCommand(RemoveRecentlyOpened);
  50. Owner.OnStartupEvent += Owner_OnStartupEvent;
  51. RecentlyOpened = new RecentlyOpenedCollection(GetRecentlyOpenedDocuments());
  52. if (RecentlyOpened.Count > 0)
  53. {
  54. HasRecent = true;
  55. }
  56. IPreferences.Current.AddCallback("MaxOpenedRecently", UpdateMaxRecentlyOpened);
  57. }
  58. public void OpenRecent(object parameter)
  59. {
  60. string path = (string)parameter;
  61. foreach (Document document in Owner.BitmapManager.Documents)
  62. {
  63. if (document.DocumentFilePath == path)
  64. {
  65. Owner.BitmapManager.ActiveDocument = document;
  66. return;
  67. }
  68. }
  69. if (!File.Exists(path))
  70. {
  71. NoticeDialog.Show("The file does not exist", "Failed to open the file");
  72. RecentlyOpened.Remove(path);
  73. return;
  74. }
  75. Open((string)parameter);
  76. }
  77. public void RemoveRecentlyOpened(object parameter)
  78. {
  79. if (RecentlyOpened.Contains((string)parameter))
  80. {
  81. RecentlyOpened.Remove((string)parameter);
  82. }
  83. }
  84. /// <summary>
  85. /// Generates new Layer and sets it as active one.
  86. /// </summary>
  87. /// <param name="parameter">CommandParameter.</param>
  88. public void OpenNewFilePopup(object parameter)
  89. {
  90. NewFileDialog newFile = new NewFileDialog();
  91. if (newFile.ShowDialog())
  92. {
  93. NewDocument(newFile.Width, newFile.Height);
  94. }
  95. }
  96. public void OpenHelloTherePopup()
  97. {
  98. new HelloTherePopup(this).Show();
  99. }
  100. public void NewDocument(int width, int height, bool addBaseLayer = true)
  101. {
  102. Owner.BitmapManager.Documents.Add(new Document(width, height));
  103. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents[^1];
  104. if (addBaseLayer)
  105. {
  106. Owner.BitmapManager.ActiveDocument.AddNewLayer("Base Layer");
  107. }
  108. Owner.ResetProgramStateValues();
  109. }
  110. /// <summary>
  111. /// Opens file from path.
  112. /// </summary>
  113. /// <param name="path">Path to file.</param>
  114. public void OpenFile(string path)
  115. {
  116. ImportFileDialog dialog = new ImportFileDialog();
  117. if (path != null && File.Exists(path))
  118. {
  119. dialog.FilePath = path;
  120. }
  121. if (dialog.ShowDialog())
  122. {
  123. NewDocument(dialog.FileWidth, dialog.FileHeight, false);
  124. Owner.BitmapManager.ActiveDocument.DocumentFilePath = path;
  125. Owner.BitmapManager.ActiveDocument.AddNewLayer(
  126. "Image",
  127. Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight));
  128. Owner.BitmapManager.ActiveDocument.UpdatePreviewImage();
  129. }
  130. }
  131. public void SaveDocument(bool asNew)
  132. {
  133. SaveDocument(parameter: asNew ? "asnew" : null);
  134. }
  135. public void OpenAny()
  136. {
  137. Open((object)null);
  138. }
  139. public void Open(string path)
  140. {
  141. try
  142. {
  143. if (path.EndsWith(".pixi"))
  144. {
  145. OpenDocument(path);
  146. }
  147. else
  148. {
  149. OpenFile(path);
  150. }
  151. Owner.ResetProgramStateValues();
  152. }
  153. catch (CorruptedFileException ex)
  154. {
  155. NoticeDialog.Show(ex.Message, "Failed to open the file");
  156. }
  157. catch (OldFileFormatException)
  158. {
  159. NoticeDialog.Show("This .pixi file uses the old format,\n which is no longer supported and can't be opened.", "Old file format");
  160. }
  161. }
  162. private void Owner_OnStartupEvent(object sender, System.EventArgs e)
  163. {
  164. var args = StartupArgs.Args;
  165. var file = args.FirstOrDefault(x => Importer.IsSupportedFile(x) && File.Exists(x));
  166. if (file != null)
  167. {
  168. Open(file);
  169. }
  170. else if ((Owner.BitmapManager.Documents.Count == 0
  171. || !args.Contains("--crash")) && !args.Contains("--openedInExisting"))
  172. {
  173. if (IPreferences.Current.GetPreference("ShowStartupWindow", true))
  174. {
  175. OpenHelloTherePopup();
  176. }
  177. }
  178. }
  179. private void Open(object property)
  180. {
  181. var filter = SupportedFilesHelper.BuildOpenFilter();
  182. OpenFileDialog dialog = new OpenFileDialog
  183. {
  184. Filter = filter,
  185. FilterIndex = 0
  186. };
  187. if ((bool)dialog.ShowDialog())
  188. {
  189. if (Importer.IsSupportedFile(dialog.FileName))
  190. {
  191. Open(dialog.FileName);
  192. if (Owner.BitmapManager.Documents.Count > 0)
  193. {
  194. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  195. }
  196. }
  197. }
  198. }
  199. private void OpenDocument(string path)
  200. {
  201. Document document = Importer.ImportDocument(path);
  202. if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
  203. {
  204. Owner.BitmapManager.Documents.Add(document);
  205. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  206. }
  207. else
  208. {
  209. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.First(y => y.DocumentFilePath == path);
  210. }
  211. }
  212. private void SaveDocument(object parameter)
  213. {
  214. bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
  215. if (paramIsAsNew ||
  216. string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath))
  217. {
  218. Owner.BitmapManager.ActiveDocument.SaveWithDialog();
  219. }
  220. else
  221. {
  222. Owner.BitmapManager.ActiveDocument.Save();
  223. }
  224. }
  225. /// <summary>
  226. /// Generates export dialog or saves directly if save data is known.
  227. /// </summary>
  228. /// <param name="parameter">CommandProperty.</param>
  229. private void ExportFile(object parameter)
  230. {
  231. ViewModelMain.Current.ActionDisplay = "";
  232. WriteableBitmap bitmap = Owner.BitmapManager.ActiveDocument.Renderer.FinalBitmap;
  233. Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  234. }
  235. /// <summary>
  236. /// Returns true if file save is possible.
  237. /// </summary>
  238. /// <param name="property">CommandProperty.</param>
  239. /// <returns>True if active document is not null.</returns>
  240. private bool CanSave(object property)
  241. {
  242. return Owner.BitmapManager.ActiveDocument != null;
  243. }
  244. private void UpdateMaxRecentlyOpened(object parameter)
  245. {
  246. int newAmount = (int)parameter;
  247. if (newAmount >= RecentlyOpened.Count)
  248. {
  249. return;
  250. }
  251. var recentlyOpeneds = new List<RecentlyOpenedDocument>(RecentlyOpened.Take(newAmount));
  252. RecentlyOpened.Clear();
  253. foreach (var recent in recentlyOpeneds)
  254. {
  255. RecentlyOpened.Add(recent);
  256. }
  257. }
  258. private List<RecentlyOpenedDocument> GetRecentlyOpenedDocuments()
  259. {
  260. var paths = IPreferences.Current.GetLocalPreference(nameof(RecentlyOpened), new JArray()).ToObject<string[]>()
  261. .Take(IPreferences.Current.GetPreference("MaxOpenedRecently", 8));
  262. List<RecentlyOpenedDocument> documents = new List<RecentlyOpenedDocument>();
  263. foreach (string path in paths)
  264. {
  265. documents.Add(new RecentlyOpenedDocument(path));
  266. }
  267. return documents;
  268. }
  269. }
  270. }