Got text wrapping working.
authorDavid Sharpe <decoydavid@gmail.com>
Sun, 11 May 2014 19:54:38 +0000 (21:54 +0200)
committerDavid Sharpe <decoydavid@gmail.com>
Sun, 11 May 2014 19:54:38 +0000 (21:54 +0200)
naja/scenes/credits.py
naja/widgets/text.py

index a1c7d8f4ff3aefeb3b6fa4106b49a34e41e1e5e2..05e2f7d3ddf94fba577258a3a294617c47d4986e 100644 (file)
@@ -5,22 +5,23 @@ Main menu scene.
 import pygame.locals as pgl
 
 from naja.scenes.scene import Scene
-from naja.widgets.text import TextWidget
+from naja.widgets.text import TextWidget, TextBoxWidget
 from naja.events import SceneChangeEvent
 
-
 class CreditsScene(Scene):
 
     base_menu = None
 
     def __init__(self, state):
         super(CreditsScene, self).__init__(state)
-        self.widgets.append(TextWidget((60, 10), 'Credits', fontsize=32,
-                            colour='white'))
-        self.widgets.append(TextWidget((60, 30),
-                                       'Your mom\n'
-                                       'A stranger', fontsize=32,
+        self.widgets.append(TextWidget((120, 120), 'Credits', fontsize=32,
                             colour='white'))
+        self.widgets.append(TextBoxWidget((120, 30),
+                                       'Your mom '
+                                       'A stranger \nA \nbag full of snakes', fontsize=32,
+                            colour='white', padding=1, border=1,
+                            bg_colour='black', border_colour='black',
+                            box_width=100))
 
     def handle_event(self, ev):
         if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
index 53030c55b8ad730c4a7725f07dc1bf9c5dcf1f52..8b1e3c68314cec2786f7fbf5aaae8a1291289a5b 100644 (file)
@@ -26,3 +26,72 @@ class TextWidget(Widget):
 
     def draw(self, surface):
         surface.blit(self.surface, self.rect)
+
+class TextBoxWidget(TextWidget):
+    def __init__(self, *args, **kwargs):
+        self.padding = kwargs.pop('padding', 5)
+        self.border = kwargs.pop('border', 2)
+        self.bg_colour = convert_colour(kwargs.pop('bg_colour',
+                                                   (255, 255, 255, 192)))
+        self.border_colour = convert_colour(kwargs.pop('border_colour',
+                                                       (0, 0, 0)))
+        self.box_width = kwargs.pop('box_width', 0)
+
+        super(TextBoxWidget,self).__init__(*args, **kwargs)
+
+    def prepare(self):
+        self.font = resources.get_font(self.fontname, self.fontsize)
+        self.lines = []
+        height = 0
+        width = 0
+        for line in self.text.split('\n'):
+            if self.box_width is not 0:
+                current_line = ''
+                current_line_rendered = None
+                for word in line.split(' '):
+                    if len(current_line) > 0:
+                        current_line += ' '+ word
+                    else:
+                        current_line = word
+                    line = self.font.render(current_line, True, self.colour)
+                    if line.get_rect().width <= self.box_width:
+                        current_line_rendered = line
+                        width = max(width, line.get_rect().width)
+                        height += line.get_rect().height
+                    elif current_line_rendered is not None:
+                        line_rect = current_line_rendered.get_rect()
+                        self.lines.append(pygame.transform.scale(
+                            current_line_rendered,(line_rect.width * 2,
+                                                   line_rect.height * 2)))
+                        current_line = word
+                        current_line_rendered = self.font.render(word, True, self.colour)
+                    else:
+                        raise Exception('Line too long. Please resize or rephrase')
+                if current_line_rendered is not None:
+                    line_rect = current_line_rendered.get_rect()
+                    self.lines.append(pygame.transform.scale(
+                        current_line_rendered,(line_rect.width * 2,
+                                               line_rect.height * 2)))
+            else:
+                line = self.font.render(line, True, self.colour)
+                line_rect = line.get_rect()
+                width = max(width, line.get_rect().width)
+                height += line.get_rect().height
+                self.lines.append(pygame.transform.scale(line, (line_rect.width * 2,
+                                                             line_rect.height * 2)))
+
+        width += 2 + self.padding
+        height += 2 + self.padding
+        self.surface = pygame.surface.Surface((width, height),
+                                              pygame.locals.SRCALPHA)
+
+    def draw(self, surface):
+        self.surface.fill(self.bg_colour)
+        y = 0
+        for line in self.lines:
+            surface.blit(line, (self.padding, y + self.padding))
+            y += line.get_rect().height
+        self.text_rect = surface.get_rect()
+        if not self.size:
+            self.rect.size = self.text_rect.size
+