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