public static TouchCollection GetState() { #if SILVERLIGHT touches = new TouchCollection(); MouseState mouse = Mouse.GetState(); if (!isPressed && mouse.LeftButton == ButtonState.Pressed) { touches.Add(new TouchLocation(1, TouchLocationState.Pressed, new Vector2(mouse.X, mouse.Y))); isPressed = true; } else if (isPressed && mouse.LeftButton == ButtonState.Pressed) { touches.Add(new TouchLocation(1, TouchLocationState.Moved, new Vector2(mouse.X, mouse.Y))); } else if (isPressed && mouse.LeftButton == ButtonState.Released) { touches.Add(new TouchLocation(1, TouchLocationState.Released, new Vector2(mouse.X, mouse.Y))); isPressed = false; } return touches; #else return touches; #endif }
private void FillTouchCollection(NSSet touches) { UITouch [] touchesArray = touches.ToArray <UITouch>(); for (int i = 0; i < touchesArray.Length; i++) { //Get IOS touch UITouch touch = touchesArray[i]; //Get position touch Vector2 position = new Vector2(touch.LocationInView(touch.View)); Vector2 translatedPosition = GetOffsetPosition(position); TouchLocation tlocation; TouchCollection collection = TouchPanel.Collection; int index; switch (touch.Phase) { case UITouchPhase.Stationary: case UITouchPhase.Moved: index = collection.FindById(touch.Handle.ToInt32(), out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Moved; tlocation.Position = translatedPosition; collection[index] = tlocation; } break; case UITouchPhase.Began: tlocation = new TouchLocation(touch.Handle.ToInt32(), TouchLocationState.Pressed, translatedPosition); collection.Add(tlocation); break; case UITouchPhase.Ended: index = collection.FindById(touch.Handle.ToInt32(), out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Released; collection[index] = tlocation; } break; default: break; } } }
TouchCollection TransformState(TouchCollection input) { if (input.Count == 0 || hasTransform == false) { return(input); } var retval = new TouchCollection(); foreach (var touchLocation in input) { retval.Add(TransformLocation(touchLocation)); } return(retval); }
//private Vector2 GetOffsetPosition(Vector2 position) //{ // Vector2 translatedPosition = position; // switch (CurrentOrientation) // { // case DisplayOrientation.Portrait: // break; // case DisplayOrientation.LandscapeRight: // translatedPosition = new Vector2(ClientBounds.Height - position.Y, position.X); // break; // case DisplayOrientation.LandscapeLeft: // translatedPosition = new Vector2(position.Y, ClientBounds.Width - position.X); // break; // case DisplayOrientation.PortraitUpsideDown: // translatedPosition = new Vector2(ClientBounds.Width - position.X, ClientBounds.Height - position.Y); // break; // } // return translatedPosition * UIScreen.MainScreen.Scale; //} public bool OnTouch(View v, MotionEvent e) { TouchLocation tlocation; TouchCollection collection = TouchPanel.Collection; Vector2 position = Vector2.Zero; position.X = e.GetX(e.ActionIndex); position.Y = e.GetY(e.ActionIndex); int id = e.GetPointerId(e.ActionIndex); int index; switch (e.ActionMasked) { // DOWN case 0: case 5: tlocation = new TouchLocation(id, TouchLocationState.Pressed, position); collection.Add(tlocation); break; // UP case 1: case 6: index = collection.FindById(e.GetPointerId(e.ActionIndex), out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Released; collection[index] = tlocation; } break; // MOVE case 2: for (int i = 0; i < e.PointerCount; i++) { id = e.GetPointerId(i); position.X = e.GetX(i); position.Y = e.GetY(i); index = collection.FindById(id, out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Moved; tlocation.Position = position; collection[index] = tlocation; } } break; // CANCEL, OUTSIDE case 3: case 4: index = collection.FindById(id, out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Invalid; collection[index] = tlocation; } break; } return(true); //for (int i = 0; i < e.PointerCount; i++) //{ // Vector2 position = Vector2.Zero; // position.X = e.GetX(i); // position.Y = e.GetY(i); // TouchLocation tlocation; // TouchCollection collection = TouchPanel.Collection; // int index; // int id = e.GetPointerId(i); // switch (e.ActionMasked) // { // //case MotionEventActions.Down: // case 0: // //Log.Debug("TESTING", string.Format("DOWN: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked)); // tlocation = new TouchLocation(id, TouchLocationState.Pressed, position); // collection.Add(tlocation); // break; // //case MotionEventActions.PointerDown: // case 5: // //Log.Debug("TESTING", string.Format("DOWN: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked)); // tlocation = new TouchLocation(e.GetPointerId(e.ActionIndex), TouchLocationState.Pressed, position); // collection.Add(tlocation); // break; // //case MotionEventActions.Up: // case 1: // index = collection.FindById(id, out tlocation); // if (index >= 0) // { // tlocation.State = TouchLocationState.Released; // collection[index] = tlocation; // } // break; // //case MotionEventActions.PointerUp: // case 6: // //Log.Debug("TESTING", string.Format("UP: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked)); // index = collection.FindById(e.GetPointerId(e.ActionIndex), out tlocation); // if (index >= 0) // { // tlocation.State = TouchLocationState.Released; // collection[index] = tlocation; // } // break; // //case MotionEventActions.Move: // case 2: // //Log.Debug("TESTING", string.Format("MOVE: Count=[{3}] ID=[{2}] ({0:N},{1:N})", position.X, position.Y, id, e.PointerCount)); // index = collection.FindById(id, out tlocation); // if (index >= 0) // { // tlocation.State = TouchLocationState.Moved; // tlocation.Position = position; // collection[index] = tlocation; // } // break; // default: // //Log.Debug("TESTING", string.Format("OTHER {4}: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked)); // index = collection.FindById(id, out tlocation); // if (index >= 0) // { // tlocation.State = TouchLocationState.Released; // collection[index] = tlocation; // } // break; // } //} //return true; //TouchLocationState state = TouchLocationState.Invalid; //if (e.Action == MotionEventActions.Cancel) //{ // state = TouchLocationState.Invalid; //} //if (e.Action == MotionEventActions.Up) //{ // state = TouchLocationState.Released; //} //if (e.Action == MotionEventActions.Move) //{ // state = TouchLocationState.Moved; // Mouse.SetPosition((int)e.GetX(), (int)e.GetY()); //} //if (e.Action == MotionEventActions.Down) //{ // state = TouchLocationState.Pressed; // Mouse.SetPosition((int)e.GetX(), (int)e.GetY()); //} //TouchLocation tprevious; //TouchLocation tlocation; //Vector2 position = new Vector2(e.GetX(), e.GetY()); //Vector2 translatedPosition = position; //if (state != TouchLocationState.Pressed && _previousTouches.TryGetValue(e.Handle, out tprevious)) //{ // tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure, tprevious.State, tprevious.Position, tprevious.Pressure); //} //else //{ // tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure); //} //TouchPanel.Collection.Clear(); //TouchPanel.Collection.Add(tlocation); //if (state != TouchLocationState.Released) // _previousTouches[e.Handle] = tlocation; //else // _previousTouches.Remove(e.Handle); //return true; ////////// //TouchLocationState state = TouchLocationState.Invalid; //if (e.Action == MotionEventActions.Cancel) //{ // state = TouchLocationState.Invalid; //} //if (e.Action == MotionEventActions.Up) //{ // state = TouchLocationState.Released; //} //if (e.Action == MotionEventActions.Move) //{ // state = TouchLocationState.Moved; // Mouse.SetPosition((int)e.GetX(), (int)e.GetY()); //} //if (e.Action == MotionEventActions.Down) //{ // state = TouchLocationState.Pressed; // Mouse.SetPosition((int)e.GetX(), (int)e.GetY()); //} //TouchLocation tprevious; //TouchLocation tlocation; //Vector2 position = new Vector2(e.GetX(), e.GetY()); //Vector2 translatedPosition = position; //switch (CurrentOrientation) //{ // case DisplayOrientation.Portrait: // break; // case DisplayOrientation.LandscapeRight: // translatedPosition = new Vector2(ClientBounds.Height - position.Y, position.X); // break; // case DisplayOrientation.LandscapeLeft: // translatedPosition = new Vector2(position.Y, ClientBounds.Width - position.X); // break; // case DisplayOrientation.PortraitUpsideDown: // translatedPosition = new Vector2(ClientBounds.Width - position.X, ClientBounds.Height - position.Y); // break; //} //if (state != TouchLocationState.Pressed && _previousTouches.TryGetValue(e.Handle, out tprevious)) //{ // tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure, tprevious.State, tprevious.Position, tprevious.Pressure); //} //else //{ // tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure); //} //TouchPanel.Collection.Clear(); //TouchPanel.Collection.Add(tlocation); //if (state != TouchLocationState.Released) // _previousTouches[e.Handle] = tlocation; //else // _previousTouches.Remove(e.Handle); //GamePad.Instance.Update(e); //return true; }
public override bool OnTouchEvent(MotionEvent e) { TouchLocation tlocation; TouchCollection collection = TouchPanel.Collection; Vector2 position = Vector2.Zero; position.X = e.GetX(e.ActionIndex); position.Y = e.GetY(e.ActionIndex); UpdateTouchPosition(ref position); int id = e.GetPointerId(e.ActionIndex); int index; switch (e.ActionMasked) { // DOWN case MotionEventActions.Down: case MotionEventActions.PointerDown: index = collection.FindIndexById(e.GetPointerId(e.ActionIndex), out tlocation); if (index < 0) { tlocation = new TouchLocation(id, TouchLocationState.Pressed, position); collection.Add(tlocation); } else { tlocation.State = TouchLocationState.Pressed; tlocation.Position = position; } break; // UP case MotionEventActions.Up: case MotionEventActions.PointerUp: index = collection.FindIndexById(e.GetPointerId(e.ActionIndex), out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Released; collection[index] = tlocation; } break; // MOVE case MotionEventActions.Move: for (int i = 0; i < e.PointerCount; i++) { id = e.GetPointerId(i); position.X = e.GetX(i); position.Y = e.GetY(i); UpdateTouchPosition(ref position); index = collection.FindIndexById(id, out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Moved; tlocation.Position = position; collection[index] = tlocation; } } break; // CANCEL, OUTSIDE case MotionEventActions.Cancel: case MotionEventActions.Outside: index = collection.FindIndexById(id, out tlocation); if (index >= 0) { tlocation.State = TouchLocationState.Invalid; collection[index] = tlocation; } break; } if (gesture != null) { GestureListener.CheckForDrag(e, position); gesture.OnTouchEvent(e); } return(true); }
// TODO: Review FillTouchCollection private void FillTouchCollection(NSSet touches) { if (touches.Count == 0) { return; } TouchCollection collection = TouchPanel.Collection; var touchesArray = touches.ToArray <UITouch> (); for (int i = 0; i < touchesArray.Length; ++i) { var touch = touchesArray [i]; //Get position touch var location = touch.LocationInView(touch.View); var position = GetOffsetPosition(new Vector2(location.X, location.Y), true); var id = touch.Handle.ToInt32(); switch (touch.Phase) { case UITouchPhase.Stationary: case UITouchPhase.Moved: collection.Update(id, TouchLocationState.Moved, position); if (i == 0) { Mouse.State.X = (int)position.X; Mouse.State.Y = (int)position.Y; } break; case UITouchPhase.Began: collection.Add(id, position); if (i == 0) { Mouse.State.X = (int)position.X; Mouse.State.Y = (int)position.Y; Mouse.State.LeftButton = ButtonState.Pressed; } break; case UITouchPhase.Ended: collection.Update(id, TouchLocationState.Released, position); if (i == 0) { Mouse.State.X = (int)position.X; Mouse.State.Y = (int)position.Y; Mouse.State.LeftButton = ButtonState.Released; } break; case UITouchPhase.Cancelled: collection.Update(id, TouchLocationState.Invalid, position); break; default: break; } } }