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