|
89 | 89 | from collections.abc import Callable, Iterator, Mapping |
90 | 90 | from math import floor |
91 | 91 | from pathlib import Path |
92 | | -from typing import TYPE_CHECKING, Any, Final, Generic, Literal, NamedTuple, TypeAlias, TypedDict, TypeVar, overload |
| 92 | +from typing import ( |
| 93 | + TYPE_CHECKING, |
| 94 | + Any, |
| 95 | + Final, |
| 96 | + Generic, |
| 97 | + Literal, |
| 98 | + NamedTuple, |
| 99 | + Protocol, |
| 100 | + TypeAlias, |
| 101 | + TypedDict, |
| 102 | + TypeVar, |
| 103 | + overload, |
| 104 | + runtime_checkable, |
| 105 | +) |
93 | 106 |
|
94 | 107 | import attrs |
95 | 108 | import numpy as np |
@@ -630,7 +643,7 @@ class MouseButtonEvent(Event): |
630 | 643 | """ |
631 | 644 |
|
632 | 645 | position: Point[float] = attrs.field(default=Point(0.0, 0.0)) |
633 | | - """The pixel coordinates of the mouse.""" |
| 646 | + """The coordinates of the mouse.""" |
634 | 647 | _tile: Point[int] | None = attrs.field(default=Point(0, 0), alias="tile") |
635 | 648 | """The tile integer coordinates of the mouse on the screen. Deprecated.""" |
636 | 649 | button: MouseButton |
@@ -709,13 +722,63 @@ class MouseWheel(Event): |
709 | 722 | """Vertical scrolling. A positive value means scrolling away from the user.""" |
710 | 723 | flipped: bool |
711 | 724 | """If True then the values of `x` and `y` are the opposite of their usual values. |
712 | | - This depends on the settings of the Operating System. |
| 725 | + This depends on the operating system settings. |
| 726 | + """ |
| 727 | + |
| 728 | + position: Point[float] = attrs.field(default=Point(0.0, 0.0)) |
| 729 | + """Coordinates of the mouse for this event. |
| 730 | +
|
| 731 | + .. versionadded:: Unreleased |
| 732 | + """ |
| 733 | + |
| 734 | + which: int = 0 |
| 735 | + """Mouse device ID for this event. |
| 736 | +
|
| 737 | + .. versionadded:: Unreleased |
| 738 | + """ |
| 739 | + |
| 740 | + window_id: int = 0 |
| 741 | + """Window ID with mouse focus. |
| 742 | +
|
| 743 | + .. versionadded:: Unreleased |
713 | 744 | """ |
714 | 745 |
|
| 746 | + @property |
| 747 | + def integer_position(self) -> Point[int]: |
| 748 | + """Integer coordinates of this event. |
| 749 | +
|
| 750 | + .. versionadded:: Unreleased |
| 751 | + """ |
| 752 | + x, y = self.position |
| 753 | + return Point(floor(x), floor(y)) |
| 754 | + |
715 | 755 | @classmethod |
716 | 756 | def _from_sdl_event(cls, sdl_event: _C_SDL_Event) -> Self: |
717 | 757 | wheel = sdl_event.wheel |
718 | | - return cls(x=int(wheel.x), y=int(wheel.y), flipped=bool(wheel.direction), **_unpack_sdl_event(sdl_event)) |
| 758 | + return cls( |
| 759 | + x=int(wheel.integer_x), |
| 760 | + y=int(wheel.integer_y), |
| 761 | + flipped=bool(wheel.direction), |
| 762 | + position=Point(float(wheel.mouse_x), float(wheel.mouse_y)), |
| 763 | + which=int(wheel.which), |
| 764 | + window_id=int(wheel.windowID), |
| 765 | + **_unpack_sdl_event(sdl_event), |
| 766 | + ) |
| 767 | + |
| 768 | + |
| 769 | +@runtime_checkable |
| 770 | +class _MouseEventWithPosition(Protocol): |
| 771 | + """Mouse event with position. Used internally to handle conversions.""" |
| 772 | + |
| 773 | + position: Point[float] |
| 774 | + |
| 775 | + |
| 776 | +@runtime_checkable |
| 777 | +class _MouseEventWithTile(Protocol): |
| 778 | + """Mouse event with position and deprecated tile attribute. Used internally to handle conversions.""" |
| 779 | + |
| 780 | + position: Point[float] |
| 781 | + _tile: Point[int] | None |
719 | 782 |
|
720 | 783 |
|
721 | 784 | @attrs.define(slots=True, kw_only=True) |
@@ -1742,9 +1805,10 @@ def convert_coordinates_from_window( |
1742 | 1805 | event._tile_motion = Point( |
1743 | 1806 | floor(position[0]) - floor(previous_position[0]), floor(position[1]) - floor(previous_position[1]) |
1744 | 1807 | ) |
1745 | | - if isinstance(event, (MouseState, MouseMotion)): |
| 1808 | + elif isinstance(event, _MouseEventWithPosition): |
1746 | 1809 | event.position = Point(*convert_coordinates_from_window(event.position, context, console, dest_rect)) |
1747 | | - event._tile = Point(floor(event.position[0]), floor(event.position[1])) |
| 1810 | + if isinstance(event, _MouseEventWithTile): |
| 1811 | + event._tile = Point(floor(event.position[0]), floor(event.position[1])) |
1748 | 1812 | return event |
1749 | 1813 |
|
1750 | 1814 |
|
|
0 commit comments