Grid iron.
[naja.git] / naja / widgets / save_slot.py
1 import pygame
2
3 from naja.constants import BITS, PALETTE
4 from naja.widgets.base import Widget
5 from naja.widgets.text import TextWidget, TextBoxWidget
6
7
8 class SaveSlotWidget(Widget):
9     """
10     Widget for displaying a save slot.
11     """
12     def __init__(self, pos, slot, game_data):
13         super(SaveSlotWidget, self).__init__(pos, (600, 64))
14         self.slot = slot
15         self.game_data = game_data
16
17     def prepare(self):
18         self.surface = pygame.surface.Surface(self.size)
19         header = self.get_slot_header()
20         name_text = "Slot %s: %s" % (self.slot, header)
21         name = TextWidget((0, 0), name_text, colour=PALETTE.WHITE)
22         name.render(self.surface)
23         self.prepare_game_state()
24
25     def get_slot_header(self):
26         if self.game_data is None:
27             return "--"
28         else:
29             return self.game_data['timestamp']
30
31     def render_state_glyph(self, x_offset, text, colour):
32         widget = TextBoxWidget(
33             (x_offset, 32), text, box_width=32, colour=colour,
34             bg_colour=PALETTE.BLACK)
35         widget.render(self.surface)
36
37     def prepare_game_state(self):
38         if self.game_data is None:
39             state_text = TextWidget((32, 32), '-EMPTY-', colour=PALETTE.WHITE)
40             state_text.render(self.surface)
41         else:
42             state_data = self.game_data['data']
43             x_offset = 0
44
45             for i in range(state_data['max_health']):
46                 x_offset += 32
47                 if i < state_data['health']:
48                     colour = PALETTE.PINK
49                 else:
50                     colour = PALETTE.DARK_RED
51                 self.render_state_glyph(x_offset, '{HEALTH_NOCOLOUR}', colour)
52
53             for i in range(state_data['wins_required']):
54                 x_offset += 32
55                 if i < state_data['wins']:
56                     colour = PALETTE.LIGHT_TURQUOISE
57                 else:
58                     colour = PALETTE.DARK_OLIVE
59                 self.render_state_glyph(
60                     x_offset, '{WINTOKEN_NOCOLOUR}', colour)
61
62             x_offset += 64
63             bit_names = dict((v, k) for k, v in BITS.items())
64             player_bits = state_data['player']['bits']
65             for bit in reversed(range(8)):
66                 x_offset += 32
67                 if (1 << bit) & player_bits:
68                     text = '{%s}' % (bit_names[bit],)
69                     self.render_state_glyph(x_offset, text, PALETTE.GREY)
70
71     def draw(self, surface):
72         surface.blit(self.surface, self.pos)