From 846315017c1b4c2f6c90d1ce85c0af5b2bb6fa88 Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Fri, 30 May 2025 13:13:31 +0200 Subject: [PATCH 1/3] gui: enhance UIWidget usability with intuitive property setters for position and size --- CHANGELOG.md | 5 ++++ arcade/gui/widgets/__init__.py | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61fff6f8db..7a61c66357 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page. - Added `Window.close` (bool) attribute indicating if the window is closed - GUI - Fix `UILabel` with enabled multiline sometimes cut off text + - Improved `UIWidget` usability for resizing and positioning: + - Added property setters for `width`, `height`, and `size` that ensure positive values + - Added property setters for `center_x` and `center_y` + - Added property setters for `left`, `right`, `top`, and `bottom` + - Users can now set widget position and size more intuitively without needing to access the `rect` property ## Version 3.2 diff --git a/arcade/gui/widgets/__init__.py b/arcade/gui/widgets/__init__.py index 244024cb37..cd610327b9 100644 --- a/arcade/gui/widgets/__init__.py +++ b/arcade/gui/widgets/__init__.py @@ -361,21 +361,37 @@ def left(self) -> float: """Left coordinate of the widget""" return self.rect.left + @left.setter + def left(self, value: float): + self.rect = LBWH(value, self.bottom, self.width, self.height) + @property def right(self) -> float: """Right coordinate of the widget""" return self.rect.right + @right.setter + def right(self, value: float): + self.rect = LBWH(value - self.width, self.bottom, self.width, self.height) + @property def bottom(self) -> float: """Bottom coordinate of the widget""" return self.rect.bottom + @bottom.setter + def bottom(self, value: float): + self.rect = LBWH(self.left, value, self.width, self.height) + @property def top(self) -> float: """Top coordinate of the widget""" return self.rect.top + @top.setter + def top(self, value: float): + self.rect = LBWH(self.left, value - self.height, self.width, self.height) + @property def position(self) -> Vec2: """Returns bottom left coordinates""" @@ -395,11 +411,19 @@ def center_x(self) -> float: """Center x coordinate""" return self.rect.x + @center_x.setter + def center_x(self, value: float): + self.rect = self.rect.align_center_x(value) + @property def center_y(self) -> float: """Center y coordinate""" return self.rect.y + @center_y.setter + def center_y(self, value: float): + self.rect = self.rect.align_center_y(value) + @property def padding(self): """Returns padding as tuple (top, right, bottom, left)""" @@ -545,16 +569,35 @@ def width(self) -> float: """Width of the widget.""" return self.rect.width + @width.setter + def width(self, value: float): + if value <= 0: + raise ValueError("Width must be positive") + self.rect = LBWH(self.left, self.bottom, value, self.height) + @property def height(self) -> float: """Height of the widget.""" return self.rect.height + @height.setter + def height(self, value: float): + if value <= 0: + raise ValueError("Height must be positive") + self.rect = LBWH(self.left, self.bottom, self.width, value) + @property def size(self) -> Vec2: """Size of the widget.""" return Vec2(self.width, self.height) + @size.setter + def size(self, value): + width, height = value + if width <= 0 or height <= 0: + raise ValueError("Width and height must be positive") + self.rect = LBWH(self.left, self.bottom, width, height) + def center_on_screen(self: W) -> W: """Places this widget in the center of the current window. From 03f38093a6c64761c36e14345f72515e22bda22b Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Fri, 30 May 2025 13:17:00 +0200 Subject: [PATCH 2/3] Update arcade/gui/widgets/__init__.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- arcade/gui/widgets/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/gui/widgets/__init__.py b/arcade/gui/widgets/__init__.py index cd610327b9..b2695bd171 100644 --- a/arcade/gui/widgets/__init__.py +++ b/arcade/gui/widgets/__init__.py @@ -592,7 +592,7 @@ def size(self) -> Vec2: return Vec2(self.width, self.height) @size.setter - def size(self, value): + def size(self, value: Tuple[float, float] | Vec2): width, height = value if width <= 0 or height <= 0: raise ValueError("Width and height must be positive") From 4efa6b003c7e9b85dffb24da2767e29ad3e0a596 Mon Sep 17 00:00:00 2001 From: Maic Siemering Date: Fri, 30 May 2025 13:20:36 +0200 Subject: [PATCH 3/3] fix --- arcade/gui/widgets/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/gui/widgets/__init__.py b/arcade/gui/widgets/__init__.py index b2695bd171..95881b48db 100644 --- a/arcade/gui/widgets/__init__.py +++ b/arcade/gui/widgets/__init__.py @@ -592,7 +592,7 @@ def size(self) -> Vec2: return Vec2(self.width, self.height) @size.setter - def size(self, value: Tuple[float, float] | Vec2): + def size(self, value: tuple[float, float] | Vec2): width, height = value if width <= 0 or height <= 0: raise ValueError("Width and height must be positive")