ViewModelMain.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Microsoft.Extensions.DependencyInjection;
  8. using PixiEditor.Helpers;
  9. using PixiEditor.Models.Controllers;
  10. using PixiEditor.Models.Controllers.Shortcuts;
  11. using PixiEditor.Models.DataHolders;
  12. using PixiEditor.Models.Dialogs;
  13. using PixiEditor.Models.Enums;
  14. using PixiEditor.Models.Events;
  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. using PixiEditor.Views.Dialogs;
  21. namespace PixiEditor.ViewModels
  22. {
  23. public class ViewModelMain : ViewModelBase
  24. {
  25. public static ViewModelMain Current { get; set; }
  26. public Action CloseAction { get; set; }
  27. public event EventHandler OnStartupEvent;
  28. public RelayCommand OnStartupCommand { get; set; }
  29. public RelayCommand CloseWindowCommand { get; set; }
  30. public FileViewModel FileSubViewModel { get; set; }
  31. public UpdateViewModel UpdateSubViewModel { get; set; }
  32. public ToolsViewModel ToolsSubViewModel { get; set; }
  33. public IoViewModel IoSubViewModel { get; set; }
  34. public LayersViewModel LayersSubViewModel { get; set; }
  35. public ClipboardViewModel ClipboardSubViewModel { get; set; }
  36. public UndoViewModel UndoSubViewModel { get; set; }
  37. public SelectionViewModel SelectionSubViewModel { get; set; }
  38. public ViewportViewModel ViewportSubViewModel { get; set; }
  39. public ColorsViewModel ColorsSubViewModel { get; set; }
  40. public DocumentViewModel DocumentSubViewModel { get; set; }
  41. public MiscViewModel MiscSubViewModel { get; set; }
  42. public DiscordViewModel DiscordViewModel { get; set; }
  43. #if DEBUG
  44. public DebugViewModel DebugSubViewModel { get; set; }
  45. #endif
  46. public BitmapManager BitmapManager { get; set; }
  47. public PixelChangesController ChangesController { get; set; }
  48. public ShortcutController ShortcutController { get; set; }
  49. public IPreferences Preferences { get; set; }
  50. public bool IsDebug
  51. {
  52. get =>
  53. #if DEBUG
  54. true;
  55. #else
  56. false;
  57. #endif
  58. }
  59. public ViewModelMain(IServiceProvider services)
  60. {
  61. Current = this;
  62. Preferences = services.GetRequiredService<IPreferences>();
  63. Preferences.Init();
  64. BitmapManager = new BitmapManager();
  65. BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
  66. BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
  67. BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
  68. SelectionSubViewModel = new SelectionViewModel(this);
  69. ChangesController = new PixelChangesController();
  70. OnStartupCommand = new RelayCommand(OnStartup);
  71. CloseWindowCommand = new RelayCommand(CloseWindow);
  72. FileSubViewModel = new FileViewModel(this);
  73. UpdateSubViewModel = new UpdateViewModel(this);
  74. ToolsSubViewModel = new ToolsViewModel(this);
  75. IoSubViewModel = new IoViewModel(this);
  76. LayersSubViewModel = new LayersViewModel(this);
  77. ClipboardSubViewModel = new ClipboardViewModel(this);
  78. UndoSubViewModel = new UndoViewModel(this);
  79. ViewportSubViewModel = new ViewportViewModel(this);
  80. ColorsSubViewModel = new ColorsViewModel(this);
  81. DocumentSubViewModel = new DocumentViewModel(this);
  82. DiscordViewModel = new DiscordViewModel(this, "764168193685979138");
  83. #if DEBUG
  84. DebugSubViewModel = new DebugViewModel(this);
  85. #endif
  86. ShortcutController = new ShortcutController(
  87. new ShortcutGroup(
  88. "Tools",
  89. CreateToolShortcut<PenTool>(Key.B, "Select Pen Tool"),
  90. CreateToolShortcut<EraserTool>(Key.E, "Select Eraser Tool"),
  91. CreateToolShortcut<ColorPickerTool>(Key.O, "Select Color Picker Tool"),
  92. CreateToolShortcut<RectangleTool>(Key.R, "Select Rectangle Tool"),
  93. CreateToolShortcut<CircleTool>(Key.C, "Select Circle Tool"),
  94. CreateToolShortcut<LineTool>(Key.L, "Select Line Tool"),
  95. CreateToolShortcut<FloodFill>(Key.G, "Select Flood Fill Tool"),
  96. CreateToolShortcut<BrightnessTool>(Key.U, "Select Brightness Tool"),
  97. CreateToolShortcut<MoveTool>(Key.V, "Select Move Tool"),
  98. CreateToolShortcut<SelectTool>(Key.M, "Select Select Tool"),
  99. CreateToolShortcut<ZoomTool>(Key.Z, "Select Zoom Tool"),
  100. CreateToolShortcut<MoveViewportTool>(Key.H, "Select Viewport Move Tool"),
  101. new Shortcut(Key.OemPlus, ViewportSubViewModel.ZoomCommand, "Zoom in", 115),
  102. new Shortcut(Key.OemMinus, ViewportSubViewModel.ZoomCommand, "Zoom out", 85),
  103. new Shortcut(Key.OemOpenBrackets, ToolsSubViewModel.ChangeToolSizeCommand, "Decrease Tool Size", -1),
  104. new Shortcut(Key.OemCloseBrackets, ToolsSubViewModel.ChangeToolSizeCommand, "Increase Tool Size", 1)),
  105. new ShortcutGroup(
  106. "Editor",
  107. new Shortcut(Key.X, ColorsSubViewModel.SwapColorsCommand, "Swap primary and secondary color"),
  108. new Shortcut(Key.Y, UndoSubViewModel.RedoCommand, "Redo", modifier: ModifierKeys.Control),
  109. new Shortcut(Key.Z, UndoSubViewModel.UndoCommand, "Undo", modifier: ModifierKeys.Control),
  110. new Shortcut(Key.D, SelectionSubViewModel.DeselectCommand, "Deselect all command", modifier: ModifierKeys.Control),
  111. new Shortcut(Key.A, SelectionSubViewModel.SelectAllCommand, "Select all command", modifier: ModifierKeys.Control),
  112. new Shortcut(Key.C, ClipboardSubViewModel.CopyCommand, "Copy", modifier: ModifierKeys.Control),
  113. new Shortcut(Key.V, ClipboardSubViewModel.PasteCommand, "Paste", modifier: ModifierKeys.Control),
  114. new Shortcut(Key.J, ClipboardSubViewModel.DuplicateCommand, "Duplicate", modifier: ModifierKeys.Control),
  115. new Shortcut(Key.X, ClipboardSubViewModel.CutCommand, "Cut", modifier: ModifierKeys.Control),
  116. new Shortcut(Key.Delete, DocumentSubViewModel.DeletePixelsCommand, "Delete selected pixels"),
  117. new Shortcut(Key.I, DocumentSubViewModel.OpenResizePopupCommand, "Resize document", modifier: ModifierKeys.Control | ModifierKeys.Shift),
  118. new Shortcut(Key.C, DocumentSubViewModel.OpenResizePopupCommand, "Resize canvas", "canvas", ModifierKeys.Control | ModifierKeys.Shift),
  119. new Shortcut(Key.F11, SystemCommands.MaximizeWindowCommand, "Maximize")),
  120. new ShortcutGroup(
  121. "File",
  122. new Shortcut(Key.O, FileSubViewModel.OpenFileCommand, "Open a Document", modifier: ModifierKeys.Control),
  123. new Shortcut(Key.S, FileSubViewModel.ExportFileCommand, "Export as image", modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
  124. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, "Save Document", modifier: ModifierKeys.Control),
  125. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, "Save Docuemnt As New", "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
  126. new Shortcut(Key.N, FileSubViewModel.OpenNewFilePopupCommand, "Create new Document", modifier: ModifierKeys.Control)),
  127. new ShortcutGroup(
  128. "Layers",
  129. new Shortcut(Key.F2, LayersSubViewModel.RenameLayerCommand, "Rename active layer", BitmapManager.ActiveDocument?.ActiveLayerGuid)),
  130. new ShortcutGroup(
  131. "View",
  132. new Shortcut(Key.OemTilde, ViewportSubViewModel.ToggleGridLinesCommand, "Toggle gridlines", modifier: ModifierKeys.Control)));
  133. MiscSubViewModel = new MiscViewModel(this);
  134. // Add F1 shortcut after MiscSubViewModel is constructed
  135. ShortcutController.ShortcutGroups.Add(
  136. new ShortcutGroup(
  137. "Misc",
  138. new Shortcut(Key.F1, MiscSubViewModel.OpenShortcutWindowCommand, "Open the shortcut window", true)));
  139. BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
  140. }
  141. /// <summary>
  142. /// Resets most variables and controller, so new documents can be handled.
  143. /// </summary>
  144. public void ResetProgramStateValues()
  145. {
  146. foreach (var document in BitmapManager.Documents)
  147. {
  148. document.PreviewLayer = null;
  149. }
  150. BitmapManager.ActiveDocument?.CenterViewport();
  151. }
  152. public bool DocumentIsNotNull(object property)
  153. {
  154. return BitmapManager.ActiveDocument != null;
  155. }
  156. private Shortcut CreateToolShortcut<T>(Key key, ModifierKeys modifier = ModifierKeys.None)
  157. where T : Tool
  158. {
  159. return new Shortcut(key, ToolsSubViewModel.SelectToolCommand, typeof(T), modifier);
  160. }
  161. private Shortcut CreateToolShortcut<T>(Key key, string description, ModifierKeys modifier = ModifierKeys.None)
  162. where T : Tool
  163. {
  164. return new Shortcut(key, ToolsSubViewModel.SelectToolCommand, description, typeof(T), modifier);
  165. }
  166. public void CloseWindow(object property)
  167. {
  168. if (!(property is CancelEventArgs))
  169. {
  170. throw new ArgumentException();
  171. }
  172. ((CancelEventArgs)property).Cancel = !RemoveDocumentsWithSaveConfirmation();
  173. }
  174. /// <summary>
  175. /// Removes documents with unsaved changes confirmation dialog.
  176. /// </summary>
  177. /// <returns>If documents was removed successfully.</returns>
  178. private bool RemoveDocumentsWithSaveConfirmation()
  179. {
  180. int docCount = BitmapManager.Documents.Count;
  181. for (int i = 0; i < docCount; i++)
  182. {
  183. BitmapManager.ActiveDocument = BitmapManager.Documents.First();
  184. bool canceled = !RemoveDocumentWithSaveConfirmation();
  185. if (canceled)
  186. {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. /// <summary>
  193. /// Removes document with unsaved changes confirmation dialog.
  194. /// </summary>
  195. /// <returns>If document was removed successfully.</returns>
  196. private bool RemoveDocumentWithSaveConfirmation()
  197. {
  198. ConfirmationType result = ConfirmationType.No;
  199. if (!BitmapManager.ActiveDocument.ChangesSaved)
  200. {
  201. result = ConfirmationDialog.Show(DocumentViewModel.ConfirmationDialogMessage);
  202. if (result == ConfirmationType.Yes)
  203. {
  204. FileSubViewModel.SaveDocument(false);
  205. }
  206. }
  207. if (result != ConfirmationType.Canceled)
  208. {
  209. BitmapManager.Documents.Remove(BitmapManager.ActiveDocument);
  210. return true;
  211. }
  212. else
  213. {
  214. return false;
  215. }
  216. }
  217. private void OnStartup(object parameter)
  218. {
  219. OnStartupEvent?.Invoke(this, EventArgs.Empty);
  220. }
  221. private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
  222. {
  223. if (e.NewDocument != null)
  224. {
  225. e.NewDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
  226. }
  227. }
  228. private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
  229. {
  230. BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>());
  231. BitmapManager.ActiveDocument.CenterViewport();
  232. BitmapManager.ActiveDocument.ChangesSaved = false;
  233. }
  234. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  235. {
  236. UndoSubViewModel.TriggerNewUndoChange(BitmapManager.SelectedTool);
  237. }
  238. private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
  239. {
  240. ChangesController.AddChanges(
  241. new LayerChange(e.PixelsChanged, e.ChangedLayerGuid),
  242. new LayerChange(e.OldPixelsValues, e.ChangedLayerGuid));
  243. BitmapManager.ActiveDocument.ChangesSaved = false;
  244. if (BitmapManager.IsOperationTool())
  245. {
  246. ColorsSubViewModel.AddSwatch(ColorsSubViewModel.PrimaryColor);
  247. }
  248. }
  249. }
  250. }