db3ffb48cda8ddf5f5d19593f165ee600af2f733
[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 (
5     TILE_SIZE, BITS, LOCK_HEIGHT, SMALL_LOCK_HEIGHT, EXAMINE, PALETTE)
6 from naja.resources import resources
7 from naja.resources.mutators import EIGHT_BIT, blender
8 from naja.widgets.base import Widget
9
10
11 BIT_MAP = {
12     frozenset([BITS.RED]): 'board/tile_red.png',
13     frozenset([BITS.GREEN]): 'board/tile_green.png',
14     frozenset([BITS.BLUE]): 'board/tile_blue.png',
15     frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
16     frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
17     frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
18     frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
19         'board/tile_red_green_blue.png',
20 }
21
22
23 class TileWidget(Widget):
24     """Widget which holds a tile on the game board."""
25     def __init__(self, pos, state=None, board_pos=None):
26         super(TileWidget, self).__init__(pos, TILE_SIZE)
27         self.state = state
28         self.board_pos = board_pos
29         self.highlighted = False
30
31     def prepare(self):
32         # Draw background
33         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
34
35         if (x + y) % 2 == 0:
36             bg_name = 'board/tile_2.png'
37         else:
38             bg_name = 'board/tile_1.png'
39         bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
40         overlays = []
41
42         legal_move = (self.board_pos in self.state.player.legal_moves())
43
44         if self.state.gameboard.player_mode == EXAMINE and legal_move:
45             overlays.append(resources.get_image(
46                 'board/tile_available.png', transforms=(EIGHT_BIT,)))
47         if self.highlighted:
48             overlays.append(resources.get_image(
49                 'board/tile_selected.png',
50                 transforms=(EIGHT_BIT,)))
51
52         self.surface = pygame.surface.Surface(TILE_SIZE)
53         self.surface.blit(bg, (0, 0))
54         for overlay in overlays:
55             self.surface.blit(overlay, (0, 0))
56         # Look up the required bits on the board location
57         card = self.state.board_locations[self.board_pos]
58         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
59         for action in card.actions:
60             y_offset = self._prepare_action(action, y_offset)
61
62     def _prepare_lock(self, action, y_offset):
63         if not action.required_bits:
64             return 4
65
66         img_name = BIT_MAP[action.required_bits]
67
68         if self.board_pos != self.state.player.position:
69             x_offset = 0
70         else:
71             img_name = img_name.replace('.png', '_small.png')
72             x_offset = 2
73             if y_offset == LOCK_HEIGHT:
74                 y_offset = 0  # middle -> top
75             elif y_offset == 2 * LOCK_HEIGHT:
76                 # bottom -> further down
77                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
78
79         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
80         self.surface.blit(img, (x_offset, y_offset))
81         return x_offset + img.get_width() + 2
82
83     def _prepare_action(self, action, y_offset):
84         x_offset = self._prepare_lock(action, y_offset)
85         if self.board_pos != self.state.player.position:
86             for glyph in action.GLYPHS:
87                 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
88                 self.surface.blit(img, (x_offset, y_offset + 4))
89                 x_offset += img.get_width()
90             if action.MSB_GLYPH is not None:
91                 img = resources.get_image(
92                     action.MSB_GLYPH,
93                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
94                 self.surface.blit(img, (x_offset, y_offset + 4))
95                 x_offset += img.get_width()
96         return y_offset + LOCK_HEIGHT
97
98     def set_highlight(self, pos):
99         self.highlighted = False
100         if (self.state.gameboard.player_mode == EXAMINE and
101                 self.board_pos == pos):
102             self.highlighted = True
103
104     def draw(self, surface):
105         surface.blit(self.surface, self.pos)