Hook up shift glyph
[naja.git] / naja / widgets / text.py
index 7c1039ff4d2ef517f019c7d52b71f8a75c0149ea..302d2370f3fdf7d85d11c88c754f57c3b9e72102 100644 (file)
@@ -3,7 +3,7 @@ import pygame.locals as pgl
 
 from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE, KEYS
 from naja.resources import resources
-from naja.resources.mutators import EIGHT_BIT, blender
+from naja.resources.mutators import EIGHT_BIT, R180, blender
 from naja.utils import convert_colour
 from naja.widgets.base import Widget
 
@@ -22,6 +22,8 @@ MARKUP_MAP = {
     'BLUE': ('glyphs/key.png', PALETTE.BLUE),
     'CLOCKWISE': ('glyphs/clockwise.png', None),
     'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None),
+    'SHIFT_LEFT': ('glyphs/shift_left.png', None),
+    'SHIFT_RIGHT': ('glyphs/shift_right.png', None),
 
     'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
     'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
@@ -40,7 +42,8 @@ class TextWidget(Widget):
     VIEW_PORT_DY = 50
 
     def __init__(self, pos, text, fontname=None, fontsize=None,
-                 colour=None, unselectable_colour=None, view_port=None):
+                 colour=None, unselectable_colour=None, view_port=None,
+                 centre=False):
         super(TextWidget, self).__init__(pos)
 
         self.text = text
@@ -52,6 +55,8 @@ class TextWidget(Widget):
         self.unselectable_colour = unselectable_colour
         self.view_port = (
             pygame.Rect((0, 0), view_port) if view_port is not None else None)
+        self.centre = centre
+        self.centre_pos = pos
 
     def render_line(self, text):
         colour = self.colour
@@ -68,6 +73,9 @@ class TextWidget(Widget):
             self.size = self.view_port.size
         else:
             self.size = self.surface.get_rect().size
+        if self.centre:
+            self.pos = (self.centre_pos[0] - self.size[0] // 2,
+                        self.centre_pos[1])
 
     def prepare(self):
         self.font = resources.get_font(self.fontname, self.fontsize)
@@ -98,6 +106,23 @@ class TextWidget(Widget):
             rect = self.pos
             area = self.view_port
         surface.blit(self.surface, rect, area)
+        if self.view_port is not None:
+            self.draw_arrows(surface)
+
+    def draw_arrows(self, surface):
+        if self.view_port.top > 0:
+            up = resources.get_image('bits', 'arrow_on.png',
+                                     transforms=(EIGHT_BIT,))
+            icon_size = up.get_rect().height
+            pos = (self.pos[0] + self.view_port.width - icon_size, self.pos[1])
+            surface.blit(up, pos)
+        if self.view_port.bottom < self.surface.get_rect().bottom:
+            down = resources.get_image('bits', 'arrow_on.png',
+                                       transforms=(R180, EIGHT_BIT))
+            icon_size = down.get_rect().height
+            pos = (self.pos[0] + self.view_port.width - icon_size,
+                   self.pos[1] + self.view_port.height - icon_size)
+            surface.blit(down, pos)
 
 
 class TextBoxWidget(TextWidget):
@@ -110,6 +135,8 @@ class TextBoxWidget(TextWidget):
                                                        PALETTE.BLACK))
         self.box_width = kwargs.pop('box_width', 0)
 
+        self.full_width = kwargs.pop('full_width', True)
+
         super(TextBoxWidget, self).__init__(*args, **kwargs)
 
     def lines(self, image_map):
@@ -147,7 +174,7 @@ class TextBoxWidget(TextWidget):
     def _wrapped_lines(self, image_map):
         def words_fit(words):
             words_line = ' '.join(words)
-            width = self.font.size(words_line)[0]
+            width = self.font.size(words_line)[0] * EIGHT_BIT_SCALE
             if width < self.box_width:
                 return True
             elif len(words) == 1:
@@ -196,6 +223,9 @@ class TextBoxWidget(TextWidget):
             width = max(width, line_rect.width + self.padding * 2)
             height += line_rect.height
 
+        if self.full_width:
+            width = max(width, self.box_width)
+
         self.surface = pygame.surface.Surface((width, height),
                                               pygame.locals.SRCALPHA)
         self.surface.fill(self.bg_colour)
@@ -203,6 +233,9 @@ class TextBoxWidget(TextWidget):
 
         x, y = self.padding, self.padding
         for line_surface in rendered_lines:
+            if self.centre:
+                x = (width - line_surface.get_rect().width) / 2
+                x += self.padding
             self.surface.blit(line_surface, (x, y))
             y += line_surface.get_rect().height
         for pos, img in image_map.items():