293
|
1 import pygame
|
|
2 from pygame.locals import SRCALPHA
|
|
3
|
|
4 from mamba.constants import COLOR, FOCUS_COLOR
|
|
5 from mamba.data import load_image
|
|
6 from mamba.widgets.base import Button
|
|
7 from mamba.widgets.text import TextWidget
|
|
8
|
|
9
|
|
10 class LevelButton(Button):
|
|
11
|
|
12 def __init__(self, rect, text, locked=True, done=False):
|
|
13 super(LevelButton, self).__init__(rect)
|
|
14 self.text = text
|
|
15 self.locked = locked
|
|
16 self.done = done
|
|
17 self.focussable = True
|
|
18 self.border = 3
|
|
19 self.rect.width = 50
|
|
20 self.rect.height = 50
|
|
21 self.prepare()
|
|
22
|
|
23 def prepare(self):
|
|
24 self.surface = pygame.Surface(self.rect.size, SRCALPHA)
|
|
25 self.surface.fill(0)
|
|
26
|
|
27 image = None
|
|
28 if self.locked:
|
|
29 image = load_image('menus/lock.png')
|
|
30 elif self.done:
|
|
31 image = load_image('menus/tick.png')
|
|
32 if image:
|
|
33 self.surface.blit(image, image.get_rect())
|
|
34
|
|
35 self._text = TextWidget((0, 0), self.text, fontsize=24)
|
|
36 self._text.prepare()
|
|
37
|
|
38 color = pygame.Color(FOCUS_COLOR if self.focussed else COLOR)
|
|
39 pygame.draw.rect(self.surface, color, self.surface.get_rect(),
|
|
40 self.border)
|
|
41 text_rect = pygame.Rect((0, 0), self.rect.size).inflate(
|
|
42 self._text.rect.width - self.rect.width,
|
|
43 self._text.rect.height - self.rect.height)
|
|
44 self.surface.blit(self._text.surface, text_rect)
|
|
45 self._state = (self.locked, self.done, self.focussed)
|
|
46
|
|
47 def draw(self, surface):
|
|
48 if self._state != (self.locked, self.done, self.focussed):
|
|
49 self.prepare()
|
|
50 surface.blit(self.surface, self.rect)
|