Refactor widget rendering a bit.
authorSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 15:56:50 +0000 (17:56 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 15:56:50 +0000 (17:56 +0200)
naja/scenes/menu.py
naja/widgets/base.py
naja/widgets/text.py

index 3c703c3990d4d7e68c4d988e383da31888012a1a..7b42ad7b730d90959f078e89c4ffe98ed13207d5 100644 (file)
@@ -12,7 +12,7 @@ class MenuScene(Scene):
         self.tw = TextWidget((10, 10), 'Haai', colour=(255, 255, 255))
 
     def render(self, surface):
-        self.tw.draw(surface)
+        self.tw.render(surface)
 
     def handle_event(self, ev):
         pass
index b922b62280a595d61b1e422685add1b87ba9ad6a..32ff46fa21811e3b99b61d84e6fbac8325cc2433 100644 (file)
@@ -11,13 +11,14 @@ class Widget(object):
     def rect(self):
         return pygame.Rect(self.pos, self.size)
 
+    def render(self, surface):
+        if not self._prepared:
+            self.prepare()
+            self._prepared = True
+        self.draw(surface)
+
     def draw(self, surface):
         raise NotImplemented()
 
     def prepare(self):
-        if not self._prepared:
-            self._prepare()
-            self._prepared = True
-
-    def _prepare(self):
         raise NotImplemented()
index 239e10a621b2a74bf9af8a1ad2320582a9308a09..39178ed8777ee3e500276b95c975993721e681e2 100644 (file)
@@ -13,11 +13,10 @@ class TextWidget(Widget):
         self.fontsize = fontsize or FONT_SIZE
         self.colour = colour or (0, 0, 0)
 
-    def _prepare(self):
+    def prepare(self):
         self.font = resources.get_font(self.fontname, self.fontsize)
         self.surface = self.font.render(self.text, True, self.colour)
         self.size = self.surface.get_rect().size
 
     def draw(self, surface):
-        self.prepare()
         surface.blit(self.surface, self.rect)