Skip to content

Change mouse.get_(pos|rel) to output floats #2429

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
4 changes: 2 additions & 2 deletions buildconfig/stubs/pygame/mouse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/reST/ref/mouse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src_c/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand All @@ -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 *
Expand Down
4 changes: 2 additions & 2 deletions test/mouse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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."""
Expand Down