From c622352f7d216ce29ddf5993e4134e20e9240759 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Tue, 29 Aug 2023 00:33:55 -0700 Subject: [PATCH] Change relevant mouse event attributes to floats In preparation for SDL3, where these attributes are floats. --- docs/reST/ref/event.rst | 14 ++++++++++++-- src_c/event.c | 43 +++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/docs/reST/ref/event.rst b/docs/reST/ref/event.rst index e8a30c073a..26105ea98d 100644 --- a/docs/reST/ref/event.rst +++ b/docs/reST/ref/event.rst @@ -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. @@ -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 @@ -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. diff --git a/src_c/event.c b/src_c/event.c index cd381f95d7..f1c0ffcbbe 100644 --- a/src_c/event.c +++ b/src_c/event.c @@ -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, @@ -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( @@ -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",