Skip to content

Commit 4822c2b

Browse files
committed
Resolve window event regressions
1 parent 43038b2 commit 4822c2b

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- Checking "WindowSizeChanged" was not valid since SDL 3 and was also not valid in previous examples.
12+
You must no longer check the type of the `WindowResized` event.
13+
914
### Fixed
1015

1116
- Corrected some inconsistent angle brackets in the `__str__` of Event subclasses. #165
17+
- Fix regression with window events causing them to be `Unknown` and uncheckable.
1218

1319
## [19.3.1] - 2025-08-02
1420

docs/tcod/getting-started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ clearing the console every frame and replacing it only on resizing the window.
114114
match event:
115115
case tcod.event.Quit():
116116
raise SystemExit
117-
case tcod.event.WindowResized(type="WindowSizeChanged"):
117+
case tcod.event.WindowResized(width=width, height=height): # Size in pixels
118118
pass # The next call to context.new_console may return a different size.
119119
120120

examples/eventget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main() -> None:
4242
print(repr(event))
4343
if isinstance(event, tcod.event.Quit):
4444
raise SystemExit
45-
if isinstance(event, tcod.event.WindowResized) and event.type == "WindowSizeChanged":
45+
if isinstance(event, tcod.event.WindowResized) and event.type == "WindowResized":
4646
console = context.new_console()
4747
if isinstance(event, tcod.event.ControllerDevice):
4848
if event.type == "CONTROLLERDEVICEADDED":

