From e4e8caa47e730542e5688267e4bda23b1c206998 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 18 Nov 2019 16:10:42 -0800 Subject: [PATCH] UWP: Handle touch events --- .../hololens/ServoApp/ServoControl/Servo.h | 12 +++++ .../ServoApp/ServoControl/ServoControl.cpp | 48 +++++++++++++++---- .../ServoApp/ServoControl/ServoControl.h | 3 +- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/support/hololens/ServoApp/ServoControl/Servo.h b/support/hololens/ServoApp/ServoControl/Servo.h index a27313d37929..ead1c708e509 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.h +++ b/support/hololens/ServoApp/ServoControl/Servo.h @@ -62,6 +62,18 @@ class Servo { void MouseUp(float x, float y, capi::CMouseButton b) { capi::mouse_up(x, y, b); } + void TouchDown(float x, float y, int32_t id) { + capi::touch_down(x, y, id); + } + void TouchUp(float x, float y, int32_t id) { + capi::touch_up(x, y, id); + } + void TouchMove(float x, float y, int32_t id) { + capi::touch_move(x, y, id); + } + void TouchCancel(float x, float y, int32_t id) { + capi::touch_cancel(x, y, id); + } void MouseMove(float x, float y) { capi::mouse_move(x, y); } void Reload() { capi::reload(); } diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.cpp b/support/hololens/ServoApp/ServoControl/ServoControl.cpp index 5aefe85b8132..4e151013590b 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.cpp +++ b/support/hololens/ServoApp/ServoControl/ServoControl.cpp @@ -38,9 +38,9 @@ void ServoControl::OnLoaded(IInspectable const &, RoutedEventArgs const &) { auto panel = Panel(); panel.Tapped(std::bind(&ServoControl::OnSurfaceTapped, this, _1, _2)); panel.PointerPressed( - std::bind(&ServoControl::OnSurfacePointerPressed, this, _1, _2)); + std::bind(&ServoControl::OnSurfacePointerPressed, this, _1, _2, true)); panel.PointerReleased( - std::bind(&ServoControl::OnSurfacePointerPressed, this, _1, _2)); + std::bind(&ServoControl::OnSurfacePointerPressed, this, _1, _2, false)); panel.PointerCanceled( std::bind(&ServoControl::OnSurfacePointerCanceled, this, _1, _2)); panel.PointerCaptureLost( @@ -105,6 +105,7 @@ void ServoControl::OnSurfaceManipulationDelta( void ServoControl::OnSurfaceTapped(IInspectable const &, Input::TappedRoutedEventArgs const &e) { + auto coords = e.GetPosition(Panel()); auto x = coords.X * mDPI; auto y = coords.Y * mDPI; @@ -113,9 +114,9 @@ void ServoControl::OnSurfaceTapped(IInspectable const &, } void ServoControl::OnSurfacePointerPressed( - IInspectable const &, Input::PointerRoutedEventArgs const &e) { - if (e.Pointer().PointerDeviceType() == - Windows::Devices::Input::PointerDeviceType::Mouse) { + IInspectable const &, Input::PointerRoutedEventArgs const &e, bool down) { + auto ty = e.Pointer().PointerDeviceType(); + if (ty == Windows::Devices::Input::PointerDeviceType::Mouse) { auto point = e.GetCurrentPoint(Panel()); auto x = point.Position().X * mDPI; @@ -144,23 +145,54 @@ void ServoControl::OnSurfacePointerPressed( } mPressedMouseButton = button; + } else if (ty == Windows::Devices::Input::PointerDeviceType::Touch) { + auto point = e.GetCurrentPoint(Panel()); + + auto x = point.Position().X * mDPI; + auto y = point.Position().Y * mDPI; + + if (down) { + RunOnGLThread([=] { mServo->TouchDown(x, y, point.PointerId()); }); + } else { + RunOnGLThread([=] { mServo->TouchUp(x, y, point.PointerId()); }); + } + e.Handled(true); } } void ServoControl::OnSurfacePointerCanceled( IInspectable const &, Input::PointerRoutedEventArgs const &e) { - mPressedMouseButton = {}; + auto ty = e.Pointer().PointerDeviceType(); + if (ty == Windows::Devices::Input::PointerDeviceType::Mouse) { + mPressedMouseButton = {}; + } else if (ty == Windows::Devices::Input::PointerDeviceType::Touch) { + auto point = e.GetCurrentPoint(Panel()); + auto x = point.Position().X * mDPI; + auto y = point.Position().Y * mDPI; + RunOnGLThread([=] { mServo->TouchCancel(x, y, point.PointerId()); }); + } } void ServoControl::OnSurfacePointerMoved( IInspectable const &, Input::PointerRoutedEventArgs const &e) { - if (e.Pointer().PointerDeviceType() == - Windows::Devices::Input::PointerDeviceType::Mouse) { + auto ty = e.Pointer().PointerDeviceType(); + if (ty == Windows::Devices::Input::PointerDeviceType::Mouse) { auto point = e.GetCurrentPoint(Panel()); auto x = point.Position().X * mDPI; auto y = point.Position().Y * mDPI; e.Handled(true); RunOnGLThread([=] { mServo->MouseMove(x, y); }); + } else if (ty == Windows::Devices::Input::PointerDeviceType::Touch) { + auto point = e.GetCurrentPoint(Panel()); + // UWP triggers this event even when the finger is moved + // in front of the application without contact + if (point.IsInContact()) { + auto x = point.Position().X * mDPI; + auto y = point.Position().Y * mDPI; + + RunOnGLThread([=] { mServo->TouchMove(x, y, point.PointerId()); }); + e.Handled(true); + } } } diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.h b/support/hololens/ServoApp/ServoControl/ServoControl.h index 921cf767021b..a55fe54add3b 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.h +++ b/support/hololens/ServoApp/ServoControl/ServoControl.h @@ -115,7 +115,8 @@ struct ServoControl : ServoControlT, public servo::ServoDelegate { void OnSurfacePointerPressed( IInspectable const &, - Windows::UI::Xaml::Input::PointerRoutedEventArgs const &); + Windows::UI::Xaml::Input::PointerRoutedEventArgs const &, + bool); void OnSurfacePointerCanceled( IInspectable const &,