5baa5171018075c0da533e99bbcf7fa442608806
[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 from naja.events import finish_event
11 from naja.resources import resources
12 from naja.resources.mutators import EIGHT_BIT, blender
13 from naja.sound import sound
14 from naja.utils import bit_glyphs
15
16 from naja.widgets.base import Widget
17 from naja.widgets.tile import BIT_MAP
18 from naja.widgets.text import TextBoxWidget, TextWidget
19
20
21 HINTS = {
22     ACT: ("Choose an action using the Up/Down keys.\n"
23           "Press Return to execute the action."),
24     EXAMINE: "Select a card to examine using the arrow keys.",
25 }
26
27 HINT_LEGAL_MOVE = "\nPress Return to move to this card."
28
29 TITLES = {
30     ACT: "Choose an Action",
31     EXAMINE: "Select a Card",
32 }
33
34
35 class InfoAreaWidget(Widget):
36     """
37     Widget for the game board information area.
38     """
39     def __init__(self, pos, state):
40         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
41         self.state = state
42         self.chosen = None
43         self.card_position = state.player.position
44         self.set_position(state.player.position)
45
46     def prepare(self):
47         if self.state.gameboard.player_mode == ACT:
48             self.set_position(self.state.player.position)
49         self.surface = pygame.surface.Surface(INFO_SIZE)
50         self.surface.fill((0, 0, 0))
51         # Extract actions and such from the card
52         title = TextWidget(
53             (INFO_LEFT_PADDING, 0), TITLES[self.state.gameboard.player_mode],
54             colour=PALETTE.WHITE)
55         title.render(self.surface)
56         y_offset = title.surface.get_rect().height - 4
57
58         # TODO: Make this better.
59         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
60                             for bit in reversed(range(8)))
61         if self.card.bitwise_operand:
62             bits_text = '%s %s' % (
63                 bits_text, bit_glyphs(self.card.bitwise_operand))
64         card_bits = TextBoxWidget((INFO_LEFT_PADDING, y_offset), bits_text,
65                                   box_width=INFO_SIZE[0],
66                                   colour=PALETTE.LIGHT_TURQUOISE,
67                                   bg_colour=PALETTE.BLACK)
68         card_bits.render(self.surface)
69         y_offset += card_bits.surface.get_rect().height + 4
70
71         for choice, action in enumerate(self.card.actions):
72             y_offset = self.prepare_action(choice, action, y_offset)
73         # We cheat horribly for layout reasons
74         hint_text = HINTS[self.state.gameboard.player_mode]
75         if self.state.gameboard.player_mode == EXAMINE:
76             if self.card_position in self.state.player.legal_moves():
77                 hint_text += HINT_LEGAL_MOVE
78
79         hint = TextBoxWidget((4, 0), hint_text, padding=2,
80                              box_width=(INFO_SIZE[0] - 4))
81         hint.prepare()
82         y_offset = (
83             INFO_SIZE[1] - hint.surface.get_rect().height - INFO_LEFT_PADDING
84             - 2)
85         self.surface.blit(hint.surface, (INFO_LEFT_PADDING, y_offset))
86
87     def prepare_action(self, choice, action, y_offset):
88         x_offset = INFO_LEFT_PADDING
89         glyphs_x_offset = 0
90         glyphs_y_offset = y_offset
91         y_offset += ACTION_TEXT_OFFSET
92         action_viable = action.check_available(self.state.player)
93         text_colour = PALETTE.BLACK if action_viable else PALETTE.GREY
94
95         text = TextBoxWidget(
96             (x_offset, y_offset), action.get_text(self.card),
97             box_width=(INFO_SIZE[0] - 16), fontsize=28, colour=text_colour)
98         text.render(self.surface)
99
100         border_colour = None
101         if choice == self.chosen:
102             border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
103         if border_colour:
104             bottom = y_offset + text.surface.get_rect().height
105             right = text.surface.get_rect().width + x_offset
106             pygame.draw.lines(self.surface, border_colour, True,
107                               [(x_offset, y_offset), (right, y_offset),
108                                (right, bottom), (x_offset, bottom)], 4)
109
110         if action.required_bits:
111             img_name = BIT_MAP[action.required_bits].replace(
112                 '.png', '_small.png')
113             img = resources.get_image(img_name,
114                                       transforms=(EIGHT_BIT,))
115             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
116             glyphs_x_offset += img.get_width() + 4
117         else:
118             glyphs_x_offset = INFO_LEFT_PADDING
119
120         for glyph in action.GLYPHS:
121             img = resources.get_image(
122                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
123             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
124             glyphs_x_offset += img.get_width()
125         if action.MSB_GLYPH is not None:
126             img = resources.get_image(
127                 action.MSB_GLYPH,
128                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
129             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
130
131         return y_offset + text.surface.get_rect().height + 16
132
133     def set_position(self, position):
134         self.card_position = position
135         self.card = self.state.board_locations[self.card_position]
136         if self.state.gameboard.player_mode == ACT:
137             if self.chosen is None:
138                 self.chosen = 0
139         else:
140             self.chosen = None
141
142     def draw(self, surface):
143         surface.blit(self.surface, self.pos)
144
145     def next_action(self, viable_only=False, step=1):
146         num_actions = len(self.card.actions)
147         if num_actions == 0:
148             return
149         player = self.state.player
150         chosen = self.chosen
151         for i in range(num_actions - 1):
152             # loop through each action at most once.
153             chosen = (chosen + step) % num_actions
154             action = self.card.actions[chosen]
155             if not viable_only or action.check_available(player):
156                 sound.play_sound('zoop.ogg', volume=0.05)
157                 self.chosen = chosen
158
159     def prev_action(self, viable_only=False):
160         return self.next_action(viable_only=viable_only, step=-1)
161
162     def try_perform_action(self):
163         player = self.state.player
164         action = self.card.actions[self.chosen]
165         if not action.check_available(player):
166             sound.play_sound('error.ogg')
167         else:
168             sound.play_sound('chirp.ogg', volume=0.5)
169             action.perform_action(self.state.gameboard, self.card)
170             self.state.gameboard.card_used(player.position)
171             self.state.gameboard.change_mode(EXAMINE)
172             self.set_position(player.position)
173
174     def handle_event(self, ev):
175         if self.state.gameboard.player_mode == EXAMINE:
176             return super(InfoAreaWidget, self).handle_event(ev)
177         if ev.type == pgl.KEYDOWN:
178             if ev.key in KEYS.SELECT:
179                 self.try_perform_action()
180                 return finish_event()
181             if ev.key in KEYS.UP:
182                 self.next_action()
183                 return finish_event()
184             if ev.key in KEYS.DOWN:
185                 self.prev_action()
186                 return finish_event()
187             if ev.key in KEYS.SWITCH:
188                 self.next_action(viable_only=True)
189                 return finish_event()
190         return super(InfoAreaWidget, self).handle_event(ev)