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