Mercurial > sypikslang
view gamelib/gui.py @ 228:3955d126ca26
merge
author | Rizmari Versfeld <rizziepit@gmail.com> |
---|---|
date | Sat, 12 May 2012 23:09:33 +0200 |
parents | 69941f04f1c0 |
children |
line wrap: on
line source
import pygame from gamelib import data from gamelib.gui_base import Drawable, TextButton, font_auto, ToggleButton class ImageDrawable(Drawable): def __init__(self, rect, image): super(ImageDrawable, self).__init__(rect) self.image = image def draw(self, surface): surface.blit(self.image, (self.rect[0], self.rect[1])) class BigButton(TextButton): WIDTH = 128 HEIGHT = 64 BG_IMAGE_NORMAL = data.load_image('images/button_normal.png') BG_IMAGE_DOWN = data.load_image('images/button_down.png') def __init__(self, pos, text, font=font_auto, shadow=True): rect1 = (0, 0, self.WIDTH, self.HEIGHT) n = ImageDrawable(rect1, self.BG_IMAGE_NORMAL) d = ImageDrawable(rect1, self.BG_IMAGE_DOWN) rect2 = (pos[0], pos[1], self.WIDTH, self.HEIGHT) super(BigButton, self).__init__(rect2, n, d, text, font, shadow) class IconButton(ToggleButton): WIDTH = 64 HEIGHT = 64 BG_IMAGE_NORMAL = data.load_image('images/research_normal.png') BG_IMAGE_DOWN = data.load_image('images/research_down.png') def __init__(self, pos, name): rect = (pos[0], pos[1], self.WIDTH, self.HEIGHT) try: n_icon = data.load_image('images/icons/%s_normal.png' % name) d_icon = data.load_image('images/icons/%s_down.png' % name) except: n_icon = pygame.Surface((64, 64), pygame.SRCALPHA) d_icon = pygame.Surface((64, 64), pygame.SRCALPHA) n = ImageDrawable(rect, self.BG_IMAGE_NORMAL.copy()) n.image.blit(n_icon, (0, 0)) d = ImageDrawable(rect, self.BG_IMAGE_DOWN.copy()) d.image.blit(d_icon, (0, 0)) rect2 = (pos[0], pos[1], self.WIDTH, self.HEIGHT) super(IconButton, self).__init__(rect2, n, d) class IconTextButton(TextButton): WIDTH = 198 HEIGHT = 73 TEXT_OFFSET = (0, 58) def __init__(self, pos, name, text, font=font_auto, shadow=True): rect = (pos[0], pos[1], self.WIDTH, self.HEIGHT) n = self._mk_image(name, 'normal') d = self._mk_image(name, 'down') text_rect = (self.TEXT_OFFSET, (self.WIDTH - self.TEXT_OFFSET[0], self.HEIGHT - self.TEXT_OFFSET[1])) super(IconTextButton, self).__init__( rect, n, d, text, font, shadow, text_rect, (255, 255, 255), (128, 128, 128)) def _mk_image(self, name, suffix): bg = data.load_image('images/research_%s.png' % (suffix,)) try: icon = data.load_image('images/icons/%s_%s.png' % (name, suffix)) except pygame.error: print('Unable to load %s' % data.filepath('images/icons/%s_%s.png' % (name, suffix))) icon = pygame.Surface((64, 64), pygame.SRCALPHA) base = pygame.Surface((self.WIDTH, self.HEIGHT), pygame.SRCALPHA) base.blit(bg, (self.WIDTH / 2 - 32, 0)) drawable = ImageDrawable((0, 0, self.WIDTH, self.HEIGHT), base) # Centre icon drawable.image.blit(icon, (self.WIDTH / 2 - 32, 0)) return drawable class RadioButton(IconButton): SELECTED_BUTTON = None def on_click(self): print(RadioButton.CHECK) super(RadioButton, self).on_click() if self.toggled: if RadioButton.SELECTED_BUTTON: RadioButton.SELECTED_BUTTON.on_click() RadioButton.SELECTED_BUTTON = self print("I am selected") else: RadioButton.SELECTED_BUTTON = None print("I am deselected")