Merge branch 'master' of git://ctpug.org.za/naja
[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         # Bits
61         y_offset += 12
62         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
63                             for bit in reversed(range(8)))
64         if self.card.bitwise_operand:
65             bits_text = '%s %s' % (
66                 bits_text, bit_glyphs(self.card.bitwise_operand))
67         card_bits = TextBoxWidget(
68             (0, 0), bits_text, padding=4, centre=True,
69             colour=PALETTE.WHITE, border=2,
70             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
71             box_width=box_width)
72         card_bits.prepare()
73         self.surface.blit(card_bits.surface, pos())
74         y_offset += card_bits.surface.get_rect().height + 12
75
76         # Actions
77         for choice, action in enumerate(self.card.actions):
78             y_offset = self.prepare_action(choice, action, y_offset, box_width)
79
80         # Hint text
81         hint_text = HINTS[self.state.gameboard.player_mode]
82         if self.state.gameboard.player_mode == EXAMINE:
83             if self.card_position in self.state.player.legal_moves():
84                 hint_text += HINT_LEGAL_MOVE
85
86         hint = TextBoxWidget(
87             (0, 0), hint_text, padding=4,
88             colour=PALETTE.WHITE, border=2,
89             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
90             box_width=box_width)
91         hint.prepare()
92         y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
93                     - BIT_SIZE[1] - 2)
94         self.surface.blit(hint.surface, pos())
95
96         # Game mode (puzzle, easy, standard, hard, etc ...)
97         game_mode = TextBoxWidget(
98             (0, 0), self.mode_hint(), padding=4, centre=True,
99             colour=PALETTE.WHITE, border=2,
100             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
101             box_width=box_width)
102         game_mode.prepare()
103
104         y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
105         self.surface.blit(game_mode.surface, pos())
106
107     def mode_hint(self):
108         gameboard = self.state.gameboard
109         if gameboard.puzzle:
110             return "PUZZLE"
111         return {
112             0: "DEATH", 1: "LUDICROUS",
113             2: "RAMBO", 3: "HARD",
114             4: "STANDARD", 5: "EASY",
115         }.get(gameboard.max_health, "UNKNOWN")
116
117     def prepare_action(self, choice, action, y_offset, box_width):
118         x_offset = INFO_LEFT_PADDING
119         glyphs_x_offset = 2
120         glyphs_y_offset = y_offset
121         y_offset += ACTION_TEXT_OFFSET
122         action_viable = action.check_available(self.state.player)
123         text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
124
125         text = TextBoxWidget(
126             (x_offset, y_offset), action.get_text(self.card),
127             box_width=box_width,
128             fontsize=28, colour=text_colour,
129             border=2, border_colour=PALETTE.GREY)
130         text.render(self.surface)
131
132         border_colour = None
133         if choice == self.chosen:
134             if self.flash_light:
135                 border_colour = (PALETTE.GREEN if action_viable else
136                                  PALETTE.ORANGE)
137             else:
138                 border_colour = (PALETTE.DARK_GREEN if action_viable else
139                                  PALETTE.DARK_RED)
140
141         if border_colour:
142             bottom = y_offset + text.surface.get_rect().height
143             right = text.surface.get_rect().width + x_offset
144             pygame.draw.lines(self.surface, border_colour, True,
145                               [(x_offset, y_offset), (right, y_offset),
146                                (right, bottom), (x_offset, bottom)], 4)
147
148         required_keys = action.required_bits & frozenset([
149             BITS.RED, BITS.GREEN, BITS.BLUE])
150         if required_keys in BIT_MAP:
151             img_name = BIT_MAP[required_keys].replace(
152                 '.png', '_small.png')
153             img = resources.get_image(img_name,
154                                       transforms=(EIGHT_BIT,))
155             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
156             glyphs_x_offset += img.get_width() + 4
157         else:
158             glyphs_x_offset = INFO_LEFT_PADDING
159
160         if BITS.MSB in action.required_bits:
161             msb = resources.get_image('board/msb_lock_decoration.png',
162                                       transforms=(EIGHT_BIT,))
163             msb_rect = msb.get_rect()
164             self.surface.blit(
165                 msb, (glyphs_x_offset - msb_rect.width - 4, glyphs_y_offset)
166             )
167
168         for glyph in action.get_glyphs():
169             img = resources.get_image(
170                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
171             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
172             glyphs_x_offset += img.get_width()
173         if action.get_msb_glyph() is not None:
174             img = resources.get_image(
175                 action.get_msb_glyph(),
176                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
177             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
178
179         return y_offset + text.surface.get_rect().height + 16
180
181     def set_position(self, position):
182         self.card_position = position
183         self.card = self.state.board_locations[self.card_position]
184         if self.state.gameboard.player_mode == ACT:
185             if self.chosen is None:
186                 self.chosen = 0
187         else:
188             self.chosen = None
189
190     def draw(self, surface):
191         self.flash_count += 1
192         if self.flash_count >= (constants.FPS // 2):
193             self.flash_light = not self.flash_light
194             self.flash_count = 0
195             self.prepare()
196         surface.blit(self.surface, self.pos)
197
198     def next_action(self, viable_only=False, step=1):
199         num_actions = len(self.card.actions)
200         if num_actions == 0:
201             return
202         player = self.state.player
203         chosen = self.chosen
204         for i in range(num_actions - 1):
205             # loop through each action at most once.
206             chosen = (chosen + step) % num_actions
207             action = self.card.actions[chosen]
208             if not viable_only or action.check_available(player):
209                 sound.play_sound('zoop.ogg', volume=0.05)
210                 self.chosen = chosen
211
212     def prev_action(self, viable_only=False):
213         return self.next_action(viable_only=viable_only, step=-1)
214
215     def try_perform_action(self):
216         player = self.state.player
217         action = self.card.actions[self.chosen]
218         if not action.check_available(player):
219             sound.play_sound('error.ogg')
220         else:
221             sound.play_sound('chirp.ogg', volume=0.5)
222             action.perform_action(self.state.gameboard, self.card)
223             self.state.gameboard.card_used(player.position)
224             self.state.gameboard.change_mode(EXAMINE)
225             self.set_position(player.position)
226
227     def handle_event(self, ev):
228         if self.state.gameboard.player_mode == EXAMINE:
229             return super(InfoAreaWidget, self).handle_event(ev)
230         if ev.type == pgl.KEYDOWN:
231             if ev.key in KEYS.SELECT:
232                 self.try_perform_action()
233                 return finish_event()
234             if ev.key in KEYS.UP:
235                 self.next_action()
236                 return finish_event()
237             if ev.key in KEYS.DOWN:
238                 self.prev_action()
239                 return finish_event()
240             if ev.key in KEYS.SWITCH:
241                 self.next_action(viable_only=True)
242                 return finish_event()
243         return super(InfoAreaWidget, self).handle_event(ev)