3 from naja.constants import BITS, PALETTE
4 from naja.widgets.base import Widget
5 from naja.widgets.text import TextWidget, TextBoxWidget
8 class SaveSlotWidget(Widget):
10 Widget for displaying a save slot.
12 def __init__(self, pos, slot, game_data):
13 super(SaveSlotWidget, self).__init__(pos, (600, 64))
15 self.game_data = game_data
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()
25 def get_slot_header(self):
26 if self.game_data is None:
29 return self.game_data['timestamp']
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)
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)
42 state_data = self.game_data['data']
45 for i in range(state_data['max_health']):
47 if i < state_data['health']:
50 colour = PALETTE.DARK_RED
51 self.render_state_glyph(x_offset, '{HEALTH_NOCOLOUR}', colour)
53 for i in range(state_data['wins_required']):
55 if i < state_data['wins']:
56 colour = PALETTE.LIGHT_TURQUOISE
58 colour = PALETTE.DARK_OLIVE
59 self.render_state_glyph(
60 x_offset, '{WINTOKEN_NOCOLOUR}', colour)
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)):
67 if (1 << bit) & player_bits:
68 text = '{%s}' % (bit_names[bit],)
69 self.render_state_glyph(x_offset, text, PALETTE.GREY)
71 def draw(self, surface):
72 surface.blit(self.surface, self.pos)