StylusViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using GalaSoft.MvvmLight.CommandWpf;
  2. using PixiEditor.Models.Tools;
  3. using PixiEditor.Models.Tools.Tools;
  4. using PixiEditor.Models.UserPreferences;
  5. using System.Windows.Input;
  6. namespace PixiEditor.ViewModels.SubViewModels.Main
  7. {
  8. public class StylusViewModel : SubViewModel<ViewModelMain>
  9. {
  10. private bool isPenModeEnabled;
  11. private bool useTouchGestures;
  12. public bool ToolSetByStylus { get; set; }
  13. /// <summary>
  14. /// Gets or sets a value indicating whether touch gestures are enabled even when the MoveViewportTool and ZoomTool are not selected.
  15. /// </summary>
  16. public bool IsPenModeEnabled
  17. {
  18. get => isPenModeEnabled;
  19. set
  20. {
  21. if (SetProperty(ref isPenModeEnabled, value))
  22. {
  23. IPreferences.Current.UpdateLocalPreference(nameof(IsPenModeEnabled), value);
  24. UpdateUseTouchGesture();
  25. }
  26. }
  27. }
  28. public bool UseTouchGestures
  29. {
  30. get => useTouchGestures;
  31. set => SetProperty(ref useTouchGestures, value);
  32. }
  33. private Tool PreviousTool { get; set; }
  34. public RelayCommand<StylusButtonEventArgs> StylusDownCommand { get; }
  35. public RelayCommand<StylusButtonEventArgs> StylusUpCommand { get; }
  36. public RelayCommand<StylusEventArgs> StylusOutOfRangeCommand { get; }
  37. public RelayCommand<StylusSystemGestureEventArgs> StylusGestureCommand { get; }
  38. public StylusViewModel(ViewModelMain owner)
  39. : base(owner)
  40. {
  41. StylusDownCommand = new(StylusDown);
  42. StylusUpCommand = new(StylusUp);
  43. StylusOutOfRangeCommand = new(StylusOutOfRange);
  44. StylusGestureCommand = new(StylusSystemGesture);
  45. isPenModeEnabled = IPreferences.Current.GetLocalPreference<bool>(nameof(IsPenModeEnabled));
  46. Owner.ToolsSubViewModel.AddPropertyChangedCallback(nameof(ToolsViewModel.ActiveTool), UpdateUseTouchGesture);
  47. UpdateUseTouchGesture();
  48. }
  49. private void UpdateUseTouchGesture()
  50. {
  51. if (Owner.ToolsSubViewModel.ActiveTool is not (MoveViewportTool or ZoomTool))
  52. {
  53. UseTouchGestures = IsPenModeEnabled;
  54. }
  55. else
  56. {
  57. UseTouchGestures = true;
  58. }
  59. }
  60. private void StylusOutOfRange(StylusEventArgs e)
  61. {
  62. Owner.BitmapManager.UpdateHighlightIfNecessary(true);
  63. }
  64. private void StylusSystemGesture(StylusSystemGestureEventArgs e)
  65. {
  66. if (e.SystemGesture == SystemGesture.Drag || e.SystemGesture == SystemGesture.Tap)
  67. {
  68. return;
  69. }
  70. e.Handled = true;
  71. }
  72. private void StylusDown(StylusButtonEventArgs e)
  73. {
  74. e.Handled = true;
  75. if (e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted)
  76. {
  77. PreviousTool = Owner.ToolsSubViewModel.ActiveTool;
  78. Owner.ToolsSubViewModel.SetActiveTool<EraserTool>();
  79. ToolSetByStylus = true;
  80. }
  81. }
  82. private void StylusUp(StylusButtonEventArgs e)
  83. {
  84. e.Handled = true;
  85. if (ToolSetByStylus && e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted)
  86. {
  87. Owner.ToolsSubViewModel.SetActiveTool(PreviousTool);
  88. }
  89. }
  90. }
  91. }