Render bit pattern markers on tile widgets
[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.widgets.base import Widget
8
9
10 BIT_MAP = {
11         BITS['CYAN']: 'board/tile_cyan.png',
12         BITS['MAGENTA']: 'board/tile_magenta.png',
13         BITS['YELLOW']: 'board/tile_yellow.png',
14         }
15
16
17 class TileWidget(Widget):
18     """Widget which holds a tile on the game board."""
19     def __init__(self, pos, state=None, board_pos=None):
20         super(TileWidget, self).__init__(pos, TILE_SIZE)
21         self.state = state
22         self.board_pos = board_pos
23
24     def prepare(self):
25         # Placeholder logic - just draw the outline of a square
26         self.surface = pygame.surface.Surface(TILE_SIZE)
27         pygame.draw.lines(self.surface, pgl.color.THECOLORS['yellow'],
28                           True, [(1, 1), (1, 95), (95, 95), (95, 1)], 2)
29         self.surface.convert_alpha(pygame.display.get_surface())
30         # Look up the required bits on the board location
31         card = self.state.gameboard.board_locations[self.board_pos]
32         bits = []
33         for action in card.actions:
34             if action.required_bits:
35                 bits.append(action.required_bits)
36         # Sort so we have longer lists of bits later
37         self.size = self.surface.get_rect().size
38         if bits:
39             bits.sort(key=lambda x: len(x))
40             y_offset = TILE_SIZE[1] - 32 * len(bits)
41             for pattern in bits:
42                 x_offset = 10
43                 for bit in pattern:
44                     img = resources.get_image(BIT_MAP[bit])
45                     self.surface.blit(img, (x_offset, y_offset))
46                     x_offset += 32
47                 y_offset += 32
48
49     def draw(self, surface):
50         surface.blit(self.surface, self.rect)