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