Tweak hint position and add padding
[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.set_position(state.player.position)
42
43     def prepare(self):
44         if self.state.gameboard.player_mode in (ACT, MOVE):
45             self.set_position(self.state.player.position)
46         self.surface = pygame.surface.Surface(INFO_SIZE)
47         self.surface.fill((0, 0, 0))
48         # Extract actions and such from the card
49         title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
50                            colour=(255, 255, 255))
51         title.render(self.surface)
52         y_offset = title.surface.get_rect().height + 8
53
54         # TODO: Make this better.
55         bits_text = ''.join('1' if bit in self.card.bitwise_operand else '0'
56                             for bit in reversed(range(8)))
57         card_bits = TextWidget((0, y_offset), bits_text, colour=(255, 255, 0))
58         card_bits.render(self.surface)
59         y_offset += card_bits.surface.get_rect().height + 8
60
61         for choice, action in enumerate(self.card.actions):
62             y_offset = self.prepare_action(choice, action, y_offset)
63         # We cheat horribly for layout reasons
64         hint = TextBoxWidget((4, 0), HINTS[self.state.gameboard.player_mode],
65                              padding=2,
66                              box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
67         hint.prepare()
68         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
69         self.surface.blit(hint.surface, (4, y_offset))
70
71     def prepare_action(self, choice, action, y_offset):
72         if action.required_bits:
73             img_name = BIT_MAP[action.required_bits].replace(
74                 '.png', '_small.png')
75             img = resources.get_image(img_name,
76                                       transforms=(EIGHT_BIT,))
77             self.surface.blit(img, (0, y_offset))
78             y_offset += 8
79         text = TextBoxWidget(
80             (12, y_offset), action.get_text(),
81             box_width=(INFO_SIZE[0] - 12) // EIGHT_BIT_SCALE,
82             fontsize=28)
83         text.render(self.surface)
84
85         # self.chosen may be None, in which case we don't draw the border.
86         if choice == self.chosen:
87             if not action.check_available(self.state.player):
88                 colour = (255, 0, 0, 255)
89             else:
90                 colour = (255, 255, 0, 128)
91             bottom = y_offset + text.surface.get_rect().height
92             right = text.surface.get_rect().width + 12
93             pygame.draw.lines(self.surface, colour, True,
94                               [(12, y_offset), (right, y_offset),
95                                (right, bottom), (12, bottom)], 4)
96         return y_offset + text.surface.get_rect().height + 16
97
98     def set_position(self, position):
99         self.card = self.state.board_locations[position]
100         if self.state.gameboard.player_mode == ACT:
101             if self.chosen is None:
102                 self.chosen = 0
103         else:
104             self.chosen = None
105
106     def draw(self, surface):
107         surface.blit(self.surface, self.pos)
108
109     def handle_event(self, ev):
110         if self.state.gameboard.player_mode in (MOVE, EXAMINE):
111             return super(InfoAreaWidget, self).handle_event(ev)
112         if ev.type == pgl.KEYDOWN:
113             if ev.key in KEYS.SELECT:
114                 player = self.state.player
115                 action = self.card.actions[self.chosen]
116                 if not action.check_available(player):
117                     print "BEEP!"
118                 else:
119                     action.perform_action(self.state.gameboard, self.card)
120                     self.state.gameboard.replace_card(player.position)
121                     self.state.gameboard.change_mode(MOVE)
122                 return finish_event()
123             if ev.key in KEYS.UP:
124                 if self.chosen > 0:
125                     self.chosen -= 1
126                 return finish_event()
127             if ev.key in KEYS.DOWN:
128                 if self.chosen + 1 < len(self.card.actions):
129                     self.chosen += 1
130                 return finish_event()
131         return super(InfoAreaWidget, self).handle_event(ev)