Mercurial > mamba
view mamba/widgets/imagebutton.py @ 115:d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 11 Sep 2011 20:40:26 +0200 |
parents | 45397addd80d |
children | 9afea431af36 |
line wrap: on
line source
import pygame from mamba.widgets.base import Button from mamba.widgets.text import TextWidget class ImageButtonWidget(Button, TextWidget): """Text label with image on the left""" 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._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)