Skip to content

Commit b128704

Browse files
committed
Move mouse docs to stubs
1 parent 065a7c7 commit b128704

File tree

3 files changed

+280
-337
lines changed

3 files changed

+280
-337
lines changed

buildconfig/stubs/pygame/mouse.pyi

Lines changed: 264 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
1-
from typing import Literal, overload
1+
"""Pygame module to work with the mouse.
2+
3+
The mouse functions can be used to get the current state of the mouse device.
4+
These functions can also alter the system cursor for the mouse.
5+
6+
When the display mode is set, the event queue will start receiving mouse
7+
events. The mouse buttons generate ``pygame.MOUSEBUTTONDOWN`` and
8+
``pygame.MOUSEBUTTONUP`` events when they are pressed and released. These
9+
events contain a button attribute representing which button was pressed. The
10+
mouse wheel will generate ``pygame.MOUSEBUTTONDOWN`` and
11+
``pygame.MOUSEBUTTONUP`` events when rolled. The button will be set to 4
12+
when the wheel is rolled up, and to button 5 when the wheel is rolled down.
13+
Whenever the mouse is moved it generates a ``pygame.MOUSEMOTION`` event. The
14+
mouse movement is broken into small and accurate motion events. As the mouse
15+
is moving many motion events will be placed on the queue. Mouse motion events
16+
that are not properly cleaned from the event queue are the primary reason the
17+
event queue fills up.
18+
19+
If the mouse cursor is hidden, and input is grabbed to the current display the
20+
mouse will enter a virtual input mode, where the relative movements of the
21+
mouse will never be stopped by the borders of the screen. See the functions
22+
``pygame.mouse.set_visible()`` and ``pygame.event.set_grab()`` to get this
23+
configured.
24+
25+
**Mouse Wheel Behavior in pygame 2**
26+
27+
There is proper functionality for mouse wheel behaviour with pygame 2 supporting
28+
``pygame.MOUSEWHEEL`` events. The new events support horizontal and vertical
29+
scroll movements, with signed integer values representing the amount scrolled
30+
(``x`` and ``y``), as well as ``flipped`` direction (the set positive and
31+
negative values for each axis is flipped). Read more about SDL2
32+
input-related changes here `<https://wiki.libsdl.org/MigrationGuide#input>`_
33+
34+
In pygame 2, the mouse wheel functionality can be used by listening for the
35+
``pygame.MOUSEWHEEL`` type of an event (Bear in mind they still emit
36+
``pygame.MOUSEBUTTONDOWN`` events like in pygame 1.x, as well).
37+
When this event is triggered, a developer can access the appropriate ``Event`` object
38+
with ``pygame.event.get()``. The object can be used to access data about the mouse
39+
scroll, such as ``which`` (it will tell you what exact mouse device trigger the event).
40+
41+
.. code-block:: python
42+
:caption: Code example of mouse scroll (tested on 2.0.0.dev7)
43+
:name: test.py
44+
45+
# Taken from husano896's PR thread (slightly modified)
46+
import pygame
47+
from pygame.locals import *
48+
pygame.init()
49+
screen = pygame.display.set_mode((640, 480))
50+
clock = pygame.time.Clock()
51+
52+
def main():
53+
while True:
54+
for event in pygame.event.get():
55+
if event.type == QUIT:
56+
pygame.quit()
57+
return
58+
elif event.type == MOUSEWHEEL:
59+
print(event)
60+
print(event.x, event.y)
61+
print(event.flipped)
62+
print(event.which)
63+
# can access properties with
64+
# proper notation(ex: event.y)
65+
clock.tick(60)
66+
67+
# Execute game:
68+
main()
69+
"""
70+
71+
from typing import Any, Literal, overload
272

