view mamba/widgets/imagebutton.py @ 157:31881bf8ddda

Lockable text button
author Stefano Rivera <stefano@rivera.za.net>
date Tue, 13 Sep 2011 22:42:44 +0200
parents 77ea895e4d37
children e6a3b00f997b
line wrap: on
line source

import pygame
from pygame.locals import SRCALPHA

from mamba.constants import COLOR, FONT_SIZE, FOCUS_COLOR
from mamba.data import load_image
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=FONT_SIZE, color=COLOR):
        self.image = image
        self.focus_color = pygame.Color(FOCUS_COLOR)
        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.surface
        # 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, SRCALPHA)
        self.surface.fill(0)
        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)


class LockableTextButton(Button):

    def __init__(self, rect, text, locked=False, fontsize=FONT_SIZE,
                 color=COLOR):
        super(LockableTextButton, self).__init__(rect)
        self.text = text
        self.locked = locked
        self.fontsize = fontsize
        self.color = color
        self.focus_color = pygame.Color(FOCUS_COLOR)
        self.background = 0
        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 not isinstance(self.color, pygame.Color):
            self.color = pygame.Color(self.color)

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

        color = self.focus_color if self.focussed else self.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)
        if self.locked:
            lock = load_image('menus/lock.png')
            self.surface.blit(lock, lock.get_rect())
        self._state = (self.locked, self.focussed)

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