From b3be3f8ee4441c08b9809b3677aa3defe61c13ac Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Sat, 1 Apr 2023 13:30:19 +0200 Subject: [PATCH] Temp fix UIManager.adjust_mouse_coordinates --- arcade/camera.py | 43 ++++++++++++++++++++-------------------- arcade/gui/ui_manager.py | 22 ++++---------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/arcade/camera.py b/arcade/camera.py index 51d144b6c..c2eb0668d 100644 --- a/arcade/camera.py +++ b/arcade/camera.py @@ -27,12 +27,13 @@ class SimpleCamera: :param viewport: Size of the viewport: (left, bottom, width, height) :param projection: Space to allocate in the viewport of the camera (left, right, bottom, top) """ - - def __init__(self, *, - viewport: Optional[FourIntTuple] = None, - projection: Optional[FourFloatTuple] = None, - window: Optional["arcade.Window"] = None) -> None: - + def __init__( + self, + *, + viewport: Optional[FourIntTuple] = None, + projection: Optional[FourFloatTuple] = None, + window: Optional["arcade.Window"] = None, + ) -> None: # Reference to Context, used to update projection matrix self._window: "arcade.Window" = window or arcade.get_window() @@ -48,7 +49,7 @@ def __init__(self, *, # will match the provided viewport self._projection = (viewport[0], viewport[2], viewport[1], viewport[3]) - # Matrixes + # Matrices # Projection Matrix is used to apply the camera viewport size self._projection_matrix: Mat4 = Mat4() @@ -67,7 +68,7 @@ def __init__(self, *, self.moving: bool = False # Init matrixes - # This will precompute the projection, view and combined matrixes + # This will pre-compute the projection, view and combined matrixes self._set_projection_matrix(update_combined_matrix=False) self._set_view_matrix() @@ -141,7 +142,7 @@ def _set_projection_matrix(self, *, update_combined_matrix: bool = True) -> None :param bool update_combined_matrix: if True will also update the combined matrix (projection @ view) """ - self._projection_matrix = Mat4.orthogonal_projection(*self._projection, -1, 1) + self._projection_matrix = Mat4.orthogonal_projection(*self._projection, -100, 100) if update_combined_matrix: self._set_combined_matrix() @@ -204,7 +205,7 @@ def center(self, vector: Union[Vec2, tuple], speed: float = 1.0) -> None: vector2 = Vec2(vector2.x * self.viewport_to_projection_width_ratio, vector2.y * self.viewport_to_projection_height_ratio) - # move to the vector substracting the center + # move to the vector subtracting the center target = (vector2 - center) self.move_to(target, speed) @@ -225,7 +226,6 @@ def resize(self, viewport_width: int, viewport_height: int, *, :param int viewport_width: Width of the viewport :param int viewport_height: Height of the viewport :param bool resize_projection: if True the projection will also be resized - """ new_viewport = (self._viewport[0], self._viewport[1], viewport_width, viewport_height) self.set_viewport(new_viewport) @@ -255,7 +255,8 @@ def use(self) -> None: # set Viewport / projection self._window.ctx.viewport = self._viewport # sets viewport of the camera - self._window.ctx.projection_2d_matrix = self._combined_matrix # sets projection position and zoom + self._window.projection = self._combined_matrix # sets projection position and zoom + self._window.view = Mat4() # Set to identity matrix for now class Camera(SimpleCamera): @@ -271,17 +272,15 @@ class Camera(SimpleCamera): :param tuple anchor: the x, y point where the camera rotation will anchor. Default is the center of the viewport. :param Window window: Window to associate with this camera, if working with a multi-window program. """ - def __init__( - self, *, - viewport: Optional[FourIntTuple] = None, - projection: Optional[FourFloatTuple] = None, - zoom: float = 1.0, - rotation: float = 0.0, - anchor: Optional[Tuple[float, float]] = None, - window: Optional["arcade.Window"] = None, + self, *, + viewport: Optional[FourIntTuple] = None, + projection: Optional[FourFloatTuple] = None, + zoom: float = 1.0, + rotation: float = 0.0, + anchor: Optional[Tuple[float, float]] = None, + window: Optional["arcade.Window"] = None, ): - # scale and zoom # zoom it's just x scale value. Setting zoom will set scale x, y to the same value self._scale: Tuple[float, float] = (zoom, zoom) @@ -340,7 +339,7 @@ def _set_projection_matrix(self, *, update_combined_matrix: bool = True) -> None def _set_view_matrix(self, *, update_combined_matrix: bool = True) -> None: """ - Helper method. This will just precompute the view and combined matrix + Helper method. This will just pre-compute the view and combined matrix :param bool update_combined_matrix: if True will also update the combined matrix (projection @ view) """ diff --git a/arcade/gui/ui_manager.py b/arcade/gui/ui_manager.py index 16c8b2953..27b7a10a6 100644 --- a/arcade/gui/ui_manager.py +++ b/arcade/gui/ui_manager.py @@ -302,24 +302,10 @@ def adjust_mouse_coordinates(self, x, y): ui_manager.adjust_mouse_coordinates = camera.mouse_coordinates_to_world """ - # TODO This code does not work anymore, for now no camera support by default - # 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 - - # NOTE: For now we just take view and projection scrolling into account - # This doesn't support rotation and scaling - # Get x and y translation in the view matrix - x -= self.window.view[12] - y -= self.window.view[13] - # Get x and y translation in the projection matrix - proj_2d = self.window.ctx.projection_2d - x += proj_2d[0] - y += proj_2d[2] - # Return adjusted coordinates - return x, y + # NOTE: Only support scrolling until cameras support transforming + # mouse coordinates + px, py = self.camera.position + return x + px, y + py def on_event(self, event) -> bool: layers = sorted(self.children.keys(), reverse=True)