is it possible to trap the mouse curser inside a rectangle as long as the left button is down ?
so it doesn’t interact with other components on the desktop
but keeping the mouse position, free to update the camera
similar to blender when you click on the mini axis on top left the mouse disappear but the view rotates
protected override void Update(GameTime gameTime)
{
_mouseComponent.Update(gameTime);
_keyboardComponent.Update(gameTime);
IndicatorArea = new Rectangle { X = GraphicsDevice.Viewport.Width - 90, Y = 60, Height = 80, Width = 80 };
IndicatorAreaCinter = new Vector2(GraphicsDevice.Viewport.Width - 90, 60);
// Check if the left mouse button is clicked
// _mouseComponent.IsMouseButtonDown(MouseButton.Left)()
if (IndicatorArea.Contains(_mouseComponent.Position()))
{
IsCameraOwner = true;
_camera.Update(gameTime);
}
else
{
IsCameraOwner = false;
}
// Other update logic
base.Update(gameTime);
}
A crude example…
protected override void Update(GameTime gameTime)
{
// Get current mouse state
MouseState currentMouseState = Mouse.GetState();
// Check if left button is pressed
if (currentMouseState.LeftButton == ButtonState.Pressed)
{
// Calculate center coordinates and mouse position
int centerX = GraphicsDevice.Viewport.Width / 2 - 300;
int centerY = GraphicsDevice.Viewport.Height / 2 - 100;
int mousePosX = currentMouseState.X;
int mousePosY = currentMouseState.Y;
// Set mouse position based on relative position to center
if (mousePosX < centerX)
{
Mouse.SetPosition(centerX, mousePosY);
}
else if (mousePosX > centerX + 600)
{
Mouse.SetPosition(centerX + 600, mousePosY);
}
if (mousePosY < centerY)
{
Mouse.SetPosition(mousePosX, centerY);
}
else if (mousePosY > centerY + 200)
{
Mouse.SetPosition(mousePosX, centerY + 200);
}
}
base.Update(gameTime);
}
1 Like
To hide the mouse use Game.IsMouseVisible
https://docs.monogame.net/api/Microsoft.Xna.Framework.Game.html#Microsoft_Xna_Framework_Game_IsMouseVisible
When the mouse moves outside of the rectangle use Mouse.SetPosition
https://docs.monogame.net/api/Microsoft.Xna.Framework.Input.Mouse.html#Microsoft_Xna_Framework_Input_Mouse_SetPosition_System_Int32_System_Int32_
While holding click to allow continued camera movement outside of the rectangle, instead of using the mouse position directly use your own variable which accumulates changes in position before moving back with SetPosition. Then revert to using the mouse position directly on releasing the click.
1 Like