comparison pyntnclick/widgets/base.py @ 803:bcc9277a23e6 pyntnclick

Refactor widget positioning API. Remove unused widgets
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 27 Jan 2013 14:52:16 +0200
parents efa58c92b304
children 3a875256f795
comparison
equal deleted inserted replaced
802:5ec7905b2365 803:bcc9277a23e6
10 10
11 class Widget(object): 11 class Widget(object):
12 12
13 highlight_cursor = False 13 highlight_cursor = False
14 14
15 def __init__(self, rect, gd): 15 def __init__(self, pos, gd, size):
16 if not isinstance(rect, pygame.Rect): 16 self.pos = pos
17 rect = pygame.Rect(rect, (0, 0))
18 self.rect = rect
19 self.gd = gd 17 self.gd = gd
20 self.resource = gd.resource 18 self.resource = gd.resource
19 self.size = size
20 self.rect = pygame.Rect(pos, size if size else (0, 0))
21 self.modal = False 21 self.modal = False
22 self.parent = None 22 self.parent = None
23 self.disabled = False 23 self.disabled = False
24 self.callbacks = collections.defaultdict(list) 24 self.callbacks = collections.defaultdict(list)
25 # To track which widget the mouse is over 25 # To track which widget the mouse is over
101 return False 101 return False
102 102
103 103
104 class Container(Widget): 104 class Container(Widget):
105 105
106 def __init__(self, rect, gd): 106 def __init__(self, pos, gd, size=None):
107 if rect is None: 107 super(Container, self).__init__(pos, gd, size)
108 rect = pygame.Rect(0, 0, 0, 0)
109 super(Container, self).__init__(rect, gd)
110 self.children = [] 108 self.children = []
111 109
112 def event(self, ev): 110 def event(self, ev):
113 """Push an event down through the tree, and fire our own event as a 111 """Push an event down through the tree, and fire our own event as a
114 last resort 112 last resort
132 130
133 def add(self, widget): 131 def add(self, widget):
134 widget.set_parent(self) 132 widget.set_parent(self)
135 widget.prepare() 133 widget.prepare()
136 self.children.append(widget) 134 self.children.append(widget)
137 self.rect = self.rect.union(widget.rect) 135 if not self.size:
136 self.rect = self.rect.union(widget.rect)
138 return widget 137 return widget
139 138
140 def remove(self, widget): 139 def remove(self, widget):
141 widget.set_parent(None) 140 widget.set_parent(None)
142 self.children.remove(widget) 141 self.children.remove(widget)
151 child.draw(surface) 150 child.draw(surface)
152 151
153 152
154 class ModalStackContainer(Container): 153 class ModalStackContainer(Container):
155 154
156 def __init__(self, rect, gd, obscure_color=None): 155 def __init__(self, pos, gd, size, obscure_color=None):
157 super(ModalStackContainer, self).__init__(rect, gd) 156 super(ModalStackContainer, self).__init__(pos, gd, size)
158 if obscure_color is None: 157 if obscure_color is None:
159 obscure_color = gd.constants.modal_obscure_color 158 obscure_color = gd.constants.modal_obscure_color
160 self.obscure_color = convert_color(obscure_color) 159 self.obscure_color = convert_color(obscure_color)
161 160
162 @property 161 @property
188 for child in self.children: 187 for child in self.children:
189 surface.blit(obscure, self.rect) 188 surface.blit(obscure, self.rect)
190 child.draw(surface) 189 child.draw(surface)
191 190
192 191
193 class GridContainer(Container):
194 """Hacky container that only supports grids, won't work with Container
195 children, or modal children.
196 """
197
198 def __init__(self, width, rect=None):
199 super(GridContainer, self).__init__(rect)
200 self.width = width
201
202 def add(self, widget):
203 assert not isinstance(widget, Container)
204 assert not widget.modal
205 super(GridContainer, self).add(widget)
206
207
208 class Box(Container): 192 class Box(Container):
209 """A container that draws a filled background with a border""" 193 """A container that draws a filled background with a border"""
210 padding = 4 194 padding = 4
211 195
212 def draw(self, surface): 196 def draw(self, surface):
213 self.do_prepare() 197 self.do_prepare()
198 # TODO: Why isn't this done in prepare?
214 expandrect = self.rect.move((-self.padding, -self.padding)) 199 expandrect = self.rect.move((-self.padding, -self.padding))
215 expandrect.width = self.rect.width + 2 * self.padding 200 expandrect.width = self.rect.width + 2 * self.padding
216 expandrect.height = self.rect.height + 2 * self.padding 201 expandrect.height = self.rect.height + 2 * self.padding
217 border = pygame.Surface(expandrect.size, SRCALPHA) 202 border = pygame.Surface(expandrect.size, SRCALPHA)
218 border.fill(pygame.Color('black')) 203 border.fill(pygame.Color('black'))
234 219
235 class ModalWrapper(Container): 220 class ModalWrapper(Container):
236 "A wrapper around a widget that removes itself when a mouse click occurs" 221 "A wrapper around a widget that removes itself when a mouse click occurs"
237 222
238 def __init__(self, widget, close_callback=None): 223 def __init__(self, widget, close_callback=None):
239 super(ModalWrapper, self).__init__(widget.rect, widget.gd) 224 super(ModalWrapper, self).__init__(widget.rect.topleft, widget.gd,
225 widget.rect.size)
240 self.close_callback = close_callback 226 self.close_callback = close_callback
241 self.add(widget) 227 self.add(widget)
242 self.add_callback(MOUSEBUTTONDOWN, self.close) 228 self.add_callback(MOUSEBUTTONDOWN, self.close)
243 widget.add_callback(MOUSEBUTTONDOWN, self.close) 229 widget.add_callback(MOUSEBUTTONDOWN, self.close)
244
245 def set_parent(self, parent):
246 super(ModalWrapper, self).set_parent(parent)
247 if parent:
248 self.rect = self.parent.rect
249 230
250 def close(self, ev, widget): 231 def close(self, ev, widget):
251 if self.parent: 232 if self.parent:
252 self.parent.remove(self) 233 self.parent.remove(self)
253 if self.close_callback: 234 if self.close_callback:
256 237
257 238
258 class Image(Widget): 239 class Image(Widget):
259 """Basic widget that draws an image, with an associated rect""" 240 """Basic widget that draws an image, with an associated rect"""
260 241
261 def __init__(self, rect, gd, image): 242 def __init__(self, pos, gd, image, size=None):
262 super(Image, self).__init__(rect, gd) 243 super(Image, self).__init__(pos, gd, size)
263 self.image = image 244 self.image = image
264 self.rect.width = image.get_rect().width 245 if not size:
265 self.rect.height = image.get_rect().height 246 self.rect.size = image.get_rect().size
266 self.visible = True 247 self.visible = True
267 248
268 def draw(self, surface): 249 def draw(self, surface):
269 self.do_prepare() 250 self.do_prepare()
270 if self.visible: 251 if self.visible:
272 253
273 254
274 class TranslucentImage(Image): 255 class TranslucentImage(Image):
275 """Image that can also be translucent""" 256 """Image that can also be translucent"""
276 257
277 def __init__(self, rect, gd, image): 258 def __init__(self, pos, gd, image, size=None):
278 super(TranslucentImage, self).__init__(rect, gd, image) 259 super(TranslucentImage, self).__init__(pos, gd, image, size)
279 self.translucent = False 260 self.translucent = False
280 surf = pygame.surface.Surface((self.rect.width, self.rect.height), 261 surf = pygame.surface.Surface((self.rect.width, self.rect.height),
281 SRCALPHA).convert_alpha() 262 SRCALPHA).convert_alpha()
282 surf.fill(pygame.color.Color(255, 255, 255, 96)) 263 surf.fill(pygame.color.Color(255, 255, 255, 96))
283 surf.blit(self.image, (0, 0), None, BLEND_RGBA_MIN) 264 surf.blit(self.image, (0, 0), None, BLEND_RGBA_MIN)