Getting the window coordinates in MonoGame - c#

What I'm actually trying to do, is to get the mouse coordinates inside a windowed game. So far, I've only found ways to retrieve the screen coordinates of the mouse. Therefore, I would like to know the position of the window, so that I can subtract that from the mouse's screen coordinates in order to get the mouse's window coordinates.
Is this possible, and if so, how?

Have you tried Mouse.GetState? It returns a MouseState with X and Y properties on it. The documentation on MSDN is pretty sparse, but if you look carefully at documentation for the specific X and Y properties you'll see that it returns a position relative to the upper left corner of the window which is more in line with what your after.
MouseState.X - Horizontal position of the mouse cursor in relation to the upper-left corner of the game window.
MouseState.Y - Vertical position of the mouse cursor in relation to the upper-left corner of the game window.
The advantage of doing this way is that it's not platform specific, so if you port your game to another platform in the future, the code won't have to change.

Related

How to make 3d viewport available within a square?

How to make 3D Viewport within that 3D viewport square
You can use the Normalized Viewport Rectangles' approach, achieved by editing the Viewport Rect of the Camera.
The Documentation explains an example of split screen for a two-player game. You can adapt the explanation having the game in a particular area, and the GUI in the other screen space.
Normalized Viewport Rectangles
Normalized Viewport Rectangle is specifically for defining a certain
portion of the screen that the current camera view will be drawn upon.
You can put a map view in the lower-right hand corner of the screen,
or a missile-tip view in the upper-left corner. With a bit of design
work, you can use Viewport Rectangle to create some unique behaviors.
It’s easy to create a two-player split screen effect using Normalized
Viewport Rectangle. After you have created your two cameras, change
both camera’s H values to be 0.5 then set player one’s Y value to 0.5,
and player two’s Y value to 0. This will make player one’s camera
display from halfway up the screen to the top, and player two’s camera
start at the bottom and stop halfway up the screen.

Get cursor position on multiple monitors?

I'm trying to get the position of the cursor from a multi-monitor display. Using
Point cursorPosition = Cursor.Position;
Returns the cursor's position relative to the monitor it is currently on. If I had two monitors, and I used the above lines of code, and my mouse was on my second monitor (Which is to the right of my first monitor), and I drew the cursor to an image, it would appear on the left hand side monitor, where as it should be on the right hand side monitor.
EDIT:
I'm taking a screenshot of all my monitors and would like to add the cursor to the Bitmap, however using Cursor.Position will put the cursor on the "left hand side" monitor of the screenshot Bitmap even if the cursor was on the "right hand side" monitor.
Thanks
You can get screen where cursor is:
var screen = Screen.FromPoint(Cursor.Position);
Take screen into account before you display your image

Snaping circle to rectangle/line

I am trying to implement some kind of snapping functionality in WPF for a circle (which represents my mouse) and it should snap to another object (normally this would be a line or a rectangle).
Is there a way to do this kind of functionality with WPF without doing all the calculations on my own and if not is there an easy way (library?) to get this kind of information?
Edit: I want to snap the border of the circle to the border of the rectangle/line.
As a first step, you should find the point on the rectangle that is the closest to the cursor, and the distance between the two: extending the edges of the rectangle, you partition the plane into 9 regions. Depending on the region where the cursor lies, the searched distance will be the distance to a corner (Euclidean distance formula) or the distance to an edge (difference of abscissas or ordinates).
Subtract the circle radius from this distance. This will tell you if you are close enough for a snap.
When a snap is possible, move the cursor along the line from the current cursor position to the closest point until you hit the corner or edge. You will need to use the parametric equation of the line segment.
The complete discussion requires some care but only involves simple math.
A similar approach is possible to snap to a line segment. Here is a trick: if you rotate the line segment to make it horizontal, you can consider the line segment as a degenerate rectangle and use the same snapping algorithm. Rotate the line segment and the cursor, apply the snapping logics and then counter-rotate the updated cursor.
That kind of functionality only takes a few lines of code to replicate... I doubt that you'll find a 'library' of code to do it for you. The method is as follows:
Keep a collection that contains the 4 Points that form each shape's bounding box. You then need to handle to MouseMove event on the Canvas, or shape container. In this event, you simply need to ascertain whether the current mouse position is within a certain distance from any of the shape edges... you'll have a little bit more work to do with non-rectangular shapes to calculate their edges, but the principal is the same.
If you detect the presence of a nearby shape, then you simply need to change the value of the nearest dimension to that of the nearby shape... the snap. That's it... much easier than you think.

Current position of Cursor

the problem is that I try to get current position of picturebox. When I use MouseEvenArgs everything is ok. But when I try to detect position using
Cursor.Position.X;
It doesn't work the right way. The difference is near 20-30px. It seems to me that it gives me a bit different positions.
So, the question: is there any difference between the positions that were get byMouseEventArgs or Cursor.Position? And are there any other ways to detect my current cursor position without using Mouse Events?
Cursor.Position measures screen coordinates. You can map between screen and client (the form) coordinates using PointToClient and PointToScreen methods.
MouseEventArgs.GetPosition returns
the position of the mouse pointer relative to the specified element.
MSDN reference - Windows Forms Coordinates
The default clipping rectangle of the Cursor is the whole screen and the coordinates you receive from MouseMove are translated to the current control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

How paint.net accomplish it: show pixel coordinates when mouse is moving

There is a picture in my winform, I want to show pixel coordinates when mouse is moving, and
whether zoom in or out, the same pixel's coordinates don't change, just like what paint.net does.
Appreciate for any idea.
You need to handle the MouseMove event and get the location from e.X and e.Y.
You can divide the location by your zoom factor.
about the mouse coordinate, you should use Matrix to track back mouse coordinate.
see: Back track the mouse

Categories