Add count down bar.
authorSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 19:06:30 +0000 (21:06 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 19:06:30 +0000 (21:06 +0200)
tabakrolletjie/infobar.py

index 58aa3b6a27bdfe10932ea6f1283bbc2599999346..ac9f1a95923f53b4aa1b5eb702428e617d6bfe94 100644 (file)
@@ -54,3 +54,41 @@ class InfoBar(object):
         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._countbar = pygame.surface.Surface(
+            (width, height), pgl.SWSURFACE).convert_alpha()
+        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()