From 0b4f46ac5262aed61cf3aeb994a1b55fa7823c7c Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 11 May 2014 17:50:03 +0200 Subject: [PATCH] A TextWidget --- naja/scenes/menu.py | 15 +++++++++++---- naja/widgets/__init__.py | 0 naja/widgets/base.py | 23 +++++++++++++++++++++++ naja/widgets/text.py | 23 +++++++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 naja/widgets/__init__.py create mode 100644 naja/widgets/base.py create mode 100644 naja/widgets/text.py diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index 27b7e9c..3c703c3 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -2,10 +2,17 @@ Main menu scene. """ -from .scene import Scene +from naja.scenes.scene import Scene +from naja.widgets.text import TextWidget class MenuScene(Scene): - """ - Main menu. - """ + + def __init__(self): + self.tw = TextWidget((10, 10), 'Haai', colour=(255, 255, 255)) + + def render(self, surface): + self.tw.draw(surface) + + def handle_event(self, ev): + pass diff --git a/naja/widgets/__init__.py b/naja/widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/naja/widgets/base.py b/naja/widgets/base.py new file mode 100644 index 0000000..b922b62 --- /dev/null +++ b/naja/widgets/base.py @@ -0,0 +1,23 @@ +import pygame + + +class Widget(object): + def __init__(self, pos, size=None): + self.pos = pos + self.size = size or (0, 0) + self._prepared = False + + @property + def rect(self): + return pygame.Rect(self.pos, self.size) + + 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 new file mode 100644 index 0000000..239e10a --- /dev/null +++ b/naja/widgets/text.py @@ -0,0 +1,23 @@ +from naja.constants import FONT, FONT_SIZE +from naja.widgets.base import Widget +from naja.resources import resources + + +class TextWidget(Widget): + def __init__(self, pos, text, size=None, fontname=None, fontsize=None, + colour=None): + super(TextWidget, self).__init__(pos, size) + + self.text = text + self.fontname = fontname or FONT + self.fontsize = fontsize or FONT_SIZE + self.colour = colour or (0, 0, 0) + + 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