915c6da7fd40c16dd4f33bac3642ec7654cae640
[naja.git] / naja / widgets / info_area.py
1 """
2 Widget for the game board information area.
3 """
4 import pygame
5 import pygame.locals as pgl
6
7 from naja.constants import (
8     INFO_SIZE, KEYS, PALETTE,
9     ACTION_TEXT_OFFSET, INFO_LEFT_PADDING,
10     INFO_RIGHT_PADDING, BIT_SIZE, BITS)
11 from naja.events import InvalidateTheWorld, finish_event
12 from naja.resources import resources
13 from naja.resources.mutators import EIGHT_BIT, blender
14 from naja.sound import sound
15 from naja.utils import bit_glyphs, Flashlight
16
17 from naja.widgets.base import Widget
18 from naja.widgets.tile import BIT_MAP
19 from naja.widgets.text import TextBoxWidget
20 from naja import constants
21
22
23 HINTS = [
24     "Browse the tiles with {NORTH,SOUTH,EAST,WEST} keys.",
25     "Switch actions using TAB.",
26 ]
27
28 HINT_LEGAL_MOVE = "\nPress {RETURN} to move and execute the action."
29
30
31 class InfoAreaWidget(Widget):
32     """
33     Widget for the game board information area.
34     """
35     def __init__(self, pos, state):
36         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
37         self.state = state
38         self.set_position(state.player.position, legal=True)
39         self.flash_light = Flashlight(constants.FPS // 2)
40
41     def prepare(self):
42         self.surface = pygame.surface.Surface(INFO_SIZE)
43         self.surface.fill((0, 0, 0))
44
45         box_width = INFO_SIZE[0] - INFO_LEFT_PADDING - INFO_RIGHT_PADDING
46         y_offset = 0
47         pos = lambda: (INFO_LEFT_PADDING, y_offset)
48
49         # Bits
50         y_offset += 12
51         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
52                             for bit in reversed(range(8)))
53         if self.card.bitwise_operand:
54             bits_text = '%s %s' % (
55                 bits_text, bit_glyphs(self.card.bitwise_operand))
56         card_bits = TextBoxWidget(
57             (0, 0), bits_text, padding=4, centre=True,
58             colour=PALETTE.WHITE, border=2,
59             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
60             box_width=box_width)
61         card_bits.prepare()
62         self.surface.blit(card_bits.surface, pos())
63         y_offset += card_bits.surface.get_rect().height + 12
64
65         # Actions
66         for choice, action in enumerate(self.card.actions):
67             y_offset = self.prepare_action(choice, action, y_offset, box_width)
68
69         # Hint text
70         hint_text = " ".join(HINTS)
71         if self.card_position in self.state.player.legal_moves():
72             hint_text += HINT_LEGAL_MOVE
73
74         hint = TextBoxWidget(
75             (0, 0), hint_text, padding=4,
76             colour=PALETTE.WHITE, border=2,
77             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
78             box_width=box_width)
79         hint.prepare()
80         y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
81                     - BIT_SIZE[1] - 2)
82         self.surface.blit(hint.surface, pos())
83
84         # Game mode (puzzle, easy, standard, hard, etc ...)
85         game_mode = TextBoxWidget(
86             (0, 0), self.mode_hint(), padding=4, centre=True,
87             colour=PALETTE.WHITE, border=2,
88             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
89             box_width=box_width)
90         game_mode.prepare()
91
92         y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
93         self.surface.blit(game_mode.surface, pos())
94
95     def mode_hint(self):
96         gameboard = self.state.gameboard
97         if gameboard.puzzle:
98             return "PUZZLE"
99         return {
100             0: "DEATH", 1: "NINTENDO HARD",
101             2: "VERY HARD", 3: "HARD",
102             4: "STANDARD", 5: "EASY",
103         }.get(gameboard.max_health, "UNKNOWN")
104
105     def prepare_action(self, choice, action, y_offset, box_width):
106         x_offset = INFO_LEFT_PADDING
107         glyphs_x_offset = 2
108         glyphs_y_offset = y_offset
109         y_offset += ACTION_TEXT_OFFSET
110         action_viable = (
111             action.check_available(self.state.player) and self.legal)
112         text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
113
114         text = TextBoxWidget(
115             (x_offset, y_offset), action.get_text(self.card),
116             box_width=box_width,
117             fontsize=28, colour=text_colour,
118             border=2, border_colour=PALETTE.GREY)
119         text.render(self.surface)
120
121         border_colour = None
122         if choice == self.chosen:
123             if self.flash_light.on:
124                 border_colour = (PALETTE.GREEN if action_viable else
125                                  PALETTE.ORANGE)
126             else:
127                 border_colour = (PALETTE.DARK_GREEN if action_viable else
128                                  PALETTE.DARK_RED)
129
130         if border_colour:
131             bottom = y_offset + text.surface.get_rect().height
132             right = text.surface.get_rect().width + x_offset
133             pygame.draw.lines(self.surface, border_colour, True,
134                               [(x_offset, y_offset), (right, y_offset),
135                                (right, bottom), (x_offset, bottom)], 4)
136
137         required_keys = action.required_bits & frozenset([
138             BITS.RED, BITS.GREEN, BITS.BLUE])
139         if required_keys in BIT_MAP:
140             img_name = BIT_MAP[required_keys].replace(
141                 '.png', '_small.png')
142             img = resources.get_image(img_name,
143                                       transforms=(EIGHT_BIT,))
144             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
145             glyphs_x_offset += img.get_width() + 4
146         else:
147             glyphs_x_offset = INFO_LEFT_PADDING
148
149         if BITS.MSB in action.required_bits:
150             msb = resources.get_image('board/msb_lock_decoration.png',
151                                       transforms=(EIGHT_BIT,))
152             msb_rect = msb.get_rect()
153             self.surface.blit(
154                 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
155             )
156
157         for glyph in action.get_glyphs():
158             img = resources.get_image(
159                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
160             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
161             glyphs_x_offset += img.get_width()
162         if action.get_msb_glyph() is not None:
163             img = resources.get_image(
164                 action.get_msb_glyph(),
165                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
166             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
167
168         return y_offset + text.surface.get_rect().height + 16
169
170     def set_position(self, position, legal):
171         self.legal = legal
172         self.card_position = position
173         self.card = self.state.board_locations[self.card_position]
174         self.chosen = None
175         self.next_action(viable_only=True)
176
177     def draw(self, surface):
178         if self.flash_light.tick():
179             self.prepare()
180         surface.blit(self.surface, self.pos)
181
182     def next_action(self, viable_only=False, step=1):
183         num_actions = len(self.card.actions)
184         if num_actions == 0:
185             return
186         player = self.state.player
187         chosen = self.chosen if self.chosen is not None else -step
188         for i in range(num_actions):
189             # loop through each action at most once.
190             chosen = (chosen + step) % num_actions
191             action = self.card.actions[chosen]
192             if not viable_only or action.check_available(player):
193                 sound.play_sound('zoop.ogg', volume=0.05)
194                 self.chosen = chosen
195                 break
196
197     def prev_action(self, viable_only=False):
198         return self.next_action(viable_only=viable_only, step=-1)
199
200     def try_perform_action(self):
201         player = self.state.player
202         if self.chosen is None or not self.legal:
203             action = None
204         else:
205             action = self.card.actions[self.chosen]
206         if action is None or not action.check_available(player):
207             sound.play_sound('error.ogg')
208             return False
209         else:
210             sound.play_sound('chirp.ogg', volume=0.5)
211             player.set_position(self.card_position)
212             action.perform_action(self.state.gameboard, self.card)
213             self.state.gameboard.card_used(self.card_position)
214             self.set_position(self.card_position, legal=True)
215             return True
216
217     def handle_event(self, ev):
218         if InvalidateTheWorld.matches(ev):
219             self.flash_light.reset()
220         if ev.type == pgl.KEYDOWN:
221             if ev.key in KEYS.SWITCH:
222                 self.next_action(viable_only=True)
223                 return finish_event()
224             elif ev.key in KEYS.SELECT:
225                 self.try_perform_action()
226                 return finish_event()
227         return super(InfoAreaWidget, self).handle_event(ev)