所以我正在制作一个有 GUI 的游戏。所以我使用了 Arcade库提供的 UIManager 类。
我制作了一个子类,以便更好地满足我对游戏的需求:
- 调整当前视口的鼠标坐标(游戏滚动)
- 调整当前窗口大小的鼠标坐标
Arcade 2.5.7 版本的当前 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
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
所以我所做的就是将减法改为加法:
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
这确实解决了滚动问题,但是窗口必须是原始大小。如果调整大小,GUI 元素不会更新到正确的位置。
所以我想知道是否有办法解决这两种情况?