diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-06-07 09:50:29 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-06-07 09:50:29 +0300 |
| commit | 714c2fd7941c3f65e363a3d9cc33765c746f0c59 (patch) | |
| tree | d936bc5a7c64b5a3227a41ae742142fbb835bca8 /Software/Visual_Studio/Tango.SimulateTouch.UI | |
| parent | 8f02bc33eab3c82aac624c390c0805bf29816108 (diff) | |
| download | Tango-714c2fd7941c3f65e363a3d9cc33765c746f0c59.tar.gz Tango-714c2fd7941c3f65e363a3d9cc33765c746f0c59.zip | |
Refined touch simulation test.
Diffstat (limited to 'Software/Visual_Studio/Tango.SimulateTouch.UI')
5 files changed, 144 insertions, 115 deletions
diff --git a/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml b/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml index 5c7075e8a..1bca41386 100644 --- a/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml +++ b/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml @@ -5,19 +5,18 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Tango.SimulateTouch.UI" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800" > - <Grid Background="White" x:Name="GdRootZm"> - <Border Width="200" BorderBrush="Silver" BorderThickness="1" CornerRadius="5" VerticalAlignment="Bottom" HorizontalAlignment="Right" - Margin="50" Background="White" x:Name="BdrSimulateZm"> - <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> - <Border Margin="3" Height="45" BorderBrush="Blue" BorderThickness="1" CornerRadius="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> - <TextBlock x:Name="messageTextBlock" Height="45" Width="Auto" Background="Azure" ></TextBlock> - </Border> - <Border Margin="3" Height="45" BorderBrush="Blue" BorderThickness="1" CornerRadius="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> - <TextBlock x:Name="TouchUpTextBlock" Height="45" Width="Auto" Background="Azure" ></TextBlock> - </Border> - <Button Margin="0 20 0 0 " Width="200" PreviewMouseLeftButtonUp="OnButtonLeftButtonDown">Touch Button</Button> - </StackPanel> - </Border> + Title="MainWindow" Height="450" Width="800" WindowState="Maximized"> + + <Grid> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="1*"/> + <ColumnDefinition Width="150"/> + </Grid.ColumnDefinitions> + + <Canvas Background="Gainsboro"> + <Rectangle x:Name="rect" Width="100" Height="100" Fill="Red" Canvas.Top="0" Canvas.Left="0" IsHitTestVisible="True"></Rectangle> + </Canvas> + + <Button x:Name="btnStart" Click="BtnStart_Click" Margin="10" Grid.Column="1" VerticalAlignment="Bottom" Padding="10">START TEST</Button> </Grid> </Window> diff --git a/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml.cs b/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml.cs index 33181b13d..27f316bb3 100644 --- a/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml.cs +++ b/Software/Visual_Studio/Tango.SimulateTouch.UI/MainWindow.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Navigation; using System.Windows.Shapes; using Tango.SimulateTouch.UI.Native; using System.Security.Cryptography; +using System.Threading; namespace Tango.SimulateTouch.UI { @@ -23,129 +24,75 @@ namespace Tango.SimulateTouch.UI /// </summary> public partial class MainWindow : Window { - //Windows.UI.Input.Preview.Injection.InputInjector _inputInjector; - private Line ProxyLine; + private Point downPosition; public MainWindow() { InitializeComponent(); - WindowState = WindowState.Maximized; - TouchInjector.InitializeTouchInjection(); - this.TouchDown += MainWindow_TouchDown; - this.TouchMove += MainWindow_TouchMove; - this.TouchUp += MainWindow_TouchUp; + + TouchController.Init(); + + rect.TouchDown += Rect_TouchDown; + rect.TouchUp += Rect_TouchUp; + rect.TouchMove += Rect_TouchMove; } - private void MainWindow_TouchUp(object sender, TouchEventArgs e) + + private void Rect_TouchDown(object sender, TouchEventArgs e) { - System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this); - this.ProxyLine.X2 = oPos.Position.X; - this.ProxyLine.Y2 = oPos.Position.Y; - this.GdRootZm.Children.Add(this.ProxyLine); - Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchUp " + oPos.Position.X + " " + oPos.Position.Y); - TouchUpTextBlock.Text = "TOUCH UP"; + downPosition = e.GetTouchPoint(rect).Position; + rect.Fill = Brushes.Blue; } - private void MainWindow_TouchMove(object sender, TouchEventArgs e) + private void Rect_TouchUp(object sender, TouchEventArgs e) { - System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this); - Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchMove " + oPos.Position.X + " " + oPos.Position.Y); + rect.Fill = Brushes.Red; } - private void MainWindow_TouchDown(object sender, TouchEventArgs e) + private void Rect_TouchMove(object sender, TouchEventArgs e) { - System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this); - Line oLine = new Line(); - oLine.Stroke = new SolidColorBrush(Colors.Red); - oLine.StrokeThickness = 2; - oLine.X1 = oPos.Position.X; - oLine.Y1 = oPos.Position.Y; - this.ProxyLine = oLine; - Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchDown " + oPos.Position.X + " " + oPos.Position.Y); - messageTextBlock.Text = "TOUCH Down"; + var position = e.GetTouchPoint(rect).Position; + + Canvas.SetLeft(rect, (Canvas.GetLeft(rect) + position.X) - downPosition.X); + Canvas.SetTop(rect, (Canvas.GetTop(rect) + position.Y) - downPosition.Y); } - - private void OnButtonLeftButtonDown(object sender, MouseButtonEventArgs e) + + private void BtnStart_Click(object sender, RoutedEventArgs e) { - // Point targetLoc = messageTextBlock.PointToScreen(new Point(0, 0)); - // HwndSource source = (HwndSource)HwndSource.FromVisual(messageTextBlock); - // IntPtr hWnd = source.Handle; - Console.WriteLine("OnButtonLeftButtonDown"); + Point rectPosition = rect.PointToScreen(new Point(0, 0)); + Size rectSize = new Size(rect.ActualWidth, rect.ActualHeight); - TouchSimulate ts = new TouchSimulate(); + Point lastDelta = new Point(0, 0); + bool completed = false; - //TEST UWP - error COM!!!! - /* _inputInjector = InputInjector.TryCreate(); - if (_inputInjector != null) - { - _inputInjector.InitializeTouchInjection( InjectedInputVisualizationMode.Default); - uint pointerId = 1; - var appBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds; - Point appBoundsTopLeft = new Point(appBounds.Left, appBounds.Top); - Point targetLoc = messageTextBlock.PointToScreen(new Point(0, 0)); + Thread t = new Thread(() => + { + TouchController.TouchDown((int)(rectPosition.X + rectSize.Width / 2), (int)(rectPosition.Y + rectSize.Height / 2)); //Touch the middle of the red rectangle. - // Get the screen coordinates (relative to the input area) - // of the input pointer. - Point screenPointPosition = button.PointToScreen(coordinates); - int pointerPointX = (int)screenPointPosition.X; - int pointerPointY = (int)screenPointPosition.Y; + Task.Factory.StartNew(() => //This is necessary to keep the touch session alive. + { + while (!completed) + { + TouchController.TouchMove((int)lastDelta.X, (int)lastDelta.Y); + Thread.Sleep(100); + } + }); - // Create the point for input injection and calculate its screen location. - Point injectionPoint = - new Point( - appBoundsTopLeft.X + targetLoc.X + pointerPointX, - appBoundsTopLeft.Y + targetLoc.Y + pointerPointY); + Thread.Sleep(1000); - // Create a touch data point for pointer down. - // Each element in the touch data list represents a single touch contact. - // For this example, we're mirroring a single mouse pointer. - List<InjectedInputTouchInfo> touchData = new List<InjectedInputTouchInfo> - { - new InjectedInputTouchInfo - { - Contact = new InjectedInputRectangle - { - Left = 30, Top = 30, Bottom = 30, Right = 30 - }, - PointerInfo = new InjectedInputPointerInfo - { - PointerId = pointerId, - PointerOptions = - InjectedInputPointerOptions.PointerDown | - InjectedInputPointerOptions.InContact | - InjectedInputPointerOptions.New, - TimeOffsetInMilliseconds = 0, - PixelLocation = new InjectedInputPoint - { - PositionX = (int)injectionPoint.X , - PositionY = (int)injectionPoint.Y - } - }, - Pressure = 1.0, - TouchParameters = - InjectedInputTouchParameters.Pressure | - InjectedInputTouchParameters.Contact - } - }; + for (int i = 0; i < 51; i++) //Animate movement. + { + TouchController.TouchMove(i, i); + lastDelta = new Point(i, i); + Thread.Sleep(300); + } - // Inject the touch input. - _inputInjector.InjectTouchInput(touchData); + completed = true; - // Create a touch data point for pointer up. - touchData = new List<InjectedInputTouchInfo> - { - new InjectedInputTouchInfo - { - PointerInfo = new InjectedInputPointerInfo - { - PointerId = pointerId, - PointerOptions = InjectedInputPointerOptions.PointerUp - } - } - }; + TouchController.TouchUp(); //Complete the touch. - // Inject the touch input. - _inputInjector.InjectTouchInput(touchData); - }*/ + }); + t.IsBackground = true; + t.Start(); } } } diff --git a/Software/Visual_Studio/Tango.SimulateTouch.UI/Native/TouchSimulate.cs b/Software/Visual_Studio/Tango.SimulateTouch.UI/Native/TouchSimulate.cs index 05c73217f..f0fd1095e 100644 --- a/Software/Visual_Studio/Tango.SimulateTouch.UI/Native/TouchSimulate.cs +++ b/Software/Visual_Studio/Tango.SimulateTouch.UI/Native/TouchSimulate.cs @@ -15,6 +15,7 @@ namespace Tango.SimulateTouch.UI.Native int y = this.GetRandomSeed().Next(50, 1080 - 100); SimulateTouch(x, y); } + private Random GetRandomSeed() { byte[] bytes = new byte[4]; diff --git a/Software/Visual_Studio/Tango.SimulateTouch.UI/Tango.SimulateTouch.UI.csproj b/Software/Visual_Studio/Tango.SimulateTouch.UI/Tango.SimulateTouch.UI.csproj index c632e82e0..b9f562c61 100644 --- a/Software/Visual_Studio/Tango.SimulateTouch.UI/Tango.SimulateTouch.UI.csproj +++ b/Software/Visual_Studio/Tango.SimulateTouch.UI/Tango.SimulateTouch.UI.csproj @@ -55,6 +55,7 @@ <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </ApplicationDefinition> + <Compile Include="TouchController.cs" /> <Page Include="MainWindow.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> diff --git a/Software/Visual_Studio/Tango.SimulateTouch.UI/TouchController.cs b/Software/Visual_Studio/Tango.SimulateTouch.UI/TouchController.cs new file mode 100644 index 000000000..144047b0c --- /dev/null +++ b/Software/Visual_Studio/Tango.SimulateTouch.UI/TouchController.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.SimulateTouch.UI.Native; + +namespace Tango.SimulateTouch.UI +{ + public static class TouchController + { + private static bool _initialized; + private static PointerTouchInfo? _currentContact; + + public static void Init() + { + if (!_initialized) + { + _initialized = true; + TouchInjector.InitializeTouchInjection(); + } + } + + public static void TouchDown(int x, int y) + { + Init(); + + var contact = MakePointerTouchInfo(x, y, 1); + PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT; + contact.PointerInfo.PointerFlags = oFlags; + bool bIsSuccess = TouchInjector.InjectTouchInput(1, new[] { contact }); + _currentContact = contact; + } + + public static void TouchUp() + { + Init(); + + if (_currentContact != null) + { + var contact = _currentContact.Value; + contact.PointerInfo.PointerFlags = PointerFlags.UP; + TouchInjector.InjectTouchInput(1, new[] { contact }); + _currentContact = null; + } + } + + public static void TouchMove(int deltaX, int deltaY) + { + Init(); + + if (_currentContact != null) + { + var contact = _currentContact.Value; + contact.Move(deltaX, deltaY); + var oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE; + contact.PointerInfo.PointerFlags = oFlags; + TouchInjector.InjectTouchInput(1, new[] { contact }); + } + } + + private static PointerTouchInfo MakePointerTouchInfo(int x, int y, int radius, uint orientation = 90, uint pressure = 32000) + { + PointerTouchInfo contact = new PointerTouchInfo(); + contact.PointerInfo.pointerType = PointerInputType.TOUCH; + contact.TouchFlags = TouchFlags.NONE; + contact.Orientation = orientation; + contact.Pressure = pressure; + contact.TouchMasks = TouchMask.CONTACTAREA | TouchMask.ORIENTATION | TouchMask.PRESSURE; + contact.PointerInfo.PtPixelLocation.X = x; + contact.PointerInfo.PtPixelLocation.Y = y; + uint unPointerId = IdGenerator.GetUinqueUInt(); + contact.PointerInfo.PointerId = unPointerId; + contact.ContactArea.left = x - radius; + contact.ContactArea.right = x + radius; + contact.ContactArea.top = y - radius; + contact.ContactArea.bottom = y + radius; + return contact; + } + } +} |
