Mercurial > mamba
changeset 18:a0604a61762e
More widget API
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 11 Sep 2011 13:49:03 +0200 |
parents | 236de980209a |
children | 6d195a3a4557 |
files | mamba/widgets/__init__.py mamba/widgets/text.py |
diffstat | 2 files changed, 51 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/widgets/__init__.py Sun Sep 11 13:46:30 2011 +0200 +++ b/mamba/widgets/__init__.py Sun Sep 11 13:49:03 2011 +0200 @@ -1,12 +1,38 @@ +import pygame + class Widget(object): - def __init__(self, pos): + + def __init__(self, rect): + self.rect = pygame.Rect(rect) + + def do_event(self, event): + "Override me" + pass + + def do_draw(self, surface): + "Override me" pass - def dispatch(self, event): - pass +class Container(object): + + def __init__(self): + self.children = [] + + def event(self, event): + for child in self.children: + # TODO mouse events + if issubclass(child, Container): + child.event(event) + else: + child.do_event(event) def add(self, widget): - pass + self.children.append(widget) def draw(self, surface): - surface.blit(self.surface, self.pos) + self.do_draw(surface) + for child in self.children: + if issubclass(child, Container): + child.draw(surface) + else: + child.do_draw(surface)
--- a/mamba/widgets/text.py Sun Sep 11 13:46:30 2011 +0200 +++ b/mamba/widgets/text.py Sun Sep 11 13:49:03 2011 +0200 @@ -5,12 +5,24 @@ class Text(Widget): fontcache = {} - def __init__(self, text, pos, size=16, color='black'): + def __init__(self, rect, text, fontsize=16, color='black'): + super(Widget, self).__init(rect) self.text = text - self.pos = pos - font = 'DejaVuSans.ttf' - if (font, size) not in Text.fontcache: - fontfn = filepath('fonts/' + font) - Text.fontcache[(font, size)] = pygame.font.Font(fontfn, size) - self.font = Text.fontcache[(font, size)] - self.surface = self.font.render(text, True, color) + self.fontsize = fontsize + self.color = color + self.prepare() + + def prepare(self): + self.fontname = 'DejaVuSans.ttf' + font = (self.fontname, self.fontsize) + if font not in Text.fontcache: + fontfn = filepath('fonts/' + self.fontname) + Text.fontcache[font] = pygame.font.Font(fontfn, self.fontsize) + self.font = Text.fontcache[font] + if not isinstance(self.color, pygame.Color): + self.color = pygame.Color(self.color) + self.surface = self.font.render(self.text, True, self.color) + self.rect = self.surface.get_rect() + + def do_draw(self, surface): + surface.blit(self.surface, self.pos)