# HG changeset patch # User Stefano Rivera # Date 1316295965 -7200 # Node ID f4b53c56757a15822a675989c081473baf87f687 # Parent 243b6f6ced3b1aaa9c1ad3e56502a2f98a40df0c Multi line level names \o/ diff -r 243b6f6ced3b -r f4b53c56757a TODO.txt --- 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 diff -r 243b6f6ced3b -r f4b53c56757a mamba/widgets/levelbutton.py --- 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()