comparison pyntnclick/widgets/base.py @ 813:3a875256f795 pyntnclick

better visible handling
author Neil Muller <neil@dip.sun.ac.za>
date Sun, 27 Jan 2013 17:33:04 +0200
parents bcc9277a23e6
children 9f542ef6e498
comparison
equal deleted inserted replaced
812:2dd400a7c16d 813:3a875256f795
19 self.size = size 19 self.size = size
20 self.rect = pygame.Rect(pos, size if size else (0, 0)) 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.visible = True
24 self.callbacks = collections.defaultdict(list) 25 self.callbacks = collections.defaultdict(list)
25 # To track which widget the mouse is over 26 # To track which widget the mouse is over
26 self.mouseover_widget = self 27 self.mouseover_widget = self
27 self.is_prepared = False 28 self.is_prepared = False
28 29
32 def add_callback(self, eventtype, callback, *args): 33 def add_callback(self, eventtype, callback, *args):
33 self.callbacks[eventtype].append((callback, args)) 34 self.callbacks[eventtype].append((callback, args))
34 35
35 def event(self, ev): 36 def event(self, ev):
36 "Don't override this without damn good reason" 37 "Don't override this without damn good reason"
37 if self.disabled: 38 if self.disabled or not self.visible:
38 return True 39 return True
39 40
40 type_ = ev.type 41 type_ = ev.type
41 if type_ == USEREVENT: 42 if type_ == USEREVENT:
42 for k in self.callbacks.iterkeys(): 43 for k in self.callbacks.iterkeys():
70 self.is_prepared = True 71 self.is_prepared = True
71 72
72 def enable(self): 73 def enable(self):
73 if self.disabled: 74 if self.disabled:
74 self.disabled = False 75 self.disabled = False
76 self.prepare()
77 self.is_prepared = True
78
79 def set_visible(self, visible):
80 if self.visible != visible:
81 self.visible = visible
75 self.prepare() 82 self.prepare()
76 self.is_prepared = True 83 self.is_prepared = True
77 84
78 def global_to_local(self, pos): 85 def global_to_local(self, pos):
79 x, y = pos 86 x, y = pos
143 def remove_all(self): 150 def remove_all(self):
144 for widget in reversed(self.children[:]): 151 for widget in reversed(self.children[:]):
145 self.remove(widget) 152 self.remove(widget)
146 153
147 def draw(self, surface): 154 def draw(self, surface):
148 self.do_prepare() 155 if self.visible:
149 for child in self.children: 156 self.do_prepare()
150 child.draw(surface) 157 for child in self.children:
158 child.draw(surface)
151 159
152 160
153 class ModalStackContainer(Container): 161 class ModalStackContainer(Container):
154 162
155 def __init__(self, pos, gd, size, obscure_color=None): 163 def __init__(self, pos, gd, size, obscure_color=None):
179 187
180 def is_top(self, widget): 188 def is_top(self, widget):
181 return self.top is widget 189 return self.top is widget
182 190
183 def draw(self, surface): 191 def draw(self, surface):
184 self.do_prepare() 192 if self.visible:
185 obscure = pygame.Surface(self.rect.size, SRCALPHA) 193 self.do_prepare()
186 obscure.fill(self.obscure_color) 194 obscure = pygame.Surface(self.rect.size, SRCALPHA)
187 for child in self.children: 195 obscure.fill(self.obscure_color)
188 surface.blit(obscure, self.rect) 196 for child in self.children:
189 child.draw(surface) 197 surface.blit(obscure, self.rect)
198 child.draw(surface)
190 199
191 200
192 class Box(Container): 201 class Box(Container):
193 """A container that draws a filled background with a border""" 202 """A container that draws a filled background with a border"""
194 padding = 4 203 padding = 4
195 204
196 def draw(self, surface): 205 def draw(self, surface):
197 self.do_prepare() 206 if self.visible:
198 # TODO: Why isn't this done in prepare? 207 self.do_prepare()
199 expandrect = self.rect.move((-self.padding, -self.padding)) 208 # TODO: Why isn't this done in prepare?
200 expandrect.width = self.rect.width + 2 * self.padding 209 expandrect = self.rect.move((-self.padding, -self.padding))
201 expandrect.height = self.rect.height + 2 * self.padding 210 expandrect.width = self.rect.width + 2 * self.padding
202 border = pygame.Surface(expandrect.size, SRCALPHA) 211 expandrect.height = self.rect.height + 2 * self.padding
203 border.fill(pygame.Color('black')) 212 border = pygame.Surface(expandrect.size, SRCALPHA)
204 surface.blit(border, expandrect) 213 border.fill(pygame.Color('black'))
205 background = pygame.Surface(self.rect.size, SRCALPHA) 214 surface.blit(border, expandrect)
206 background.fill(pygame.Color('gray')) 215 background = pygame.Surface(self.rect.size, SRCALPHA)
207 surface.blit(background, self.rect) 216 background.fill(pygame.Color('gray'))
208 super(Box, self).draw(surface) 217 surface.blit(background, self.rect)
218 super(Box, self).draw(surface)
209 219
210 220
211 def convert_color(color): 221 def convert_color(color):
212 """Give me a pygame Color, dammit""" 222 """Give me a pygame Color, dammit"""
213 if isinstance(color, pygame.Color): 223 if isinstance(color, pygame.Color):