ToolsViewModel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Windows.Input;
  5. using PixiEditor.Helpers;
  6. using PixiEditor.Models.Tools;
  7. using PixiEditor.Models.Tools.Tools;
  8. namespace PixiEditor.ViewModels.SubViewModels.Main
  9. {
  10. public class ToolsViewModel : SubViewModel<ViewModelMain>
  11. {
  12. private Cursor toolCursor;
  13. public RelayCommand SelectToolCommand { get; set; } // Command that handles tool switching.
  14. public RelayCommand ChangeToolSizeCommand { get; set; }
  15. public Tool LastActionTool { get; private set; }
  16. public ObservableCollection<Tool> ToolSet { get; set; }
  17. public Cursor ToolCursor
  18. {
  19. get => toolCursor;
  20. set
  21. {
  22. toolCursor = value;
  23. RaisePropertyChanged("ToolCursor");
  24. }
  25. }
  26. public ToolsViewModel(ViewModelMain owner)
  27. : base(owner)
  28. {
  29. SelectToolCommand = new RelayCommand(SetTool, Owner.DocumentIsNotNull);
  30. ChangeToolSizeCommand = new RelayCommand(ChangeToolSize);
  31. ToolSet = new ObservableCollection<Tool>
  32. {
  33. new MoveViewportTool(), new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
  34. new CircleTool(), new RectangleTool(), new EraserTool(), new ColorPickerTool(), new BrightnessTool(),
  35. new ZoomTool()
  36. };
  37. SetActiveTool(typeof(MoveViewportTool));
  38. }
  39. public void SetActiveTool<T>()
  40. where T : Tool
  41. {
  42. SetActiveTool(typeof(T));
  43. }
  44. public void SetActiveTool(Tool tool)
  45. {
  46. Tool activeTool = ToolSet.FirstOrDefault(x => x.IsActive);
  47. if (activeTool != null)
  48. {
  49. activeTool.IsActive = false;
  50. }
  51. tool.IsActive = true;
  52. LastActionTool = Owner.BitmapManager.SelectedTool;
  53. Owner.BitmapManager.SetActiveTool(tool);
  54. SetToolCursor(tool.GetType());
  55. }
  56. public void SetTool(object parameter)
  57. {
  58. if (parameter is Type type)
  59. {
  60. SetActiveTool(type);
  61. return;
  62. }
  63. Tool tool = (Tool)parameter;
  64. SetActiveTool(tool.GetType());
  65. }
  66. private void ChangeToolSize(object parameter)
  67. {
  68. int increment = (int)parameter;
  69. int newSize = Owner.BitmapManager.ToolSize + increment;
  70. if (newSize > 0)
  71. {
  72. Owner.BitmapManager.ToolSize = newSize;
  73. }
  74. }
  75. private void SetActiveTool(Type toolType)
  76. {
  77. if (toolType == null && toolType.IsAssignableTo(typeof(Tool)))
  78. {
  79. return;
  80. }
  81. Tool foundTool = ToolSet.First(x => x.GetType() == toolType);
  82. SetActiveTool(foundTool);
  83. }
  84. private void SetToolCursor(Type tool)
  85. {
  86. if (tool != null)
  87. {
  88. ToolCursor = Owner.BitmapManager.SelectedTool.Cursor;
  89. }
  90. else
  91. {
  92. ToolCursor = Cursors.Arrow;
  93. }
  94. }
  95. }
  96. }