Skip to content

Commit 6f6e3f0

Browse files
committed
Update getting started guide. Refactor key event enums.
Some lookup tables are no longer needed now that enums are being used. Scancode and KeySym repr strings are now fully qualified.
1 parent 399de2f commit 6f6e3f0

5 files changed

Lines changed: 27 additions & 523 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"dtype",
9191
"dtypes",
9292
"DVLINE",
93+
"elif",
9394
"endianness",
9495
"epub",
9596
"EQUALSAS",
@@ -281,6 +282,7 @@
281282
"voronoi",
282283
"vsync",
283284
"WASD",
285+
"WINDOWRESIZED",
284286
"xdst",
285287
"xrel",
286288
"ydst",

build_libtcod.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,15 @@ def fix_header(filepath: str) -> None:
331331
py_limited_api=True,
332332
)
333333

334-
CONSTANT_MODULE_HEADER = '''"""
335-
Constants from the libtcod C API.
334+
CONSTANT_MODULE_HEADER = '''"""Constants from the libtcod C API.
336335
337336
This module is auto-generated by `build_libtcod.py`.
338337
"""
339338
from tcod.color import Color
340339
341340
'''
342341

343-
EVENT_CONSTANT_MODULE_HEADER = '''"""
344-
Event constants from SDL's C API.
342+
EVENT_CONSTANT_MODULE_HEADER = '''"""Event constants from SDL's C API.
345343
346344
This module is auto-generated by `build_libtcod.py`.
347345
"""
@@ -476,11 +474,11 @@ def write_library_constants() -> None:
476474
with open("tcod/event_constants.py", "w", encoding="utf-8") as f:
477475
all_names = []
478476
f.write(EVENT_CONSTANT_MODULE_HEADER)
479-
f.write("# --- SDL scancodes ---\n")
480-
f.write("%s\n_REVERSE_SCANCODE_TABLE = %s\n" % parse_sdl_attrs("SDL_SCANCODE", all_names))
477+
f.write("\n# --- SDL scancodes ---\n")
478+
f.write(f"""{parse_sdl_attrs("SDL_SCANCODE", all_names)[0]}\n""")
481479

482480
f.write("\n# --- SDL keyboard symbols ---\n")
483-
f.write("%s\n_REVERSE_SYM_TABLE = %s\n" % parse_sdl_attrs("SDLK", all_names))
481+
f.write(f"""{parse_sdl_attrs("SDLK", all_names)[0]}\n""")
484482

485483
f.write("\n# --- SDL keyboard modifiers ---\n")
486484
f.write("%s\n_REVERSE_MOD_TABLE = %s\n" % parse_sdl_attrs("KMOD", all_names))

docs/tcod/getting-started.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Example::
4747

4848
for event in tcod.event.wait():
4949
context.convert_event(event) # Sets tile coordinates for mouse events.
50-
print(event) # Print event information to stdout.
51-
if event.type == "QUIT":
50+
print(event) # Print event names and attributes.
51+
if isinstance(event, tcod.event.Quit):
5252
raise SystemExit()
5353
# The window will be closed after the above with-block exits.
5454

@@ -97,16 +97,16 @@ Example::
9797
width=WIDTH, height=HEIGHT, sdl_window_flags=FLAGS
9898
) as context:
9999
while True:
100-
console = context.new_console(order="F")
100+
console = context.new_console(order="F") # Console size based on window resolution and tile size.
101101
console.print(0, 0, "Hello World")
102102
context.present(console, integer_scaling=True)
103103

104104
for event in tcod.event.wait():
105105
context.convert_event(event) # Sets tile coordinates for mouse events.
106-
print(event)
107-
if event.type == "QUIT":
106+
print(event) # Print event names and attributes.
107+
if isinstance(event, tcod.event.Quit):
108108
raise SystemExit()
109-
if event.type == "WINDOWRESIZED":
109+
elif isinstance(event, tcod.event.WindowResized) and event.type == "WINDOWRESIZED":
110110
pass # The next call to context.new_console may return a different size.
111111

112112

tcod/event.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ class Modifier(enum.IntFlag):
211211

212212
_REVERSE_BUTTON_TABLE_PREFIX = _ConstantsWithPrefix(_REVERSE_BUTTON_TABLE)
213213
_REVERSE_BUTTON_MASK_TABLE_PREFIX = _ConstantsWithPrefix(_REVERSE_BUTTON_MASK_TABLE)
214-
_REVERSE_SCANCODE_TABLE_PREFIX = _ConstantsWithPrefix(tcod.event_constants._REVERSE_SCANCODE_TABLE)
215-
_REVERSE_SYM_TABLE_PREFIX = _ConstantsWithPrefix(tcod.event_constants._REVERSE_SYM_TABLE)
216214

217215

218216
_REVERSE_MOD_TABLE = tcod.event_constants._REVERSE_MOD_TABLE.copy()
@@ -301,37 +299,17 @@ def from_sdl_event(cls, sdl_event: Any) -> Any:
301299
self.sdl_event = sdl_event
302300
return self
303301

304-
def _scancode_constant(self, table: Mapping[int, str]) -> str:
305-
"""Return the constant name for this scan-code from a table."""
306-
try:
307-
return table[self.scancode]
308-
except KeyError:
309-
return str(self.scancode)
310-
311-
def _sym_constant(self, table: Mapping[int, str]) -> str:
312-
"""Return the constant name for this symbol from a table."""
313-
try:
314-
return table[self.sym]
315-
except KeyError:
316-
return str(self.sym)
317-
318302
def __repr__(self) -> str:
319-
return "tcod.event.%s(scancode=%s, sym=%s, mod=%s%s)" % (
303+
return "tcod.event.%s(scancode=%r, sym=%r, mod=%s%s)" % (
320304
self.__class__.__name__,
321-
self._scancode_constant(_REVERSE_SCANCODE_TABLE_PREFIX),
322-
self._sym_constant(_REVERSE_SYM_TABLE_PREFIX),
305+
self.scancode,
306+
self.sym,
323307
_describe_bitmask(self.mod, _REVERSE_MOD_TABLE_PREFIX),
324308
", repeat=True" if self.repeat else "",
325309
)
326310

327311
def __str__(self) -> str:
328-
return "<%s, scancode=%s, sym=%s, mod=%s, repeat=%r>" % (
329-
super().__str__().strip("<>"),
330-
self._scancode_constant(tcod.event_constants._REVERSE_SCANCODE_TABLE),
331-
self._sym_constant(tcod.event_constants._REVERSE_SYM_TABLE),
332-
_describe_bitmask(self.mod, _REVERSE_MOD_TABLE),
333-
self.repeat,
334-
)
312+
return self.__repr__().replace("tcod.event.", "")
335313

336314

337315
class KeyDown(KeyboardEvent):
@@ -1678,6 +1656,10 @@ def __hash__(self) -> int:
16781656
# __eq__ was defined, so __hash__ must be defined.
16791657
return super().__hash__()
16801658

1659+
def __repr__(self) -> str:
1660+
"""Return the fully qualified name of this enum."""
1661+
return f"tcod.event.{self.__class__.__name__}.{self.name}"
1662+
16811663

16821664
class KeySym(enum.IntEnum):
16831665
"""Keyboard constants based on their symbol.
@@ -2225,6 +2207,10 @@ def __hash__(self) -> int:
22252207
# __eq__ was defined, so __hash__ must be defined.
22262208
return super().__hash__()
22272209

2210+
def __repr__(self) -> str:
2211+
"""Return the fully qualified name of this enum."""
2212+
return f"tcod.event.{self.__class__.__name__}.{self.name}"
2213+
22282214

22292215
__all__ = [ # noqa: F405
22302216
"Modifier",

0 commit comments

Comments
 (0)