ViewModelMain.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Input;
  7. using PixiEditor.Helpers;
  8. using PixiEditor.Models.Controllers;
  9. using PixiEditor.Models.Controllers.Shortcuts;
  10. using PixiEditor.Models.DataHolders;
  11. using PixiEditor.Models.Dialogs;
  12. using PixiEditor.Models.Enums;
  13. using PixiEditor.Models.Events;
  14. using PixiEditor.Models.IO;
  15. using PixiEditor.Models.Position;
  16. using PixiEditor.Models.Tools;
  17. using PixiEditor.Models.Tools.Tools;
  18. using PixiEditor.Models.UserPreferences;
  19. using PixiEditor.ViewModels.SubViewModels.Main;
  20. namespace PixiEditor.ViewModels
  21. {
  22. public class ViewModelMain : ViewModelBase
  23. {
  24. public static ViewModelMain Current { get; set; }
  25. public Action CloseAction { get; set; }
  26. public event EventHandler OnStartupEvent;
  27. public RelayCommand OnStartupCommand { get; set; }
  28. public RelayCommand CloseWindowCommand { get; set; }
  29. public FileViewModel FileSubViewModel { get; set; }
  30. public UpdateViewModel UpdateSubViewModel { get; set; }
  31. public ToolsViewModel ToolsSubViewModel { get; set; }
  32. public IoViewModel IoSubViewModel { get; set; }
  33. public LayersViewModel LayersSubViewModel { get; set; }
  34. public ClipboardViewModel ClipboardSubViewModel { get; set; }
  35. public UndoViewModel UndoSubViewModel { get; set; }
  36. public SelectionViewModel SelectionSubViewModel { get; set; }
  37. public ViewportViewModel ViewportSubViewModel { get; set; }
  38. public ColorsViewModel ColorsSubViewModel { get; set; }
  39. public DocumentViewModel DocumentSubViewModel { get; set; }
  40. public MiscViewModel MiscSubViewModel { get; set; }
  41. public DiscordViewModel DiscordViewModel { get; set; }
  42. public BitmapManager BitmapManager { get; set; }
  43. public PixelChangesController ChangesController { get; set; }
  44. public ShortcutController ShortcutController { get; set; }
  45. public ViewModelMain()
  46. {
  47. PreferencesSettings.Init();
  48. BitmapManager = new BitmapManager();
  49. BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
  50. BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
  51. BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
  52. SelectionSubViewModel = new SelectionViewModel(this);
  53. ChangesController = new PixelChangesController();
  54. OnStartupCommand = new RelayCommand(OnStartup);
  55. CloseWindowCommand = new RelayCommand(CloseWindow);
  56. FileSubViewModel = new FileViewModel(this);
  57. UpdateSubViewModel = new UpdateViewModel(this);
  58. ToolsSubViewModel = new ToolsViewModel(this);
  59. IoSubViewModel = new IoViewModel(this);
  60. LayersSubViewModel = new LayersViewModel(this);
  61. ClipboardSubViewModel = new ClipboardViewModel(this);
  62. UndoSubViewModel = new UndoViewModel(this);
  63. ViewportSubViewModel = new ViewportViewModel(this);
  64. ColorsSubViewModel = new ColorsViewModel(this);
  65. DocumentSubViewModel = new DocumentViewModel(this);
  66. MiscSubViewModel = new MiscViewModel(this);
  67. DiscordViewModel = new DiscordViewModel(this, "764168193685979138");
  68. ShortcutController = new ShortcutController
  69. {
  70. Shortcuts = new List<Shortcut>
  71. {
  72. // Tools
  73. CreateToolShortcut<PenTool>(Key.B),
  74. CreateToolShortcut<EraserTool>(Key.E),
  75. CreateToolShortcut<ColorPickerTool>(Key.O),
  76. CreateToolShortcut<RectangleTool>(Key.R),
  77. CreateToolShortcut<CircleTool>(Key.C),
  78. CreateToolShortcut<LineTool>(Key.L),
  79. CreateToolShortcut<FloodFill>(Key.G),
  80. CreateToolShortcut<BrightnessTool>(Key.U),
  81. CreateToolShortcut<MoveTool>(Key.V),
  82. CreateToolShortcut<SelectTool>(Key.M),
  83. CreateToolShortcut<ZoomTool>(Key.Z),
  84. CreateToolShortcut<MoveViewportTool>(Key.H),
  85. new Shortcut(Key.OemPlus, ViewportSubViewModel.ZoomCommand, 115),
  86. new Shortcut(Key.OemMinus, ViewportSubViewModel.ZoomCommand, 85),
  87. new Shortcut(Key.OemOpenBrackets, ToolsSubViewModel.ChangeToolSizeCommand, -1),
  88. new Shortcut(Key.OemCloseBrackets, ToolsSubViewModel.ChangeToolSizeCommand, 1),
  89. // Editor
  90. new Shortcut(Key.X, ColorsSubViewModel.SwapColorsCommand),
  91. new Shortcut(Key.Y, UndoSubViewModel.RedoCommand, modifier: ModifierKeys.Control),
  92. new Shortcut(Key.Z, UndoSubViewModel.UndoCommand, modifier: ModifierKeys.Control),
  93. new Shortcut(Key.D, SelectionSubViewModel.DeselectCommand, modifier: ModifierKeys.Control),
  94. new Shortcut(Key.A, SelectionSubViewModel.SelectAllCommand, modifier: ModifierKeys.Control),
  95. new Shortcut(Key.C, ClipboardSubViewModel.CopyCommand, modifier: ModifierKeys.Control),
  96. new Shortcut(Key.V, ClipboardSubViewModel.PasteCommand, modifier: ModifierKeys.Control),
  97. new Shortcut(Key.J, ClipboardSubViewModel.DuplicateCommand, modifier: ModifierKeys.Control),
  98. new Shortcut(Key.X, ClipboardSubViewModel.CutCommand, modifier: ModifierKeys.Control),
  99. new Shortcut(Key.Delete, DocumentSubViewModel.DeletePixelsCommand),
  100. new Shortcut(Key.I, DocumentSubViewModel.OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
  101. new Shortcut(Key.C, DocumentSubViewModel.OpenResizePopupCommand, "canvas", ModifierKeys.Control | ModifierKeys.Shift),
  102. new Shortcut(Key.F11, SystemCommands.MaximizeWindowCommand),
  103. // File
  104. new Shortcut(Key.O, FileSubViewModel.OpenFileCommand, modifier: ModifierKeys.Control),
  105. new Shortcut(Key.S, FileSubViewModel.ExportFileCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
  106. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, modifier: ModifierKeys.Control),
  107. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
  108. new Shortcut(Key.N, FileSubViewModel.OpenNewFilePopupCommand, modifier: ModifierKeys.Control)
  109. }
  110. };
  111. BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
  112. Current = this;
  113. }
  114. /// <summary>
  115. /// Resets most variables and controller, so new documents can be handled.
  116. /// </summary>
  117. public void ResetProgramStateValues()
  118. {
  119. foreach (var document in BitmapManager.Documents)
  120. {
  121. document.PreviewLayer = null;
  122. }
  123. BitmapManager.ActiveDocument?.CenterViewport();
  124. }
  125. public bool DocumentIsNotNull(object property)
  126. {
  127. return BitmapManager.ActiveDocument != null;
  128. }
  129. private Shortcut CreateToolShortcut<T>(Key key, ModifierKeys modifier = ModifierKeys.None)
  130. where T : Tool
  131. {
  132. return new Shortcut(key, ToolsSubViewModel.SelectToolCommand, typeof(T), modifier);
  133. }
  134. private void CloseWindow(object property)
  135. {
  136. if (!(property is CancelEventArgs))
  137. {
  138. throw new ArgumentException();
  139. }
  140. ((CancelEventArgs)property).Cancel = !RemoveDocumentsWithSaveConfirmation();
  141. }
  142. /// <summary>
  143. /// Removes documents with unsaved changes confirmation dialog.
  144. /// </summary>
  145. /// <returns>If documents was removed successfully.</returns>
  146. private bool RemoveDocumentsWithSaveConfirmation()
  147. {
  148. int docCount = BitmapManager.Documents.Count;
  149. for (int i = 0; i < docCount; i++)
  150. {
  151. BitmapManager.ActiveDocument = BitmapManager.Documents.First();
  152. bool canceled = !RemoveDocumentWithSaveConfirmation();
  153. if (canceled)
  154. {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. /// <summary>
  161. /// Removes document with unsaved changes confirmation dialog.
  162. /// </summary>
  163. /// <returns>If document was removed successfully.</returns>
  164. private bool RemoveDocumentWithSaveConfirmation()
  165. {
  166. ConfirmationType result = ConfirmationType.No;
  167. if (!BitmapManager.ActiveDocument.ChangesSaved)
  168. {
  169. result = ConfirmationDialog.Show(DocumentViewModel.ConfirmationDialogMessage);
  170. if (result == ConfirmationType.Yes)
  171. {
  172. FileSubViewModel.SaveDocument(false);
  173. }
  174. }
  175. if (result != ConfirmationType.Canceled)
  176. {
  177. BitmapManager.Documents.Remove(BitmapManager.ActiveDocument);
  178. return true;
  179. }
  180. else
  181. {
  182. return false;
  183. }
  184. }
  185. private void OnStartup(object parameter)
  186. {
  187. OnStartupEvent?.Invoke(this, EventArgs.Empty);
  188. }
  189. private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
  190. {
  191. if (e.NewDocument != null)
  192. {
  193. e.NewDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
  194. }
  195. }
  196. private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
  197. {
  198. BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>());
  199. BitmapManager.ActiveDocument.CenterViewport();
  200. BitmapManager.ActiveDocument.ChangesSaved = false;
  201. }
  202. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  203. {
  204. UndoSubViewModel.TriggerNewUndoChange(BitmapManager.SelectedTool);
  205. }
  206. private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
  207. {
  208. ChangesController.AddChanges(
  209. new LayerChange(e.PixelsChanged, e.ChangedLayerGuid),
  210. new LayerChange(e.OldPixelsValues, e.ChangedLayerGuid));
  211. BitmapManager.ActiveDocument.ChangesSaved = false;
  212. if (BitmapManager.IsOperationTool())
  213. {
  214. ColorsSubViewModel.AddSwatch(ColorsSubViewModel.PrimaryColor);
  215. }
  216. }
  217. }
  218. }