using GalaSoft.MvvmLight.CommandWpf; using PixiEditor.Models.Tools; using PixiEditor.Models.Tools.Tools; using PixiEditor.Models.UserPreferences; using System.Windows.Input; namespace PixiEditor.ViewModels.SubViewModels.Main { public class StylusViewModel : SubViewModel { private bool isPenModeEnabled; private bool useTouchGestures; public bool ToolSetByStylus { get; set; } /// /// Gets or sets a value indicating whether touch gestures are enabled even when the MoveViewportTool and ZoomTool are not selected. /// public bool IsPenModeEnabled { get => isPenModeEnabled; set { if (SetProperty(ref isPenModeEnabled, value)) { IPreferences.Current.UpdateLocalPreference(nameof(IsPenModeEnabled), value); UpdateUseTouchGesture(); } } } public bool UseTouchGestures { get => useTouchGestures; set => SetProperty(ref useTouchGestures, value); } private Tool PreviousTool { get; set; } public RelayCommand StylusDownCommand { get; } public RelayCommand StylusUpCommand { get; } public RelayCommand StylusOutOfRangeCommand { get; } public RelayCommand StylusGestureCommand { get; } public StylusViewModel(ViewModelMain owner) : base(owner) { StylusDownCommand = new(StylusDown); StylusUpCommand = new(StylusUp); StylusOutOfRangeCommand = new(StylusOutOfRange); StylusGestureCommand = new(StylusSystemGesture); isPenModeEnabled = IPreferences.Current.GetLocalPreference(nameof(IsPenModeEnabled)); Owner.ToolsSubViewModel.AddPropertyChangedCallback(nameof(ToolsViewModel.ActiveTool), UpdateUseTouchGesture); UpdateUseTouchGesture(); } private void UpdateUseTouchGesture() { if (Owner.ToolsSubViewModel.ActiveTool is not (MoveViewportTool or ZoomTool)) { UseTouchGestures = IsPenModeEnabled; } else { UseTouchGestures = true; } } private void StylusOutOfRange(StylusEventArgs e) { Owner.BitmapManager.UpdateHighlightIfNecessary(true); } private void StylusSystemGesture(StylusSystemGestureEventArgs e) { if (e.SystemGesture == SystemGesture.Drag || e.SystemGesture == SystemGesture.Tap) { return; } e.Handled = true; } private void StylusDown(StylusButtonEventArgs e) { e.Handled = true; if (e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted) { PreviousTool = Owner.ToolsSubViewModel.ActiveTool; Owner.ToolsSubViewModel.SetActiveTool(); ToolSetByStylus = true; } } private void StylusUp(StylusButtonEventArgs e) { e.Handled = true; if (ToolSetByStylus && e.StylusButton.Guid == StylusPointProperties.TipButton.Id && e.Inverted) { Owner.ToolsSubViewModel.SetActiveTool(PreviousTool); } } } }