Draw locks on infobar above the text
[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                             EXAMINE)
9 from naja.events import finish_event
10 from naja.resources import resources
11 from naja.resources.mutators import EIGHT_BIT
12
13 from naja.widgets.base import Widget
14 from naja.widgets.tile import BIT_MAP
15 from naja.widgets.text import TextBoxWidget, TextWidget
16
17
18 HINTS = {
19         MOVE: "Move using the arrow keys.\nPress SPACE to stay in place\n"
20               "Press TAB to change to Examine mode",
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.\n"
24                  "Press TAB to change to Move mode",
25         }
26
27 TITLES = {
28         MOVE: "Move the Robot",
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.set_position(state.player.position)
43
44     def prepare(self):
45         if self.state.gameboard.player_mode in (ACT, MOVE):
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=(255, 255, 255))
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, colour=(255, 255, 0))
59         card_bits.render(self.surface)
60         y_offset += card_bits.surface.get_rect().height + 8
61
62         for choice, action in enumerate(self.card.actions):
63             y_offset = self.prepare_action(choice, action, y_offset)
64         # We cheat horribly for layout reasons
65         hint = TextBoxWidget((4, 0), HINTS[self.state.gameboard.player_mode],
66                              padding=2,
67                              box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
68         hint.prepare()
69         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
70         self.surface.blit(hint.surface, (4, y_offset))
71
72     def prepare_action(self, choice, action, y_offset):
73         text = TextBoxWidget(
74             (18, y_offset), action.get_text(),
75             box_width=(INFO_SIZE[0] - 16) // EIGHT_BIT_SCALE,
76             fontsize=28)
77         text.render(self.surface)
78         # self.chosen may be None, in which case we don't draw the border.
79         if choice == self.chosen:
80             if not action.check_available(self.state.player):
81                 colour = (255, 0, 0, 255)
82             else:
83                 colour = (255, 255, 0, 128)
84             bottom = y_offset + text.surface.get_rect().height
85             right = text.surface.get_rect().width + 12
86             pygame.draw.lines(self.surface, colour, True,
87                               [(18, y_offset), (right, y_offset),
88                                (right, bottom), (18, bottom)], 4)
89         if action.required_bits:
90             img_name = BIT_MAP[action.required_bits].replace(
91                 '.png', '_small.png')
92             img = resources.get_image(img_name,
93                                       transforms=(EIGHT_BIT,))
94             self.surface.blit(img, (0, y_offset))
95         return y_offset + text.surface.get_rect().height + 16
96
97     def set_position(self, position):
98         self.card = self.state.board_locations[position]
99         if self.state.gameboard.player_mode == ACT:
100             if self.chosen is None:
101                 self.chosen = 0
102         else:
103             self.chosen = None
104
105     def draw(self, surface):
106         surface.blit(self.surface, self.pos)
107
108     def handle_event(self, ev):
109         if self.state.gameboard.player_mode in (MOVE, EXAMINE):
110             return super(InfoAreaWidget, self).handle_event(ev)
111         if ev.type == pgl.KEYDOWN:
112             if ev.key in KEYS.SELECT:
113                 player = self.state.player
114                 action = self.card.actions[self.chosen]
115                 if not action.check_available(player):
116                     print "BEEP!"
117                 else:
118                     action.perform_action(self.state.gameboard, self.card)
119                     self.state.gameboard.replace_card(player.position)
120                     self.state.gameboard.change_mode(MOVE)
121                 return finish_event()
122             if ev.key in KEYS.UP:
123                 if self.chosen > 0:
124                     self.chosen -= 1
125                 return finish_event()
126             if ev.key in KEYS.DOWN:
127                 if self.chosen + 1 < len(self.card.actions):
128                     self.chosen += 1
129                 return finish_event()
130         return super(InfoAreaWidget, self).handle_event(ev)