view gamelib/gui.py @ 143:821ecb98e888

some icons and toggle button
author Rizmari Versfeld <rizziepit@gmail.com>
date Fri, 11 May 2012 03:13:22 +0200
parents 3e02a8ccd72b
children 53277724645b
line wrap: on
line source

from pygame import image

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 = image.load(data.filepath('images/button_normal.png'))
    BG_IMAGE_DOWN = image.load(data.filepath('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 = image.load(data.filepath('images/research_normal.png'))
    BG_IMAGE_DOWN = image.load(data.filepath('images/research_down.png'))

    def __init__(self, pos, name):
        rect = (pos[0], pos[1], self.WIDTH, self.HEIGHT)
        n_icon = image.load(data.filepath('images/icons/%s_normal.png' % name))
        d_icon = image.load(data.filepath('images/icons/%s_down.png' % name))
        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 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")