Skip to content

Commit 0470062

Browse files
committed
Add timestamp to events, SDL time functions
Adjust inherited members from Event, hide the deprecated `type` attribute.
1 parent 74d74bc commit 0470062

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
1616
- `KeyboardEvent.pressed`, `KeyboardEvent.which`, `KeyboardEvent.window_id` attributes.
1717
- `WindowEvent.data` and `WindowEvent.window_id` attributes and added missing SDL3 window events.
1818
- `which` and `window_id` attributes for mouse events.
19+
- Events now have `Event.timestamp` and `Event.timestamp_ns` which use SDL's timer at `tcod.event.time` and `tcod.event.time_ns`.
1920

2021
### Changed
2122

docs/tcod/event.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SDL Event Handling ``tcod.event``
33

44
.. automodule:: tcod.event
55
:members:
6-
:inherited-members: object, int, str, tuple, Event
6+
:inherited-members: object, int, str, tuple
77
:member-order: bysource
88
:exclude-members:
99
KeySym, Scancode, Modifier, get, wait

tcod/event.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,14 @@ class _CommonSDLEventAttributes(TypedDict):
314314
"""Common keywords for Event subclasses."""
315315

316316
sdl_event: _C_SDL_Event
317+
timestamp_ns: int
317318

318319

319320
def _unpack_sdl_event(sdl_event: _C_SDL_Event) -> _CommonSDLEventAttributes:
320321
"""Unpack an SDL_Event union into common attributes, such as timestamp."""
321322
return {
322323
"sdl_event": sdl_event,
324+
"timestamp_ns": sdl_event.common.timestamp,
323325
}
324326

325327

@@ -328,7 +330,27 @@ class Event:
328330
"""The base event class."""
329331

330332
sdl_event: _C_SDL_Event = attrs.field(default=None, eq=False, repr=False)
331-
"""When available, this holds a python-cffi 'SDL_Event*' pointer. All sub-classes have this attribute."""
333+
"""Holds a python-cffi ``SDL_Event*`` pointer for this event when available."""
334+
335+
timestamp_ns: int = attrs.field(default=0, eq=False)
336+
"""The time of this event in nanoseconds since SDL has been initialized.
337+
338+
.. seealso::
339+
:any:`tcod.event.time_ns`
340+
341+
.. versionadded:: Unreleased
342+
"""
343+
344+
@property
345+
def timestamp(self) -> float:
346+
"""The time of this event in seconds since SDL has been initialized.
347+
348+
.. seealso::
349+
:any:`tcod.event.time`
350+
351+
.. versionadded:: Unreleased
352+
"""
353+
return self.timestamp_ns / 1_000_000_000
332354

333355
@property
334356
@deprecated("The Event.type attribute is deprecated, use isinstance instead.")
@@ -337,6 +359,8 @@ def type(self) -> str:
337359
338360
.. deprecated:: Unreleased
339361
Using this attribute is now actively discouraged. Use :func:`isinstance` or :ref:`match`.
362+
363+
:meta private:
340364
"""
341365
type_override: str | None = getattr(self, "_type", None)
342366
if type_override is not None:
@@ -3070,6 +3094,22 @@ def __getattr__(name: str) -> int:
30703094
return value
30713095

30723096

3097+
def time_ns() -> int:
3098+
"""Return the nanoseconds elapsed since SDL was initialized.
3099+
3100+
.. versionadded:: Unreleased
3101+
"""
3102+
return int(lib.SDL_GetTicksNS())
3103+
3104+
3105+
def time() -> float:
3106+
"""Return the seconds elapsed since SDL was initialized.
3107+
3108+
.. versionadded:: Unreleased
3109+
"""
3110+
return time_ns() / 1_000_000_000
3111+
3112+
30733113
__all__ = ( # noqa: F405 RUF022
30743114
"Point",
30753115
"Modifier",
@@ -3111,6 +3151,8 @@ def __getattr__(name: str) -> int:
31113151
"get_modifier_state",
31123152
"Scancode",
31133153
"KeySym",
3154+
"time_ns",
3155+
"time",
31143156
# --- From event_constants.py ---
31153157
"MOUSEWHEEL_NORMAL",
31163158
"MOUSEWHEEL_FLIPPED",

0 commit comments

Comments
 (0)