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