1
\$\begingroup\$

I am currently implementing a map editor for a 2D platformer and run into trouble with my viewport and mouse movement.

I have two viewports: one main viewport in the top 2/3 of the screen which has a camera and shows the tilemap and a sub viewport in the bottom 1/3 of the screen which shows the menu:

editor

defaultView = gDevice.Viewport;
defaultView.Width = width;
defaultView.Height = height;

mainView = defaultView;
subView = defaultView;

mainView.Height = (mainView.Height / 3) * 2;
subView.Height = defaultView.Height / 3;
subView.Y = mainView.Height;

Drawing on both viewports separately works very well

public static void Render()
        {
            transform = camera.Transform;

            gDevice.Viewport = mainView;
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, samplerState, null, null, null, transformMatrix: transform);
            currentScreen.RenderMain();
            spriteBatch.End();

            gDevice.Viewport = subView;
            spriteBatch.Begin();
            currentScreen.RenderSub();
            spriteBatch.End();
        }

But now I'm getting problems with my mouse position. As you can see on the screenshot, the main view works very well but the sub view shows the mouse in odd places. In my main view I use this for rendering:

Basic.spriteBatch.Draw(Textures.cursor, new Rectangle((int)mousePosMap.X, (int)mousePosMap.Y, cursorWidth, cursorHeight), Color.White);

and updating the mousePosMap in my UpdateMain() method:

mousePosMap = Basic.camera.Unproject(new Vector2(Basic.mousePoint.X, Basic.mousePoint.Y));

with:

public Vector2 Unproject(Vector2 screenPosition)
        {
            return centre + (screenPosition - new Vector2(Basic.width, Basic.height) / 2) / Zoom;
        }

In my RenderSub() method I draw like this:

Basic.spriteBatch.Draw(Textures.cursor, new Rectangle(Basic.mousePoint.X, Basic.mousePoint.Y, cursorWidth, cursorHeight), Color.White);

Since the real mouse position in my main view is shifted because of the camera, how do I can implement a smooth change between the two views so that the cursor smoothly navigates from one view to the other and still locates the objects in the world space (main view - highlight cells) and the screen space (sub view - highlight buttons)?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The mouse cursor should always be drawn in screen coordinates(outside of any view).

The view containing the mouse should be the only one to deproject the coordinates and act upon the input.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.