Skip to content

Commit d1d85a9

Browse files
committed
Refactor events to deprecate mouse tile attributes.
Renamed mouse pixel attributes to position and motion. Old names have been deprecated. Context.convert_event now returns an event with tile coordinates active.
1 parent 1524197 commit d1d85a9

File tree

4 files changed

+124
-45
lines changed

4 files changed

+124
-45
lines changed

examples/samples_tcod.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,8 @@ def on_draw(self) -> None:
11051105
"Right button : %s\n"
11061106
"Middle button : %s\n"
11071107
% (
1108-
self.motion.pixel.x,
1109-
self.motion.pixel.y,
1108+
self.motion.position.x,
1109+
self.motion.position.y,
11101110
self.motion.tile.x,
11111111
self.motion.tile.y,
11121112
self.motion.tile_motion.x,
@@ -1489,11 +1489,13 @@ def handle_events() -> None:
14891489
if context.sdl_renderer:
14901490
# Manual handing of tile coordinates since context.present is skipped.
14911491
if isinstance(event, (tcod.event.MouseState, tcod.event.MouseMotion)):
1492-
event.tile = tcod.event.Point(event.pixel.x // tileset.tile_width, event.pixel.y // tileset.tile_height)
1492+
event.tile = tcod.event.Point(
1493+
event.position.x // tileset.tile_width, event.position.y // tileset.tile_height
1494+
)
14931495
if isinstance(event, tcod.event.MouseMotion):
14941496
prev_tile = (
1495-
(event.pixel[0] - event.pixel_motion[0]) // tileset.tile_width,
1496-
(event.pixel[1] - event.pixel_motion[1]) // tileset.tile_height,
1497+
(event.position[0] - event.motion[0]) // tileset.tile_width,
1498+
(event.position[1] - event.motion[1]) // tileset.tile_height,
14971499
)
14981500
event.tile_motion = tcod.event.Point(event.tile[0] - prev_tile[0], event.tile[1] - prev_tile[1])
14991501
else:

tcod/context.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@
4949
""" # noqa: E501
5050
from __future__ import annotations
5151

52+
import copy
5253
import os
5354
import pickle
5455
import sys
5556
import warnings
56-
from typing import Any, Iterable, List, Optional, Tuple
57+
from typing import Any, Iterable, List, Optional, Tuple, TypeVar
5758

5859
from typing_extensions import Literal, NoReturn
5960

@@ -87,6 +88,8 @@
8788
"RENDERER_XTERM",
8889
)
8990

91+
_Event = TypeVar("_Event", bound=tcod.event.Event)
92+
9093
SDL_WINDOW_FULLSCREEN = lib.SDL_WINDOW_FULLSCREEN
9194
"""Exclusive fullscreen mode.
9295
@@ -247,30 +250,38 @@ def pixel_to_subtile(self, x: int, y: int) -> Tuple[float, float]:
247250
_check(lib.TCOD_context_screen_pixel_to_tile_d(self._context_p, xy, xy + 1))
248251
return xy[0], xy[1]
249252

250-
def convert_event(self, event: tcod.event.Event) -> None:
251-
"""Fill in the tile coordinates of a mouse event using this context.
253+
def convert_event(self, event: _Event) -> _Event:
254+
"""Return an event with mouse pixel coordinates converted into tile coordinates.
252255
253256
Example::
254257
255258
context: tcod.context.Context
256259
for event in tcod.event.get():
260+
event_tile = context.convert_event(event)
257261
if isinstance(event, tcod.event.MouseMotion):
258-
# Pixel coordinates are always accessible.
259-
print(f"{event.pixel=}, {event.pixel_motion=}")
260-
context.convert_event(event)
261-
if isinstance(event, tcod.event.MouseMotion):
262-
# Now tile coordinate attributes can be accessed.
263-
print(f"{event.tile=}, {event.tile_motion=}")
264-
# A warning will be raised if you try to access these without convert_event.
262+
# Events start with pixel coordinates and motion.
263+
print(f"Pixels: {event.position=}, {event.motion=}")
264+
if isinstance(event_tile, tcod.event.MouseMotion):
265+
# Tile coordinates are used in the returned event.
266+
print(f"Tiles: {event_tile.position=}, {event_tile.motion=}")
267+
268+
.. versionchanged:: Unreleased
269+
Now returns a new event with the coordinates converted into tiles.
265270
"""
271+
event_copy = copy.copy(event)
266272
if isinstance(event, (tcod.event.MouseState, tcod.event.MouseMotion)):
267-
event.tile = tcod.event.Point(*self.pixel_to_tile(*event.pixel))
273+
assert isinstance(event_copy, (tcod.event.MouseState, tcod.event.MouseMotion))
274+
event_copy.position = event.tile = tcod.event.Point(*self.pixel_to_tile(*event.position))
268275
if isinstance(event, tcod.event.MouseMotion):
276+
assert isinstance(event_copy, tcod.event.MouseMotion)
269277
prev_tile = self.pixel_to_tile(
270-
event.pixel[0] - event.pixel_motion[0],
271-
event.pixel[1] - event.pixel_motion[1],
278+
event.position[0] - event.motion[0],
279+
event.position[1] - event.motion[1],
280+
)
281+
event_copy.motion = event.tile_motion = tcod.event.Point(
282+
event.tile[0] - prev_tile[0], event.tile[1] - prev_tile[1]
272283
)
273-
event.tile_motion = tcod.event.Point(event.tile[0] - prev_tile[0], event.tile[1] - prev_tile[1])
284+
return event_copy
274285

275286
def save_screenshot(self, path: Optional[str] = None) -> None:
276287
"""Save a screen-shot to the given file path."""

tcod/event.py

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class MouseState(Event):
384384
"""
385385
Attributes:
386386
type (str): Always "MOUSESTATE".
387-
pixel (Point): The pixel coordinates of the mouse.
387+
position (Point): The position coordinates of the mouse.
388388
tile (Point): The integer tile coordinates of the mouse on the screen.
389389
state (int): A bitmask of which mouse buttons are currently held.
390390
@@ -397,39 +397,70 @@ class MouseState(Event):
397397
* tcod.event.BUTTON_X2MASK
398398
399399
.. versionadded:: 9.3
400+
401+
.. versionchanged:: Unreleased
402+
Renamed `pixel` attribute to `position`.
400403
"""
401404

402405
def __init__(
403406
self,
404-
pixel: Tuple[int, int] = (0, 0),
407+
position: Tuple[int, int] = (0, 0),
405408
tile: Optional[Tuple[int, int]] = (0, 0),
406409
state: int = 0,
407410
):
408411
super().__init__()
409-
self.pixel = Point(*pixel)
412+
self.position = Point(*position)
410413
self.__tile = Point(*tile) if tile is not None else None
411414
self.state = state
412415

416+
@property
417+
def pixel(self) -> Point:
418+
warnings.warn(
419+
"The mouse.pixel attribute is deprecated. Use mouse.position instead.",
420+
DeprecationWarning,
421+
stacklevel=2,
422+
)
423+
return self.position
424+
425+
@pixel.setter
426+
def pixel(self, value: Point) -> None:
427+
warnings.warn(
428+
"The mouse.pixel attribute is deprecated. Use mouse.position instead.",
429+
DeprecationWarning,
430+
stacklevel=2,
431+
)
432+
self.position = value
433+
413434
@property
414435
def tile(self) -> Point:
436+
warnings.warn(
437+
"The mouse.tile attribute is deprecated. Use mouse.position of the event returned by context.convert_event instead.",
438+
DeprecationWarning,
439+
stacklevel=2,
440+
)
415441
return _verify_tile_coordinates(self.__tile)
416442

417443
@tile.setter
418444
def tile(self, xy: Tuple[int, int]) -> None:
445+
warnings.warn(
446+
"The mouse.tile attribute is deprecated. Use mouse.position of the event returned by context.convert_event instead.",
447+
DeprecationWarning,
448+
stacklevel=2,
449+
)
419450
self.__tile = Point(*xy)
420451

421452
def __repr__(self) -> str:
422-
return ("tcod.event.%s(pixel=%r, tile=%r, state=%s)") % (
453+
return ("tcod.event.%s(position=%r, tile=%r, state=%s)") % (
423454
self.__class__.__name__,
424-
tuple(self.pixel),
455+
tuple(self.position),
425456
tuple(self.tile),
426457
_describe_bitmask(self.state, _REVERSE_BUTTON_MASK_TABLE_PREFIX),
427458
)
428459

429460
def __str__(self) -> str:
430-
return ("<%s, pixel=(x=%i, y=%i), tile=(x=%i, y=%i), state=%s>") % (
461+
return ("<%s, position=(x=%i, y=%i), tile=(x=%i, y=%i), state=%s>") % (
431462
super().__str__().strip("<>"),
432-
*self.pixel,
463+
*self.position,
433464
*self.tile,
434465
_describe_bitmask(self.state, _REVERSE_BUTTON_MASK_TABLE),
435466
)
@@ -439,8 +470,8 @@ class MouseMotion(MouseState):
439470
"""
440471
Attributes:
441472
type (str): Always "MOUSEMOTION".
442-
pixel (Point): The pixel coordinates of the mouse.
443-
pixel_motion (Point): The pixel delta.
473+
position (Point): The pixel coordinates of the mouse.
474+
motion (Point): The pixel delta.
444475
tile (Point): The integer tile coordinates of the mouse on the screen.
445476
tile_motion (Point): The integer tile delta.
446477
state (int): A bitmask of which mouse buttons are currently held.
@@ -452,26 +483,60 @@ class MouseMotion(MouseState):
452483
* tcod.event.BUTTON_RMASK
453484
* tcod.event.BUTTON_X1MASK
454485
* tcod.event.BUTTON_X2MASK
486+
487+
.. versionchanged:: Unreleased
488+
Renamed `pixel` attribute to `position`.
489+
Renamed `pixel_motion` attribute to `motion`.
455490
"""
456491

457492
def __init__(
458493
self,
459-
pixel: Tuple[int, int] = (0, 0),
460-
pixel_motion: Tuple[int, int] = (0, 0),
494+
position: Tuple[int, int] = (0, 0),
495+
motion: Tuple[int, int] = (0, 0),
461496
tile: Optional[Tuple[int, int]] = (0, 0),
462497
tile_motion: Optional[Tuple[int, int]] = (0, 0),
463498
state: int = 0,
464499
):
465-
super().__init__(pixel, tile, state)
466-
self.pixel_motion = Point(*pixel_motion)
500+
super().__init__(position, tile, state)
501+
self.motion = Point(*motion)
467502
self.__tile_motion = Point(*tile_motion) if tile_motion is not None else None
468503

504+
@property
505+
def pixel_motion(self) -> Point:
506+
warnings.warn(
507+
"The mouse.pixel_motion attribute is deprecated. Use mouse.motion instead.",
508+
DeprecationWarning,
509+
stacklevel=2,
510+
)
511+
return self.motion
512+
513+
@pixel_motion.setter
514+
def pixel_motion(self, value: Point) -> None:
515+
warnings.warn(
516+
"The mouse.pixel_motion attribute is deprecated. Use mouse.motion instead.",
517+
DeprecationWarning,
518+
stacklevel=2,
519+
)
520+
self.motion = value
521+
469522
@property
470523
def tile_motion(self) -> Point:
524+
warnings.warn(
525+
"The mouse.tile_motion attribute is deprecated."
526+
" Use mouse.motion of the event returned by context.convert_event instead.",
527+
DeprecationWarning,
528+
stacklevel=2,
529+
)
471530
return _verify_tile_coordinates(self.__tile_motion)
472531

473532
@tile_motion.setter
474533
def tile_motion(self, xy: Tuple[int, int]) -> None:
534+
warnings.warn(
535+
"The mouse.tile_motion attribute is deprecated."
536+
" Use mouse.motion of the event returned by context.convert_event instead.",
537+
DeprecationWarning,
538+
stacklevel=2,
539+
)
475540
self.__tile_motion = Point(*xy)
476541

477542
@classmethod
@@ -494,19 +559,19 @@ def from_sdl_event(cls, sdl_event: Any) -> MouseMotion:
494559
return self
495560

496561
def __repr__(self) -> str:
497-
return ("tcod.event.%s(pixel=%r, pixel_motion=%r, " "tile=%r, tile_motion=%r, state=%s)") % (
562+
return ("tcod.event.%s(position=%r, motion=%r, tile=%r, tile_motion=%r, state=%s)") % (
498563
self.__class__.__name__,
499-
tuple(self.pixel),
500-
tuple(self.pixel_motion),
564+
tuple(self.position),
565+
tuple(self.motion),
501566
tuple(self.tile),
502567
tuple(self.tile_motion),
503568
_describe_bitmask(self.state, _REVERSE_BUTTON_MASK_TABLE_PREFIX),
504569
)
505570

506571
def __str__(self) -> str:
507-
return ("<%s, pixel_motion=(x=%i, y=%i), tile_motion=(x=%i, y=%i)>") % (
572+
return ("<%s, motion=(x=%i, y=%i), tile_motion=(x=%i, y=%i)>") % (
508573
super().__str__().strip("<>"),
509-
*self.pixel_motion,
574+
*self.motion,
510575
*self.tile_motion,
511576
)
512577

@@ -516,7 +581,7 @@ class MouseButtonEvent(MouseState):
516581
Attributes:
517582
type (str): Will be "MOUSEBUTTONDOWN" or "MOUSEBUTTONUP",
518583
depending on the event.
519-
pixel (Point): The pixel coordinates of the mouse.
584+
position (Point): The pixel coordinates of the mouse.
520585
tile (Point): The integer tile coordinates of the mouse on the screen.
521586
button (int): Which mouse button.
522587
@@ -527,6 +592,7 @@ class MouseButtonEvent(MouseState):
527592
* tcod.event.BUTTON_RIGHT
528593
* tcod.event.BUTTON_X1
529594
* tcod.event.BUTTON_X2
595+
530596
"""
531597

532598
def __init__(
@@ -559,17 +625,17 @@ def from_sdl_event(cls, sdl_event: Any) -> Any:
559625
return self
560626

561627
def __repr__(self) -> str:
562-
return "tcod.event.%s(pixel=%r, tile=%r, button=%s)" % (
628+
return "tcod.event.%s(position=%r, tile=%r, button=%s)" % (
563629
self.__class__.__name__,
564-
tuple(self.pixel),
630+
tuple(self.position),
565631
tuple(self.tile),
566632
_REVERSE_BUTTON_TABLE_PREFIX[self.button],
567633
)
568634

569635
def __str__(self) -> str:
570-
return "<type=%r, pixel=(x=%i, y=%i), tile=(x=%i, y=%i), button=%s)" % (
636+
return "<type=%r, position=(x=%i, y=%i), tile=(x=%i, y=%i), button=%s)" % (
571637
self.type,
572-
*self.pixel,
638+
*self.position,
573639
*self.tile,
574640
_REVERSE_BUTTON_TABLE[self.button],
575641
)

tcod/sdl/mouse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_global_state() -> tcod.event.MouseState:
189189
"""
190190
xy = ffi.new("int[2]")
191191
state = lib.SDL_GetGlobalMouseState(xy, xy + 1)
192-
return tcod.event.MouseState(pixel=(xy[0], xy[1]), state=state)
192+
return tcod.event.MouseState((xy[0], xy[1]), state=state)
193193

194194

195195
def get_relative_state() -> tcod.event.MouseState:
@@ -200,7 +200,7 @@ def get_relative_state() -> tcod.event.MouseState:
200200
"""
201201
xy = ffi.new("int[2]")
202202
state = lib.SDL_GetRelativeMouseState(xy, xy + 1)
203-
return tcod.event.MouseState(pixel=(xy[0], xy[1]), state=state)
203+
return tcod.event.MouseState((xy[0], xy[1]), state=state)
204204

205205

206206
def get_state() -> tcod.event.MouseState:
@@ -211,7 +211,7 @@ def get_state() -> tcod.event.MouseState:
211211
"""
212212
xy = ffi.new("int[2]")
213213
state = lib.SDL_GetMouseState(xy, xy + 1)
214-
return tcod.event.MouseState(pixel=(xy[0], xy[1]), state=state)
214+
return tcod.event.MouseState((xy[0], xy[1]), state=state)
215215

216216

217217
def get_focus() -> Optional[tcod.sdl.video.Window]:

0 commit comments

Comments
 (0)