Grid iron.
[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.bright = 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             if self.bright:
61                 select_name = 'board/tile_selected_pulse.png'
62             else:
63                 select_name = 'board/tile_selected.png'
64             overlays.append(resources.get_image(
65                 select_name, transforms=(EIGHT_BIT,)))
66         self.surface = pygame.surface.Surface(TILE_SIZE)
67         self.surface.blit(bg, (0, 0))
68         for overlay in overlays:
69             self.surface.blit(overlay, (0, 0))
70         # Look up the required bits on the board location
71         card = self.state.board_locations[self.board_pos]
72         if card is not self.current_card:
73             self.animation = TILE_SIZE[0]
74             self.current_card = card
75
76         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
77         for action in card.actions:
78             y_offset = self._prepare_action(action, y_offset)
79
80         self._prepare_countdown(card)
81
82     def _prepare_countdown(self, card):
83         if card.replacement_time is None:
84             return
85         elif card.replacement_time <= 1:
86             glyph = 'glyphs/countdown_1.png'
87         elif card.replacement_time == 2:
88             glyph = 'glyphs/countdown_2.png'
89         elif card.replacement_time == 3:
90             glyph = 'glyphs/countdown_3.png'
91         elif card.replacement_time < 8:
92             glyph = 'glyphs/countdown_4.png'
93         else:
94             glyph = 'glyphs/countdown_5.png'
95         img = resources.get_image(
96             glyph, transforms=(EIGHT_BIT, blender(PALETTE.DARK_VIOLET)))
97         self.surface.blit(img, (TILE_SIZE[0] - 20, 0))
98
99     def _prepare_lock(self, action, y_offset):
100         required_keys = action.required_bits & frozenset([
101             BITS.RED, BITS.GREEN, BITS.BLUE])
102         if required_keys not in BIT_MAP:
103             return 4
104
105         img_name = BIT_MAP[required_keys]
106
107         if self.board_pos != self.state.player.position:
108             x_offset = 0
109         else:
110             img_name = img_name.replace('.png', '_small.png')
111             x_offset = 2
112             if y_offset == LOCK_HEIGHT:
113                 y_offset = 0  # middle -> top
114             elif y_offset == 2 * LOCK_HEIGHT:
115                 # bottom -> further down
116                 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
117
118         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
119         img_rect = img.get_rect()
120         self.surface.blit(img, (x_offset, y_offset))
121
122         if BITS.MSB in action.required_bits:
123             msb = resources.get_image('board/msb_lock_decoration.png',
124                                       transforms=(EIGHT_BIT,))
125             msb_rect = msb.get_rect()
126             self.surface.blit(
127                 msb, (x_offset + img_rect.width - msb_rect.width, y_offset)
128             )
129
130         return x_offset + img_rect.width + 2
131
132     def _prepare_action(self, action, y_offset):
133         x_offset = self._prepare_lock(action, y_offset)
134         if self.board_pos != self.state.player.position:
135             for glyph in action.get_glyphs():
136                 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
137                 self.surface.blit(img, (x_offset, y_offset + 4))
138                 x_offset += img.get_width()
139             if action.get_msb_glyph() is not None:
140                 img = resources.get_image(
141                     action.get_msb_glyph(),
142                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
143                 self.surface.blit(img, (x_offset, y_offset + 4))
144                 x_offset += img.get_width()
145         return y_offset + LOCK_HEIGHT
146
147     def set_highlight(self, pos, bright=False):
148         self.highlighted = False
149         self.bright = bright
150         if (self.state.gameboard.player_mode == EXAMINE and
151                 self.board_pos == pos):
152             self.highlighted = True
153
154     def draw(self, surface):
155         scaled_width = self.surface.get_width() - self.animation
156         scaled_height = self.surface.get_height() - self.animation
157         scaled_position = (self.pos[0] + (self.animation / 2),
158                            self.pos[1] + (self.animation / 2))
159         if self.animation > 0:
160             self.animation = max(0, self.animation - 4)
161         scaled_surface = pygame.transform.scale(
162             self.surface, (scaled_width, scaled_height))
163         surface.blit(scaled_surface, scaled_position)