1 """Basic widgets and various utility functions.
12 from .errors import PguError
18 # Turns a descriptive string or a tuple into a pygame color
19 def parse_color(desc):
23 elif (desc and desc[0] == "#"):
24 # Because of a bug in pygame 1.8.1 we need to explicitly define the
25 # alpha value otherwise it will default to transparent.
28 return pygame.Color(desc)
30 # Determines if the given object is a pygame-compatible color or not
32 # In every version of pygame (up to 1.8.1 so far) will interpret
34 if (type(col) == tuple or type(col) == list):
36 if (hasattr(pygame, "Color") and type(pygame.Color) == type):
37 # This is a recent version of pygame that uses a proper type
38 # instance for colors.
39 return (isinstance(col, pygame.Color))
40 # Otherwise, this version of pygame only supports tuple colors
47 class Spacer(widget.Widget):
48 """An invisible space widget."""
50 def __init__(self,width,height,**params):
51 params.setdefault('focusable',False)
52 widget.Widget.__init__(self,width=width,height=height,**params)
55 class Color(widget.Widget):
56 """A widget that renders as a solid block of color.
58 Note the color can be changed by setting the 'value' field, and the
59 widget will automatically be repainted, eg:
67 # The pygame Color instance
70 def __init__(self,value=None,**params):
71 params.setdefault('focusable',False)
72 if value != None: params['value']=value
73 widget.Widget.__init__(self,**params)
76 if hasattr(self,'value') and is_color(self.value):
85 if (isinstance(val, basestring)):
86 # Parse the string as a color
87 val = parse_color(val)
91 # Emit a change signal
96 class Label(widget.Widget):
97 """A text label widget."""
99 def __init__(self, value="", **params):
100 params.setdefault('focusable', False)
101 params.setdefault('cls', 'label')
102 widget.Widget.__init__(self, **params)
103 self.style.check("font")
105 self.font = self.style.font
106 self.style.width, self.style.height = self.font.size(self.value)
109 """Renders the label onto the given surface in the upper-left corner."""
110 s.blit(self.font.render(self.value, 1, self.style.color),(0,0))
112 def set_text(self, txt):
113 """Set the text of this label."""
115 # Signal to the application that we need to resize this widget
118 def set_font(self, font):
119 """Set the font used to render this label."""
121 # Signal to the application that we need a resize
124 def resize(self,width=None,height=None):
125 # Calculate the size of the rendered text
126 (self.style.width, self.style.height) = self.font.size(self.value)
127 return (self.style.width, self.style.height)
130 class Image(widget.Widget):
131 """An image widget. The constructor takes a file name or a pygame surface."""
133 def __init__(self,value,**params):
134 params.setdefault('focusable',False)
135 widget.Widget.__init__(self,**params)
138 raise PguError("Image widget takes a path or pygame surface as first argument")
140 if (isinstance(value, basestring)):
141 # Assume the argument is a path
142 value = pygame.image.load(value)
144 raise PguError("Cannot load the image '%s'" % value)
146 ow,oh = iw,ih = value.get_width(),value.get_height()
147 sw,sh = self.style.width,self.style.height
156 if (ow,oh) != (iw,ih):
157 value = pygame.transform.scale(value,(iw,ih))
158 self.style.width,self.style.height = iw,ih
162 s.blit(self.value,(0,0))