FileViewModel.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. using PixiEditor.Views.Dialogs;
  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 no longer exist at that path");
  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. }
  129. }
  130. public void SaveDocument(bool asNew)
  131. {
  132. SaveDocument(parameter: asNew ? "asnew" : null);
  133. }
  134. public void OpenAny()
  135. {
  136. Open((object)null);
  137. }
  138. private void Owner_OnStartupEvent(object sender, System.EventArgs e)
  139. {
  140. var lastArg = Environment.GetCommandLineArgs().Last();
  141. if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
  142. {
  143. Open(lastArg);
  144. }
  145. else
  146. {
  147. if (IPreferences.Current.GetPreference("ShowStartupWindow", true))
  148. {
  149. OpenHelloTherePopup();
  150. }
  151. }
  152. }
  153. private void Open(string path)
  154. {
  155. try
  156. {
  157. if (path.EndsWith(".pixi"))
  158. {
  159. OpenDocument(path);
  160. }
  161. else
  162. {
  163. OpenFile(path);
  164. }
  165. Owner.ResetProgramStateValues();
  166. }
  167. catch (CorruptedFileException ex)
  168. {
  169. NoticeDialog.Show(ex.Message, "Failed to open file.");
  170. }
  171. catch (OldFileFormatException)
  172. {
  173. NoticeDialog.Show("This .pixi file uses the old format, which is no longer supported and can't be opened.", "Old file format");
  174. }
  175. }
  176. private void Open(object property)
  177. {
  178. OpenFileDialog dialog = new OpenFileDialog
  179. {
  180. Filter =
  181. "Any|*.pixi;*.png;*.jpg;*.jpeg;|" +
  182. "PixiEditor Files | *.pixi|" +
  183. "Image Files|*.png;*.jpg;*.jpeg;",
  184. DefaultExt = "pixi"
  185. };
  186. if ((bool)dialog.ShowDialog())
  187. {
  188. if (Importer.IsSupportedFile(dialog.FileName))
  189. {
  190. Open(dialog.FileName);
  191. if (Owner.BitmapManager.Documents.Count > 0)
  192. {
  193. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  194. }
  195. }
  196. }
  197. }
  198. private void OpenDocument(string path)
  199. {
  200. Document document = Importer.ImportDocument(path);
  201. if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
  202. {
  203. Owner.BitmapManager.Documents.Add(document);
  204. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
  205. }
  206. else
  207. {
  208. Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.First(y => y.DocumentFilePath == path);
  209. }
  210. }
  211. private void SaveDocument(object parameter)
  212. {
  213. bool paramIsAsNew = parameter != null && parameter.ToString()?.ToLower() == "asnew";
  214. if (paramIsAsNew ||
  215. string.IsNullOrEmpty(Owner.BitmapManager.ActiveDocument.DocumentFilePath) ||
  216. !Owner.BitmapManager.ActiveDocument.DocumentFilePath.EndsWith(".pixi"))
  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. WriteableBitmap bitmap = Owner.BitmapManager.GetCombinedLayersBitmap();
  232. Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  233. }
  234. /// <summary>
  235. /// Returns true if file save is possible.
  236. /// </summary>
  237. /// <param name="property">CommandProperty.</param>
  238. /// <returns>True if active document is not null.</returns>
  239. private bool CanSave(object property)
  240. {
  241. return Owner.BitmapManager.ActiveDocument != null;
  242. }
  243. private void UpdateMaxRecentlyOpened(object parameter)
  244. {
  245. int newAmount = (int)parameter;
  246. if (newAmount >= RecentlyOpened.Count)
  247. {
  248. return;
  249. }
  250. var recentlyOpeneds = new List<RecentlyOpenedDocument>(RecentlyOpened.Take(newAmount));
  251. RecentlyOpened.Clear();
  252. foreach (var recent in recentlyOpeneds)
  253. {
  254. RecentlyOpened.Add(recent);
  255. }
  256. }
  257. private List<RecentlyOpenedDocument> GetRecentlyOpenedDocuments()
  258. {
  259. var paths = IPreferences.Current.GetLocalPreference(nameof(RecentlyOpened), new JArray()).ToObject<string[]>()
  260. .Take(IPreferences.Current.GetPreference("MaxOpenedRecently", 8));
  261. List<RecentlyOpenedDocument> documents = new List<RecentlyOpenedDocument>();
  262. foreach (string path in paths)
  263. {
  264. documents.Add(new RecentlyOpenedDocument(path));
  265. }
  266. return documents;
  267. }
  268. }
  269. }