FileViewModel.cs 9.8 KB

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