X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=tabakrolletjie%2Finfobar.py;h=6697892708fbfefba1eaf741e9050a7909b1ddaf;hb=81237ce63dc2e193dc5055dc3d997a0c191a3b7e;hp=f468ef60aa5611eec8aa7508cd9ccf0786318510;hpb=0d17029c082e948401706d0e7350f919378d12b7;p=tabakrolletjie.git diff --git a/tabakrolletjie/infobar.py b/tabakrolletjie/infobar.py index f468ef6..6697892 100644 --- a/tabakrolletjie/infobar.py +++ b/tabakrolletjie/infobar.py @@ -26,24 +26,69 @@ class InfoBar(object): "{gamestate.seeds} seeds", "{scene.turnip_count} plants", "battery {battery.current}/{battery.max}", - "power usage {scene.power_usage}" + "power usage {scene.power_usage}/h" ]) self._font = loader.load_font(FONTS['sans'], size=20) + self._text = None + self._infobar = None - def render(self, surface): + def render(self, surface, gamestate): + self._update(gamestate) surface.blit(self._infobar, self.pos, None) - def update(self, gamestate): + def _update_infobar(self): + text_img = self._font.render(self._text, True, self.color) + width = text_img.get_width() + 10 + height = text_img.get_height() + 10 + self._infobar = pygame.surface.Surface( + (width, height), pgl.SWSURFACE).convert_alpha() + self._infobar.fill((0, 0, 0, 64)) + self._infobar.blit(text_img, (5, 3), None) + + def _update(self, gamestate): options = { "gamestate": gamestate, "battery": self.battery, "scene": self.scene, "time_of_day": self.time_of_day, } text = self.template.format(**options) - text_img = self._font.render(text, True, self.color) + if text != self._text: + self._text = text + self._update_infobar() + + +class CountDownBar(object): + """ Count Down Bar. """ + + def __init__(self, units="h", color=(255, 255, 255), pos=(1024, 10)): + self.units = units + self.color = color + self.pos = pos + self.template = "{remaining:d}{units}" + self._font = loader.load_font(FONTS['sans'], size=48) + self._text = None + self._countbar = None + self._dest = None + + def render(self, surface, remaining): + self._update(remaining) + surface.blit(self._countbar, self._dest, None) + def _update_countbar(self): + text_img = self._font.render(self._text, True, self.color) width = text_img.get_width() + 10 height = text_img.get_height() + 10 - self._infobar = pygame.surface.Surface( + self._countbar = pygame.surface.Surface( (width, height), pgl.SWSURFACE).convert_alpha() - self._infobar.fill((0, 0, 0, 64)) - self._infobar.blit(text_img, (5, 3), None) + self._countbar.fill((0, 0, 0, 64)) + self._countbar.blit(text_img, (5, 3), None) + self._dest = (self.pos[0] - width, self.pos[1]) + + def _update(self, remaining): + options = { + "remaining": remaining, + "units": self.units, + } + text = self.template.format(**options) + if text != self._text: + self._text = text + self._update_countbar()