FileViewModel.cs 6.3 KB

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