Skip to content

Change relevant mouse event attributes to floats #2428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/reST/ref/event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,15 @@ specific attributes.
VIDEOEXPOSE none
USEREVENT code

.. versionchangedold:: 2.0.0 The ``joy`` attribute was deprecated, ``instance_id`` was added.
.. versionchangedold:: 2.0.0 The ``joy`` attribute was deprecated,
``instance_id`` was added.

.. versionchangedold:: 2.0.1 The ``unicode`` attribute was added to ``KEYUP`` event.

.. versionchanged:: 2.4.0 The ``pos`` and ``rel`` attributes of ``MOUSEMOTION``,
``MOUSEBUTTONUP``, and ``MOUSEBUTTONDOWN`` are now 2-tuples of floats,
in preparation for SDL3.

Note that ``ACTIVEEVENT``, ``VIDEORESIZE`` and ``VIDEOEXPOSE`` are considered
as "legacy" events, the use of pygame2 ``WINDOWEVENT`` API is recommended over
the use of this older API.
Expand All @@ -106,7 +111,8 @@ attributes.
FINGERMOTION touch_id, finger_id, x, y, dx, dy
FINGERDOWN touch_id, finger_id, x, y, dx, dy
FINGERUP touch_id, finger_id, x, y, dx, dy
MOUSEWHEEL which, flipped, x, y, touch, precise_x, precise_y
MOUSEWHEEL which, flipped, x, y, touch,
precise_x (deprecated), precise_y (deprecated)
MULTIGESTURE touch_id, x, y, pinched, rotated, num_fingers
TEXTEDITING text, start, length
TEXTINPUT text
Expand All @@ -123,6 +129,10 @@ already handles ``FINGERMOTION``, ``FINGERDOWN`` and ``FINGERUP`` events.

.. versionadded:: 2.1.3 Added ``precise_x`` and ``precise_y`` to ``MOUSEWHEEL`` events

.. versionchanged:: 2.4.0 ``MOUSEWHEEL`` event ``x`` and ``y`` are now floats
instead of ints, and "precise" by default. This makes ``precise_x`` and
``precise_y`` redundant, so they are now deprecated.

|

Many new events were introduced in pygame 2.
Expand Down
43 changes: 29 additions & 14 deletions src_c/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,14 @@ dict_from_event(SDL_Event *event)
PyLong_FromLong(event->key.keysym.scancode));
break;
case SDL_MOUSEMOTION:
obj = Py_BuildValue("(ii)", event->motion.x, event->motion.y);
// Why cast x, y, xrel, and yrel to floats now? In SDL3, these
// mousemotion attributes are floats, so outputting the attributes
// as floats now prepares for that.
obj = Py_BuildValue("(ff)", (float)event->motion.x,
(float)event->motion.y);
_pg_insobj(dict, "pos", obj);
obj =
Py_BuildValue("(ii)", event->motion.xrel, event->motion.yrel);
obj = Py_BuildValue("(ff)", (float)event->motion.xrel,
(float)event->motion.yrel);
_pg_insobj(dict, "rel", obj);
if ((tuple = PyTuple_New(3))) {
PyTuple_SET_ITEM(tuple, 0,
Expand All @@ -1008,7 +1012,11 @@ dict_from_event(SDL_Event *event)
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
obj = Py_BuildValue("(ii)", event->button.x, event->button.y);
// Why cast x and y to floats now? In SDL3, these button event
// attributes are floats, so outputting the attributes as floats
// now prepares for that.
obj = Py_BuildValue("(ff)", (float)event->button.x,
(float)event->button.y);
_pg_insobj(dict, "pos", obj);
_pg_insobj(dict, "button", PyLong_FromLong(event->button.button));
_pg_insobj(
Expand Down Expand Up @@ -1114,23 +1122,30 @@ dict_from_event(SDL_Event *event)
#else
_pg_insobj(dict, "flipped", PyBool_FromLong(0));
#endif
_pg_insobj(dict, "x", PyLong_FromLong(event->wheel.x));
_pg_insobj(dict, "y", PyLong_FromLong(event->wheel.y));

#if SDL_VERSION_ATLEAST(2, 0, 18)
_pg_insobj(dict, "precise_x",
PyFloat_FromDouble((double)event->wheel.preciseX));
_pg_insobj(dict, "precise_y",
PyFloat_FromDouble((double)event->wheel.preciseY));

#else /* ~SDL_VERSION_ATLEAST(2, 0, 18) */
/* Previously, we had x and y as integers and precise_x and
* precise_y as floats. In SDL3, there is no "precise", it's always
* a float. To prepare for SDL3 then, lets starting outputting
* mousewheel position as floats and with maximum precision. */
#if !SDL_VERSION_ATLEAST(2, 0, 18)
/* fallback to regular x and y when SDL version used does not
* support precise fields */
_pg_insobj(dict, "x", PyFloat_FromDouble((double)event->wheel.x));
_pg_insobj(dict, "y", PyFloat_FromDouble((double)event->wheel.y));
_pg_insobj(dict, "precise_x",
PyFloat_FromDouble((double)event->wheel.x));
_pg_insobj(dict, "precise_y",
PyFloat_FromDouble((double)event->wheel.y));

#else /* ~SDL_VERSION_ATLEAST(2, 0, 18) */
_pg_insobj(dict, "x",
PyFloat_FromDouble((double)event->wheel.preciseX));
_pg_insobj(dict, "y",
PyFloat_FromDouble((double)event->wheel.preciseY));
_pg_insobj(dict, "precise_x",
PyFloat_FromDouble((double)event->wheel.preciseX));
_pg_insobj(dict, "precise_y",
PyFloat_FromDouble((double)event->wheel.preciseY));

#endif /* ~SDL_VERSION_ATLEAST(2, 0, 18) */
_pg_insobj(
dict, "touch",
Expand Down