Mercurial > mamba
changeset 103:45397addd80d
Make image button behave a bit more like text button
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 19:50:58 +0200 |
parents | 59ccbc1539db |
children | 5b92ff941de3 |
files | mamba/habitats/editor.py mamba/widgets/imagebutton.py |
diffstat | 2 files changed, 22 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/habitats/editor.py Sun Sep 11 19:45:12 2011 +0200 +++ b/mamba/habitats/editor.py Sun Sep 11 19:50:58 2011 +0200 @@ -62,8 +62,3 @@ (button_left, button_height), self.level.tileset.floor, 'Floor', color='white') self.container.add(floor_button) - - - - -
--- a/mamba/widgets/imagebutton.py Sun Sep 11 19:45:12 2011 +0200 +++ b/mamba/widgets/imagebutton.py Sun Sep 11 19:50:58 2011 +0200 @@ -8,14 +8,31 @@ def __init__(self, rect, image, text, fontsize=16, color='black'): self.image = image + self.focus_color = pygame.Color('yellow') + self.padding = 5 + self.border = 2 super(ImageButtonWidget, self).__init__(rect, text, fontsize, color) + self.focussable = True def prepare(self): super(ImageButtonWidget, self).prepare() text_surface = self.font.render(self.text, True, self.color) # Image is already a surface - self.rect.width = text_surface.get_width() + self.image.get_width() + 5 - self.rect.height = text_surface.get_height() + self.image.get_height() - self.surface = pygame.Surface((self.rect.width, self.rect.height)) - self.surface.blit(self.image, (0, 0)) - self.surface.blit(text_surface, (self.image.get_width() + 5, 0)) + self._focussed = self.focussed + color = self.focus_color if self.focussed else self.color + + self.rect.width = text_surface.get_width() + self.image.get_width() \ + + 5 + self.padding * 2 + self.rect.height = max(text_surface.get_height(), + self.image.get_height()) + self.padding * 2 + self.surface = pygame.Surface(self.rect.size) + self.surface.blit(self.image, (self.padding, self.padding)) + self.surface.blit(text_surface, + (self.image.get_width() + 5 + self.padding, self.padding)) + pygame.draw.rect(self.surface, color, self.surface.get_rect(), + self.border) + + def draw(self, surface): + if self._focussed != self.focussed: + self.prepare() + super(ImageButtonWidget, self).draw(surface)