Prototype tile glyphs.
[naja.git] / naja / widgets / tile.py
1 # These will probably need to go away when we have images
2 import pygame
3
4 from naja.constants import TILE_SIZE, BITS, LOCK_HEIGHT, EXAMINE, ACTION_GLYPHS
5 from naja.resources import resources
6 from naja.resources.mutators import EIGHT_BIT
7 from naja.widgets.base import Widget
8
9
10 BIT_MAP = {
11     frozenset([BITS.RED]): 'board/tile_red.png',
12     frozenset([BITS.GREEN]): 'board/tile_green.png',
13     frozenset([BITS.BLUE]): 'board/tile_blue.png',
14     frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
15     frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
16     frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
17     frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
18         'board/tile_red_green_blue.png',
19 }
20
21
22 GLYPH_MAP = {
23     ACTION_GLYPHS.CLEAR_BITS: 'glyphs/bits_clear.png',
24     ACTION_GLYPHS.TOGGLE_BITS: 'glyphs/bits_toggle.png',
25     ACTION_GLYPHS.SET_BITS: 'glyphs/bits_set.png',
26     ACTION_GLYPHS.CHANGE_BOARD: 'glyphs/board.png',
27     ACTION_GLYPHS.DAMAGE: 'glyphs/damage.png',
28     ACTION_GLYPHS.HEAL: 'glyphs/health.png',
29     ACTION_GLYPHS.MOVEMENT: 'glyphs/move.png',
30     ACTION_GLYPHS.WINTOKEN: 'glyphs/win.png',
31     ACTION_GLYPHS.MSB: 'glyphs/msb.png',
32     ACTION_GLYPHS.NOTHING: 'glyphs/do_nothing.png',
33 }
34
35
36 class TileWidget(Widget):
37     """Widget which holds a tile on the game board."""
38     def __init__(self, pos, state=None, board_pos=None):
39         super(TileWidget, self).__init__(pos, TILE_SIZE)
40         self.state = state
41         self.board_pos = board_pos
42         self.highlighted = False
43
44     def prepare(self):
45         # Draw background
46         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
47
48         if (x + y) % 2 == 0:
49             bg = resources.get_image('board/tile_2.png',
50                                      transforms=(EIGHT_BIT,))
51         else:
52             bg = resources.get_image('board/tile_1.png',
53                                      transforms=(EIGHT_BIT,))
54         overlays = []
55
56         legal_move = (self.board_pos in self.state.player.legal_moves())
57
58         if self.state.gameboard.player_mode == EXAMINE and legal_move:
59             overlays.append(resources.get_image('board/tile_available.png',
60                                      transforms=(EIGHT_BIT,)))
61         if self.highlighted:
62             overlays.append(resources.get_image('board/tile_selected.png',
63                                           transforms=(EIGHT_BIT,)))
64
65         self.surface = pygame.surface.Surface(TILE_SIZE)
66         self.surface.blit(bg, (0, 0))
67         for overlay in overlays:
68             self.surface.blit(overlay, (0, 0))
69         # Look up the required bits on the board location
70         card = self.state.board_locations[self.board_pos]
71         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
72         for action in card.actions:
73             y_offset = self._prepare_action(action, y_offset)
74
75     def _prepare_lock(self, action, y_offset):
76         if not action.required_bits:
77             return 4
78
79         img_name = BIT_MAP[action.required_bits]
80         if self.board_pos != self.state.player.position:
81             x_offset = 0
82         else:
83             img_name = img_name.replace('.png', '_small.png')
84             x_offset = 4
85             if y_offset >= TILE_SIZE[1] // 2:
86                 # FIXME: Hack'ish
87                 # Bump the lock down by some hand-tuned factor
88                 # to not overlap with the robot
89                 y_offset += LOCK_HEIGHT // 2 - 4
90         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
91         self.surface.blit(img, (x_offset, y_offset))
92         return x_offset + img.get_width() + 2
93
94     def _prepare_action(self, action, y_offset):
95         x_offset = self._prepare_lock(action, y_offset)
96         if self.board_pos != self.state.player.position:
97             for glyph in action.GLYPHS:
98                 img = resources.get_image(
99                     GLYPH_MAP[glyph], transforms=(EIGHT_BIT,))
100                 self.surface.blit(img, (x_offset, y_offset + 4))
101                 x_offset += img.get_width()
102         return y_offset + LOCK_HEIGHT
103
104     def set_highlight(self, pos):
105         self.highlighted = False
106         if (self.state.gameboard.player_mode == EXAMINE and
107                 self.board_pos == pos):
108             self.highlighted = True
109
110     def draw(self, surface):
111         surface.blit(self.surface, self.pos)