11from __future__ import annotations
22
33from abc import ABC
4- from random import randint
54from typing import NamedTuple , Iterable , Optional , Union , TYPE_CHECKING , TypeVar , Tuple , List , Dict
65
76from pyglet .event import EventDispatcher , EVENT_HANDLED , EVENT_UNHANDLED
2221from arcade .gui .nine_patch import NinePatchTexture
2322from arcade .gui .property import Property , bind , ListProperty
2423from arcade .gui .surface import Surface
25- from arcade .types import RGBA255 , Color , AnchorPoint , AsFloat
24+ from arcade .types import Color , AnchorPoint , AsFloat
2625from arcade .utils import copy_dunders_unimplemented
2726
2827if TYPE_CHECKING :
@@ -389,7 +388,7 @@ def resize(self, *, width=None, height=None, anchor: Vec2 = AnchorPoint.CENTER):
389388 """
390389 self .rect = self .rect .resize (width = width , height = height , anchor = anchor )
391390
392- def with_border (self , * , width = 2 , color = arcade .color .GRAY ) -> Self :
391+ def with_border (self , * , width = 2 , color : Color | None = arcade .color .GRAY ) -> Self :
393392 """Sets border properties
394393
395394 Args:
@@ -526,7 +525,6 @@ class UIInteractiveWidget(UIWidget):
526525 size_hint_max: max width and height in pixel
527526 interaction_buttons: defines, which mouse buttons should trigger
528527 the interaction (default: left mouse button)
529- style: not used
530528 """
531529
532530 # States
@@ -616,8 +614,9 @@ def on_click(self, event: UIOnClickEvent):
616614
617615
618616class UIDummy (UIInteractiveWidget ):
619- """Solid color widget used for testing & examples
617+ """Solid color widget used for testing & examples.
620618
619+ Starts with a random color.
621620 It should not be subclassed for real-world usage.
622621
623622 When clicked, it does the following:
@@ -628,14 +627,13 @@ class UIDummy(UIInteractiveWidget):
628627 Args:
629628 x: x coordinate of bottom left
630629 y: y coordinate of bottom left
631- color: fill color for the widget
632630 width: width of widget
633631 height: height of widget
634632 size_hint: Tuple of floats (0.0-1.0), how much space of the
635633 parent should be requested
636634 size_hint_min: min width and height in pixel
637635 size_hint_max: max width and height in pixel
638- style: not used
636+ **kwargs: passed to UIWidget
639637 """
640638
641639 def __init__ (
@@ -658,25 +656,22 @@ def __init__(
658656 size_hint = size_hint ,
659657 size_hint_min = size_hint_min ,
660658 size_hint_max = size_hint_max ,
659+ ** kwargs ,
661660 )
662- self .color : RGBA255 = (randint (0 , 255 ), randint (0 , 255 ), randint (0 , 255 ), 255 )
663- self .border_color = arcade .color .BATTLESHIP_GREY
664- self .border_width = 0
661+ self .with_background (color = Color .random (a = 255 ))
662+ self .with_border (color = arcade .color .BATTLESHIP_GREY , width = 0 )
665663
666664 def on_click (self , event : UIOnClickEvent ):
667665 """Prints the rect and changes the color"""
668666 print ("UIDummy.rect:" , self .rect )
669- self .color = Color .random (a = 255 )
667+ self .with_background ( color = Color .random (a = 255 ) )
670668
671669 def on_update (self , dt ):
672670 """Update the border of the widget if hovered"""
673- self .border_width = 2 if self .hovered else 0
674- self .border_color = arcade .color .WHITE if self .pressed else arcade .color .BATTLESHIP_GREY
675-
676- def do_render (self , surface : Surface ):
677- """Render solid color"""
678- self .prepare_render (surface )
679- surface .clear (self .color )
671+ self .with_border (
672+ width = 2 if self .hovered else 0 ,
673+ color = arcade .color .WHITE if self .pressed else arcade .color .BATTLESHIP_GREY ,
674+ )
680675
681676
682677class UISpriteWidget (UIWidget ):
@@ -692,7 +687,6 @@ class UISpriteWidget(UIWidget):
692687 parent should be requested
693688 size_hint_min: min width and height in pixel
694689 size_hint_max: max width and height in pixel
695- style: not used
696690 """
697691
698692 def __init__ (
@@ -748,7 +742,6 @@ class UILayout(UIWidget):
748742 parent should be requested
749743 size_hint_min: min width and height in pixel
750744 size_hint_max: max width and height in pixel
751- style: not used
752745 """
753746
754747 @staticmethod
@@ -808,7 +801,8 @@ class UISpace(UIWidget):
808801 y: y coordinate of bottom left
809802 width: width of widget
810803 height: height of widget
811- color: Color for widget area
804+ color: Color for widget area, if None, it will be transparent
805+ (this will set the background color)
812806 size_hint: Tuple of floats (0.0-1.0), how much space of the
813807 parent should be requested
814808 size_hint_min: min width and height in pixel
@@ -839,20 +833,13 @@ def __init__(
839833 size_hint_max = size_hint_max ,
840834 ** kwargs ,
841835 )
842- self ._color = color
836+ self .with_background ( color = color )
843837
844838 @property
845839 def color (self ):
846- """Color of the widget"""
847- return self ._color
840+ """Color of the widget, alias for background color """
841+ return self ._bg_color
848842
849843 @color .setter
850844 def color (self , value ):
851- self ._color = value
852- self .trigger_render ()
853-
854- def do_render (self , surface : Surface ):
855- """Render the widget, mainly the background color"""
856- self .prepare_render (surface )
857- if self ._color :
858- surface .clear (self ._color )
845+ self .with_background (color = value )
0 commit comments