Skip to content

Temp fix UIManager.adjust_mouse_coordinates #1665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions arcade/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
"""

Expand Down
22 changes: 4 additions & 18 deletions arcade/gui/ui_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down