python-3.xarcade

Adjusting mouse coordinates in on_click event


I`m trying to create a game with a map, that is dragable with the mouse and has some clickable "buildings" on it.

So I know I need to add the camera viewport lower left corner coordinates to the mouse position, to get the world and screen coordinates adjusted. But i don´t know how to do this, like how to manipulate the mouse coordinates in the on_click / UIMouseEvent and how to constantly add adjustment to the current mouse position to cover hover events as well.


Solution

  • ok found the solution meself:

    I created a class inheriting from the arcade.gui.UIManager

    class UIManagerPlus(arcade.gui.UIManager):
        def __init__(self, window: arcade.Window | None = None):
            super().__init__(window)
            self.cam_lower_left_x = 0
            self.cam_lower_left_y = 0
    
        def adjust_mouse_coordinates(self, x, y):
            return (x + self.cam_lower_left_x), (y + self.cam_lower_left_y)
    
    class MyView(arcade.View):
        def __init__(self):
            super().__init__()
            self.manager = UIManagerPlus()
            self.camera = arcade.Camera2D()
            ...
    
        def on_update(self):
            self.manager.cam_lower_left_x = self.camera.bottom_left.x
            self.manager.cam_lower_left_y = self.camera.bottom_left.y
    

    Im using arcade 3.0.0.dev29, where the new Camera2D is implemented