private MotionEvent.PointerCoords GetPointerCoords(Android.Views.MotionEvent e, int pointerId) { // Get the pointer coords var pointerIndex = e.FindPointerIndex(pointerId); if (pointerIndex < 0) { return(null); } var point = new MotionEvent.PointerCoords(); e.GetPointerCoords(pointerIndex, point); return(point); }
public override bool OnTouchEvent(Android.Views.MotionEvent e) { var action = e.ActionMasked; var index = e.ActionIndex; Debug.WriteLine($"ACTION:{action}"); var coords = new MotionEvent.PointerCoords(); e.GetPointerCoords(index, coords); var position = new SKPoint(coords.X, coords.Y); if (action == MotionEventActions.Down) { var current = new Touch() { StartPosition = position, Position = position, State = TouchState.Began, }; this.touches[index] = current; this.view.Touch(new [] { current }); return(true); } else if (action == MotionEventActions.Move) { var current = this.touches[index]; current.Position = position; current.State = TouchState.Moved; this.view.Touch(new[] { current }); return(true); } else if (action == MotionEventActions.Cancel || action == MotionEventActions.Up) { var current = this.touches[index]; this.touches.Remove(index); current.Position = position; current.State = TouchState.Ended; this.view.Touch(new[] { current }); return(true); } return(base.OnTouchEvent(e)); }
void HandleMoveActions(Android.Views.MotionEvent e) { var pc = new Android.Views.MotionEvent.PointerCoords(); for (int i = 0; i < e.PointerCount; i++) { int id = e.GetPointerId(i); int touchIndex = Array.IndexOf(pointerIds, id); if (touchIndex < 0) { continue; } e.GetPointerCoords(i, pc); var position = new Vector2(pc.X, pc.Y); input.SetDesktopTouchPosition(touchIndex, position); if (touchIndex == 0) { input.DesktopMousePosition = position; } } }
void HandleMoveActions(Android.Views.MotionEvent e) { var pc = new Android.Views.MotionEvent.PointerCoords(); for (int i = 0; i < e.PointerCount; i++) { int id = e.GetPointerId(i); int touchIndex = Array.IndexOf(pointerIds, id); if (touchIndex < 0) { continue; } e.GetPointerCoords(i, pc); var position = new Vector2(pc.X, pc.Y) * input.ScreenToWorldTransform / Window.Current.PixelScale; input.SetTouchPosition(touchIndex, position); if (touchIndex == 0) { input.MousePosition = position; } } }
public override bool OnTouchEvent(MotionEvent e) { MotionEvent.PointerCoords mepc = new MotionEvent.PointerCoords(); int iEventPointerIndex = (int)(e.Action & MotionEventActions.PointerIdMask) >> (int)MotionEventActions.PointerIdShift; int iEventPointerId = e.GetPointerId(iEventPointerIndex); MotionEventActions action = e.Action & MotionEventActions.Mask; switch (action) { case MotionEventActions.Down: case MotionEventActions.PointerDown: Log.Debug(tag, String.Format("Point Down")); //DataClass.Drawables[iEventPointerId].Visible = true; e.GetPointerCoords(iEventPointerIndex, mepc); bool bFound = false; foreach (var item in DataClass.Sources) { if (item.Collision(mepc.X, mepc.Y)) { var nc = new Circle(item) { Visible = true }; DataClass.Drawables.Add(nc); dctControlable.Add(iEventPointerId, nc); bFound = true; break; } } if (bFound) break; foreach (var item in DataClass.Drawables) { if (item.Collision(mepc.X, mepc.Y)) { dctControlable.Add(iEventPointerId, item); break; } } break; case MotionEventActions.Move: var l = new List<string>(); for (int iPointerIndex = 0; iPointerIndex < e.PointerCount; iPointerIndex++) { int iPointerId = e.GetPointerId(iPointerIndex); l.Add(String.Format("{0} => {1}", iPointerIndex, iPointerId)); try { e.GetPointerCoords(iPointerIndex, mepc); dctControlable[iPointerId].X = mepc.X; dctControlable[iPointerId].Y = mepc.Y; } catch { l.Add(String.Format("ex", iPointerIndex, iPointerId)); } } Log.Debug(tag, string.Format("{0}, {1}", iEventPointerIndex, string.Join(", ", l))); break; case MotionEventActions.Up: case MotionEventActions.PointerUp: Log.Debug(tag, String.Format("Point Up")); //DataClass.Drawables[iEventPointerId].Visible = false; dctControlable.Remove(iEventPointerId); break; } this.Invalidate(); return true; }
public bool Display_OnTouchEvent( MotionEvent e ) { if( e.Action == MotionEventActions.Down || e.Action != MotionEventActions.Up ) { if( !touch_down ) first_down = true; else first_down = false; touch_down = true; } else { foreach( EventConsumer consumer in game.EventManager.ConsumerList ) consumer.MouseButtonRelease( MouseButton.Button1, mouse_x, mouse_y ); touch_down = false; return true; // nothing to compute } MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords(); /* Log.log( "Motion Events {0} on display of {1} {2}", e.PointerCount, Width, Height ); //if( e.EdgeFlags & Edge.Top ) for( int n = 0; n < e.PointerCount; n++ ) { e.GetPointerCoords( n, coords ); float x = coords.X; float y = coords.Y; float p = e.GetPressure( n ); Log.log( "got event at {0},{1} {2}", x, y, p ); } */ e.GetPointerCoords( 0, coords ); mouse_x = coords.X * 2.0f / Width - 1; mouse_y = ( Height - coords.Y ) * 2.0f / Height - 1; if( first_down ) { _mouse_x = mouse_x; _mouse_y = mouse_y; foreach( EventConsumer consumer in game.EventManager.ConsumerList ) consumer.MouseButtonClick( MouseButton.Button1, mouse_x, mouse_y ); } else { if( _mouse_x != -2 ) { del_mouse_x += mouse_x - _mouse_x; del_mouse_y += mouse_y - _mouse_y; } foreach (EventConsumer consumer in game.EventManager.ConsumerList) consumer.MouseMove( del_mouse_x, del_mouse_y, mouse_x, mouse_y ); if( HiddenMouse ) { #if !USE_GLES2 System.Windows.Forms.Cursor.Position = new Point( ( X + Width ) / 2, ( Y + Height ) / 2 ); #endif //_mouse_x = 0; //_mouse_y = 0; } else { //_mouse_x = mouse_x; //_mouse_y = mouse_y; } _mouse_x = mouse_x; _mouse_y = mouse_y; } return true; }
public override bool OnTouchEvent(MotionEvent e) { // Get current position of pointer at index 0. // This will be the first pointer down and the last pointer up even if not the same System.Drawing.PointF curr = new System.Drawing.PointF(e.GetX(), e.GetY()); switch (e.Action) { case MotionEventActions.PointerDown: pointer2Index = 1; break; case MotionEventActions.Down: // Set the current box and add it to the List<Box> mBoxes if (mCurrentBox == null) { mCurrentBox = new Box(curr, e.GetPointerId(0)); mBoxes.Add(mCurrentBox); } break; case MotionEventActions.Pointer2Down: // Handle 2nd touch. Set the start point of the rotation if (mRotationStart == null) { // Get coordinates of pointer 2 MotionEvent.PointerCoords pCoords = new MotionEvent.PointerCoords(); e.GetPointerCoords(1, pCoords); // Set the starting coordinates for the rotation mRotationStart = new Box(new System.Drawing.PointF(pCoords.X, pCoords.Y), e.GetPointerId(1)); mInitialRotation = mCurrentBox.Rotation; pointer2Index = 1; } break; case MotionEventActions.Move: // Handle first pointer move, set end point for rectangle if (mCurrentBox != null && mCurrentBox.PointerId == e.GetPointerId(0)) { mCurrentBox.Current = curr; } // Handle second pointer move, set rotation amount if (mRotationStart != null && mRotationStart.PointerId == e.GetPointerId(pointer2Index)) { // Get coordinates of pointer 2 MotionEvent.PointerCoords pCoords = new MotionEvent.PointerCoords(); e.GetPointerCoords(pointer2Index, pCoords); // Set the rotation of the box to the difference between the origin of mRotation and the current position of pointer 2 mCurrentBox.Rotation = mInitialRotation + pCoords.Y - mRotationStart.Origin.Y; } Invalidate(); break; case MotionEventActions.Pointer2Up: mRotationStart = null; break; case MotionEventActions.PointerUp: pointer2Index = 0; break; case MotionEventActions.Up: mCurrentBox = null; mRotationStart = null; break; case MotionEventActions.Cancel: mCurrentBox = null; mRotationStart = null; break; } return true; }