This commit is contained in:
Matthew Leibowitz 2017-06-01 14:22:10 -05:00
Родитель 165f9bdcf8
Коммит bc6ec734f1
3 изменённых файлов: 301 добавлений и 259 удалений

Просмотреть файл

@ -1,91 +1,91 @@
using System;
using Android.Views;
namespace SkiaSharp.Views.Forms
{
internal class SKTouchHandler
{
private Action<SKTouchActionEventArgs> onTouchAction;
private Func<float, float> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<float, float> 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<SKTouchActionEventArgs> onTouchAction;
private Func<float, float> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<float, float> 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;
}
}
}
}
}

Просмотреть файл

@ -1,76 +1,118 @@
using System;
using System.Linq;
using Foundation;
using AppKit;
namespace SkiaSharp.Views.Forms
{
internal class SKTouchHandler : NSGestureRecognizer
{
private Action<SKTouchActionEventArgs> onTouchAction;
private Func<nfloat, nfloat> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<nfloat, nfloat> 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<SKTouchActionEventArgs> onTouchAction;
private Func<nfloat, nfloat> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<nfloat, nfloat> 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;
}
}
}

Просмотреть файл

@ -1,92 +1,92 @@
using System;
using System.Linq;
using Foundation;
using UIKit;
namespace SkiaSharp.Views.Forms
{
internal class SKTouchHandler : UIGestureRecognizer
{
private Action<SKTouchActionEventArgs> onTouchAction;
private Func<nfloat, nfloat> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<nfloat, nfloat> 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<UITouch>())
{
FireEvent(SKTouchActionType.Pressed, touch, true);
}
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
FireEvent(SKTouchActionType.Moved, touch, true);
}
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
FireEvent(SKTouchActionType.Released, touch, false);
}
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
base.TouchesCancelled(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
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<SKTouchActionEventArgs> onTouchAction;
private Func<nfloat, nfloat> scalePixels;
public SKTouchHandler(Action<SKTouchActionEventArgs> onTouchAction, Func<nfloat, nfloat> 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<UITouch>())
{
FireEvent(SKTouchActionType.Pressed, touch, true);
}
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
FireEvent(SKTouchActionType.Moved, touch, true);
}
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
FireEvent(SKTouchActionType.Released, touch, false);
}
}
public override void TouchesCancelled(NSSet touches, UIEvent evt)
{
base.TouchesCancelled(touches, evt);
foreach (UITouch touch in touches.Cast<UITouch>())
{
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;
}
}
}