Optional sanity check for actions.
[naja.git] / naja / widgets / player_bits.py
1 """
2 Widget that holds the player's bits.
3 """
4
5 import pygame
6
7 from naja.constants import BIT_SIZE, BITS
8 from naja.resources import resources
9 from naja.resources.mutators import R90, R180, R270, EIGHT_BIT
10 from naja.widgets.base import Widget
11 from naja.widgets.text import TextWidget
12
13
14 class PlayerBitsWidget(Widget):
15     """
16     Widget which holds the player's bits.
17     """
18     def __init__(self, pos, state):
19         super(PlayerBitsWidget, self).__init__(pos, BIT_SIZE)
20         self.state = state
21
22     def prepare(self):
23         self.surface = pygame.Surface(BIT_SIZE)
24
25         size = BIT_SIZE[1]
26
27         zero = TextWidget((0, 0), '0', fontsize=24, colour='white')
28         one = TextWidget((0, 0), '1', fontsize=24, colour='white')
29         zero.prepare()
30         zero.pos = (0, (size - zero.size[1]))
31         one.pos = zero.pos
32
33         bits = (
34             (BITS.MSB, 'msb', ()),
35             (BITS.RED, 'red', ()),
36             (BITS.GREEN, 'green', ()),
37             (BITS.BLUE, 'blue', ()),
38             (BITS.WEST, 'arrow', (R90,)),
39             (BITS.EAST, 'arrow', (R270,)),
40             (BITS.SOUTH, 'arrow', (R180,)),
41             (BITS.NORTH, 'arrow', ()),
42         )
43         for pos, (bit, image, transforms) in enumerate(bits):
44             is_set = self.state.player.bits.check_bit(bit)
45             img = resources.get_image(
46                 'bits', '%s_%s.png' % (image, 'on' if is_set else 'off'),
47                 transforms=transforms + (EIGHT_BIT,))
48             self.surface.blit(img, (size * pos, 0))
49             if is_set:
50                 one.render(self.surface)
51             else:
52                 zero.render(self.surface)
53             one.pos = zero.pos = (zero.pos[0] + size, zero.pos[1])
54
55     def draw(self, surface):
56         surface.blit(self.surface, self.pos)