From bc6ec734f1cfc69e9b621a2a994dd5b95f4622db Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Thu, 1 Jun 2017 14:22:10 -0500 Subject: [PATCH] The newlines are dodgy --- .../SKTouchHandler.cs | 182 ++++++++-------- .../SKTouchHandler.cs | 194 +++++++++++------- .../SKTouchHandler.cs | 184 ++++++++--------- 3 files changed, 301 insertions(+), 259 deletions(-) diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Android/SKTouchHandler.cs b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Android/SKTouchHandler.cs index 1b03e71d..0906a141 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Android/SKTouchHandler.cs +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Android/SKTouchHandler.cs @@ -1,91 +1,91 @@ -using System; -using Android.Views; - -namespace SkiaSharp.Views.Forms -{ - internal class SKTouchHandler - { - private Action onTouchAction; - private Func scalePixels; - - public SKTouchHandler(Action onTouchAction, Func scalePixels) - { - this.onTouchAction = onTouchAction; - this.scalePixels = scalePixels; - } - - public void Attach(View view) - { - view.Touch += OnTouch; - } - - public void Detach(View view) - { - // clean the view - if (view != null) - { - view.Touch -= OnTouch; - } - - // remove references - onTouchAction = null; - scalePixels = null; - } - - private void OnTouch(object sender, View.TouchEventArgs e) - { - if (onTouchAction == null || scalePixels == null) - return; - - var evt = e.Event; - var pointer = evt.ActionIndex; - - var id = evt.GetPointerId(pointer); - var coords = new SKPoint(scalePixels(evt.GetX(pointer)), scalePixels(evt.GetY(pointer))); - - switch (evt.ActionMasked) - { - case MotionEventActions.Down: - case MotionEventActions.PointerDown: - { - var args = new SKTouchActionEventArgs(id, SKTouchActionType.Pressed, coords, true); - onTouchAction(args); - e.Handled = args.Handled; - break; - } - - case MotionEventActions.Move: - { - var count = evt.PointerCount; - for (pointer = 0; pointer < count; pointer++) - { - id = evt.GetPointerId(pointer); - coords = new SKPoint(scalePixels(evt.GetX(pointer)), scalePixels(evt.GetY(pointer))); - - var args = new SKTouchActionEventArgs(id, SKTouchActionType.Moved, coords, true); - onTouchAction(args); - e.Handled = e.Handled || args.Handled; - } - break; - } - - case MotionEventActions.Up: - case MotionEventActions.PointerUp: - { - var args = new SKTouchActionEventArgs(id, SKTouchActionType.Released, coords, false); - onTouchAction(args); - e.Handled = args.Handled; - break; - } - - case MotionEventActions.Cancel: - { - var args = new SKTouchActionEventArgs(id, SKTouchActionType.Cancelled, coords, false); - onTouchAction(args); - e.Handled = args.Handled; - break; - } - } - } - } -} +using System; +using Android.Views; + +namespace SkiaSharp.Views.Forms +{ + internal class SKTouchHandler + { + private Action onTouchAction; + private Func scalePixels; + + public SKTouchHandler(Action onTouchAction, Func scalePixels) + { + this.onTouchAction = onTouchAction; + this.scalePixels = scalePixels; + } + + public void Attach(View view) + { + view.Touch += OnTouch; + } + + public void Detach(View view) + { + // clean the view + if (view != null) + { + view.Touch -= OnTouch; + } + + // remove references + onTouchAction = null; + scalePixels = null; + } + + private void OnTouch(object sender, View.TouchEventArgs e) + { + if (onTouchAction == null || scalePixels == null) + return; + + var evt = e.Event; + var pointer = evt.ActionIndex; + + var id = evt.GetPointerId(pointer); + var coords = new SKPoint(scalePixels(evt.GetX(pointer)), scalePixels(evt.GetY(pointer))); + + switch (evt.ActionMasked) + { + case MotionEventActions.Down: + case MotionEventActions.PointerDown: + { + var args = new SKTouchActionEventArgs(id, SKTouchActionType.Pressed, coords, true); + onTouchAction(args); + e.Handled = args.Handled; + break; + } + + case MotionEventActions.Move: + { + var count = evt.PointerCount; + for (pointer = 0; pointer < count; pointer++) + { + id = evt.GetPointerId(pointer); + coords = new SKPoint(scalePixels(evt.GetX(pointer)), scalePixels(evt.GetY(pointer))); + + var args = new SKTouchActionEventArgs(id, SKTouchActionType.Moved, coords, true); + onTouchAction(args); + e.Handled = e.Handled || args.Handled; + } + break; + } + + case MotionEventActions.Up: + case MotionEventActions.PointerUp: + { + var args = new SKTouchActionEventArgs(id, SKTouchActionType.Released, coords, false); + onTouchAction(args); + e.Handled = args.Handled; + break; + } + + case MotionEventActions.Cancel: + { + var args = new SKTouchActionEventArgs(id, SKTouchActionType.Cancelled, coords, false); + onTouchAction(args); + e.Handled = args.Handled; + break; + } + } + } + } +} diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Mac/SKTouchHandler.cs b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Mac/SKTouchHandler.cs index bf75b9f8..a7813049 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Mac/SKTouchHandler.cs +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Mac/SKTouchHandler.cs @@ -1,76 +1,118 @@ -using System; -using System.Linq; -using Foundation; -using AppKit; - -namespace SkiaSharp.Views.Forms -{ - internal class SKTouchHandler : NSGestureRecognizer - { - private Action onTouchAction; - private Func scalePixels; - - public SKTouchHandler(Action onTouchAction, Func scalePixels) - { - this.onTouchAction = onTouchAction; - this.scalePixels = scalePixels; - } - - public void Attach(NSView view) - { - view.AddGestureRecognizer(this); - } - - public void Detach(NSView view) - { - // clean the view - if (view != null) - { - view.RemoveGestureRecognizer(this); - } - - // remove references - onTouchAction = null; - scalePixels = null; - } - - public override void MouseDown(NSEvent mouseEvent) - { - base.MouseDown(mouseEvent); - - FireEvent(SKTouchActionType.Pressed, mouseEvent, true); - } - - public override void MouseUp(NSEvent mouseEvent) - { - base.MouseUp(mouseEvent); - - FireEvent(SKTouchActionType.Released, mouseEvent, false); - } - - public override void MouseDragged(NSEvent mouseEvent) - { - base.MouseDragged(mouseEvent); - - FireEvent(SKTouchActionType.Moved, mouseEvent, true); - } - - private bool FireEvent(SKTouchActionType actionType, NSEvent mouseEvent, bool inContact) - { - if (onTouchAction == null || scalePixels == null) - return false; - - var id = mouseEvent.ButtonNumber; - - var cgPoint = LocationInView(View); - // flip the Y coordinate for macOS - cgPoint.Y = View.Bounds.Height - cgPoint.Y; - - var point = new SKPoint((float)scalePixels(cgPoint.X), (float)scalePixels(cgPoint.Y)); - - var args = new SKTouchActionEventArgs(id, actionType, point, inContact); - onTouchAction(args); - return args.Handled; - } - } -} +using System; +using System.Linq; +using Foundation; +using AppKit; + +namespace SkiaSharp.Views.Forms +{ + internal class SKTouchHandler : NSGestureRecognizer + { + private Action onTouchAction; + private Func scalePixels; + + public SKTouchHandler(Action onTouchAction, Func scalePixels) + { + this.onTouchAction = onTouchAction; + this.scalePixels = scalePixels; + } + + public void Attach(NSView view) + { + view.AddGestureRecognizer(this); + } + + public void Detach(NSView view) + { + // clean the view + if (view != null) + { + view.RemoveGestureRecognizer(this); + } + + // remove references + onTouchAction = null; + scalePixels = null; + } + + public override void MouseDown(NSEvent mouseEvent) + { + base.MouseDown(mouseEvent); + + FireEvent(SKTouchActionType.Pressed, SKMouseButton.Left, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + public override void MouseUp(NSEvent mouseEvent) + { + base.MouseUp(mouseEvent); + + FireEvent(SKTouchActionType.Released, SKMouseButton.Left, SKTouchDeviceType.Mouse, mouseEvent, false); + } + + public override void MouseDragged(NSEvent mouseEvent) + { + base.MouseDragged(mouseEvent); + + FireEvent(SKTouchActionType.Moved, SKMouseButton.Left, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + public override void OtherMouseDown(NSEvent mouseEvent) + { + base.OtherMouseDown(mouseEvent); + + FireEvent(SKTouchActionType.Pressed, SKMouseButton.Middle, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + public override void OtherMouseUp(NSEvent mouseEvent) + { + base.OtherMouseUp(mouseEvent); + + FireEvent(SKTouchActionType.Released, SKMouseButton.Middle, SKTouchDeviceType.Mouse, mouseEvent, false); + } + + public override void OtherMouseDragged(NSEvent mouseEvent) + { + base.OtherMouseDragged(mouseEvent); + + FireEvent(SKTouchActionType.Moved, SKMouseButton.Middle, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + public override void RightMouseDown(NSEvent mouseEvent) + { + base.RightMouseDown(mouseEvent); + + FireEvent(SKTouchActionType.Pressed, SKMouseButton.Right, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + public override void RightMouseUp(NSEvent mouseEvent) + { + base.RightMouseUp(mouseEvent); + + FireEvent(SKTouchActionType.Released, SKMouseButton.Right, SKTouchDeviceType.Mouse, mouseEvent, false); + } + + public override void RightMouseDragged(NSEvent mouseEvent) + { + base.RightMouseDragged(mouseEvent); + + FireEvent(SKTouchActionType.Moved, SKMouseButton.Right, SKTouchDeviceType.Mouse, mouseEvent, true); + } + + private bool FireEvent(SKTouchActionType actionType, SKMouseButton mouse, SKTouchDeviceType device, NSEvent mouseEvent, bool inContact) + { + if (onTouchAction == null || scalePixels == null) + return false; + + var id = mouseEvent.ButtonNumber; + + var cgPoint = LocationInView(View); + // flip the Y coordinate for macOS + cgPoint.Y = View.Bounds.Height - cgPoint.Y; + + var point = new SKPoint((float)scalePixels(cgPoint.X), (float)scalePixels(cgPoint.Y)); + + var args = new SKTouchActionEventArgs(id, actionType, mouse, device, point, inContact); + onTouchAction(args); + return args.Handled; + } + } +} diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.iOS/SKTouchHandler.cs b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.iOS/SKTouchHandler.cs index 203d5bdf..a5b844ad 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.iOS/SKTouchHandler.cs +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.iOS/SKTouchHandler.cs @@ -1,92 +1,92 @@ -using System; -using System.Linq; -using Foundation; -using UIKit; - -namespace SkiaSharp.Views.Forms -{ - internal class SKTouchHandler : UIGestureRecognizer - { - private Action onTouchAction; - private Func scalePixels; - - public SKTouchHandler(Action onTouchAction, Func scalePixels) - { - this.onTouchAction = onTouchAction; - this.scalePixels = scalePixels; - } - - public void Attach(UIView view) - { - view.AddGestureRecognizer(this); - } - - public void Detach(UIView view) - { - // clean the view - if (view != null) - { - view.RemoveGestureRecognizer(this); - } - - // remove references - onTouchAction = null; - scalePixels = null; - } - - public override void TouchesBegan(NSSet touches, UIEvent evt) - { - base.TouchesBegan(touches, evt); - - foreach (UITouch touch in touches.Cast()) - { - FireEvent(SKTouchActionType.Pressed, touch, true); - } - } - - public override void TouchesMoved(NSSet touches, UIEvent evt) - { - base.TouchesMoved(touches, evt); - - foreach (UITouch touch in touches.Cast()) - { - FireEvent(SKTouchActionType.Moved, touch, true); - } - } - - public override void TouchesEnded(NSSet touches, UIEvent evt) - { - base.TouchesEnded(touches, evt); - - foreach (UITouch touch in touches.Cast()) - { - FireEvent(SKTouchActionType.Released, touch, false); - } - } - - public override void TouchesCancelled(NSSet touches, UIEvent evt) - { - base.TouchesCancelled(touches, evt); - - foreach (UITouch touch in touches.Cast()) - { - FireEvent(SKTouchActionType.Cancelled, touch, false); - } - } - - private bool FireEvent(SKTouchActionType actionType, UITouch touch, bool inContact) - { - if (onTouchAction == null || scalePixels == null) - return false; - - var id = touch.Handle.ToInt64(); - - var cgPoint = touch.LocationInView(View); - var point = new SKPoint((float)scalePixels(cgPoint.X), (float)scalePixels(cgPoint.Y)); - - var args = new SKTouchActionEventArgs(id, actionType, point, inContact); - onTouchAction(args); - return args.Handled; - } - } -} +using System; +using System.Linq; +using Foundation; +using UIKit; + +namespace SkiaSharp.Views.Forms +{ + internal class SKTouchHandler : UIGestureRecognizer + { + private Action onTouchAction; + private Func scalePixels; + + public SKTouchHandler(Action onTouchAction, Func scalePixels) + { + this.onTouchAction = onTouchAction; + this.scalePixels = scalePixels; + } + + public void Attach(UIView view) + { + view.AddGestureRecognizer(this); + } + + public void Detach(UIView view) + { + // clean the view + if (view != null) + { + view.RemoveGestureRecognizer(this); + } + + // remove references + onTouchAction = null; + scalePixels = null; + } + + public override void TouchesBegan(NSSet touches, UIEvent evt) + { + base.TouchesBegan(touches, evt); + + foreach (UITouch touch in touches.Cast()) + { + FireEvent(SKTouchActionType.Pressed, touch, true); + } + } + + public override void TouchesMoved(NSSet touches, UIEvent evt) + { + base.TouchesMoved(touches, evt); + + foreach (UITouch touch in touches.Cast()) + { + FireEvent(SKTouchActionType.Moved, touch, true); + } + } + + public override void TouchesEnded(NSSet touches, UIEvent evt) + { + base.TouchesEnded(touches, evt); + + foreach (UITouch touch in touches.Cast()) + { + FireEvent(SKTouchActionType.Released, touch, false); + } + } + + public override void TouchesCancelled(NSSet touches, UIEvent evt) + { + base.TouchesCancelled(touches, evt); + + foreach (UITouch touch in touches.Cast()) + { + FireEvent(SKTouchActionType.Cancelled, touch, false); + } + } + + private bool FireEvent(SKTouchActionType actionType, UITouch touch, bool inContact) + { + if (onTouchAction == null || scalePixels == null) + return false; + + var id = touch.Handle.ToInt64(); + + var cgPoint = touch.LocationInView(View); + var point = new SKPoint((float)scalePixels(cgPoint.X), (float)scalePixels(cgPoint.Y)); + + var args = new SKTouchActionEventArgs(id, actionType, point, inContact); + onTouchAction(args); + return args.Handled; + } + } +}