changeset 503:f4b53c56757a

Multi line level names \o/
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 17 Sep 2011 23:46:05 +0200
parents 243b6f6ced3b
children c85b4affd80f
files TODO.txt mamba/widgets/levelbutton.py
diffstat 2 files changed, 32 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/TODO.txt	Sat Sep 17 23:43:29 2011 +0200
+++ b/TODO.txt	Sat Sep 17 23:46:05 2011 +0200
@@ -4,7 +4,6 @@
 General:
 * Highlight tile borders
 * Sounds, everywhere
-* Multi-line level names
 * Remove white buttons / text
 * Text widget:
   - numpad keys
--- a/mamba/widgets/levelbutton.py	Sat Sep 17 23:43:29 2011 +0200
+++ b/mamba/widgets/levelbutton.py	Sat Sep 17 23:46:05 2011 +0200
@@ -34,7 +34,6 @@
         dest_rect = pygame.Rect((self.border, self.border),
                                 (self.rect.width - self.border,
                                  self.rect.height - self.border))
-        # Level Thumbnail
         if not hasattr(self.level, 'button_thumbnail'):
             self.level.button_thumbnail = self.make_thumbnail(dest_rect)
         self.surface.blit(self.level.button_thumbnail, dest_rect)
@@ -43,19 +42,44 @@
             image = load_image('menus/tick.png')
             self.surface.blit(image, image.get_rect())
 
-        self._text = TextWidget((0, 0), self.text, fontsize=12, color='white')
-        self._text.prepare()
-        text_rect = pygame.Rect((0, 0), self.rect.size).inflate(
-                self._text.rect.width - self.rect.width,
-                self._text.rect.height - self.rect.height)
-        text_rect.top = self.rect.height - self._text.rect.height - self.border
-        self.surface.blit(self._text.surface, text_rect)
+        # We only have space for two lines
+        self._text_lines = self.wrap_text(self.text)[:2]
+
+        text_height = sum(line.rect.height for line in self._text_lines)
+        text_pos = self.level.button_thumbnail.get_rect().height
+        text_pos += (self.rect.height - text_height - text_pos) // 2
+        for line in self._text_lines:
+            text_rect = pygame.Rect(((self.rect.width - line.rect.width) // 2,
+                                     text_pos),
+                                    line.rect.size)
+            text_pos = text_rect.bottom
+            self.surface.blit(line.surface, text_rect)
 
         color = pygame.Color(FOCUS_COLOR if self.focussed else '#444444')
         pygame.draw.rect(self.surface, color, self.surface.get_rect(),
                          self.border + 1)
         self._state = (self.done, self.focussed)
 
+    def wrap_text(self, text):
+        args = {'rect': (0, 0),
+                'text': text,
+                'fontsize': 12,
+                'color': 'white',
+               }
+        w = TextWidget(**args)
+        w.prepare()
+        splitpoint = len(text)
+        max_width = self.rect.width - (self.border * 3)
+        while w.rect.width > max_width and ' ' in text[:splitpoint]:
+            splitpoint = text.rfind(' ', 0, splitpoint)
+            args['text'] = text[:splitpoint]
+            w = TextWidget(**args)
+            w.prepare()
+        if splitpoint < len(text):
+            return [w] + self.wrap_text(text[splitpoint + 1:])
+        else:
+            return [w]
+
     def draw(self, surface):
         if self._state != (self.done, self.focussed):
             self.prepare()