public override void Update(GameTime gameTime) { MouseState ms = Mouse.GetState(); DrawnLocation = new Point(ms.X, ms.Y); if (!windowManager.HasFocus || Disabled) { LeftClicked = false; RightClicked = false; LeftDown = false; return; } Point location = DrawnLocation; IsOnScreen = !(location.X <0 || location.Y <0 || location.X> windowManager.WindowWidth || location.Y> windowManager.WindowHeight); location = new Point(location.X - windowManager.SceneXPosition, location.Y - windowManager.SceneYPosition); location = new Point((int)(location.X / windowManager.ScaleRatio), (int)(location.Y / windowManager.ScaleRatio)); HasMoved = (location != Location); Location = location; ScrollWheelValue = (ms.ScrollWheelValue - previousMouseState.ScrollWheelValue) / 40; LeftDown = ms.LeftButton == ButtonState.Pressed; LeftPressedDown = LeftDown && previousMouseState.LeftButton != ButtonState.Pressed; LeftClicked = !LeftDown && previousMouseState.LeftButton == ButtonState.Pressed; if (LeftClicked) { LeftClickEvent?.Invoke(this, EventArgs.Empty); } RightPressedDown = ms.RightButton == ButtonState.Pressed && previousMouseState.RightButton != ButtonState.Pressed; RightClicked = ms.RightButton == ButtonState.Released && previousMouseState.RightButton == ButtonState.Pressed; previousMouseState = ms; }
public void Update() { MouseState mouseState = Mouse.GetState(); KeyboardState keyboardState = Keyboard.GetState(); // MouseClick / MousePress handling if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released && keyboardState.IsKeyUp(Keys.LeftShift)) { LeftClickEvent?.Invoke(mouseState.X, mouseState.Y); MouseLeftPressedEvent?.Invoke(mouseState.X, mouseState.Y); } if (lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { MouseLeftReleasedEvent?.Invoke(mouseState.X, mouseState.Y); } // Shift + MouseLeftPress handling if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released && keyboardState.IsKeyDown(Keys.LeftShift)) { MouseShiftLeftPressedEvent?.Invoke(mouseState.X, mouseState.Y); } if ((lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) || keyboardState.IsKeyUp(Keys.LeftShift)) { MouseShiftLeftReleasedEvent?.Invoke(mouseState.X, mouseState.Y); } // MouseMoved handling if (lastMouseState.X != mouseState.X || lastMouseState.Y != mouseState.Y) { MouseMovedEvent?.Invoke(mouseState.X, mouseState.Y); } // MouseScroll Handling if (lastMouseState.ScrollWheelValue > mouseState.ScrollWheelValue) { MouseScrollDown?.Invoke(mouseState.X, mouseState.Y); MouseMovedEvent?.Invoke(mouseState.X, mouseState.Y); } if (lastMouseState.ScrollWheelValue < mouseState.ScrollWheelValue) { MouseScrollUp?.Invoke(mouseState.X, mouseState.Y); MouseMovedEvent?.Invoke(mouseState.X, mouseState.Y); } // G-key tapping if (IsKeyTapped(keyboardState, Keys.G)) { KeyTappedEvent?.Invoke(Keys.G); } // ESC-key tapping if (IsKeyTapped(keyboardState, Keys.Escape)) { KeyTappedEvent?.Invoke(Keys.Escape); } // Delete-key tapping if (IsKeyTapped(keyboardState, Keys.Delete)) { KeyTappedEvent?.Invoke(Keys.Delete); } if (IsKeyTapped(keyboardState, Keys.D)) { KeyTappedEvent.Invoke(Keys.D); } lastMouseState = mouseState; lastKeyboardState = keyboardState; }