Display power usage per hour.
[tabakrolletjie.git] / tabakrolletjie / infobar.py
1 # -*- coding: utf-8 -*-
2
3 """ A bar. With info. """
4
5 import pygame.surface
6 import pygame.locals as pgl
7
8 from .constants import FONTS
9 from .loader import loader
10
11
12 class InfoBar(object):
13     """ Info.bar. """
14
15     def __init__(
16             self, time_of_day, battery, scene,
17             color=(255, 255, 255), pos=(50, 10)):
18         self.time_of_day = time_of_day
19         self.battery = battery
20         self.scene = scene
21         self.color = color
22         self.pos = pos
23         self.template = u" ยท ".join([
24             "{time_of_day} {gamestate.days}",
25             "{gamestate.harvested}/{gamestate.turnip_target} turnips",
26             "{gamestate.seeds} seeds",
27             "{scene.turnip_count} plants",
28             "battery {battery.current}/{battery.max}",
29             "power usage {scene.power_usage}/h"
30         ])
31         self._font = loader.load_font(FONTS['sans'], size=20)
32         self._text = None
33         self._infobar = None
34
35     def render(self, surface, gamestate):
36         self._update(gamestate)
37         surface.blit(self._infobar, self.pos, None)
38
39     def _update_infobar(self):
40         text_img = self._font.render(self._text, True, self.color)
41         width = text_img.get_width() + 10
42         height = text_img.get_height() + 10
43         self._infobar = pygame.surface.Surface(
44             (width, height), pgl.SWSURFACE).convert_alpha()
45         self._infobar.fill((0, 0, 0, 64))
46         self._infobar.blit(text_img, (5, 3), None)
47
48     def _update(self, gamestate):
49         options = {
50             "gamestate": gamestate, "battery": self.battery,
51             "scene": self.scene, "time_of_day": self.time_of_day,
52         }
53         text = self.template.format(**options)
54         if text != self._text:
55             self._text = text
56             self._update_infobar()
57
58
59 class CountDownBar(object):
60     """ Count Down Bar. """
61
62     def __init__(self, units="h", color=(255, 255, 255), pos=(1024, 10)):
63         self.units = units
64         self.color = color
65         self.pos = pos
66         self.template = "{remaining:d}{units}"
67         self._font = loader.load_font(FONTS['sans'], size=48)
68         self._text = None
69         self._countbar = None
70         self._dest = None
71
72     def render(self, surface, remaining):
73         self._update(remaining)
74         surface.blit(self._countbar, self._dest, None)
75
76     def _update_countbar(self):
77         text_img = self._font.render(self._text, True, self.color)
78         width = text_img.get_width() + 10
79         height = text_img.get_height() + 10
80         self._countbar = pygame.surface.Surface(
81             (width, height), pgl.SWSURFACE).convert_alpha()
82         self._countbar.fill((0, 0, 0, 64))
83         self._countbar.blit(text_img, (5, 3), None)
84         self._dest = (self.pos[0] - width, self.pos[1])
85
86     def _update(self, remaining):
87         options = {
88             "remaining": remaining,
89             "units": self.units,
90         }
91         text = self.template.format(**options)
92         if text != self._text:
93             self._text = text
94             self._update_countbar()