ViewModelMain.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 System.Windows.Threading;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using PixiEditor.Helpers;
  11. using PixiEditor.Models.Controllers;
  12. using PixiEditor.Models.Controllers.Shortcuts;
  13. using PixiEditor.Models.DataHolders;
  14. using PixiEditor.Models.Dialogs;
  15. using PixiEditor.Models.Enums;
  16. using PixiEditor.Models.Events;
  17. using PixiEditor.Models.Position;
  18. using PixiEditor.Models.Tools;
  19. using PixiEditor.Models.Tools.Tools;
  20. using PixiEditor.Models.UserPreferences;
  21. using PixiEditor.ViewModels.SubViewModels.Main;
  22. using PixiEditor.Views.Dialogs;
  23. namespace PixiEditor.ViewModels
  24. {
  25. public class ViewModelMain : ViewModelBase
  26. {
  27. private string actionDisplay;
  28. private bool overrideActionDisplay;
  29. public static ViewModelMain Current { get; set; }
  30. public IServiceProvider Services { get; private set; }
  31. public Action CloseAction { get; set; }
  32. public event EventHandler OnStartupEvent;
  33. public RelayCommand OnStartupCommand { get; set; }
  34. public RelayCommand CloseWindowCommand { get; set; }
  35. public FileViewModel FileSubViewModel { get; set; }
  36. public UpdateViewModel UpdateSubViewModel { get; set; }
  37. public ToolsViewModel ToolsSubViewModel { get; set; }
  38. public IoViewModel IoSubViewModel { get; set; }
  39. public LayersViewModel LayersSubViewModel { get; set; }
  40. public ClipboardViewModel ClipboardSubViewModel { get; set; }
  41. public UndoViewModel UndoSubViewModel { get; set; }
  42. public SelectionViewModel SelectionSubViewModel { get; set; }
  43. public ViewportViewModel ViewportSubViewModel { get; set; }
  44. public ColorsViewModel ColorsSubViewModel { get; set; }
  45. public DocumentViewModel DocumentSubViewModel { get; set; }
  46. public MiscViewModel MiscSubViewModel { get; set; }
  47. public DiscordViewModel DiscordViewModel { get; set; }
  48. public DebugViewModel DebugSubViewModel { get; set; }
  49. public BitmapManager BitmapManager { 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 ToolsSubViewModel.ActiveTool.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(IServiceProvider serviceProvider)
  91. {
  92. Current = this;
  93. }
  94. public void Setup(IServiceProvider services)
  95. {
  96. Services = services;
  97. Preferences = services.GetRequiredService<IPreferences>();
  98. Preferences.Init();
  99. BitmapManager = services.GetRequiredService<BitmapManager>();
  100. BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
  101. BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
  102. SelectionSubViewModel = services.GetService<SelectionViewModel>();
  103. OnStartupCommand = new RelayCommand(OnStartup);
  104. CloseWindowCommand = new RelayCommand(CloseWindow);
  105. FileSubViewModel = services.GetService<FileViewModel>();
  106. ToolsSubViewModel = services.GetService<ToolsViewModel>();
  107. ToolsSubViewModel.SelectedToolChanged += BitmapManager_SelectedToolChanged;
  108. ToolsSubViewModel?.SetupTools(services);
  109. IoSubViewModel = services.GetService<IoViewModel>();
  110. LayersSubViewModel = services.GetService<LayersViewModel>();
  111. ClipboardSubViewModel = services.GetService<ClipboardViewModel>();
  112. UndoSubViewModel = services.GetService<UndoViewModel>();
  113. ViewportSubViewModel = services.GetService<ViewportViewModel>();
  114. ColorsSubViewModel = services.GetService<ColorsViewModel>();
  115. DocumentSubViewModel = services.GetService<DocumentViewModel>();
  116. DiscordViewModel = services.GetService<DiscordViewModel>();
  117. UpdateSubViewModel = services.GetService<UpdateViewModel>();
  118. WindowSubViewModel = services.GetService<WindowViewModel>();
  119. StylusSubViewModel = services.GetService<StylusViewModel>();
  120. AddDebugOnlyViewModels();
  121. AddReleaseOnlyViewModels();
  122. ShortcutController = new ShortcutController(
  123. new ShortcutGroup(
  124. "Tools",
  125. CreateToolShortcut<PenTool>(Key.B, "Pen"),
  126. CreateToolShortcut<EraserTool>(Key.E, "Eraser"),
  127. CreateToolShortcut<ColorPickerTool>(Key.O, "Color picker"),
  128. CreateToolShortcut<RectangleTool>(Key.R, "Rectangle"),
  129. CreateToolShortcut<CircleTool>(Key.C, "Ellipse"),
  130. CreateToolShortcut<LineTool>(Key.L, "Line"),
  131. CreateToolShortcut<FloodFillTool>(Key.G, "Flood fill"),
  132. CreateToolShortcut<BrightnessTool>(Key.U, "Brightness"),
  133. CreateToolShortcut<MoveTool>(Key.V, "Move selection"),
  134. CreateToolShortcut<SelectTool>(Key.M, "Select"),
  135. CreateToolShortcut<ZoomTool>(Key.Z, "Zoom"),
  136. CreateToolShortcut<MoveViewportTool>(Key.H, "Move viewport"),
  137. CreateToolShortcut<MagicWandTool>(Key.W, "Magic wand"),
  138. new Shortcut(Key.OemPlus, ViewportSubViewModel.ZoomCommand, "Zoom in", 1),
  139. new Shortcut(Key.OemMinus, ViewportSubViewModel.ZoomCommand, "Zoom out", -1),
  140. new Shortcut(Key.OemOpenBrackets, ToolsSubViewModel.ChangeToolSizeCommand, "Decrease tool size", -1),
  141. new Shortcut(Key.OemCloseBrackets, ToolsSubViewModel.ChangeToolSizeCommand, "Increase tool size", 1)),
  142. new ShortcutGroup(
  143. "Editor",
  144. new Shortcut(Key.X, ColorsSubViewModel.SwapColorsCommand, "Swap primary and secondary colors"),
  145. new Shortcut(Key.Y, UndoSubViewModel.RedoCommand, "Redo", modifier: ModifierKeys.Control),
  146. new Shortcut(Key.Z, UndoSubViewModel.UndoCommand, "Undo", modifier: ModifierKeys.Control),
  147. new Shortcut(Key.D, SelectionSubViewModel.DeselectCommand, "Clear selection", modifier: ModifierKeys.Control),
  148. new Shortcut(Key.A, SelectionSubViewModel.SelectAllCommand, "Select all", modifier: ModifierKeys.Control),
  149. new Shortcut(Key.C, ClipboardSubViewModel.CopyCommand, "Copy", modifier: ModifierKeys.Control),
  150. new Shortcut(Key.V, ClipboardSubViewModel.PasteCommand, "Paste", modifier: ModifierKeys.Control),
  151. new Shortcut(Key.J, ClipboardSubViewModel.DuplicateCommand, "Duplicate", modifier: ModifierKeys.Control),
  152. new Shortcut(Key.X, ClipboardSubViewModel.CutCommand, "Cut", modifier: ModifierKeys.Control),
  153. new Shortcut(Key.Delete, DocumentSubViewModel.DeletePixelsCommand, "Clear selected area"),
  154. new Shortcut(Key.I, DocumentSubViewModel.OpenResizePopupCommand, "Resize image", modifier: ModifierKeys.Control | ModifierKeys.Shift),
  155. new Shortcut(Key.C, DocumentSubViewModel.OpenResizePopupCommand, "Resize canvas", "canvas", ModifierKeys.Control | ModifierKeys.Shift),
  156. new Shortcut(Key.F11, SystemCommands.MaximizeWindowCommand, "Maximize window")),
  157. new ShortcutGroup(
  158. "File",
  159. new Shortcut(Key.O, FileSubViewModel.OpenFileCommand, "Open image", modifier: ModifierKeys.Control),
  160. new Shortcut(Key.S, FileSubViewModel.ExportFileCommand, "Export image", modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
  161. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, "Save", modifier: ModifierKeys.Control),
  162. new Shortcut(Key.S, FileSubViewModel.SaveDocumentCommand, "Save as new", "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
  163. new Shortcut(Key.N, FileSubViewModel.OpenNewFilePopupCommand, "Create new image", modifier: ModifierKeys.Control)),
  164. new ShortcutGroup(
  165. "Layers",
  166. new Shortcut(Key.F2, LayersSubViewModel.RenameLayerCommand, "Rename active layer", BitmapManager.ActiveDocument?.ActiveLayerGuid)),
  167. new ShortcutGroup(
  168. "View",
  169. new Shortcut(Key.OemTilde, ViewportSubViewModel.ToggleGridLinesCommand, "Toggle gridlines", modifier: ModifierKeys.Control)));
  170. MiscSubViewModel = services.GetService<MiscViewModel>();
  171. // Add F1 shortcut after MiscSubViewModel is constructed
  172. ShortcutController.ShortcutGroups.Add(
  173. new ShortcutGroup(
  174. "Misc",
  175. new Shortcut(Key.F1, MiscSubViewModel.OpenShortcutWindowCommand, "Open shortcuts window", true)));
  176. BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
  177. ToolsSubViewModel?.SetupToolsTooltipShortcuts(services);
  178. }
  179. /// <summary>
  180. /// Resets most variables and controller, so new documents can be handled.
  181. /// </summary>
  182. public void ResetProgramStateValues()
  183. {
  184. foreach (var document in BitmapManager.Documents)
  185. {
  186. document.PreviewLayer.Reset();
  187. }
  188. }
  189. public bool DocumentIsNotNull(object property)
  190. {
  191. return BitmapManager.ActiveDocument != null;
  192. }
  193. public void CloseWindow(object property)
  194. {
  195. if (!(property is CancelEventArgs))
  196. {
  197. throw new ArgumentException();
  198. }
  199. ((CancelEventArgs)property).Cancel = !RemoveDocumentsWithSaveConfirmation();
  200. }
  201. private void BitmapManager_SelectedToolChanged(object sender, SelectedToolEventArgs e)
  202. {
  203. if (e.OldTool != null)
  204. e.OldTool.PropertyChanged -= SelectedTool_PropertyChanged;
  205. e.NewTool.PropertyChanged += SelectedTool_PropertyChanged;
  206. NotifyToolActionDisplayChanged();
  207. BitmapManager.InputTarget.OnToolChange(e.NewTool);
  208. }
  209. private void SelectedTool_PropertyChanged(object sender, PropertyChangedEventArgs e)
  210. {
  211. if (e.PropertyName == nameof(Tool.ActionDisplay))
  212. {
  213. NotifyToolActionDisplayChanged();
  214. }
  215. }
  216. private void NotifyToolActionDisplayChanged()
  217. {
  218. if (!OverrideActionDisplay) RaisePropertyChanged(nameof(ActionDisplay));
  219. }
  220. [Conditional("DEBUG")]
  221. private void AddDebugOnlyViewModels()
  222. {
  223. DebugSubViewModel = new DebugViewModel(this);
  224. }
  225. [Conditional("RELEASE")]
  226. private void AddReleaseOnlyViewModels()
  227. {
  228. }
  229. private Shortcut CreateToolShortcut<T>(Key key, ModifierKeys modifier = ModifierKeys.None)
  230. where T : Tool
  231. {
  232. return new Shortcut(key, ToolsSubViewModel.SelectToolCommand, typeof(T), modifier);
  233. }
  234. private Shortcut CreateToolShortcut<T>(Key key, string description, ModifierKeys modifier = ModifierKeys.None)
  235. where T : Tool
  236. {
  237. return new Shortcut(key, ToolsSubViewModel.SelectToolCommand, description, typeof(T), modifier);
  238. }
  239. /// <summary>
  240. /// Removes documents with unsaved changes confirmation dialog.
  241. /// </summary>
  242. /// <returns>If documents was removed successfully.</returns>
  243. private bool RemoveDocumentsWithSaveConfirmation()
  244. {
  245. int docCount = BitmapManager.Documents.Count;
  246. for (int i = 0; i < docCount; i++)
  247. {
  248. BitmapManager.ActiveDocument = BitmapManager.Documents.First();
  249. bool canceled = !RemoveDocumentWithSaveConfirmation();
  250. if (canceled)
  251. {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. /// <summary>
  258. /// Removes document with unsaved changes confirmation dialog.
  259. /// </summary>
  260. /// <returns>If document was removed successfully.</returns>
  261. private bool RemoveDocumentWithSaveConfirmation()
  262. {
  263. ConfirmationType result = ConfirmationType.No;
  264. if (!BitmapManager.ActiveDocument.ChangesSaved)
  265. {
  266. result = ConfirmationDialog.Show(DocumentViewModel.ConfirmationDialogMessage, DocumentViewModel.ConfirmationDialogTitle);
  267. if (result == ConfirmationType.Yes)
  268. {
  269. FileSubViewModel.SaveDocument(false);
  270. //cancel was pressed in the save file dialog
  271. if (!BitmapManager.ActiveDocument.ChangesSaved)
  272. return false;
  273. }
  274. }
  275. if (result != ConfirmationType.Canceled)
  276. {
  277. var doc = BitmapManager.ActiveDocument;
  278. BitmapManager.Documents.Remove(doc);
  279. doc.Dispose();
  280. return true;
  281. }
  282. else
  283. {
  284. return false;
  285. }
  286. }
  287. private void OnStartup(object parameter)
  288. {
  289. OnStartupEvent?.Invoke(this, EventArgs.Empty);
  290. }
  291. private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
  292. {
  293. if (e.NewDocument != null)
  294. {
  295. e.NewDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
  296. }
  297. }
  298. private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
  299. {
  300. BitmapManager.ActiveDocument.ActiveSelection = new Selection(Array.Empty<Coordinates>());
  301. BitmapManager.ActiveDocument.ChangesSaved = false;
  302. BitmapManager.ActiveDocument.CenterViewportTrigger.Execute(this, new Size(BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height));
  303. }
  304. private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
  305. {
  306. BitmapManager.ActiveDocument.ChangesSaved = false;
  307. if (ToolsSubViewModel.ActiveTool is BitmapOperationTool)
  308. {
  309. ColorsSubViewModel.AddSwatch(ColorsSubViewModel.PrimaryColor);
  310. }
  311. }
  312. }
  313. }