1 # These will probably need to go away when we have images
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
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',
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)
29 self.current_card = None
30 self.board_pos = board_pos
31 self.highlighted = False
32 self.animation = TILE_SIZE[0]
36 x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
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'
43 tile_1_name = 'board/tile_1.png'
44 tile_2_name = 'board/tile_2.png'
45 tile_available_name = 'board/tile_available.png'
51 bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
54 legal_move = (self.board_pos in self.state.player.legal_moves())
56 if self.state.gameboard.player_mode == EXAMINE and legal_move:
57 overlays.append(resources.get_image(
58 tile_available_name, transforms=(EIGHT_BIT,)))
60 overlays.append(resources.get_image(
61 'board/tile_selected.png',
62 transforms=(EIGHT_BIT,)))
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
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)
78 if card.replacement_time is not None:
79 timestr = str(card.replacement_time)
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)
87 def _prepare_lock(self, action, y_offset):
88 if not action.required_bits:
91 img_name = BIT_MAP[action.required_bits]
93 if self.board_pos != self.state.player.position:
96 img_name = img_name.replace('.png', '_small.png')
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
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
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(
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
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
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)