Skip to content
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

Upgrade to pyglet==2.1.0 #2487

Merged
merged 13 commits into from
Jan 12, 2025
2 changes: 1 addition & 1 deletion arcade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def configure_logging(level: int | None = None):

import pyglet

# Enable HiDPI support
# Enable HiDPI support using stretch mode
if os.environ.get("ARCADE_TEST"):
pyglet.options.dpi_scaling = "real"
else:
Expand Down
77 changes: 44 additions & 33 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import logging
import os
import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Sequence

import pyglet
import pyglet.gl as gl
import pyglet.window.mouse
from pyglet.display.base import Screen, ScreenMode
from pyglet.event import EVENT_HANDLE_STATE, EVENT_UNHANDLED
from pyglet.window import MouseCursor

import arcade
Expand All @@ -29,7 +30,6 @@
from arcade.camera.default import DefaultProjector
from arcade.start_finish_data import StartFinishRenderData


LOG = logging.getLogger(__name__)

MOUSE_BUTTON_LEFT = 1
Expand Down Expand Up @@ -180,7 +180,7 @@ def __init__(
# Attempt to make window with antialiasing
if antialiasing:
try:
config = pyglet.gl.Config(
config = gl.Config(
major_version=gl_version[0],
minor_version=gl_version[1],
opengl_api=gl_api, # type: ignore # pending: upstream fix
Expand All @@ -204,7 +204,7 @@ def __init__(
antialiasing = False
# If we still don't have a config
if not config:
config = pyglet.gl.Config(
config = gl.Config(
major_version=gl_version[0],
minor_version=gl_version[1],
opengl_api=gl_api, # type: ignore # pending: upstream fix
Expand Down Expand Up @@ -239,7 +239,7 @@ def __init__(
if antialiasing:
try:
gl.glEnable(gl.GL_MULTISAMPLE_ARB)
except pyglet.gl.GLException:
except gl.GLException:
LOG.warning("Warning: Anti-aliasing not supported on this computer.")

_setup_clock()
Expand Down Expand Up @@ -338,7 +338,7 @@ def ctx(self) -> ArcadeContext:
"""
return self._ctx

def clear(
def clear( # type: ignore # not sure what to do here, BaseWindow.clear is static
self,
color: RGBOrA255 | None = None,
color_normalized: RGBANormalized | None = None,
Expand Down Expand Up @@ -554,7 +554,7 @@ def set_draw_rate(self, rate: float) -> None:
pyglet.clock.unschedule(pyglet.app.event_loop._redraw_windows)
pyglet.clock.schedule_interval(pyglet.app.event_loop._redraw_windows, self._draw_rate)

def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> bool | None:
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while the mouse is moving in the window area.

Expand All @@ -568,7 +568,7 @@ def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> bool | None:
"""
pass

def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever a mouse button gets pressed down.

Expand Down Expand Up @@ -596,7 +596,7 @@ def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool |

def on_mouse_drag(
self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int
) -> bool | None:
) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while the mouse moves with a button down.

Expand All @@ -619,7 +619,7 @@ def on_mouse_drag(
"""
return self.on_mouse_motion(x, y, dx, dy)

def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever a mouse button gets released.

Expand All @@ -642,9 +642,11 @@ def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool
Bitwise 'and' of all modifiers (shift, ctrl, num lock)
active during this event. See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
def on_mouse_scroll(
self, x: int, y: int, scroll_x: float, scroll_y: float
) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while a mouse scroll wheel moves.

Expand Down Expand Up @@ -676,7 +678,7 @@ def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool
scroll_y:
Number of steps scrolled vertically since the last call of this function
"""
return False
return EVENT_UNHANDLED

def set_mouse_visible(self, visible: bool = True) -> None:
"""
Expand Down Expand Up @@ -724,7 +726,7 @@ def on_action(self, action_name: str, state) -> None:
"""
pass

def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
def on_key_press(self, symbol: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once when a key gets pushed down.

Expand All @@ -741,9 +743,9 @@ def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
Bitwise 'and' of all modifiers (shift, ctrl, num lock)
active during this event. See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
def on_key_release(self, symbol: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once when a key gets released.

Expand All @@ -763,9 +765,9 @@ def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
ctrl, num lock) active during this event.
See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_draw(self) -> bool | None:
def on_draw(self) -> EVENT_HANDLE_STATE:
"""
Override this function to add your custom drawing code.

Expand All @@ -781,9 +783,9 @@ def on_draw(self) -> bool | None:
self._start_finish_render_data.draw()
return True

return False
return EVENT_UNHANDLED

def _on_resize(self, width: int, height: int) -> bool | None:
def _on_resize(self, width: int, height: int) -> EVENT_HANDLE_STATE:
"""
The internal method called when the window is resized.

Expand All @@ -799,9 +801,9 @@ def _on_resize(self, width: int, height: int) -> bool | None:
# Retain viewport
self.viewport = (0, 0, width, height)

return False
return EVENT_UNHANDLED

def on_resize(self, width: int, height: int) -> bool | None:
def on_resize(self, width: int, height: int) -> EVENT_HANDLE_STATE:
"""
Override this method to add custom actions when the window is resized.

Expand Down Expand Up @@ -855,7 +857,7 @@ def get_size(self) -> tuple[int, int]:

def get_location(self) -> tuple[int, int]:
"""Get the current X/Y coordinates of the window."""
return super().get_location()
return super().get_location() # type: ignore # Window typed at runtime

def set_visible(self, visible: bool = True):
"""
Expand Down Expand Up @@ -1038,34 +1040,34 @@ def flip(self) -> None:
num_collected = self.ctx.gc()
LOG.debug("Garbage collected %s OpenGL resource(s)", num_collected)

super().flip()
super().flip() # type: ignore # Window typed at runtime

def switch_to(self) -> None:
"""Switch the this window context.

This is normally only used in multi-window applications.
"""
super().switch_to()
super().switch_to() # type: ignore # Window typed at runtime

def set_caption(self, caption) -> None:
"""Set the caption/title of the window."""
super().set_caption(caption)
super().set_caption(caption) # type: ignore # Window typed at runtime

def set_location(self, x, y) -> None:
"""Set location of the window."""
super().set_location(x, y)
super().set_location(x, y) # type: ignore # Window typed at runtime

def activate(self) -> None:
"""Activate this window."""
super().activate()
super().activate() # type: ignore # Window typed at runtime

def minimize(self) -> None:
"""Minimize the window."""
super().minimize()
super().minimize() # type: ignore # Window typed at runtime

def maximize(self) -> None:
"""Maximize the window."""
super().maximize()
super().maximize() # type: ignore # Window typed at runtime

def set_vsync(self, vsync: bool) -> None:
"""Set if we sync our draws to the monitors vertical sync rate."""
Expand Down Expand Up @@ -1097,9 +1099,9 @@ def get_system_mouse_cursor(self, name) -> MouseCursor:

def dispatch_events(self) -> None:
"""Dispatch events"""
super().dispatch_events()
super().dispatch_events() # type: ignore # Window typed at runtime

def on_mouse_enter(self, x: int, y: int) -> bool | None:
def on_mouse_enter(self, x: int, y: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever the mouse enters the window area on screen.

Expand All @@ -1112,7 +1114,7 @@ def on_mouse_enter(self, x: int, y: int) -> bool | None:
"""
pass

def on_mouse_leave(self, x: int, y: int) -> bool | None:
def on_mouse_leave(self, x: int, y: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever the mouse leaves the window area on screen.

Expand Down Expand Up @@ -1183,6 +1185,15 @@ def fixed_delta_time(self) -> float:
"""The configured fixed update rate"""
return self._fixed_rate

# required because pyglet marks the method as abstract methods,
# but resolves class during runtime
def _create(self) -> None:
"""Internal method to create the window."""
super()._create() # type: ignore

def _recreate(self, changes: Sequence[str]) -> None:
super()._recreate(changes) # type: ignore


def open_window(
width: int,
Expand Down
22 changes: 12 additions & 10 deletions arcade/camera/camera_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,12 @@ def position(self) -> Vec2:
"""The 2D world position of the camera along the X and Y axes."""
return Vec2(self._camera_data.position[0], self._camera_data.position[1])

# Setter with different signature will cause mypy issues
# https://github.com/python/mypy/issues/3004
@position.setter
def position(self, _pos: Point) -> None:
x, y, *z = _pos
z = self._camera_data.position[2] if not z else z[0]
x, y, *_z = _pos
z = self._camera_data.position[2] if not _z else _z[0]
self._camera_data.position = (x, y, z)

@property
Expand Down Expand Up @@ -900,7 +902,7 @@ def top_left(self, new_corner: Point2):
left = self.left

x, y = new_corner
self.position = (x - ux * top - rx * left, y - uy * top - ry * left)
self.position = (x - ux * top - rx * left, y - uy * top - ry * left) # type: ignore

# top_center
@property
Expand All @@ -918,7 +920,7 @@ def top_center(self, new_top: Point2):
top = self.top

x, y = new_top
self.position = x - ux * top, y - uy * top
self.position = x - ux * top, y - uy * top # type: ignore

# top_right
@property
Expand All @@ -942,7 +944,7 @@ def top_right(self, new_corner: Point2):
right = self.right

x, y = new_corner
self.position = (x - ux * top - rx * right, y - uy * top - ry * right)
self.position = (x - ux * top - rx * right, y - uy * top - ry * right) # type: ignore

# center_right
@property
Expand All @@ -959,7 +961,7 @@ def center_right(self, new_right: Point2):
right = self.right

x, y = new_right
self.position = x - uy * right, y + ux * right
self.position = x - uy * right, y + ux * right # type: ignore

# bottom_right
@property
Expand All @@ -985,7 +987,7 @@ def bottom_right(self, new_corner: Point2):
self.position = (
x - ux * bottom - rx * right,
y - uy * bottom - ry * right,
)
) # type: ignore

# bottom_center
@property
Expand All @@ -1003,7 +1005,7 @@ def bottom_center(self, new_bottom: Point2):
bottom = self.bottom

x, y = new_bottom
self.position = x - ux * bottom, y - uy * bottom
self.position = x - ux * bottom, y - uy * bottom # type: ignore

# bottom_left
@property
Expand All @@ -1027,7 +1029,7 @@ def bottom_left(self, new_corner: Point2):
left = self.left

x, y = new_corner
self.position = (x - ux * bottom - rx * left, y - uy * bottom - ry * left)
self.position = x - ux * bottom - rx * left, y - uy * bottom - ry * left # type: ignore

# center_left
@property
Expand All @@ -1044,4 +1046,4 @@ def center_left(self, new_left: Point2):
left = self.left

x, y = new_left
self.position = x - uy * left, y + ux * left
self.position = Vec2(x - uy * left, y + ux * left)
4 changes: 2 additions & 2 deletions arcade/camera/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def unproject(self, screen_coordinate: Point) -> Vec3:

Due to the nature of viewport projector this does not do anything.
"""
x, y, *z = screen_coordinate
z = 0.0 if not z else z[0]
x, y, *_z = screen_coordinate
z = 0.0 if not _z else _z[0]

return Vec3(x, y, z)

Expand Down
Loading
Loading