Tweak infobar layout yet again
[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] - INFO_LEFT_PADDING),
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((0, 0), hint_text, padding=4,
80                              box_width=(INFO_SIZE[0] - INFO_LEFT_PADDING - 8))
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] - INFO_LEFT_PADDING - 8),
98             fontsize=28, colour=text_colour)
99         text.render(self.surface)
100
101         border_colour = None
102         if choice == self.chosen:
103             border_colour = PALETTE.GREEN if action_viable else PALETTE.ORANGE
104         if border_colour:
105             bottom = y_offset + text.surface.get_rect().height
106             right = text.surface.get_rect().width + x_offset
107             pygame.draw.lines(self.surface, border_colour, True,
108                               [(x_offset, y_offset), (right, y_offset),
109                                (right, bottom), (x_offset, bottom)], 4)
110
111         if action.required_bits:
112             img_name = BIT_MAP[action.required_bits].replace(
113                 '.png', '_small.png')
114             img = resources.get_image(img_name,
115                                       transforms=(EIGHT_BIT,))
116             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset))
117             glyphs_x_offset += img.get_width() + 4
118         else:
119             glyphs_x_offset = INFO_LEFT_PADDING
120
121         for glyph in action.GLYPHS:
122             img = resources.get_image(
123                 glyph, transforms=(EIGHT_BIT, blender(PALETTE.GREY)))
124             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
125             glyphs_x_offset += img.get_width()
126         if action.MSB_GLYPH is not None:
127             img = resources.get_image(
128                 action.MSB_GLYPH,
129                 transforms=(EIGHT_BIT, blender(PALETTE.LIGHT_VIOLET)))
130             self.surface.blit(img, (glyphs_x_offset, glyphs_y_offset - 4))
131
132         return y_offset + text.surface.get_rect().height + 16
133
134     def set_position(self, position):
135         self.card_position = position
136         self.card = self.state.board_locations[self.card_position]
137         if self.state.gameboard.player_mode == ACT:
138             if self.chosen is None:
139                 self.chosen = 0
140         else:
141             self.chosen = None
142
143     def draw(self, surface):
144         surface.blit(self.surface, self.pos)
145
146     def next_action(self, viable_only=False, step=1):
147         num_actions = len(self.card.actions)
148         if num_actions == 0:
149             return
150         player = self.state.player
151         chosen = self.chosen
152         for i in range(num_actions - 1):
153             # loop through each action at most once.
154             chosen = (chosen + step) % num_actions
155             action = self.card.actions[chosen]
156             if not viable_only or action.check_available(player):
157                 sound.play_sound('zoop.ogg', volume=0.05)
158                 self.chosen = chosen
159
160     def prev_action(self, viable_only=False):
161         return self.next_action(viable_only=viable_only, step=-1)
162
163     def try_perform_action(self):
164         player = self.state.player
165         action = self.card.actions[self.chosen]
166         if not action.check_available(player):
167             sound.play_sound('error.ogg')
168         else:
169             sound.play_sound('chirp.ogg', volume=0.5)
170             action.perform_action(self.state.gameboard, self.card)
171             self.state.gameboard.card_used(player.position)
172             self.state.gameboard.change_mode(EXAMINE)
173             self.set_position(player.position)
174
175     def handle_event(self, ev):
176         if self.state.gameboard.player_mode == EXAMINE:
177             return super(InfoAreaWidget, self).handle_event(ev)
178         if ev.type == pgl.KEYDOWN:
179             if ev.key in KEYS.SELECT:
180                 self.try_perform_action()
181                 return finish_event()
182             if ev.key in KEYS.UP:
183                 self.next_action()
184                 return finish_event()
185             if ev.key in KEYS.DOWN:
186                 self.prev_action()
187                 return finish_event()
188             if ev.key in KEYS.SWITCH:
189                 self.next_action(viable_only=True)
190                 return finish_event()
191         return super(InfoAreaWidget, self).handle_event(ev)