diff --git a/buildconfig/stubs/pygame/mouse.pyi b/buildconfig/stubs/pygame/mouse.pyi index a62c7f4d78..fcdb9a8d34 100644 --- a/buildconfig/stubs/pygame/mouse.pyi +++ b/buildconfig/stubs/pygame/mouse.pyi @@ -9,8 +9,8 @@ from ._common import Coordinate, Sequence, IntCoordinate def get_pressed(num_buttons: Literal[3] = 3) -> Tuple[bool, bool, bool]: ... @overload def get_pressed(num_buttons: Literal[5]) -> Tuple[bool, bool, bool, bool, bool]: ... -def get_pos() -> Tuple[int, int]: ... -def get_rel() -> Tuple[int, int]: ... +def get_pos() -> Tuple[float, float]: ... +def get_rel() -> Tuple[float, float]: ... @overload def set_pos(pos: Coordinate) -> None: ... @overload diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index f61ed26ec8..6af90d7446 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -117,6 +117,9 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the located outside of the display window, but is always constrained to the screen. + .. versionchanged:: 2.4.0 Now returns (x, y) as a Tuple of floats, instead + of ints, in preparation for future high precision APIs. + .. ## pygame.mouse.get_pos ## .. function:: get_rel @@ -129,6 +132,9 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the the edges of the screen, but see the virtual input mouse mode for a way around this. Virtual input mode is described at the top of the page. + .. versionchanged:: 2.4.0 Now returns (x, y) as a Tuple of floats, instead + of ints, in preparation for future high precision APIs. + .. ## pygame.mouse.get_rel ## .. function:: set_pos diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index bec945b998..08b740486c 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -557,6 +557,31 @@ pg_tuple_couple_from_values_int(int val1, int val2) return tup; } +static PG_INLINE PyObject * +pg_tuple_couple_from_values_float(float val1, float val2) +{ + PyObject *tup = PyTuple_New(2); + if (!tup) { + return NULL; + } + + PyObject *tmp = PyFloat_FromDouble((double)val1); + if (!tmp) { + Py_DECREF(tup); + return NULL; + } + PyTuple_SET_ITEM(tup, 0, tmp); + + tmp = PyFloat_FromDouble((double)val2); + if (!tmp) { + Py_DECREF(tup); + return NULL; + } + PyTuple_SET_ITEM(tup, 1, tmp); + + return tup; +} + static PG_INLINE PyObject * pg_tuple_triple_from_values_int(int val1, int val2, int val3) { diff --git a/src_c/mouse.c b/src_c/mouse.c index 4c7b2ea0e9..1a2c8fdb30 100644 --- a/src_c/mouse.c +++ b/src_c/mouse.c @@ -97,7 +97,7 @@ mouse_get_pos(PyObject *self, PyObject *_null) } } - return pg_tuple_couple_from_values_int(x, y); + return pg_tuple_couple_from_values_float((float)x, (float)y); } static PyObject * @@ -121,7 +121,7 @@ mouse_get_rel(PyObject *self, PyObject *_null) y/=scaley; } */ - return pg_tuple_couple_from_values_int(x, y); + return pg_tuple_couple_from_values_float((float)x, (float)y); } static PyObject * diff --git a/test/mouse_test.py b/test/mouse_test.py index 5cf8e3e408..9cecb88232 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -297,7 +297,7 @@ def test_get_pos(self): self.assertIsInstance(pos, tuple) self.assertEqual(len(pos), expected_length) for value in pos: - self.assertIsInstance(value, int) + self.assertIsInstance(value, float) def test_set_pos__invalid_pos(self): """Ensures set_pos handles invalid positions correctly.""" @@ -314,7 +314,7 @@ def test_get_rel(self): self.assertIsInstance(rel, tuple) self.assertEqual(len(rel), expected_length) for value in rel: - self.assertIsInstance(value, int) + self.assertIsInstance(value, float) def test_get_visible(self): """Ensures get_visible works correctly."""