Skip to content

Commit 8463150

Browse files
committed
gui: enhance UIWidget usability with intuitive property setters for position and size
1 parent 3501dbd commit 8463150

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page.
99
- Added `Window.close` (bool) attribute indicating if the window is closed
1010
- GUI
1111
- Fix `UILabel` with enabled multiline sometimes cut off text
12+
- Improved `UIWidget` usability for resizing and positioning:
13+
- Added property setters for `width`, `height`, and `size` that ensure positive values
14+
- Added property setters for `center_x` and `center_y`
15+
- Added property setters for `left`, `right`, `top`, and `bottom`
16+
- Users can now set widget position and size more intuitively without needing to access the `rect` property
1217

1318
## Version 3.2
1419

arcade/gui/widgets/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,37 @@ def left(self) -> float:
361361
"""Left coordinate of the widget"""
362362
return self.rect.left
363363

364+
@left.setter
365+
def left(self, value: float):
366+
self.rect = LBWH(value, self.bottom, self.width, self.height)
367+
364368
@property
365369
def right(self) -> float:
366370
"""Right coordinate of the widget"""
367371
return self.rect.right
368372

373+
@right.setter
374+
def right(self, value: float):
375+
self.rect = LBWH(value - self.width, self.bottom, self.width, self.height)
376+
369377
@property
370378
def bottom(self) -> float:
371379
"""Bottom coordinate of the widget"""
372380
return self.rect.bottom
373381

382+
@bottom.setter
383+
def bottom(self, value: float):
384+
self.rect = LBWH(self.left, value, self.width, self.height)
385+
374386
@property
375387
def top(self) -> float:
376388
"""Top coordinate of the widget"""
377389
return self.rect.top
378390

391+
@top.setter
392+
def top(self, value: float):
393+
self.rect = LBWH(self.left, value - self.height, self.width, self.height)
394+
379395
@property
380396
def position(self) -> Vec2:
381397
"""Returns bottom left coordinates"""
@@ -395,11 +411,19 @@ def center_x(self) -> float:
395411
"""Center x coordinate"""
396412
return self.rect.x
397413

414+
@center_x.setter
415+
def center_x(self, value: float):
416+
self.rect = self.rect.align_center_x(value)
417+
398418
@property
399419
def center_y(self) -> float:
400420
"""Center y coordinate"""
401421
return self.rect.y
402422

423+
@center_y.setter
424+
def center_y(self, value: float):
425+
self.rect = self.rect.align_center_y(value)
426+
403427
@property
404428
def padding(self):
405429
"""Returns padding as tuple (top, right, bottom, left)"""
@@ -545,16 +569,35 @@ def width(self) -> float:
545569
"""Width of the widget."""
546570
return self.rect.width
547571

572+
@width.setter
573+
def width(self, value: float):
574+
if value <= 0:
575+
raise ValueError("Width must be positive")
576+
self.rect = LBWH(self.left, self.bottom, value, self.height)
577+
548578
@property
549579
def height(self) -> float:
550580
"""Height of the widget."""
551581
return self.rect.height
552582

583+
@height.setter
584+
def height(self, value: float):
585+
if value <= 0:
586+
raise ValueError("Height must be positive")
587+
self.rect = LBWH(self.left, self.bottom, self.width, value)
588+
553589
@property
554590
def size(self) -> Vec2:
555591
"""Size of the widget."""
556592
return Vec2(self.width, self.height)
557593

594+
@size.setter
595+
def size(self, value):
596+
width, height = value
597+
if width <= 0 or height <= 0:
598+
raise ValueError("Width and height must be positive")
599+
self.rect = LBWH(self.left, self.bottom, width, height)
600+
558601
def center_on_screen(self: W) -> W:
559602
"""Places this widget in the center of the current window.
560603

0 commit comments

Comments
 (0)