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):
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
+