373
from pygame.cursors import Cursor
474
from pygame.surface import Surface
@@ -13,22 +83,156 @@ def get_pressed(
1383
def get_pressed(
1484
num_buttons: Literal[5], desktop: bool = False
1585
) -> tuple[bool, bool, bool, bool, bool]: ...
16-
def get_just_pressed() -> tuple[bool, bool, bool, bool, bool]: ...
17-
def get_just_released() -> tuple[bool, bool, bool, bool, bool]: ...
18-
def get_pos(desktop: bool = False) -> tuple[int, int]: ...
19-
def get_rel() -> tuple[int, int]: ...
86+
def get_pressed(*args, **kwargs) -> Any:
87+
"""Get the state of the mouse buttons.
88+
89+
Returns a sequence of booleans representing the state of all the mouse
90+
buttons. A true value means the mouse is currently being pressed at the time
91+
of the call.
92+
93+
To get all of the mouse events it is better to use either
94+
``pygame.event.wait()`` or ``pygame.event.get()`` and check all of those
95+
events to see if they are ``MOUSEBUTTONDOWN``, ``MOUSEBUTTONUP``, or
96+
``MOUSEMOTION``. Remember to call ``pygame.event.get()`` or ``pygame.event.pump()``
97+
before this function, otherwise it will not work as expected.
98+
99+
To support five button mice, an optional parameter ``num_buttons`` has been
100+
added in pygame 2. When this is set to ``5``, ``button4`` and ``button5``
101+
are added to the returned tuple. Only ``3`` and ``5`` are valid values
102+
for this parameter.
103+
104+
If the ``desktop`` argument is ``True`` the mouse state will be correct even
105+
if the window has no focus. In addition since it queries the OS it does not depend
106+
on the last event pump while being slightly slower.
107+
108+
.. note:: On ``X11`` some X servers use middle button emulation. When you
109+
click both buttons ``1`` and ``3`` at the same time a ``2`` button event
110+
can be emitted.
111+
112+
.. warning:: Due to design constraints it is impossible to retrieve the desktop
113+
mouse state on Wayland. The normal mouse state is returned instead.
114+
115+
.. versionchangedold:: 2.0.0 ``num_buttons`` argument added
116+
117+
.. versionchanged:: 2.5.2 Added the ``desktop`` argument
118+
"""
119+
120+
def get_just_pressed() -> tuple[bool, bool, bool, bool, bool]:
121+
"""Get the most recently pressed buttons.
122+
123+
Very similar to :func:`pygame.mouse.get_pressed()`, returning a tuple
124+
of length 5 with the important difference that the buttons are
125+
True only in the frame they start being pressed. This can be convenient
126+
for checking the buttons pressed "this frame", but for more precise results
127+
and correct ordering prefer using the ``pygame.MOUSEBUTTONDOWN`` event.
128+
129+
The result of this function is updated when new events are processed,
130+
e.g. in :func:`pygame.event.get()` or :func:`pygame.event.pump()`.
131+
132+
.. seealso:: :func:`pygame.mouse.get_just_released()`
133+
134+
::
135+
136+
if pygame.mouse.get_just_pressed()[0]:
137+
print("LMB just pressed")
138+
139+
.. versionadded:: 2.5.0
140+
"""
141+
142+
def get_just_released() -> tuple[bool, bool, bool, bool, bool]:
143+
"""Get the most recently released buttons.
144+
145+
Similar to :func:`pygame.mouse.get_pressed()`, returning a tuple
146+
of length 5 with the important difference that the buttons are
147+
True only in the frame they stop being pressed. This can be convenient
148+
for checking the buttons released "this frame", but for more precise results
149+
and correct ordering prefer using the ``pygame.MOUSEBUTTONUP`` event.
150+
151+
The result of this function is updated when new events are processed,
152+
e.g. in :func:`pygame.event.get()` or :func:`pygame.event.pump()`.
153+
154+
.. seealso:: :func:`pygame.mouse.get_just_pressed()`
155+
156+
::
157+
158+
if pygame.mouse.get_just_released()[0]:
159+
print("LMB just released")
160+
161+
.. versionadded:: 2.5.0
162+
"""
163+
164+
def get_pos(desktop: bool = False) -> tuple[int, int]:
165+
"""Get the mouse cursor position.
166+
167+
By default returns the ``x`` and ``y`` position of the mouse cursor. The position
168+
is relative to the top-left corner of the display. The cursor position can be
169+
located outside of the display window, but is always constrained to the screen.
170+
171+
If the ``desktop`` argument is ``True``, the position will be instead relative to the
172+
top-left corner of the primary monitor. The position might be negative or exceed
173+
the desktop bounds if multiple monitors are present.
174+
175+
.. warning:: Due to design constraints it is impossible to retrieve the desktop
176+
mouse state on Wayland. The relative mouse position is returned instead.
177+
178+
.. versionchanged:: 2.5.2 Added the ``desktop`` argument
179+
"""
180+
181+
def get_rel() -> tuple[int, int]:
182+
"""Get the amount of mouse movement.
183+
184+
Returns the amount of movement in ``x`` and ``y`` since the previous call to
185+
this function. The relative movement of the mouse cursor is constrained to
186+
the edges of the screen, but see the virtual input mouse mode for a way
187+
around this. Virtual input mode is described at the top of the page.
188+
"""
189+
20190
@overload
21191
def set_pos(pos: Point, /) -> None: ...
22192
@overload
23193
def set_pos(x: float, y: float, /) -> None: ...
24-
def set_visible(value: bool, /) -> int: ...
25-
def get_visible() -> bool: ...
26-
def get_focused() -> bool: ...
194+
def set_pos(*args) -> None:
195+
"""Set the mouse cursor position.
196+
197+
Set the current mouse position to arguments given. If the mouse cursor is
198+
visible it will jump to the new coordinates. Moving the mouse will generate
199+
a new ``pygame.MOUSEMOTION`` event.
200+
"""
201+
202+
def set_visible(value: bool, /) -> int:
203+
"""Hide or show the mouse cursor.
204+
205+
If the bool argument is true, the mouse cursor will be visible. This will
206+
return the previous visible state of the cursor.
207+
"""
208+
209+
def get_visible() -> bool:
210+
"""Get the current visibility state of the mouse cursor.
211+
212+
Get the current visibility state of the mouse cursor. ``True`` if the mouse is
213+
visible, ``False`` otherwise.
214+
215+
.. versionaddedold:: 2.0.0
216+
"""
217+
218+
def get_focused() -> bool:
219+
"""Check if the display is receiving mouse input.
220+
221+
Returns true when pygame is receiving mouse input events (or, in windowing
222+
terminology, is "active" or has the "focus").
223+
224+
This method is most useful when working in a window. By contrast, in
225+
full-screen mode, this method always returns true.
226+
227+
Note: under ``MS`` Windows, the window that has the mouse focus also has the
228+
keyboard focus. But under X-Windows, one window can receive mouse events and
229+
another receive keyboard events. ``pygame.mouse.get_focused()`` indicates
230+
whether the pygame window receives mouse events.
231+
"""
232+
27233
@overload
28234
def set_cursor(cursor: Cursor) -> None: ...
29235
@overload
30-
def set_cursor(constant: int) -> None: ...
31-
@overload
32236
def set_cursor(
33237
size: IntPoint,
34238
hotspot: IntPoint,
@@ -37,8 +241,55 @@ def set_cursor(
37241
) -> None: ...
38242
@overload
39243
def set_cursor(hotspot: IntPoint, surface: Surface) -> None: ...
40-
def get_cursor() -> Cursor: ...
244+
@overload
245+
def set_cursor(constant: int) -> None: ...
246+
def set_cursor(*args, **kwargs) -> None:
247+
"""Set the mouse cursor to a new cursor.
248+
249+
Set the mouse cursor to something new. This function accepts either an explicit
250+
``Cursor`` object or arguments to create a ``Cursor`` object.
251+
252+
See :class:`pygame.cursors.Cursor` for help creating cursors and for examples.
253+
254+
.. versionchangedold:: 2.0.1
255+
"""
256+
257+
def get_cursor() -> Cursor:
258+
"""Get the current mouse cursor.
259+
260+
Get the information about the mouse system cursor. The return value contains
261+
the same data as the arguments passed into :func:`pygame.mouse.set_cursor()`.
262+
263+
.. note:: Code that unpacked a get_cursor() call into
264+
``size, hotspot, xormasks, andmasks`` will still work,
265+
assuming the call returns an old school type cursor.
266+
267+
.. versionchangedold:: 2.0.1
268+
"""
269+
41270
@deprecated("since 2.2.0. Use `pygame.mouse.set_cursor` instead")
42271
def set_system_cursor(cursor: int, /) -> None: ...
43-
def get_relative_mode() -> bool: ...
44-
def set_relative_mode(enable: bool, /) -> None: ...
272+
def get_relative_mode() -> bool:
273+
"""Query whether relative mouse mode is enabled.
274+
275+
Query whether relative mouse mode is enabled.
276+
277+
.. versionadded:: 2.4.0
278+
"""
279+
280+
def set_relative_mode(enable: bool, /) -> None:
281+
"""Set relative mouse mode.
282+
283+
Sets the relative mouse mode state.
284+
While the mouse is in relative mode, the cursor is hidden,
285+
the mouse position is constrained to the window, and pygame
286+
will report continuous relative mouse motion even if the
287+
mouse is at the edge of the window.
288+
289+
*This function will flush any pending mouse motion."*
290+
291+
Calling :func:`pygame.mouse.set_visible` with argument
292+
``True`` will exit relative mouse mode.
293+
294+
.. versionadded:: 2.4.0
295+
"""

0 commit comments

Comments
 (0)