protected PointerEvent CreatePointerEvent(PointerState state, Vector2 position) { if (state == PointerState.Down) lastTouchPosition = position; var pointerEvent = new PointerEvent(0, position, position - lastTouchPosition, new TimeSpan(), state, PointerType.Touch, true); lastTouchPosition = position; return pointerEvent; }
/// <summary> /// Determine input from a user from Pointer (Touch/Mouse). /// It analyses the input from a user, and transform it to InputState using in the game, which is then returned. /// </summary> /// <returns></returns> private InputState GetPointerInputState() { // Get new state of Pointer (Touch input) if (Input.PointerEvents.Any()) { var lastPointer = Input.PointerEvents.Last(); isPointerDown = lastPointer.State != PointerState.Up; pointerState = lastPointer; } // If a user does not touch the screen, there is not input if (!isPointerDown) return InputState.None; // Transform pointer's position from normorlize coordinate to virtual resolution coordinate var resolution = new Vector2(GraphicsDevice.BackBuffer.Width, GraphicsDevice.BackBuffer.Height); var virtualCoordinatePointerPosition = resolution * (pointerState.Position - new Vector2(0.0f)); // Get current position of the agent, since the origin of the sprite is at the center, region needs to be shifted to top-left agentSpriteRegion.X = (int)VirtualCoordToPixel(Entity.Transform.Position.X) - agentSpriteRegion.Width / 2; agentSpriteRegion.Y = (int)VirtualCoordToPixel(Entity.Transform.Position.Y) - agentSpriteRegion.Height / 2; // Check if the touch position is in the x-axis region of the agent's sprite; if so, input is shoot if (agentSpriteRegion.Left <= virtualCoordinatePointerPosition.X && virtualCoordinatePointerPosition.X <= agentSpriteRegion.Right) return InputState.Shoot; // Check if a pointer falls left or right of the screen, which would correspond to Run to the left or right respectively return ((pointerState.Position.X) <= agentSpriteRegion.Center.X / resolution.X) ? InputState.RunLeft : InputState.RunRight; }