2
\$\begingroup\$

I am attempting to create a simple 2d level editor using xna and winforms.

I have used the app hub win form samples as a starting point. At first I was having trouble with the mouse coordinates but after setting the mouse handle to the control, it is working fine.

But now I want to implement scaling into the editor. I want the level editor to always be 720p. I have accomplished this by rendering to a render target set to 720 and then draw this using sprite batch to the back buffer. This also works great with letter boxing to keep the right ratio. I used the technique detailed here https://stackoverflow.com/questions/7591466/xna-resize-window-without-increasing-resolution.

However because of the letting boxing my mouse coordinates are now wrong again. Can someone help work out how to solve this? I tried getting the size of the letter box and subtracting that from the mouse coordinates but that didn't work.

Has anyone got any suggestions?

\$\endgroup\$
1
\$\begingroup\$

This is almost exactly the same method I implemented for my games, and the way I'm solving this problem is by encapsulating both the offset and the scaling in a matrix. I added this method to my resolution independence class:

public Matrix GetResolutionMatrix()
{
    float ratio = (float) virtualWidth / physicalWidth;
    return Matrix.CreateTranslation(-area.X, -area.Y, 0) * 
           Matrix.CreateScale(ratio, ratio, 1f);
}

And I use that matrix to calculate the mouse position inside my input manager, by doing something like:

Vector2.Transform(mousePosition, resolution.GetResolutionMatrix())

For comparison here's my complete implementation:

http://pastie.org/4265585

And the relevant portion of the input module:

http://pastie.org/4265592

Also, check this answer in case you're having trouble getting the correct "raw" mouse position in relation to the xna control.

\$\endgroup\$
  • \$\begingroup\$ This is also the way I do it. In terms of mouse position, I prefer the XNA mouse. As long as you set the window handle Mouse.WindowHandle = this.Handle; I've had no problems using it with win forms. \$\endgroup\$ – James Hull Jul 18 '12 at 19:36
  • \$\begingroup\$ @Bigfellahull If you're referring to the last link I posted, I also only ran into problems when I started adding the xna control deeper in the control hierarchy, e.g. inside of a panel or another container, instead of being placed directly on the form. \$\endgroup\$ – David Gouveia Jul 18 '12 at 19:51
  • \$\begingroup\$ Really. Even inside other controls, as long as I set the window handle to that of the xna control that extends the graphics device, it seems to work fine. But like you mention in the other question, using win forms for mouse input works well too. \$\endgroup\$ – James Hull Jul 18 '12 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.