view mamba/widgets/levelbutton.py @ 314:fba74abad7b7

Pass Level() objects to LevelHabitat and LevelEditorHabitat, in preparation for generating thumbnails from levels in levelmenu
author Stefano Rivera <stefano@rivera.za.net>
date Fri, 16 Sep 2011 19:07:51 +0200
parents b371c2ea309e
children 86c5c6afdac0
line wrap: on
line source

import pygame
from pygame.locals import SRCALPHA

from mamba.constants import COLOR, FOCUS_COLOR
from mamba.data import load_image
from mamba.widgets.base import Button
from mamba.widgets.text import TextWidget


class LevelButton(Button):

    def __init__(self, rect, level, done=False):
        super(LevelButton, self).__init__(rect)
        self.level = level
        self.text = level.name
        self.done = done
        self.focussable = True
        self.border = 3
        self.rect.width = 50
        self.rect.height = 50
        self.prepare()

    def prepare(self):
        self.surface = pygame.Surface(self.rect.size, SRCALPHA)
        self.surface.fill(0)

        if self.done:
            image = load_image('menus/tick.png')
            self.surface.blit(image, image.get_rect())

        self._text = TextWidget((0, 0), self.text, fontsize=24)
        self._text.prepare()

        color = pygame.Color(FOCUS_COLOR if self.focussed else COLOR)
        pygame.draw.rect(self.surface, color, self.surface.get_rect(),
                         self.border)
        text_rect = pygame.Rect((0, 0), self.rect.size).inflate(
                self._text.rect.width - self.rect.width,
                self._text.rect.height - self.rect.height)
        self.surface.blit(self._text.surface, text_rect)
        self._state = (self.done, self.focussed)

    def draw(self, surface):
        if self._state != (self.done, self.focussed):
            self.prepare()
        surface.blit(self.surface, self.rect)