ViewModelMain.cs 16 KB

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