Dvorak support (also, we need to factor these out...)
[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
8 from naja.events import InvalidateTheWorld
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.card = None
36         self.state = state
37         self.chosen = 0
38
39     def prepare(self):
40         self.surface = pygame.surface.Surface(INFO_SIZE)
41         self.surface.fill((0, 0, 0))
42         # Quick hack for testing
43         self.card = self.state.board_locations[self.state.player.position]
44         # Extract actions and such from the card
45         title = TextWidget((0, 0), TITLES[self.state.gameboard.player_mode],
46                            colour=(255, 255, 255))
47         title.render(self.surface)
48         y_offset = title.surface.get_rect().height + 8
49         for choice, action in enumerate(self.card.actions):
50             if action.required_bits:
51                 img_name = BIT_MAP[action.required_bits].replace('.png', '_small.png')
52                 img = resources.get_image(img_name,
53                                           transforms=(EIGHT_BIT,))
54                 self.surface.blit(img, (0, y_offset))
55                 y_offset += 8
56             text = TextBoxWidget((12, y_offset), action.TEXT,
57                                  box_width=(INFO_SIZE[0] - 12) // EIGHT_BIT_SCALE,
58                                  fontsize=28)
59             text.render(self.surface)
60             if choice == self.chosen:
61                 colour = (255, 255, 0, 128)
62                 bottom = y_offset + text.surface.get_rect().height
63                 right = text.surface.get_rect().width + 12
64                 pygame.draw.lines(self.surface, colour, True,
65                                   [(12, y_offset), (right, y_offset),
66                                    (right, bottom), (12, bottom)], 4)
67             y_offset += text.surface.get_rect().height + 16
68         # We cheat horribly for layout reasons
69         hint = TextBoxWidget((0, 0), HINTS[self.state.gameboard.player_mode],
70                              box_width = INFO_SIZE[0] // EIGHT_BIT_SCALE)
71         hint.prepare()
72         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
73         self.surface.blit(hint.surface, (0, y_offset))
74
75     def set_card(self, card):
76         self.card = card
77
78     def draw(self, surface):
79         surface.blit(self.surface, self.pos)
80
81     def handle_event(self, ev):
82         if self.state.gameboard.player_mode == MOVE:
83             return super(InfoAreaWidget, self).handle_event(ev)
84         if ev.type == pgl.KEYDOWN:
85             if ev.key in (pgl.K_RETURN, pgl.K_KP_ENTER):
86                 self.card.actions[self.chosen].perform_action(
87                     self.state.gameboard, self.card)
88                 self.state.gameboard.change_mode()
89                 InvalidateTheWorld.post()
90                 return True
91             if ev.key in (pgl.K_UP, pgl.K_w, pgl.K_COMMA):
92                 if self.chosen > 0:
93                     self.chosen -= 1
94                     InvalidateTheWorld.post()
95                     return True
96             if ev.key in (pgl.K_DOWN, pgl.K_s, pgl.K_o):
97                 if self.chosen + 1 < len(self.card.actions):
98                     self.chosen += 1
99                     InvalidateTheWorld.post()
100                     return True
101         return super(InfoAreaWidget, self).handle_event(ev)