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)
38 bg_name = 'board/tile_2.png'
40 bg_name = 'board/tile_1.png'
41 bg = resources.get_image(bg_name, transforms=(EIGHT_BIT,))
44 legal_move = (self.board_pos in self.state.player.legal_moves())
46 if self.state.gameboard.player_mode == EXAMINE and legal_move:
47 overlays.append(resources.get_image(
48 'board/tile_available.png', transforms=(EIGHT_BIT,)))
50 overlays.append(resources.get_image(
51 'board/tile_selected.png',
52 transforms=(EIGHT_BIT,)))
54 self.surface = pygame.surface.Surface(TILE_SIZE)
55 self.surface.blit(bg, (0, 0))
56 for overlay in overlays:
57 self.surface.blit(overlay, (0, 0))
58 # Look up the required bits on the board location
59 card = self.state.board_locations[self.board_pos]
60 if card is not self.current_card:
61 self.animation = TILE_SIZE[0]
62 self.current_card = card
64 y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
65 for action in card.actions:
66 y_offset = self._prepare_action(action, y_offset)
68 def _prepare_lock(self, action, y_offset):
69 if not action.required_bits:
72 img_name = BIT_MAP[action.required_bits]
74 if self.board_pos != self.state.player.position:
77 img_name = img_name.replace('.png', '_small.png')
79 if y_offset == LOCK_HEIGHT:
80 y_offset = 0 # middle -> top
81 elif y_offset == 2 * LOCK_HEIGHT:
82 # bottom -> further down
83 y_offset += LOCK_HEIGHT - SMALL_LOCK_HEIGHT - 2
85 img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
86 self.surface.blit(img, (x_offset, y_offset))
87 return x_offset + img.get_width() + 2
89 def _prepare_action(self, action, y_offset):
90 x_offset = self._prepare_lock(action, y_offset)
91 if self.board_pos != self.state.player.position:
92 for glyph in action.GLYPHS:
93 img = resources.get_image(glyph, transforms=(EIGHT_BIT,))
94 self.surface.blit(img, (x_offset, y_offset + 4))
95 x_offset += img.get_width()
96 if action.MSB_GLYPH is not None:
97 img = resources.get_image(
99 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
100 self.surface.blit(img, (x_offset, y_offset + 4))
101 x_offset += img.get_width()
102 return y_offset + LOCK_HEIGHT
104 def set_highlight(self, pos):
105 self.highlighted = False
106 if (self.state.gameboard.player_mode == EXAMINE and
107 self.board_pos == pos):
108 self.highlighted = True
110 def draw(self, surface):
111 scaled_width = self.surface.get_width() - self.animation
112 scaled_height = self.surface.get_height() - self.animation
113 scaled_position = (self.pos[0] + (self.animation / 2),
114 self.pos[1] + (self.animation / 2))
115 if self.animation > 0:
116 self.animation = max(0, self.animation - 4)
117 scaled_surface = pygame.transform.scale(
118 self.surface, (scaled_width, scaled_height))
119 surface.blit(scaled_surface, scaled_position)