Browse Source

Fixed Stylus

CPKreuz 3 years ago
parent
commit
fdab4178e0

+ 12 - 28
PixiEditor/ViewModels/SubViewModels/Main/StylusViewModel.cs

@@ -1,5 +1,6 @@
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
+using GalaSoft.MvvmLight.CommandWpf;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.Models.UserPreferences;
@@ -37,35 +38,18 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         private Tool PreviousTool { get; set; }
         private Tool PreviousTool { get; set; }
 
 
-        public StylusViewModel()
-            : this(null)
-        {
-        }
+        public RelayCommand<StylusButtonEventArgs> StylusDownCommand { get; }
+
+        public RelayCommand<StylusButtonEventArgs> StylusUpCommand { get; }
+
+        public RelayCommand<StylusSystemGestureEventArgs> StylusGestureCommand { get; }
 
 
         public StylusViewModel(ViewModelMain owner)
         public StylusViewModel(ViewModelMain owner)
             : base(owner)
             : base(owner)
         {
         {
-        }
-
-        public void SetOwner(ViewModelMain owner)
-        {
-            if (Owner is not null)
-            {
-                throw new System.Exception($"{nameof(StylusViewModel)} already has an owner");
-            }
-            else if (owner is null)
-            {
-                return;
-            }
-
-            Owner = owner;
-
-            // TODO: Only capture it on the Drawing View Port
-            Window mw = Application.Current.MainWindow;
-
-            mw.PreviewStylusButtonDown += Mw_StylusButtonDown;
-            mw.PreviewStylusButtonUp += Mw_StylusButtonUp;
-            mw.PreviewStylusSystemGesture += Mw_PreviewStylusSystemGesture;
+            StylusDownCommand = new(StylusDown);
+            StylusUpCommand = new(StylusUp);
+            StylusGestureCommand = new(StylusSystemGesture);
 
 
             isPenModeEnabled = IPreferences.Current.GetLocalPreference<bool>(nameof(IsPenModeEnabled));
             isPenModeEnabled = IPreferences.Current.GetLocalPreference<bool>(nameof(IsPenModeEnabled));
             Owner.ToolsSubViewModel.AddPropertyChangedCallback(nameof(ToolsViewModel.ActiveTool), UpdateUseTouchGesture);
             Owner.ToolsSubViewModel.AddPropertyChangedCallback(nameof(ToolsViewModel.ActiveTool), UpdateUseTouchGesture);
@@ -85,7 +69,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private void Mw_PreviewStylusSystemGesture(object sender, StylusSystemGestureEventArgs e)
+        private void StylusSystemGesture(StylusSystemGestureEventArgs e)
         {
         {
             if (e.SystemGesture == SystemGesture.Drag || e.SystemGesture == SystemGesture.Tap)
             if (e.SystemGesture == SystemGesture.Drag || e.SystemGesture == SystemGesture.Tap)
             {
             {
@@ -95,7 +79,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             e.Handled = true;
             e.Handled = true;
         }
         }
 
 
-        private void Mw_StylusButtonDown(object sender, StylusButtonEventArgs e)
+        private void StylusDown(StylusButtonEventArgs e)
         {
         {
             e.Handled = true;
             e.Handled = true;
 
 
@@ -107,7 +91,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private void Mw_StylusButtonUp(object sender, StylusButtonEventArgs e)
+        private void StylusUp(StylusButtonEventArgs e)
         {
         {
             e.Handled = true;
             e.Handled = true;
 
 

+ 3 - 0
PixiEditor/Views/MainWindow.xaml

@@ -287,6 +287,9 @@
                                         MouseDownCommand="{Binding XamlAccesibleViewModel.IoSubViewModel.MouseDownCommand}"
                                         MouseDownCommand="{Binding XamlAccesibleViewModel.IoSubViewModel.MouseDownCommand}"
                                         MouseXOnCanvas="{Binding MouseXOnCanvas, Mode=TwoWay}"
                                         MouseXOnCanvas="{Binding MouseXOnCanvas, Mode=TwoWay}"
                                         MouseYOnCanvas="{Binding MouseYOnCanvas, Mode=TwoWay}"
                                         MouseYOnCanvas="{Binding MouseYOnCanvas, Mode=TwoWay}"
+                                        StylusButtonDownCommand="{Binding XamlAccesibleViewModel.StylusSubViewModel.StylusDownCommand}"
+                                        StylusButtonUpCommand="{Binding XamlAccesibleViewModel.StylusSubViewModel.StylusUpCommand}"
+                                        StylusGestureCommand="{Binding XamlAccesibleViewModel.StylusSubViewModel.StylusGestureCommand}"
                                         UseTouchGestures="{Binding XamlAccesibleViewModel.StylusSubViewModel.UseTouchGestures}"
                                         UseTouchGestures="{Binding XamlAccesibleViewModel.StylusSubViewModel.UseTouchGestures}"
                                         IsUsingZoomTool="{Binding XamlAccesibleViewModel.ToolsSubViewModel.ActiveTool, Converter={converters:IsSpecifiedTypeConverter SpecifiedType={x:Type tools:ZoomTool}}}"
                                         IsUsingZoomTool="{Binding XamlAccesibleViewModel.ToolsSubViewModel.ActiveTool, Converter={converters:IsSpecifiedTypeConverter SpecifiedType={x:Type tools:ZoomTool}}}"
                                         IsUsingMoveViewportTool="{Binding XamlAccesibleViewModel.ToolsSubViewModel.ActiveTool, Converter={converters:IsSpecifiedTypeConverter SpecifiedType={x:Type tools:MoveViewportTool}}}"
                                         IsUsingMoveViewportTool="{Binding XamlAccesibleViewModel.ToolsSubViewModel.ActiveTool, Converter={converters:IsSpecifiedTypeConverter SpecifiedType={x:Type tools:MoveViewportTool}}}"

+ 1 - 0
PixiEditor/Views/MainWindow.xaml.cs

@@ -4,6 +4,7 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using PixiEditor.Views.Dialogs;
 using PixiEditor.Views.Dialogs;
+using PixiEditor.Views.UserControls;
 using System;
 using System;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Diagnostics;

+ 24 - 10
PixiEditor/Views/UserControls/DrawingViewPort.xaml

@@ -1,14 +1,16 @@
 <UserControl
 <UserControl
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PixiEditor.Views.UserControls" 
-             xmlns:tools="clr-namespace:PixiEditor.Models.Tools.Tools"
-             xmlns:vws="clr-namespace:PixiEditor.Views"
-             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
-             xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
-             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:sys="clr-namespace:System;assembly=System.Runtime" xmlns:dataholders="clr-namespace:PixiEditor.Models.DataHolders" x:Class="PixiEditor.Views.UserControls.DrawingViewPort"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:cmd="http://www.galasoft.ch/mvvmlight"
+    xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
+    xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
+    xmlns:dataholders="clr-namespace:PixiEditor.Models.DataHolders"
+    xmlns:local="clr-namespace:PixiEditor.Views.UserControls"
+    xmlns:sys="clr-namespace:System;assembly=System.Runtime"
+    x:Class="PixiEditor.Views.UserControls.DrawingViewPort"
     mc:Ignorable="d"
     mc:Ignorable="d"
     d:DesignHeight="450" d:DesignWidth="800" x:Name="uc">
     d:DesignHeight="450" d:DesignWidth="800" x:Name="uc">
     <UserControl.Resources>
     <UserControl.Resources>
@@ -30,6 +32,18 @@
             <i:EventTrigger EventName="PreviewMouseDown">
             <i:EventTrigger EventName="PreviewMouseDown">
                 <i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand, ElementName=uc}"/>
                 <i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand, ElementName=uc}"/>
             </i:EventTrigger>
             </i:EventTrigger>
+            <i:EventTrigger EventName="StylusButtonDown">
+                <cmd:EventToCommand Command="{Binding StylusButtonDownCommand, ElementName=uc}"
+                                        PassEventArgsToCommand="True"/>
+            </i:EventTrigger>
+            <i:EventTrigger EventName="StylusButtonUp">
+                <cmd:EventToCommand Command="{Binding StylusButtonUpCommand, ElementName=uc}"
+                                        PassEventArgsToCommand="True"/>
+            </i:EventTrigger>
+            <i:EventTrigger EventName="StylusSystemGesture">
+                <cmd:EventToCommand Command="{Binding StylusGestureCommand, ElementName=uc}"
+                                        PassEventArgsToCommand="True"/>
+            </i:EventTrigger>
         </i:Interaction.Triggers>
         </i:Interaction.Triggers>
         <i:Interaction.Behaviors>
         <i:Interaction.Behaviors>
             <behaviors:MouseBehavior RelativeTo="{Binding ElementName=zoombox, Path=AdditionalContent}"
             <behaviors:MouseBehavior RelativeTo="{Binding ElementName=zoombox, Path=AdditionalContent}"

+ 27 - 0
PixiEditor/Views/UserControls/DrawingViewPort.xaml.cs

@@ -20,6 +20,15 @@ namespace PixiEditor.Views.UserControls
 
 
         public static readonly DependencyProperty MouseDownCommandProperty =
         public static readonly DependencyProperty MouseDownCommandProperty =
             DependencyProperty.Register(nameof(MouseDownCommand), typeof(ICommand), typeof(DrawingViewPort), new PropertyMetadata(default(ICommand)));
             DependencyProperty.Register(nameof(MouseDownCommand), typeof(ICommand), typeof(DrawingViewPort), new PropertyMetadata(default(ICommand)));
+        
+        public static readonly DependencyProperty StylusButtonDownCommandProperty =
+            DependencyProperty.Register(nameof(StylusButtonDownCommand), typeof(ICommand), typeof(DrawingViewPort), new PropertyMetadata(default(ICommand)));
+
+        public static readonly DependencyProperty StylusGestureCommandProperty =
+            DependencyProperty.Register(nameof(StylusGestureCommand), typeof(ICommand), typeof(DrawingViewPort), new PropertyMetadata(default(ICommand)));
+
+        public static readonly DependencyProperty StylusButtonUpCommandProperty =
+            DependencyProperty.Register(nameof(StylusButtonUpCommand), typeof(ICommand), typeof(DrawingViewPort), new PropertyMetadata(default(ICommand)));
 
 
         public static readonly DependencyProperty MouseXOnCanvasProperty =
         public static readonly DependencyProperty MouseXOnCanvasProperty =
             DependencyProperty.Register(nameof(MouseXOnCanvas), typeof(double), typeof(DrawingViewPort), new PropertyMetadata(0.0));
             DependencyProperty.Register(nameof(MouseXOnCanvas), typeof(double), typeof(DrawingViewPort), new PropertyMetadata(0.0));
@@ -65,6 +74,24 @@ namespace PixiEditor.Views.UserControls
             set => SetValue(MouseDownCommandProperty, value);
             set => SetValue(MouseDownCommandProperty, value);
         }
         }
 
 
+        public ICommand StylusButtonDownCommand
+        {
+            get => (ICommand)GetValue(StylusButtonDownCommandProperty);
+            set => SetValue(StylusButtonDownCommandProperty, value);
+        }
+
+        public ICommand StylusButtonUpCommand
+        {
+            get => (ICommand)GetValue(StylusButtonUpCommandProperty);
+            set => SetValue(StylusButtonUpCommandProperty, value);
+        }
+
+        public ICommand StylusGestureCommand
+        {
+            get => (ICommand)GetValue(StylusGestureCommandProperty);
+            set => SetValue(StylusGestureCommandProperty, value);
+        }
+
         public double MouseXOnCanvas
         public double MouseXOnCanvas
         {
         {
             get => (double)GetValue(MouseXOnCanvasProperty);
             get => (double)GetValue(MouseXOnCanvasProperty);