Update info display when card changes.
[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, MOVE, ACT, KEYS
8 from naja.events import PlayerMoved, finish_event
9 from naja.resources import resources
10 from naja.resources.mutators import EIGHT_BIT
11
12 from naja.widgets.base import Widget
13 from naja.widgets.tile import BIT_MAP
14 from naja.widgets.text import TextBoxWidget, TextWidget
15
16
17 HINTS = {
18         MOVE: "Move using the arrow keys.\nPress SPACE to stay in place",
19         ACT: "Choose an action using the Up/Down keys.\n"
20              "Press Return to execute the action.",
21         }
22
23 TITLES = {
24         MOVE: "Move the Robot",
25         ACT: "Choose an Action",
26         }
27
28
29 class InfoAreaWidget(Widget):
30     """
31     Widget for the game board information area.
32     """
33     def __init__(self, pos, state):
34         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
35         self.state = state
36         self.set_position(state.player.position)
37
38     def prepare(self):
39         self.surface = pygame.surface.Surface(INFO_SIZE)
40         self.surface.fill((0, 0, 0))
41         # Extract actions and such from the card
42         title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
43                            colour=(255, 255, 255))
44         title.render(self.surface)
45         y_offset = title.surface.get_rect().height + 8
46         for choice, action in enumerate(self.card.actions):
47             if action.required_bits:
48                 img_name = BIT_MAP[action.required_bits].replace(
49                     '.png', '_small.png')
50                 img = resources.get_image(img_name,
51                                           transforms=(EIGHT_BIT,))
52                 self.surface.blit(img, (0, y_offset))
53                 y_offset += 8
54             text = TextBoxWidget(
55                 (12, y_offset), action.TEXT,
56                 box_width=(INFO_SIZE[0] - 12) // EIGHT_BIT_SCALE,
57                 fontsize=28)
58             text.render(self.surface)
59             if choice == self.chosen:
60                 colour = (255, 255, 0, 128)
61                 bottom = y_offset + text.surface.get_rect().height
62                 right = text.surface.get_rect().width + 12
63                 pygame.draw.lines(self.surface, colour, True,
64                                   [(12, y_offset), (right, y_offset),
65                                    (right, bottom), (12, bottom)], 4)
66             y_offset += text.surface.get_rect().height + 16
67         # We cheat horribly for layout reasons
68         hint = TextBoxWidget((0, 0), HINTS[self.state.gameboard.player_mode],
69                              box_width=INFO_SIZE[0] // EIGHT_BIT_SCALE)
70         hint.prepare()
71         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
72         self.surface.blit(hint.surface, (0, y_offset))
73
74     def set_position(self, position):
75         self.card = self.state.board_locations[position]
76         self.chosen = 0
77
78     def draw(self, surface):
79         surface.blit(self.surface, self.pos)
80
81     def handle_event(self, ev):
82         if PlayerMoved.matches(ev):
83             self.set_position(self.state.player.position)
84             return False
85
86         if self.state.gameboard.player_mode == MOVE:
87             return super(InfoAreaWidget, self).handle_event(ev)
88         if ev.type == pgl.KEYDOWN:
89             if ev.key in KEYS.SELECT:
90                 player = self.state.gameboard.player
91                 action = self.card.actions[self.chosen]
92                 if not action.check_available(player):
93                     print "BEEP!"
94                 else:
95                     action.perform_action(self.state.gameboard, self.card)
96                     self.state.gameboard.replace_card(player.position)
97                     self.state.gameboard.change_mode()
98                 # We haven't moved, but the card may have changed under us.
99                 return finish_event(events=[PlayerMoved])
100             if ev.key in KEYS.UP:
101                 if self.chosen > 0:
102                     self.chosen -= 1
103                 return finish_event()
104             if ev.key in KEYS.DOWN:
105                 if self.chosen + 1 < len(self.card.actions):
106                     self.chosen += 1
107                 return finish_event()
108         return super(InfoAreaWidget, self).handle_event(ev)