Coloured glyphs.
[naja.git] / naja / widgets / text.py
1 import pygame
2
3 from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE
4 from naja.resources import resources
5 from naja.resources.mutators import EIGHT_BIT, blender
6 from naja.utils import convert_colour
7 from naja.widgets.base import Widget
8
9
10 MARKUP_MAP = {
11     'NORTH': (1, 'glyphs/arrow_up.png', None),
12     'SOUTH': (1, 'glyphs/arrow_down.png', None),
13     'EAST': (1, 'glyphs/arrow_right.png', None),
14     'WEST': (1, 'glyphs/arrow_left.png', None),
15     'HEALTH': (1, 'glyphs/health.png', PALETTE.DARK_RED),
16     'WINTOKEN': (1, 'glyphs/win.png', PALETTE.DARK_OLIVE),
17     'KEY': (1, 'glyphs/key.png', None),
18     'MSB': (1, 'glyphs/msb.png', None),
19     'REDKEY': (1, 'glyphs/key.png', PALETTE.ORANGE),
20     'GREENKEY': (1, 'glyphs/key.png', PALETTE.GREEN),
21     'BLUEKEY': (1, 'glyphs/key.png', PALETTE.BLUE),
22 }
23
24
25 class TextWidget(Widget):
26
27     def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
28                  colour=None):
29         super(TextWidget, self).__init__(pos, size)
30
31         self.text = text
32         self.fontname = fontname or FONT
33         self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
34         self.colour = convert_colour(colour or PALETTE.BLACK)
35
36     def render_line(self, text):
37         text_surf = self.font.render(text, True, self.colour)
38         text_rect = text_surf.get_rect()
39         return pygame.transform.scale(
40             text_surf, (text_rect.width * EIGHT_BIT_SCALE,
41                         text_rect.height * EIGHT_BIT_SCALE))
42
43     def prepare(self):
44         self.font = resources.get_font(self.fontname, self.fontsize)
45         self.surface = self.render_line(self.text)
46         self.size = self.surface.get_rect().size
47
48     def draw(self, surface):
49         surface.blit(self.surface, self.pos)
50
51
52 class TextBoxWidget(TextWidget):
53     def __init__(self, *args, **kwargs):
54         self.padding = kwargs.pop('padding', 5)
55         self.border = kwargs.pop('border', 2)
56         self.bg_colour = convert_colour(kwargs.pop('bg_colour',
57                                                    PALETTE.LIGHT_VIOLET))
58         self.border_colour = convert_colour(kwargs.pop('border_colour',
59                                                        PALETTE.BLACK))
60         self.box_width = kwargs.pop('box_width', 0)
61
62         super(TextBoxWidget, self).__init__(*args, **kwargs)
63
64     def lines(self, image_map):
65         if self.box_width != 0:
66             return self._wrapped_lines(image_map)
67         else:
68             return self.text.splitlines()
69
70     def _wrapped_lines(self, image_map):
71         def words_fit(words):
72             words_line = ' '.join(words)
73             width = self.font.size(words_line)[0]
74             if width < self.box_width:
75                 return True
76             elif len(words) == 1:
77                 Exception("Word %r too long for box." % (words[0],))
78             return False
79
80         line_count = 0
81         for line in self.text.splitlines():
82             current_words = []
83             remaining_words = line.split()
84             while remaining_words:
85                 word = remaining_words[0]
86                 suffix = ''
87                 if word[-1] in '.,':
88                     suffix = word[-1]
89                     word = word[:-1]
90                 markup_data = MARKUP_MAP.get(word, None)
91                 if markup_data is not None:
92                     word = ' ' * markup_data[0]
93                 word += suffix
94                 current_words.append(word)
95                 if words_fit(current_words):
96                     if markup_data is not None:
97                         size = self.font.size(
98                             ' '.join(current_words[:-1] + ['']))
99                         pos = (size[0] * EIGHT_BIT_SCALE,
100                                size[1] * line_count * EIGHT_BIT_SCALE)
101                         pos = (pos[0] + self.padding, pos[1] + self.padding)
102                         colour = self.colour
103                         if markup_data[2] is not None:
104                             colour = markup_data[2]
105                         image_map[pos] = resources.get_image(
106                             markup_data[1],
107                             transforms=(EIGHT_BIT, blender(colour)))
108                     remaining_words.pop(0)
109                 else:
110                     line_count += 1
111                     yield ' '.join(current_words[:-1])
112                     current_words = []
113             if current_words and words_fit(current_words):
114                 yield ' '.join(current_words)
115
116     def prepare(self):
117         self.font = resources.get_font(self.fontname, self.fontsize)
118         image_map = {}
119         rendered_lines = []
120         width, height = self.padding * 2, self.padding * 2
121         for line in self.lines(image_map):
122             line_surface = self.render_line(line)
123             line_rect = line_surface.get_rect()
124             rendered_lines.append(line_surface)
125             width = max(width, line_rect.width + self.padding * 2)
126             height += line_rect.height
127
128         self.surface = pygame.surface.Surface((width, height),
129                                               pygame.locals.SRCALPHA)
130         self.surface.fill(self.bg_colour)
131         self.size = self.surface.get_rect().size
132
133         x, y = self.padding, self.padding
134         for line_surface in rendered_lines:
135             self.surface.blit(line_surface, (x, y))
136             y += line_surface.get_rect().height
137         for pos, img in image_map.items():
138             self.surface.blit(img, pos)
139
140     def draw(self, surface):
141         surface.blit(self.surface, self.rect)