Draw tile backgrounds
[naja.git] / naja / widgets / tile.py
1 # These will probably need to go away when we have images
2 import pygame
3 import pygame.locals as pgl
4
5 from naja.constants import TILE_SIZE, BITS
6 from naja.resources import resources
7 from naja.resources.mutators import EIGHT_BIT
8 from naja.widgets.base import Widget
9
10
11 BIT_MAP = {
12     BITS.CYAN: 'board/tile_cyan.png',
13     BITS.MAGENTA: 'board/tile_magenta.png',
14     BITS.YELLOW: 'board/tile_yellow.png',
15     }
16
17
18 class TileWidget(Widget):
19     """Widget which holds a tile on the game board."""
20     def __init__(self, pos, state=None, board_pos=None):
21         super(TileWidget, self).__init__(pos, TILE_SIZE)
22         self.state = state
23         self.board_pos = board_pos
24
25     def prepare(self):
26         # Draw background
27         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
28         if (x == 1 and y in [0, 1]) or (y == 1 and x in [0, 1]):
29             # Inner ring
30             bg = resources.get_image('board/tile_2.png',
31                                      transforms=(EIGHT_BIT,))
32         else:
33             bg = resources.get_image('board/tile_1.png',
34                                      transforms=(EIGHT_BIT,))
35         self.surface = pygame.surface.Surface(TILE_SIZE)
36         self.surface.blit(bg, (0, 0))
37         # Look up the required bits on the board location
38         card = self.state.board_locations[self.board_pos]
39         bits = []
40         for action in card.actions:
41             if action.required_bits:
42                 bits.append(action.required_bits)
43         # Sort so we have longer lists of bits later
44         self.size = self.surface.get_rect().size
45         if bits:
46             bits.sort(key=lambda x: len(x))
47             y_offset = TILE_SIZE[1] - 32 * len(bits)
48             for pattern in bits:
49                 x_offset = 10
50                 for bit in pattern:
51                     img = resources.get_image(BIT_MAP[bit],
52                                               transforms=(EIGHT_BIT,))
53                     self.surface.blit(img, (x_offset, y_offset))
54                     x_offset += 32
55                 y_offset += 32
56
57     def draw(self, surface):
58         surface.blit(self.surface, self.pos)