Conditional effects in light violet.
[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, EXAMINE, ACTION_GLYPHS, 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 GLYPH_MAP = {
24     ACTION_GLYPHS.CLEAR_BITS: 'glyphs/bits_clear.png',
25     ACTION_GLYPHS.TOGGLE_BITS: 'glyphs/bits_toggle.png',
26     ACTION_GLYPHS.SET_BITS: 'glyphs/bits_set.png',
27     ACTION_GLYPHS.CHANGE_BOARD: 'glyphs/board.png',
28     ACTION_GLYPHS.DAMAGE: 'glyphs/damage.png',
29     ACTION_GLYPHS.HEAL: 'glyphs/health.png',
30     ACTION_GLYPHS.MOVEMENT: 'glyphs/move.png',
31     ACTION_GLYPHS.WINTOKEN: 'glyphs/win.png',
32     ACTION_GLYPHS.MSB: 'glyphs/msb.png',
33     ACTION_GLYPHS.NOTHING: 'glyphs/do_nothing.png',
34 }
35
36
37 class TileWidget(Widget):
38     """Widget which holds a tile on the game board."""
39     def __init__(self, pos, state=None, board_pos=None):
40         super(TileWidget, self).__init__(pos, TILE_SIZE)
41         self.state = state
42         self.board_pos = board_pos
43         self.highlighted = False
44
45     def prepare(self):
46         # Draw background
47         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
48
49         if (x + y) % 2 == 0:
50             bg = resources.get_image('board/tile_2.png',
51                                      transforms=(EIGHT_BIT,))
52         else:
53             bg = resources.get_image('board/tile_1.png',
54                                      transforms=(EIGHT_BIT,))
55         overlays = []
56
57         legal_move = (self.board_pos in self.state.player.legal_moves())
58
59         if self.state.gameboard.player_mode == EXAMINE and legal_move:
60             overlays.append(resources.get_image('board/tile_available.png',
61                                      transforms=(EIGHT_BIT,)))
62         if self.highlighted:
63             overlays.append(resources.get_image('board/tile_selected.png',
64                                           transforms=(EIGHT_BIT,)))
65
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         y_offset = TILE_SIZE[1] - LOCK_HEIGHT * len(card.actions)
73         for action in card.actions:
74             y_offset = self._prepare_action(action, y_offset)
75
76     def _prepare_lock(self, action, y_offset):
77         if not action.required_bits:
78             return 4
79
80         img_name = BIT_MAP[action.required_bits]
81         if self.board_pos != self.state.player.position:
82             x_offset = 0
83         else:
84             img_name = img_name.replace('.png', '_small.png')
85             x_offset = 4
86             if y_offset >= TILE_SIZE[1] // 2:
87                 # FIXME: Hack'ish
88                 # Bump the lock down by some hand-tuned factor
89                 # to not overlap with the robot
90                 y_offset += LOCK_HEIGHT // 2 - 4
91         img = resources.get_image(img_name, transforms=(EIGHT_BIT,))
92         self.surface.blit(img, (x_offset, y_offset))
93         return x_offset + img.get_width() + 2
94
95     def _prepare_action(self, action, y_offset):
96         x_offset = self._prepare_lock(action, y_offset)
97         if self.board_pos != self.state.player.position:
98             for glyph in action.GLYPHS:
99                 img = resources.get_image(
100                     GLYPH_MAP[glyph], transforms=(EIGHT_BIT,))
101                 self.surface.blit(img, (x_offset, y_offset + 4))
102                 x_offset += img.get_width()
103             if action.MSB_GLYPH is not None:
104                 img = resources.get_image(
105                     GLYPH_MAP[action.MSB_GLYPH],
106                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
107                 self.surface.blit(img, (x_offset, y_offset + 4))
108                 x_offset += img.get_width()
109         return y_offset + LOCK_HEIGHT
110
111     def set_highlight(self, pos):
112         self.highlighted = False
113         if (self.state.gameboard.player_mode == EXAMINE and
114                 self.board_pos == pos):
115             self.highlighted = True
116
117     def draw(self, surface):
118         surface.blit(self.surface, self.pos)