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
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',
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)
28 self.current_card = None
29 self.board_pos = board_pos
30 self.highlighted = False
31 self.animation = TILE_SIZE[0]
35 x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
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'
42 tile_1_name = 'board/tile_1.png'
43 tile_2_name = 'board/tile_2.png'
44 tile_available_name = 'board/tile_available.png'
50 bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
53 legal_move = (self.board_pos in self.state.player.legal_moves())
55 if self.state.gameboard.player_mode == EXAMINE and legal_move:
56 overlays.append(resources.get_image(
57 tile_available_name, transforms=(EIGHT_BIT,)))
59 overlays.append(resources.get_image(
60 'board/tile_selected.png',
61 transforms=(EIGHT_BIT,)))
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
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)
77 def _prepare_lock(self, action, y_offset):
78 if not action.required_bits:
81 img_name = BIT_MAP[action.required_bits]
83 if self.board_pos != self.state.player.position:
86 img_name = img_name.replace('.png', '_small.png')
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
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
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(
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
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
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)