Use new puzzle tiles.
[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.current_card = None
29         self.board_pos = board_pos
30         self.highlighted = False
31         self.animation = TILE_SIZE[0]
32
33     def prepare(self):
34         # Draw background
35         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
36
37         if self.state.gameboard.puzzle:
38             tile_1_name = 'board/tile_1.png'
39             tile_2_name = 'board/tile_2_puzzle.png'
40             tile_available_name = 'board/tile_available_puzzle.png'
41         else:
42             tile_1_name = 'board/tile_1.png'
43             tile_2_name = 'board/tile_2.png'
44             tile_available_name = 'board/tile_available.png'
45
46         if (x + y) % 2 == 0:
47             bg_name = tile_2_name
48         else:
49             bg_name = tile_1_name
50         bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
51         overlays = []
52
53         legal_move = (self.board_pos in self.state.player.legal_moves())
54
55         if self.state.gameboard.player_mode == EXAMINE and legal_move:
56             overlays.append(resources.get_image(
57                 tile_available_name, transforms=(EIGHT_BIT,)))
58         if self.highlighted:
59             overlays.append(resources.get_image(
60                 'board/tile_selected.png',
61                 transforms=(EIGHT_BIT,)))
62
63         self.surface = pygame.surface.Surface(TILE_SIZE)
64         self.surface.blit(bg, (0, 0))
65         for overlay in overlays:
66             self.surface.blit(overlay, (0, 0))
67         # Look up the required bits on the board location
68         card = self.state.board_locations[self.board_pos]
69         if card is not self.current_card:
70             self.animation = TILE_SIZE[0]
71             self.current_card = card
72
73         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
74         for action in card.actions:
75             y_offset = self._prepare_action(action, y_offset)
76
77     def _prepare_lock(self, action, y_offset):
78         if not action.required_bits:
79             return 4
80
81         img_name = BIT_MAP[action.required_bits]
82
83         if self.board_pos != self.state.player.position:
84             x_offset = 0
85         else:
86             img_name = img_name.replace('.png', '_small.png')
87             x_offset = 2
88             if y_offset == LOCK_HEIGHT:
89                 y_offset = 0  # middle -> top
90             elif y_offset == 2 * LOCK_HEIGHT:
91                 # bottom -> further down
92                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
93
94         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
95         self.surface.blit(img, (x_offset, y_offset))
96         return x_offset + img.get_width() + 2
97
98     def _prepare_action(self, action, y_offset):
99         x_offset = self._prepare_lock(action, y_offset)
100         if self.board_pos != self.state.player.position:
101             for glyph in action.GLYPHS:
102                 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
103                 self.surface.blit(img, (x_offset, y_offset + 4))
104                 x_offset += img.get_width()
105             if action.MSB_GLYPH is not None:
106                 img = resources.get_image(
107                     action.MSB_GLYPH,
108                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
109                 self.surface.blit(img, (x_offset, y_offset + 4))
110                 x_offset += img.get_width()
111         return y_offset + LOCK_HEIGHT
112
113     def set_highlight(self, pos):
114         self.highlighted = False
115         if (self.state.gameboard.player_mode == EXAMINE and
116                 self.board_pos == pos):
117             self.highlighted = True
118
119     def draw(self, surface):
120         scaled_width = self.surface.get_width() - self.animation
121         scaled_height = self.surface.get_height() - self.animation
122         scaled_position = (self.pos[0] + (self.animation / 2),
123                            self.pos[1] + (self.animation / 2))
124         if self.animation > 0:
125             self.animation = max(0, self.animation - 4)
126         scaled_surface = pygame.transform.scale(
127             self.surface, (scaled_width, scaled_height))
128         surface.blit(scaled_surface, scaled_position)