From e970a5f5819fe63efe10193f7064224b79873385 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sat, 6 Apr 2024 18:39:50 -0400 Subject: [PATCH 1/5] relative position helpers --- arcade/application.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arcade/application.py b/arcade/application.py index 44b7d95d0..8a1ac70ca 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -915,6 +915,32 @@ def on_mouse_leave(self, x: int, y: int): """ pass + def absolute(self, normalized_pos: tuple[float, float]) -> tuple[float, float]: + """Convert a position in normalized form to pixel-based, absolute form. + + :param normalized_pos: a tuple of two floats, representing a position, + where 0.0 -> 1.0 in the x represents the far left and right of the window, + and 0.0 -> 1.0 in the y represents the very bottom and the very top. + + Returns a tuple of floats in pixels. + """ + + w, h = self.size + return (normalized_pos[0] * w, normalized_pos[1] * h) + + def relative(self, pos: tuple[float, float]) -> tuple[float, float]: + """Convert a position in normalized form to pixel-based, absolute form. + + :param pos: a tuple of two floats, representing a position in pixels. + + Returns a tuple of floats where 0.0 -> 1.0 in the x represents the + far left and right of the window, and 0.0 -> 1.0 in the y represents + the very bottom and the very top. + """ + + w, h = self.size + return (pos[0] / w, pos[1] / h) + def open_window( width: int, From bef4bce84c2b953b6f4cc81fc30cac9d0c5d8649 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sat, 6 Apr 2024 20:22:24 -0400 Subject: [PATCH 2/5] fix docstring --- arcade/application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/application.py b/arcade/application.py index 8a1ac70ca..9f6333047 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -929,7 +929,7 @@ def absolute(self, normalized_pos: tuple[float, float]) -> tuple[float, float]: return (normalized_pos[0] * w, normalized_pos[1] * h) def relative(self, pos: tuple[float, float]) -> tuple[float, float]: - """Convert a position in normalized form to pixel-based, absolute form. + """Convert a absolute position to coordinates in screenspace. :param pos: a tuple of two floats, representing a position in pixels. From 48b8600176e759b5bc84a3e2c9dd70b0b9f06510 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Sat, 6 Apr 2024 23:38:59 -0400 Subject: [PATCH 3/5] Clean up docstrings of relative window position helpers --- arcade/application.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/arcade/application.py b/arcade/application.py index 9f6333047..4be80fd55 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -916,26 +916,24 @@ def on_mouse_leave(self, x: int, y: int): pass def absolute(self, normalized_pos: tuple[float, float]) -> tuple[float, float]: - """Convert a position in normalized form to pixel-based, absolute form. + """Convert a relative fractional pair to absolute screen pixels. - :param normalized_pos: a tuple of two floats, representing a position, - where 0.0 -> 1.0 in the x represents the far left and right of the window, - and 0.0 -> 1.0 in the y represents the very bottom and the very top. + As in the rest of arcade and OpenGL, the default coordinate system + places the origin at the bottom left. - Returns a tuple of floats in pixels. + :param normalized_pos: A position where the x and y are fractional + values of the window size. + :returns: A tuple of absolute pixel X and Y relative to bottom left. """ w, h = self.size return (normalized_pos[0] * w, normalized_pos[1] * h) def relative(self, pos: tuple[float, float]) -> tuple[float, float]: - """Convert a absolute position to coordinates in screenspace. + """Convert absolute screen pixels to a relative fractional pair. - :param pos: a tuple of two floats, representing a position in pixels. - - Returns a tuple of floats where 0.0 -> 1.0 in the x represents the - far left and right of the window, and 0.0 -> 1.0 in the y represents - the very bottom and the very top. + :param pos: a tuple of X and Y pixel positions within the window. + :returns: A tuple of fractional X and Y relative to the bottom left. """ w, h = self.size From 8f1967939c87e7e10fbdbce944584fefef82f7a3 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Sat, 6 Apr 2024 23:43:30 -0400 Subject: [PATCH 4/5] Clean up naming and style a bit * Normalized -> fractional since we aren't clamping * Variable name and style tweaks (redundant parens can go) --- arcade/application.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/arcade/application.py b/arcade/application.py index 4be80fd55..9f0721d81 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -915,29 +915,29 @@ def on_mouse_leave(self, x: int, y: int): """ pass - def absolute(self, normalized_pos: tuple[float, float]) -> tuple[float, float]: + def absolute(self, fractional_pos: tuple[float, float]) -> tuple[float, float]: """Convert a relative fractional pair to absolute screen pixels. As in the rest of arcade and OpenGL, the default coordinate system places the origin at the bottom left. - :param normalized_pos: A position where the x and y are fractional + :param fractional_pos: A position where the x and y are fractional values of the window size. :returns: A tuple of absolute pixel X and Y relative to bottom left. """ - w, h = self.size - return (normalized_pos[0] * w, normalized_pos[1] * h) + frac_x, frac_y = fractional_pos + return frac_x * w, frac_y * h - def relative(self, pos: tuple[float, float]) -> tuple[float, float]: + def relative(self, pixel_pos: tuple[float, float]) -> tuple[float, float]: """Convert absolute screen pixels to a relative fractional pair. - :param pos: a tuple of X and Y pixel positions within the window. + :param pixel_pos: a tuple of X and Y pixel positions within the window. :returns: A tuple of fractional X and Y relative to the bottom left. """ - w, h = self.size - return (pos[0] / w, pos[1] / h) + pixel_x, pixel_y = pixel_pos + return pixel_x / w, pixel_y / pixel_y def open_window( From 250a631626428519c4b38ea9e6030f61a8ed1867 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sun, 7 Apr 2024 20:32:00 -0400 Subject: [PATCH 5/5] fix math error --- arcade/application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/application.py b/arcade/application.py index 9f0721d81..a1f3c5918 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -937,7 +937,7 @@ def relative(self, pixel_pos: tuple[float, float]) -> tuple[float, float]: """ w, h = self.size pixel_x, pixel_y = pixel_pos - return pixel_x / w, pixel_y / pixel_y + return pixel_x / w, pixel_y / h def open_window(