From f4c61efe7a3154f965781ea65117583750d39d3e Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sun, 11 May 2014 17:56:50 +0200 Subject: [PATCH] Refactor widget rendering a bit. --- naja/scenes/menu.py | 2 +- naja/widgets/base.py | 11 ++++++----- naja/widgets/text.py | 3 +-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index 3c703c3..7b42ad7 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -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 diff --git a/naja/widgets/base.py b/naja/widgets/base.py index b922b62..32ff46f 100644 --- a/naja/widgets/base.py +++ b/naja/widgets/base.py @@ -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() diff --git a/naja/widgets/text.py b/naja/widgets/text.py index 239e10a..39178ed 100644 --- a/naja/widgets/text.py +++ b/naja/widgets/text.py @@ -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) -- 2.34.1