Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 43 additions & 0 deletions arcade/gui/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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)"""
Expand Down Expand Up @@ -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: tuple[float, float] | Vec2):
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.

Expand Down
Loading