dispense with movement mode
[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, 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         ACT: "Choose an action using the Up/Down keys.\n"
20              "Press Return to execute the action.",
21         EXAMINE: "Select a card to examine using the arrow keys."
22         }
23
24 HINT_LEGAL_MOVE = "\nPress Return to move to this card."
25
26 TITLES = {
27         ACT: "Choose an Action",
28         EXAMINE: "Select a Card",
29         }
30
31
32 class InfoAreaWidget(Widget):
33     """
34     Widget for the game board information area.
35     """
36     def __init__(self, pos, state):
37         super(InfoAreaWidget, self).__init__(pos, INFO_SIZE)
38         self.state = state
39         self.chosen = None
40         self.card_position = state.player.position
41         self.set_position(state.player.position)
42
43     def prepare(self):
44         if self.state.gameboard.player_mode == ACT:
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_text = HINTS[self.state.gameboard.player_mode]
65         if self.state.gameboard.player_mode == EXAMINE:
66             if self.card_position in self.state.player.legal_moves():
67                 hint_text += HINT_LEGAL_MOVE
68
69         hint = TextBoxWidget((4, 0), hint_text, padding=2,
70                              box_width=(INFO_SIZE[0] - 4) // EIGHT_BIT_SCALE)
71         hint.prepare()
72         y_offset = INFO_SIZE[1] - hint.surface.get_rect().height
73         self.surface.blit(hint.surface, (4, y_offset))
74
75     def prepare_action(self, choice, action, y_offset):
76         text = TextBoxWidget(
77             (18, y_offset), action.get_text(),
78             box_width=(INFO_SIZE[0] - 16) // EIGHT_BIT_SCALE,
79             fontsize=28)
80         text.render(self.surface)
81         # self.chosen may be None, in which case we don't draw the border.
82         if choice == self.chosen:
83             if not action.check_available(self.state.player):
84                 colour = (255, 0, 0, 255)
85             else:
86                 colour = (255, 255, 0, 128)
87             bottom = y_offset + text.surface.get_rect().height
88             right = text.surface.get_rect().width + 12
89             pygame.draw.lines(self.surface, colour, True,
90                               [(18, y_offset), (right, y_offset),
91                                (right, bottom), (18, bottom)], 4)
92         if action.required_bits:
93             img_name = BIT_MAP[action.required_bits].replace(
94                 '.png', '_small.png')
95             img = resources.get_image(img_name,
96                                       transforms=(EIGHT_BIT,))
97             self.surface.blit(img, (0, y_offset))
98         return y_offset + text.surface.get_rect().height + 16
99
100     def set_position(self, position):
101         self.card_position = position
102         self.card = self.state.board_locations[self.card_position]
103         if self.state.gameboard.player_mode == ACT:
104             if self.chosen is None:
105                 self.chosen = 0
106         else:
107             self.chosen = None
108
109     def draw(self, surface):
110         surface.blit(self.surface, self.pos)
111
112     def handle_event(self, ev):
113         if self.state.gameboard.player_mode == EXAMINE:
114             return super(InfoAreaWidget, self).handle_event(ev)
115         if ev.type == pgl.KEYDOWN:
116             if ev.key in KEYS.SELECT:
117                 player = self.state.player
118                 action = self.card.actions[self.chosen]
119                 if not action.check_available(player):
120                     print "BEEP!"
121                 else:
122                     action.perform_action(self.state.gameboard, self.card)
123                     self.state.gameboard.replace_card(player.position)
124                     self.state.gameboard.change_mode(EXAMINE)
125                     self.set_position(player.position)
126                 return finish_event()
127             if ev.key in KEYS.UP:
128                 if self.chosen > 0:
129                     self.chosen -= 1
130                 return finish_event()
131             if ev.key in KEYS.DOWN:
132                 if self.chosen + 1 < len(self.card.actions):
133                     self.chosen += 1
134                 return finish_event()
135         return super(InfoAreaWidget, self).handle_event(ev)