How do I adjust mouse Coordinates Efficiently with Python Arcade?

Viewed 110

So I'm making a game where I have a GUI. So I have used the UIManager Class provided by the Arcade library.

I made a Subclass so it would better fit my needs for my game:

  1. Adjust Mouse Coordinates for the current Viewport (Game Scrolls)
  2. Adjust Mouse Coordinates for current Window Size

The current UIManager class for version 2.5.7 of Arcade only supports Adjusting Mouse coordinates for window size but doesn't take scorlling into account:

    def adjust_mouse_coordinates(self, x, y):
        """
        This method is used, to translate mouse coordinates to coordinates
        respecting the viewport and projection of cameras.
        The implementation should work in most common cases.

        If you use scrolling in the :py:class:`arcade.experimental.camera.Camera2D` you have to reset scrolling
        or overwrite this method using the camera conversion: `ui_manager.adjust_mouse_coordinates = camera.mouse_coordinates_to_world`
        """
        vx, vy, vw, vh = self.window.ctx.viewport
        pl, pr, pb, pt = self.window.ctx.projection_2d
        proj_width, proj_height = pr - pl, pt - pb
        dx, dy = proj_width / vw, proj_height / vh
        return (x - vx) * dx, (y - vy) * dy

So what I did was change the subtraction into addition:

class GUIManager(arcade.gui.UIManager):

    def adjust_mouse_coordinates(self, x, y):
        """
        This method is used, to translate mouse coordinates to coordinates
        respecting the viewport and projection of cameras.
        The implementation should work in most common cases.

        If you use scrolling in the :py:class:`arcade.experimental.camera.Camera2D` you have to reset scrolling
        or overwrite this method using the camera conversion:   `ui_manager.adjust_mouse_coordinates = camera.mouse_coordinates_to_world`
        """    
        vx, vy, vw, vh = self.window.ctx.viewport
        vx, vy = self.window.view_port
        pl, pr, pb, pt = self.window.ctx.projection_2d
        proj_width, proj_height = pr - pl, pt - pb
        dx, dy = proj_width / vw, proj_height / vh
        return (x + vx) * dx, (y + vy) * dy # only works if dx and dy = 1

This did fix the scrolling issue however the window must be at original size. If resized the GUI elements aren't updated to the correct positions.

So I was wondering if there is a way to account for both cases?

0 Answers
Related