/// <summary> /// Flushes the current frame and re-polls the mouse /// </summary> public static void FlushFrame() { myPrevState = Mouse.GetState(); myCurrentState = Mouse.GetState(); myMouseDelta = new DeltaMouseState(myPrevState, myCurrentState, Vector2.Zero, Vector2.Zero); }
public void Watch(DeltaMouseState change) { ButtonDelta deltaState = ButtonDelta.Invalid; switch (myButton) { case MouseButton.Left: deltaState = change.LeftButton; break; case MouseButton.Right: deltaState = change.RightButton; break; case MouseButton.Middle: deltaState = change.MiddleButton; break; case MouseButton.XButton1: deltaState = change.XButton1; break; case MouseButton.XButton2: deltaState = change.XButton2; break; } if (deltaState != ButtonDelta.Invalid) { if (deltaState == ButtonDelta.Pressed) { OnPressed?.Invoke(this, change); } if (deltaState == ButtonDelta.Released) { OnReleased?.Invoke(this, change); } if (deltaState == ButtonDelta.Down) { OnDown?.Invoke(this, change); } if (deltaState == ButtonDelta.Up) { OnUp?.Invoke(this, change); } OnEvent?.Invoke(this, change); } }
/// <summary> /// Polls the mouse for changes, this is the meat of the mouse manager and will be invoked /// by the core game instance /// </summary> public static void Poll(GameTime gameTime) { if (MonuxServices.Game?.IsActive ?? false) { myPrevState = myCurrentState; myCurrentState = Mouse.GetState(); myMouseSmoothQueue[myMouseSmoothQueuePos] = new Vector2(myCurrentState.X, myCurrentState.Y); myMouseSmoothQueuePos = myMouseSmoothQueuePos == NUM_SMOOTHING_SAMPLES - 1 ? 0 : myMouseSmoothQueuePos + 1; myCurrentSmoothPosition = Vector2.Zero; for (int index = 0; index < NUM_SMOOTHING_SAMPLES; index++) { myCurrentSmoothPosition += myMouseSmoothQueue[index]; } myCurrentSmoothPosition /= NUM_SMOOTHING_SAMPLES; myMouseDelta = new DeltaMouseState(myCurrentState, myPrevState, myCurrentSmoothPosition, myCurrentSmoothPosition - myPrevSmoothPosition); myPrevSmoothPosition = myCurrentSmoothPosition; if (myMouseDelta.LeftButton == ButtonDelta.Released) { if (gameTime.TotalGameTime - myLastLeftClickTime < DOUBLE_CLICK_TIME) { myMouseDelta.DoubleClicked = true; } myLastLeftClickTime = gameTime.TotalGameTime; } if (myCurrentState.ScrollWheelValue != myPrevState.ScrollWheelValue) { OnWheelChanged?.Invoke(null, myCurrentState.ScrollWheelValue - myPrevState.ScrollWheelValue); } if (myCurrentState.Position != myPrevState.Position) { OnPositionChanged?.Invoke(null, myCurrentState); } foreach (KeyValuePair <MouseButton, MouseListener> m in myWatches) { m.Value.Watch(myMouseDelta); } } }