Factor out EIGHT_BIT_SCALE.
authorSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 21:36:08 +0000 (23:36 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 11 May 2014 21:36:15 +0000 (23:36 +0200)
naja/constants.py
naja/scenes/credits.py
naja/widgets/text.py

index 7a968d06cc8cab32c81b6461f264864e90a6b5f8..45ffa3c0b6a065ea9cad4d30a982e11d1c5d12b5 100644 (file)
@@ -5,6 +5,7 @@ SCREEN = (800, 600)
 FPS = 40
 FONT = 'DejaVuSans.ttf'
 FONT_SIZE = 16
+EIGHT_BIT_SCALE = 4
 
 DEFAULTS = dict(
     debug=False,
index f50d9e1cf178cb9873a04fd4e6101b53f9284204..f7b1e5a2ca7482c49d93eac59df4595f647ff40c 100644 (file)
@@ -15,7 +15,7 @@ class CreditsScene(Scene):
 
     def __init__(self, state):
         super(CreditsScene, self).__init__(state)
-        self.add(TextWidget((360, 160), 'Credits', fontsize=32,
+        self.add(TextWidget((480, 160), 'Credits', fontsize=32,
                             colour='white'))
         self.add(TextBoxWidget((120, 30),
                                        'Your mom '
index 6ca239d6fbdc08d9e467a718fcf86d955d4cf252..0521e9b6a64483d47f3edc90c5e319f24b9b18f1 100644 (file)
@@ -1,27 +1,32 @@
 import pygame
 
-from naja.constants import FONT, FONT_SIZE
+from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE
 from naja.widgets.base import Widget
 from naja.resources import resources
 from naja.utils import convert_colour
 
 
 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) // 4
+        self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
         self.colour = convert_colour(colour or (0, 0, 0))
 
+    def render_line(self, text):
+        text_surf = self.font.render(text, True, self.colour)
+        text_rect = text_surf.get_rect()
+        return pygame.transform.scale(
+            text_surf, (text_rect.width * EIGHT_BIT_SCALE,
+                        text_rect.height * EIGHT_BIT_SCALE))
+
     def prepare(self):
         self.font = resources.get_font(self.fontname, self.fontsize)
-        text = self.font.render(self.text, True, self.colour)
-        text_rect = text.get_rect()
-        self.surface = pygame.transform.scale(text, (text_rect.width * 4,
-                                                     text_rect.height * 4))
+        self.surface = self.render_line(self.text)
         self.size = self.surface.get_rect().size
 
     def draw(self, surface):
@@ -73,10 +78,7 @@ class TextBoxWidget(TextWidget):
         rendered_lines = []
         width, height = self.padding, self.padding
         for line in self.lines():
-            line_surface = self.font.render(line, True, self.colour)
-            line_rect = line_surface.get_rect()
-            line_surface = pygame.transform.scale(
-                line_surface, (line_rect.width * 2, line_rect.height * 2))
+            line_surface = self.render_line(line)
             line_rect = line_surface.get_rect()
             rendered_lines.append(line_surface)
             width = max(width, line_rect.width)