Add (ugly) countdown timer to tile.
[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 from naja.widgets.text import TextBoxWidget
10
11
12 BIT_MAP = {
13     frozenset([BITS.RED]): 'board/tile_red.png',
14     frozenset([BITS.GREEN]): 'board/tile_green.png',
15     frozenset([BITS.BLUE]): 'board/tile_blue.png',
16     frozenset([BITS.RED, BITS.GREEN]): 'board/tile_red_green.png',
17     frozenset([BITS.RED, BITS.BLUE]): 'board/tile_red_blue.png',
18     frozenset([BITS.GREEN, BITS.BLUE]): 'board/tile_green_blue.png',
19     frozenset([BITS.RED, BITS.GREEN, BITS.BLUE]):
20         'board/tile_red_green_blue.png',
21 }
22
23
24 class TileWidget(Widget):
25     """Widget which holds a tile on the game board."""
26     def __init__(self, pos, state=None, board_pos=None):
27         super(TileWidget, self).__init__(pos, TILE_SIZE)
28         self.state = state
29         self.current_card = None
30         self.board_pos = board_pos
31         self.highlighted = False
32         self.animation = TILE_SIZE[0]
33
34     def prepare(self):
35         # Draw background
36         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
37
38         if self.state.gameboard.puzzle:
39             tile_1_name = 'board/tile_1.png'
40             tile_2_name = 'board/tile_2_puzzle.png'
41             tile_available_name = 'board/tile_available_puzzle.png'
42         else:
43             tile_1_name = 'board/tile_1.png'
44             tile_2_name = 'board/tile_2.png'
45             tile_available_name = 'board/tile_available.png'
46
47         if (x + y) % 2 == 0:
48             bg_name = tile_2_name
49         else:
50             bg_name = tile_1_name
51         bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
52         overlays = []
53
54         legal_move = (self.board_pos in self.state.player.legal_moves())
55
56         if self.state.gameboard.player_mode == EXAMINE and legal_move:
57             overlays.append(resources.get_image(
58                 tile_available_name, transforms=(EIGHT_BIT,)))
59         if self.highlighted:
60             overlays.append(resources.get_image(
61                 'board/tile_selected.png',
62                 transforms=(EIGHT_BIT,)))
63
64         self.surface = pygame.surface.Surface(TILE_SIZE)
65         self.surface.blit(bg, (0, 0))
66         for overlay in overlays:
67             self.surface.blit(overlay, (0, 0))
68         # Look up the required bits on the board location
69         card = self.state.board_locations[self.board_pos]
70         if card is not self.current_card:
71             self.animation = TILE_SIZE[0]
72             self.current_card = card
73
74         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
75         for action in card.actions:
76             y_offset = self._prepare_action(action, y_offset)
77
78         if card.replacement_time is not None:
79             timestr = str(card.replacement_time)
80             if len(timestr) > 1:
81                 timestr = '+'
82             countdown_text = TextBoxWidget(
83                 (TILE_SIZE[0] - 24, 4), timestr, padding=2,
84                 colour=PALETTE.PINK, bg_colour=PALETTE.DARK_RED)
85             countdown_text.render(self.surface)
86
87     def _prepare_lock(self, action, y_offset):
88         if not action.required_bits:
89             return 4
90
91         img_name = BIT_MAP[action.required_bits]
92
93         if self.board_pos != self.state.player.position:
94             x_offset = 0
95         else:
96             img_name = img_name.replace('.png', '_small.png')
97             x_offset = 2
98             if y_offset == LOCK_HEIGHT:
99                 y_offset = 0  # middle -> top
100             elif y_offset == 2 * LOCK_HEIGHT:
101                 # bottom -> further down
102                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
103
104         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
105         self.surface.blit(img, (x_offset, y_offset))
106         return x_offset + img.get_width() + 2
107
108     def _prepare_action(self, action, y_offset):
109         x_offset = self._prepare_lock(action, y_offset)
110         if self.board_pos != self.state.player.position:
111             for glyph in action.GLYPHS:
112                 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
113                 self.surface.blit(img, (x_offset, y_offset + 4))
114                 x_offset += img.get_width()
115             if action.MSB_GLYPH is not None:
116                 img = resources.get_image(
117                     action.MSB_GLYPH,
118                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
119                 self.surface.blit(img, (x_offset, y_offset + 4))
120                 x_offset += img.get_width()
121         return y_offset + LOCK_HEIGHT
122
123     def set_highlight(self, pos):
124         self.highlighted = False
125         if (self.state.gameboard.player_mode == EXAMINE and
126                 self.board_pos == pos):
127             self.highlighted = True
128
129     def draw(self, surface):
130         scaled_width = self.surface.get_width() - self.animation
131         scaled_height = self.surface.get_height() - self.animation
132         scaled_position = (self.pos[0] + (self.animation / 2),
133                            self.pos[1] + (self.animation / 2))
134         if self.animation > 0:
135             self.animation = max(0, self.animation - 4)
136         scaled_surface = pygame.transform.scale(
137             self.surface, (scaled_width, scaled_height))
138         surface.blit(scaled_surface, scaled_position)