examples/ttf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def main() -> None:
8484
for event in tcod.event.wait():
8585
if isinstance(event, tcod.event.Quit):
8686
raise SystemExit
87-
if isinstance(event, tcod.event.WindowResized) and event.type == "WindowSizeChanged":
87+
if isinstance(event, tcod.event.WindowResized):
8888
# Resize the Tileset to match the new screen size.
8989
context.change_tileset(
9090
load_ttf(

tcod/event.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
from __future__ import annotations
8484

8585
import enum
86+
import functools
8687
import warnings
8788
from collections.abc import Callable, Iterator, Mapping
8889
from typing import TYPE_CHECKING, Any, Final, Generic, Literal, NamedTuple, TypeVar
@@ -698,7 +699,6 @@ class WindowEvent(Event):
698699
"WindowExposed",
699700
"WindowMoved",
700701
"WindowResized",
701-
"WindowSizeChanged",
702702
"WindowMinimized",
703703
"WindowMaximized",
704704
"WindowRestored",
@@ -715,13 +715,13 @@ class WindowEvent(Event):
715715

716716
@classmethod
717717
def from_sdl_event(cls, sdl_event: Any) -> WindowEvent | Undefined:
718-
if sdl_event.window.event not in cls.__WINDOW_TYPES:
718+
if sdl_event.type not in cls._WINDOW_TYPES:
719719
return Undefined.from_sdl_event(sdl_event)
720-
event_type: Final = cls.__WINDOW_TYPES[sdl_event.window.event]
720+
event_type: Final = cls._WINDOW_TYPES[sdl_event.type]
721721
self: WindowEvent
722-
if sdl_event.window.event == lib.SDL_EVENT_WINDOW_MOVED:
722+
if sdl_event.type == lib.SDL_EVENT_WINDOW_MOVED:
723723
self = WindowMoved(sdl_event.window.data1, sdl_event.window.data2)
724-
elif sdl_event.window.event in (
724+
elif sdl_event.type in (
725725
lib.SDL_EVENT_WINDOW_RESIZED,
726726
lib.SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED,
727727
):
@@ -734,7 +734,7 @@ def from_sdl_event(cls, sdl_event: Any) -> WindowEvent | Undefined:
734734
def __repr__(self) -> str:
735735
return f"tcod.event.{self.__class__.__name__}(type={self.type!r})"
736736

737-
__WINDOW_TYPES: Final = {
737+
_WINDOW_TYPES: Final = {
738738
lib.SDL_EVENT_WINDOW_SHOWN: "WindowShown",
739739
lib.SDL_EVENT_WINDOW_HIDDEN: "WindowHidden",
740740
lib.SDL_EVENT_WINDOW_EXPOSED: "WindowExposed",
@@ -785,10 +785,13 @@ class WindowResized(WindowEvent):
785785
Attributes:
786786
width (int): The current width of the window.
787787
height (int): The current height of the window.
788+
789+
.. versionchanged:: Unreleased
790+
Removed "WindowSizeChanged" type.
788791
"""
789792

790-
type: Final[Literal["WindowResized", "WindowSizeChanged"]] # type: ignore[misc]
791-
"""WindowResized" or "WindowSizeChanged"""
793+
type: Final[Literal["WindowResized"]] # type: ignore[misc]
794+
"""Always "WindowResized"."""
792795

793796
def __init__(self, type: str, width: int, height: int) -> None:
794797
super().__init__(type)
@@ -1130,6 +1133,15 @@ def from_sdl_event(cls, sdl_event: Any) -> ControllerDevice:
11301133
return cls(type, sdl_event.cdevice.which)
11311134

11321135

1136+
@functools.cache
1137+
def _find_event_name(index: int, /) -> str:
1138+
"""Return the SDL event name for this index."""
1139+
for attr in dir(lib):
1140+
if attr.startswith("SDL_EVENT_") and getattr(lib, attr) == index:
1141+
return attr
1142+
return "???"
1143+
1144+
11331145
class Undefined(Event):
11341146
"""This class is a place holder for SDL events without their own tcod.event class."""
11351147

@@ -1144,9 +1156,12 @@ def from_sdl_event(cls, sdl_event: Any) -> Undefined:
11441156

11451157
def __str__(self) -> str:
11461158
if self.sdl_event:
1147-
return "<Undefined sdl_event.type=%i>" % self.sdl_event.type
1159+
return f"<Undefined sdl_event.type={self.sdl_event.type} {_find_event_name(self.sdl_event.type)}>"
11481160
return "<Undefined>"
11491161

1162+
def __repr__(self) -> str:
1163+
return self.__str__()
1164+
11501165

11511166
_SDL_TO_CLASS_TABLE: dict[int, type[Event]] = {
11521167
lib.SDL_EVENT_QUIT: Quit,
@@ -1157,7 +1172,6 @@ def __str__(self) -> str:
11571172
lib.SDL_EVENT_MOUSE_BUTTON_UP: MouseButtonUp,
11581173
lib.SDL_EVENT_MOUSE_WHEEL: MouseWheel,
11591174
lib.SDL_EVENT_TEXT_INPUT: TextInput,
1160-
# lib.SDL_EVENT_WINDOW_EVENT: WindowEvent,
11611175
lib.SDL_EVENT_JOYSTICK_AXIS_MOTION: JoystickAxis,
11621176
lib.SDL_EVENT_JOYSTICK_BALL_MOTION: JoystickBall,
11631177
lib.SDL_EVENT_JOYSTICK_HAT_MOTION: JoystickHat,
@@ -1176,9 +1190,11 @@ def __str__(self) -> str:
11761190

11771191
def _parse_event(sdl_event: Any) -> Event:
11781192
"""Convert a C SDL_Event* type into a tcod Event sub-class."""
1179-
if sdl_event.type not in _SDL_TO_CLASS_TABLE:
1180-
return Undefined.from_sdl_event(sdl_event)
1181-
return _SDL_TO_CLASS_TABLE[sdl_event.type].from_sdl_event(sdl_event)
1193+
if sdl_event.type in _SDL_TO_CLASS_TABLE:
1194+
return _SDL_TO_CLASS_TABLE[sdl_event.type].from_sdl_event(sdl_event)
1195+
if sdl_event.type in WindowEvent._WINDOW_TYPES:
1196+
return WindowEvent.from_sdl_event(sdl_event)
1197+
return Undefined.from_sdl_event(sdl_event)
11821198

11831199

11841200
def get() -> Iterator[Any]:
@@ -1198,10 +1214,7 @@ def get() -> Iterator[Any]:
11981214
return
11991215
sdl_event = ffi.new("SDL_Event*")
12001216
while lib.SDL_PollEvent(sdl_event):
1201-
if sdl_event.type in _SDL_TO_CLASS_TABLE:
1202-
yield _SDL_TO_CLASS_TABLE[sdl_event.type].from_sdl_event(sdl_event)
1203-
else:
1204-
yield Undefined.from_sdl_event(sdl_event)
1217+
yield _parse_event(sdl_event)
12051218

12061219

12071220
def wait(timeout: float | None = None) -> Iterator[Any]:
@@ -1425,9 +1438,6 @@ def ev_windowmoved(self, event: tcod.event.WindowMoved, /) -> T | None:
14251438
def ev_windowresized(self, event: tcod.event.WindowResized, /) -> T | None:
14261439
"""Called when the window is resized."""
14271440

1428-
def ev_windowsizechanged(self, event: tcod.event.WindowResized, /) -> T | None:
1429-
"""Called when the system or user changes the size of the window."""
1430-
14311441
def ev_windowminimized(self, event: tcod.event.WindowEvent, /) -> T | None:
14321442
"""Called when the window is minimized."""
14331443

0 commit comments

Comments
 (0)