private void Update() { _prevPointerDownState = _pointerDownState; if (UnityEngine.Input.GetMouseButtonDown(0)) { OnPointerDown?.Invoke(); _pointerDownState = true; } if (UnityEngine.Input.GetMouseButtonUp(0)) { OnPointerUp?.Invoke(); _pointerDownState = false; } Vector2 mousePosition = UnityEngine.Input.mousePosition; Vector2 posDifference = mousePosition - _prevMousePos; if (posDifference.magnitude > 0f) { OnPointerMoved?.Invoke(posDifference); _prevMousePos = mousePosition; if (_prevPointerDownState && _pointerDownState) { OnPointerDragged?.Invoke(posDifference); } } }
public void UpdateInput(GameTime gameTime) { MouseState mouseState = Mouse.GetState(); curKeyboardState = Keyboard.GetState(); Vector2 mousePos = new Vector2(mouseState.X, mouseState.Y); Vector2 ScaledOffset = renderConfig.ScaledOffset; Vector2 Scale = renderConfig.Scale; Vector2 scaledPosition = new Vector2((mousePos.X - ScaledOffset.X) / Scale.X, (mousePos.Y - ScaledOffset.Y) / Scale.Y); MousePosition = mousePos; ScaledMousePosition = scaledPosition; if (mousePos != prevMousePos) { OnPointerMoved?.Invoke(scaledPosition, PointerType.None, gameTime); prevMousePos = mousePos; } // Left mouse button if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) { OnPointerPressed?.Invoke(scaledPosition, PointerType.LeftMouse, gameTime); } if (mouseState.LeftButton == ButtonState.Released && prevMouseState.LeftButton == ButtonState.Pressed) { OnPointerReleased?.Invoke(scaledPosition, PointerType.LeftMouse, gameTime); } // Middle mouse button if (mouseState.MiddleButton == ButtonState.Pressed && prevMouseState.MiddleButton == ButtonState.Released) { OnPointerPressed?.Invoke(scaledPosition, PointerType.MiddleMouse, gameTime); } if (mouseState.MiddleButton == ButtonState.Released && prevMouseState.MiddleButton == ButtonState.Pressed) { OnPointerReleased?.Invoke(scaledPosition, PointerType.MiddleMouse, gameTime); } // Right mouse button if (mouseState.RightButton == ButtonState.Pressed && prevMouseState.RightButton == ButtonState.Released) { OnPointerPressed?.Invoke(scaledPosition, PointerType.RightMouse, gameTime); } if (mouseState.RightButton == ButtonState.Released && prevMouseState.RightButton == ButtonState.Pressed) { OnPointerReleased?.Invoke(scaledPosition, PointerType.RightMouse, gameTime); } // Mouse wheel int newWheelValue = mouseState.ScrollWheelValue; int curWheelDelta = newWheelValue - prevWheelValue; prevWheelValue = newWheelValue; if (curWheelDelta != 0) { OnPointerWheelChanged?.Invoke(new Vector2(curWheelDelta, 0), PointerType.Wheel, gameTime); } // Handle keyboard events HandleKeyboardEvents(curKeyboardState, prevKeyboardState, gameTime); prevMouseState = mouseState; prevKeyboardState = curKeyboardState; }