import pygame
-from naja.constants import FONT, FONT_SIZE
-from naja.widgets.base import Widget
+from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE
from naja.resources import resources
+from naja.resources.mutators import EIGHT_BIT, blender
from naja.utils import convert_colour
+from naja.widgets.base import Widget
+
+
+MARKUP_MAP = {
+ 'NORTH': (1, 'glyphs/arrow_up.png', None),
+ 'SOUTH': (1, 'glyphs/arrow_down.png', None),
+ 'EAST': (1, 'glyphs/arrow_right.png', None),
+ 'WEST': (1, 'glyphs/arrow_left.png', None),
+ 'HEALTH': (1, 'glyphs/health.png', PALETTE.DARK_RED),
+ 'WINTOKEN': (1, 'glyphs/win.png', PALETTE.DARK_OLIVE),
+ 'KEY': (1, 'glyphs/key.png', None),
+ 'MSB': (1, 'glyphs/msb.png', None),
+ 'REDKEY': (1, 'glyphs/key.png', PALETTE.ORANGE),
+ 'GREENKEY': (1, 'glyphs/key.png', PALETTE.GREEN),
+ 'BLUEKEY': (1, 'glyphs/key.png', PALETTE.BLUE),
+}
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.colour = convert_colour(colour or (0, 0, 0))
+ self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
+ self.colour = convert_colour(colour or PALETTE.BLACK)
+
+ 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):
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)))
+ PALETTE.LIGHT_VIOLET))
self.border_colour = convert_colour(kwargs.pop('border_colour',
- (0, 0, 0)))
+ PALETTE.BLACK))
self.box_width = kwargs.pop('box_width', 0)
super(TextBoxWidget, self).__init__(*args, **kwargs)
+ def lines(self, image_map):
+ if self.box_width != 0:
+ return self._wrapped_lines(image_map)
+ else:
+ return self.text.splitlines()
+
+ def _wrapped_lines(self, image_map):
+ def words_fit(words):
+ words_line = ' '.join(words)
+ width = self.font.size(words_line)[0]
+ if width < self.box_width:
+ return True
+ elif len(words) == 1:
+ Exception("Word %r too long for box." % (words[0],))
+ return False
+
+ line_count = 0
+ for line in self.text.splitlines():
+ current_words = []
+ remaining_words = line.split()
+ while remaining_words:
+ word = remaining_words[0]
+ suffix = ''
+ if word[-1] in '.,':
+ suffix = word[-1]
+ word = word[:-1]
+ markup_data = MARKUP_MAP.get(word, None)
+ if markup_data is not None:
+ word = ' ' * markup_data[0]
+ word += suffix
+ current_words.append(word)
+ if words_fit(current_words):
+ if markup_data is not None:
+ size = self.font.size(
+ ' '.join(current_words[:-1] + ['']))
+ pos = (size[0] * EIGHT_BIT_SCALE,
+ size[1] * line_count * EIGHT_BIT_SCALE)
+ pos = (pos[0] + self.padding, pos[1] + self.padding)
+ colour = self.colour
+ if markup_data[2] is not None:
+ colour = markup_data[2]
+ image_map[pos] = resources.get_image(
+ markup_data[1],
+ transforms=(EIGHT_BIT, blender(colour)))
+ remaining_words.pop(0)
+ else:
+ line_count += 1
+ yield ' '.join(current_words[:-1])
+ current_words = []
+ if current_words and words_fit(current_words):
+ yield ' '.join(current_words)
+
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
+ image_map = {}
+ rendered_lines = []
+ width, height = self.padding * 2, self.padding * 2
+ for line in self.lines(image_map):
+ line_surface = self.render_line(line)
+ line_rect = line_surface.get_rect()
+ rendered_lines.append(line_surface)
+ width = max(width, line_rect.width + self.padding * 2)
+ height += line_rect.height
+
self.surface = pygame.surface.Surface((width, height),
pygame.locals.SRCALPHA)
+ self.surface.fill(self.bg_colour)
+ self.size = self.surface.get_rect().size
+
+ x, y = self.padding, self.padding
+ for line_surface in rendered_lines:
+ self.surface.blit(line_surface, (x, y))
+ y += line_surface.get_rect().height
+ for pos, img in image_map.items():
+ self.surface.blit(img, pos)
def draw(self, surface):
- self.surface.fill(self.bg_colour)
- y = self.pos[1]
- for line in self.lines:
- surface.blit(line, (self.pos[0] + 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
+ surface.blit(self.surface, self.rect)