annotate mamba/widgets/imagebutton.py @ 93:8d073e402294

Add start of image button
author Neil Muller <drnlmuller@gmail.com>
date Sun, 11 Sep 2011 19:15:58 +0200
parents
children 45397addd80d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 import pygame
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from mamba.widgets.text import TextWidget
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 class ImageButtonWidget(TextWidget):
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 """Text label with image on the left"""
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 def __init__(self, rect, image, text, fontsize=16, color='black'):
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10 self.image = image
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 super(ImageButtonWidget, self).__init__(rect, text, fontsize, color)
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 def prepare(self):
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 super(ImageButtonWidget, self).prepare()
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 text_surface = self.font.render(self.text, True, self.color)
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16 # Image is already a surface
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 self.rect.width = text_surface.get_width() + self.image.get_width() + 5
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18 self.rect.height = text_surface.get_height() + self.image.get_height()
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 self.surface = pygame.Surface((self.rect.width, self.rect.height))
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 self.surface.blit(self.image, (0, 0))
8d073e402294 Add start of image button
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 self.surface.blit(text_surface, (self.image.get_width() + 5, 0))