34bbec746df01e9756ae092e31d1a9811922b96c
[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)
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 the Up/Down keys.\n"
24           "Press Return to execute the action."),
25     EXAMINE: "Select a tile to examine using the arrow 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         # Top title
58         title = TextWidget(
59             pos(), TITLES[self.state.gameboard.player_mode],
60             colour=PALETTE.WHITE)
61         title.render(self.surface)
62         y_offset += title.surface.get_rect().height - 4
63
64         # Bits
65         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
66                             for bit in reversed(range(8)))
67         if self.card.bitwise_operand:
68             bits_text = '%s %s' % (
69                 bits_text, bit_glyphs(self.card.bitwise_operand))
70         card_bits = TextBoxWidget(
71             pos(), bits_text, box_width=box_width,
72             colour=PALETTE.LIGHT_TURQUOISE, bg_colour=PALETTE.BLACK)
73         card_bits.render(self.surface)
74         y_offset += card_bits.surface.get_rect().height + 4
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             box_width=box_width,
89             border=2, border_colour=PALETTE.GREY)
90         hint.prepare()
91         y_offset = (INFO_SIZE[1] - hint.surface.get_rect().height
92                     - BIT_SIZE[1] - 2)
93         self.surface.blit(hint.surface, pos())
94
95         # Game mode (puzzle, easy, standard, hard, etc ...)
96         game_mode = TextBoxWidget(
97             (0, 0), self.mode_hint(), padding=4, centre=True,
98             colour=PALETTE.WHITE, border=2,
99             bg_colour=PALETTE.BLACK, border_colour=PALETTE.BLUE,
100             box_width=box_width)
101         game_mode.prepare()
102
103         y_offset = (INFO_SIZE[1] - BIT_SIZE[1] + 12)
104         self.surface.blit(game_mode.surface, pos())
105
106     def mode_hint(self):
107         gameboard = self.state.gameboard
108         if gameboard.puzzle:
109             return "PUZZLE"
110         return {
111             0: "DEATH", 1: "LUDICROUS",
112             2: "RAMBO", 3: "HARD",
113             4: "STANDARD", 5: "EASY",
114         }.get(gameboard.max_health, "UNKNOWN")
115
116     def prepare_action(self, choice, action, y_offset, box_width):
117         x_offset = INFO_LEFT_PADDING
118         glyphs_x_offset = 0
119         glyphs_y_offset = y_offset
120         y_offset += ACTION_TEXT_OFFSET
121         action_viable = action.check_available(self.state.player)
122         text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
123
124         text = TextBoxWidget(
125             (x_offset, y_offset), action.get_text(self.card),
126             box_width=box_width,
127             fontsize=28, colour=text_colour,
128             border=2, border_colour=PALETTE.GREY)
129         text.render(self.surface)
130
131         border_colour = None
132         if choice == self.chosen:
133             border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
134         if border_colour:
135             bottom = y_offset + text.surface.get_rect().height
136             right = text.surface.get_rect().width + x_offset
137             pygame.draw.lines(self.surface, border_colour, True,
138                               [(x_offset, y_offset), (right, y_offset),
139                                (right, bottom), (x_offset, bottom)], 4)
140
141         if action.required_bits in BIT_MAP:
142             img_name = BIT_MAP[action.required_bits].replace(
143                 '.png', '_small.png')
144             img = resources.get_image(img_name,
145                                       transforms=(EIGHT_BIT,))
146             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
147             glyphs_x_offset += img.get_width() + 4
148         else:
149             glyphs_x_offset = INFO_LEFT_PADDING
150
151         for glyph in action.get_glyphs():
152             img = resources.get_image(
153                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
154             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
155             glyphs_x_offset += img.get_width()
156         if action.get_msb_glyph() is not None:
157             img = resources.get_image(
158                 action.get_msb_glyph(),
159                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
160             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
161
162         return y_offset + text.surface.get_rect().height + 16
163
164     def set_position(self, position):
165         self.card_position = position
166         self.card = self.state.board_locations[self.card_position]
167         if self.state.gameboard.player_mode == ACT:
168             if self.chosen is None:
169                 self.chosen = 0
170         else:
171             self.chosen = None
172
173     def draw(self, surface):
174         surface.blit(self.surface, self.pos)
175
176     def next_action(self, viable_only=False, step=1):
177         num_actions = len(self.card.actions)
178         if num_actions == 0:
179             return
180         player = self.state.player
181         chosen = self.chosen
182         for i in range(num_actions - 1):
183             # loop through each action at most once.
184             chosen = (chosen + step) % num_actions
185             action = self.card.actions[chosen]
186             if not viable_only or action.check_available(player):
187                 sound.play_sound('zoop.ogg', volume=0.05)
188                 self.chosen = chosen
189
190     def prev_action(self, viable_only=False):
191         return self.next_action(viable_only=viable_only, step=-1)
192
193     def try_perform_action(self):
194         player = self.state.player
195         action = self.card.actions[self.chosen]
196         if not action.check_available(player):
197             sound.play_sound('error.ogg')
198         else:
199             sound.play_sound('chirp.ogg', volume=0.5)
200             action.perform_action(self.state.gameboard, self.card)
201             self.state.gameboard.card_used(player.position)
202             self.state.gameboard.change_mode(EXAMINE)
203             self.set_position(player.position)
204
205     def handle_event(self, ev):
206         if self.state.gameboard.player_mode == EXAMINE:
207             return super(InfoAreaWidget, self).handle_event(ev)
208         if ev.type == pgl.KEYDOWN:
209             if ev.key in KEYS.SELECT:
210                 self.try_perform_action()
211                 return finish_event()
212             if ev.key in KEYS.UP:
213                 self.next_action()
214                 return finish_event()
215             if ev.key in KEYS.DOWN:
216                 self.prev_action()
217                 return finish_event()
218             if ev.key in KEYS.SWITCH:
219                 self.next_action(viable_only=True)
220                 return finish_event()
221         return super(InfoAreaWidget, self).handle_event(ev)