A TextWidget
authorStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 15:50:03 +0000 (17:50 +0200)
committerStefano Rivera <stefano@rivera.za.net>
Sun, 11 May 2014 15:50:03 +0000 (17:50 +0200)
naja/scenes/menu.py
naja/widgets/__init__.py [new file with mode: 0644]
naja/widgets/base.py [new file with mode: 0644]
naja/widgets/text.py [new file with mode: 0644]

index 27b7e9c23b629235c21decfc774ba0578d3d4134..3c703c3990d4d7e68c4d988e383da31888012a1a 100644 (file)
@@ -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 (file)
index 0000000..e69de29
diff --git a/naja/widgets/base.py b/naja/widgets/base.py
new file mode 100644 (file)
index 0000000..b922b62
--- /dev/null
@@ -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 (file)
index 0000000..239e10a
--- /dev/null
@@ -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)