ToolsViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Microsoft.Extensions.DependencyInjection;
  2. using PixiEditor.Helpers;
  3. using PixiEditor.Models.Enums;
  4. using PixiEditor.Models.Events;
  5. using PixiEditor.Models.Tools;
  6. using PixiEditor.Models.Tools.Tools;
  7. using PixiEditor.Models.Tools.ToolSettings.Settings;
  8. using PixiEditor.Models.UserPreferences;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Windows.Input;
  13. namespace PixiEditor.ViewModels.SubViewModels.Main
  14. {
  15. public class ToolsViewModel : SubViewModel<ViewModelMain>
  16. {
  17. private Cursor toolCursor;
  18. private Tool activeTool;
  19. public RelayCommand SelectToolCommand { get; set; } // Command that handles tool switching.
  20. public RelayCommand ChangeToolSizeCommand { get; set; }
  21. public Tool LastActionTool { get; private set; }
  22. public bool ActiveToolIsTransient { get; set; }
  23. public Cursor ToolCursor
  24. {
  25. get => toolCursor;
  26. set
  27. {
  28. toolCursor = value;
  29. RaisePropertyChanged("ToolCursor");
  30. }
  31. }
  32. public Tool ActiveTool
  33. {
  34. get => activeTool;
  35. private set => SetProperty(ref activeTool, value);
  36. }
  37. public int ToolSize
  38. {
  39. get => ActiveTool.Toolbar.GetSetting<SizeSetting>("ToolSize") != null
  40. ? ActiveTool.Toolbar.GetSetting<SizeSetting>("ToolSize").Value
  41. : 1;
  42. set
  43. {
  44. if (ActiveTool.Toolbar.GetSetting<SizeSetting>("ToolSize") is SizeSetting toolSize)
  45. {
  46. toolSize.Value = value;
  47. Owner.BitmapManager.UpdateHighlightIfNecessary();
  48. }
  49. }
  50. }
  51. public List<Tool> ToolSet { get; private set; }
  52. public event EventHandler<SelectedToolEventArgs> SelectedToolChanged;
  53. public ToolsViewModel(ViewModelMain owner)
  54. : base(owner)
  55. {
  56. SelectToolCommand = new RelayCommand(SetTool, Owner.DocumentIsNotNull);
  57. ChangeToolSizeCommand = new RelayCommand(ChangeToolSize);
  58. }
  59. public void SetupTools(IServiceProvider services)
  60. {
  61. ToolSet = services.GetServices<Tool>().ToList();
  62. SetActiveTool<PenTool>();
  63. Owner.BitmapManager.BitmapOperations.BitmapChanged += (_, _) => TriggerCacheOutdated();
  64. Owner.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
  65. }
  66. public void SetupToolsTooltipShortcuts(IServiceProvider services)
  67. {
  68. foreach (var tool in ToolSet)
  69. {
  70. tool.ShortcutKey = Owner.ShortcutController.GetToolShortcutKey(tool.GetType());
  71. }
  72. }
  73. public void SetActiveTool<T>()
  74. where T : Tool
  75. {
  76. SetActiveTool(typeof(T));
  77. }
  78. public void SetActiveTool(Tool tool)
  79. {
  80. if (ActiveTool == tool) return;
  81. ActiveToolIsTransient = false;
  82. bool shareToolbar = IPreferences.Current.GetPreference<bool>("EnableSharedToolbar");
  83. if (ActiveTool != null)
  84. {
  85. activeTool.IsActive = false;
  86. if (shareToolbar)
  87. {
  88. ActiveTool.Toolbar.SaveToolbarSettings();
  89. }
  90. }
  91. LastActionTool = ActiveTool;
  92. ActiveTool = tool;
  93. if (shareToolbar)
  94. {
  95. ActiveTool.Toolbar.LoadSharedSettings();
  96. }
  97. if (LastActionTool != ActiveTool)
  98. SelectedToolChanged?.Invoke(this, new SelectedToolEventArgs(LastActionTool, ActiveTool));
  99. //update old tool
  100. Owner.BitmapManager.UpdateActionDisplay(LastActionTool);
  101. //update new tool
  102. Owner.BitmapManager.UpdateActionDisplay(ActiveTool);
  103. Owner.BitmapManager.UpdateHighlightIfNecessary();
  104. tool.IsActive = true;
  105. SetToolCursor(tool.GetType());
  106. if (Owner.StylusSubViewModel != null)
  107. {
  108. Owner.StylusSubViewModel.ToolSetByStylus = false;
  109. }
  110. }
  111. public void SetTool(object parameter)
  112. {
  113. if (parameter is Type type)
  114. {
  115. SetActiveTool(type);
  116. return;
  117. }
  118. Tool tool = (Tool)parameter;
  119. SetActiveTool(tool.GetType());
  120. }
  121. public void TriggerCacheOutdated()
  122. {
  123. foreach (Tool tool in ToolSet)
  124. {
  125. if (tool is ICachedDocumentTool cachedTool)
  126. {
  127. cachedTool.DocumentChanged();
  128. }
  129. }
  130. }
  131. private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
  132. {
  133. if (e.OldDocument != null)
  134. {
  135. e.OldDocument.DocumentSizeChanged -= Document_DocumentSizeChanged;
  136. e.OldDocument.LayersChanged -= Document_LayersChanged;
  137. }
  138. if (e.NewDocument != null)
  139. {
  140. e.NewDocument.DocumentSizeChanged += Document_DocumentSizeChanged;
  141. e.NewDocument.LayersChanged += Document_LayersChanged;
  142. }
  143. TriggerCacheOutdated();
  144. void Document_DocumentSizeChanged(object sender, Models.DataHolders.DocumentSizeChangedEventArgs e)
  145. {
  146. TriggerCacheOutdated();
  147. }
  148. void Document_LayersChanged(object sender, Models.Controllers.LayersChangedEventArgs e)
  149. {
  150. if (e.LayerChangeType is LayerAction.Add or LayerAction.Remove or LayerAction.Move)
  151. {
  152. TriggerCacheOutdated();
  153. }
  154. }
  155. }
  156. private void ChangeToolSize(object parameter)
  157. {
  158. int increment = (int)parameter;
  159. int newSize = ToolSize + increment;
  160. if (newSize > 0)
  161. {
  162. ToolSize = newSize;
  163. }
  164. }
  165. public void SetActiveTool(Type toolType)
  166. {
  167. if (!typeof(Tool).IsAssignableFrom(toolType)) { throw new ArgumentException($"'{toolType}' does not inherit from {typeof(Tool)}"); }
  168. Tool foundTool = ToolSet.First(x => x.GetType() == toolType);
  169. SetActiveTool(foundTool);
  170. }
  171. private void SetToolCursor(Type tool)
  172. {
  173. if (tool != null)
  174. {
  175. ToolCursor = ActiveTool.Cursor;
  176. }
  177. else
  178. {
  179. ToolCursor = Cursors.Arrow;
  180. }
  181. }
  182. }
  183. }