StylusViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Windows;
  2. using System.Windows.Input;
  3. using PixiEditor.Models.Tools;
  4. using PixiEditor.Models.Tools.Tools;
  5. using PixiEditor.Models.UserPreferences;
  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 StylusViewModel()
  35. : this(null)
  36. {
  37. }
  38. public StylusViewModel(ViewModelMain owner)
  39. : base(owner)
  40. {
  41. }
  42. public void SetOwner(ViewModelMain owner)
  43. {
  44. if (Owner is not null)
  45. {
  46. throw new System.Exception($"{nameof(StylusViewModel)} already has an owner");
  47. }
  48. else if (owner is null)
  49. {
  50. return;
  51. }
  52. Owner = owner;
  53. // TODO: Only capture it on the Drawing View Port
  54. Window mw = Application.Current.MainWindow;
  55. mw.PreviewStylusButtonDown += Mw_StylusButtonDown;
  56. mw.PreviewStylusButtonUp += Mw_StylusButtonUp;
  57. mw.PreviewStylusSystemGesture += Mw_PreviewStylusSystemGesture;
  58. isPenModeEnabled = IPreferences.Current.GetLocalPreference<bool>(nameof(IsPenModeEnabled));
  59. Owner.BitmapManager.AddPropertyChangedCallback(nameof(Owner.BitmapManager.SelectedTool), UpdateUseTouchGesture);
  60. UpdateUseTouchGesture();
  61. }
  62. private void UpdateUseTouchGesture()
  63. {
  64. if (Owner.BitmapManager.SelectedTool is not (MoveViewportTool or ZoomTool))
  65. {
  66. UseTouchGestures = IsPenModeEnabled;
  67. }
  68. else
  69. {
  70. UseTouchGestures = true;
  71. }
  72. }
  73. private void Mw_PreviewStylusSystemGesture(object sender, StylusSystemGestureEventArgs e)
  74. {
  75. if (e.SystemGesture == SystemGesture.Drag || e.SystemGesture == SystemGesture.Tap)
  76. {
  77. return;
  78. }
  79. e.Handled = true;
  80. }
  81. private void Mw_StylusButtonDown(object sender, StylusButtonEventArgs e)
  82. {
  83. e.Handled = true;
  84. if (e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted)
  85. {
  86. PreviousTool = Owner.BitmapManager.SelectedTool;
  87. Owner.ToolsSubViewModel.SetActiveTool<EraserTool>();
  88. ToolSetByStylus = true;
  89. }
  90. }
  91. private void Mw_StylusButtonUp(object sender, StylusButtonEventArgs e)
  92. {
  93. e.Handled = true;
  94. if (ToolSetByStylus && e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted)
  95. {
  96. Owner.ToolsSubViewModel.SetActiveTool(PreviousTool);
  97. }
  98. }
  99. }
  100. }