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