public void ProcessEvent(PointerEvent inputEvent) { pointerEvents.Add(inputEvent); // Update position and delta from whatever device sends position updates LastPointerDevice = inputEvent.Pointer; if (inputEvent.Device is IMouseDevice) { mousePosition = inputEvent.Position; absoluteMousePosition = inputEvent.AbsolutePosition; // Add deltas together, so nothing gets lost if a down events gets sent after a move event with the actual delta MouseDelta += inputEvent.DeltaPosition; AbsoluteMouseDelta += inputEvent.AbsoluteDeltaPosition; } }
/// <summary> /// Updates a pointer event with position / type / id set and updates the storted pointer data /// </summary> /// <param name="evt"></param> public void UpdatePointerState(PointerEvent evt, bool updateDelta = true) { var data = GetPointerData(evt.PointerId); if (updateDelta) { // Update delta based on change in position evt.DeltaPosition = data.Delta = evt.Position - data.Position; } else { data.Delta = evt.DeltaPosition; } // Update position data.Position = evt.Position; if (evt.EventType == PointerEventType.Pressed) { // Start pressed events with time 0 data.Clock.Restart(); data.IsDown = true; pressedPointers.Add(data); downPointers.Add(data); } else if (evt.EventType == PointerEventType.Released || evt.EventType == PointerEventType.Canceled) { releasedPointers.Add(data); downPointers.Remove(data); data.IsDown = false; } evt.IsDown = data.IsDown; evt.DeltaTime = data.Clock.Elapsed; // Reset pointer clock data.Clock.Restart(); }
/// <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.Presenter.BackBuffer.Width, GraphicsDevice.Presenter.BackBuffer.Height); var virtualCoordinatePointerPosition = resolution*pointerState.Position; // Get current position of the agent, since the origin of the sprite is at the center, region needs to be shifted to top-left var agentSize = spriteSheet["idle0"].SizeInPixels; var agentSpriteRegion = new RectangleF { X = (int) VirtualCoordToPixel(Entity.Transform.Position.X) - agentSize.X/2, Y = (int) VirtualCoordToPixel(Entity.Transform.Position.Y) - agentSize.Y/2, Width = agentSize.X, Height = agentSize.Y }; // 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; }
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.Presenter.BackBuffer.Width, GraphicsDevice.Presenter.BackBuffer.Height); var virtualCoordinatePointerPositionA = resolution.X * (pointerState.Position.X + 0.05f); var virtualCoordinatePointerPositionB = resolution.X * (pointerState.Position.X - 0.05f); var virtualX = VirtualCoordToPixel(Entity.Transform.Position.X); // Check if the touch position is in the x-axis region of the agent's sprite; if so, input is shoot if (virtualX <= virtualCoordinatePointerPositionA && virtualCoordinatePointerPositionB <= virtualX) { 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) <= virtualX / resolution.X) ? InputState.RunLeft : InputState.RunRight; }