flake8 all the things.
[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, ACTION_GLYPHS,
6     PALETTE)
7 from naja.resources import resources
8 from naja.resources.mutators import EIGHT_BIT, blender
9 from naja.widgets.base import Widget
10
11
12 BIT_MAP = {
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',
21 }
22
23
24 GLYPH_MAP = {
25     ACTION_GLYPHS.CLEAR_BITS: 'glyphs/bits_clear.png',
26     ACTION_GLYPHS.TOGGLE_BITS: 'glyphs/bits_toggle.png',
27     ACTION_GLYPHS.SET_BITS: 'glyphs/bits_set.png',
28     ACTION_GLYPHS.CHANGE_BOARD: 'glyphs/board.png',
29     ACTION_GLYPHS.DAMAGE: 'glyphs/damage.png',
30     ACTION_GLYPHS.HEAL: 'glyphs/health.png',
31     ACTION_GLYPHS.MOVEMENT: 'glyphs/move.png',
32     ACTION_GLYPHS.WINTOKEN: 'glyphs/win.png',
33     ACTION_GLYPHS.MSB: 'glyphs/msb.png',
34     ACTION_GLYPHS.NOTHING: 'glyphs/do_nothing.png',
35 }
36
37
38 class TileWidget(Widget):
39     """Widget which holds a tile on the game board."""
40     def __init__(self, pos, state=None, board_pos=None):
41         super(TileWidget, self).__init__(pos, TILE_SIZE)
42         self.state = state
43         self.board_pos = board_pos
44         self.highlighted = False
45
46     def prepare(self):
47         # Draw background
48         x, y = abs(self.board_pos[0] - 2), abs(self.board_pos[1] - 2)
49
50         if (x + y) % 2 == 0:
51             bg = resources.get_image('board/tile_2.png',
52                                      transforms=(EIGHT_BIT,))
53         else:
54             bg = resources.get_image('board/tile_1.png',
55                                      transforms=(EIGHT_BIT,))
56         overlays = []
57
58         legal_move = (self.board_pos in self.state.player.legal_moves())
59
60         if self.state.gameboard.player_mode == EXAMINE and legal_move:
61             overlays.append(resources.get_image('board/tile_available.png',
62                                                 transforms=(EIGHT_BIT,)))
63         if self.highlighted:
64             overlays.append(resources.get_image('board/tile_selected.png',
65                                                 transforms=(EIGHT_BIT,)))
66
67         self.surface = pygame.surface.Surface(TILE_SIZE)
68         self.surface.blit(bg, (0, 0))
69         for overlay in overlays:
70             self.surface.blit(overlay, (0, 0))
71         # Look up the required bits on the board location
72         card = self.state.board_locations[self.board_pos]
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)
76
77     def _prepare_lock(self, action, y_offset):
78         if not action.required_bits:
79             return 4
80
81         img_name = BIT_MAP[action.required_bits]
82
83         if self.board_pos != self.state.player.position:
84             x_offset = 0
85         else:
86             img_name = img_name.replace('.png', '_small.png')
87             x_offset = 2
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
93
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
97
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(
103                     GLYPH_MAP[glyph], transforms=(EIGHT_BIT,))
104                 self.surface.blit(img, (x_offset, y_offset + 4))
105                 x_offset += img.get_width()
106             if action.MSB_GLYPH is not None:
107                 img = resources.get_image(
108                     GLYPH_MAP[action.MSB_GLYPH],
109                     transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
110                 self.surface.blit(img, (x_offset, y_offset + 4))
111                 x_offset += img.get_width()
112         return y_offset + LOCK_HEIGHT
113
114     def set_highlight(self, pos):
115         self.highlighted = False
116         if (self.state.gameboard.player_mode == EXAMINE and
117                 self.board_pos == pos):
118             self.highlighted = True
119
120     def draw(self, surface):
121         surface.blit(self.surface, self.pos)