projects
/
naja.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
266907d
)
Text markup images.
author
Jeremy Thurgood
<firxen@gmail.com>
Thu, 15 May 2014 21:20:37 +0000
(23:20 +0200)
committer
Jeremy Thurgood
<firxen@gmail.com>
Thu, 15 May 2014 21:20:37 +0000
(23:20 +0200)
naja/widgets/text.py
patch
|
blob
|
history
diff --git
a/naja/widgets/text.py
b/naja/widgets/text.py
index d753e8a12aed87987e9f6f273e861d678eda5612..22914009f3a49fef75db2c115f7d59ada839d4b3 100644
(file)
--- a/
naja/widgets/text.py
+++ b/
naja/widgets/text.py
@@
-1,9
+1,18
@@
import pygame
from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE
import pygame
from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE
-from naja.widgets.base import Widget
from naja.resources import resources
from naja.resources import resources
+from naja.resources.mutators import EIGHT_BIT
from naja.utils import convert_colour
from naja.utils import convert_colour
+from naja.widgets.base import Widget
+
+
+MARKUP_MAP = {
+ 'NORTH': (1, 'glyphs/arrow_up.png'),
+ 'SOUTH': (1, 'glyphs/arrow_down.png'),
+ 'EAST': (1, 'glyphs/arrow_right.png'),
+ 'WEST': (1, 'glyphs/arrow_left.png'),
+}
class TextWidget(Widget):
class TextWidget(Widget):
@@
-45,13
+54,13
@@
class TextBoxWidget(TextWidget):
super(TextBoxWidget, self).__init__(*args, **kwargs)
super(TextBoxWidget, self).__init__(*args, **kwargs)
- def lines(self):
+ def lines(self
, image_map
):
if self.box_width != 0:
if self.box_width != 0:
- return self._wrapped_lines()
+ return self._wrapped_lines(
image_map
)
else:
return self.text.splitlines()
else:
return self.text.splitlines()
- def _wrapped_lines(self):
+ def _wrapped_lines(self
, image_map
):
def words_fit(words):
words_line = ' '.join(words)
width = self.font.size(words_line)[0]
def words_fit(words):
words_line = ' '.join(words)
width = self.font.size(words_line)[0]
@@
-61,13
+70,31
@@
class TextBoxWidget(TextWidget):
Exception("Word %r too long for box." % (words[0],))
return False
Exception("Word %r too long for box." % (words[0],))
return False
+ line_count = 0
for line in self.text.splitlines():
current_words = []
for word in line.split():
for line in self.text.splitlines():
current_words = []
for word in line.split():
+ 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):
current_words.append(word)
if words_fit(current_words):
- continue
+ 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)
+ image_map[pos] = resources.get_image(
+ markup_data[1], transforms=(EIGHT_BIT,))
+ continue
else:
else:
+ line_count += 1
yield ' '.join(current_words[:-1])
current_words = current_words[-1:]
if current_words and words_fit(current_words):
yield ' '.join(current_words[:-1])
current_words = current_words[-1:]
if current_words and words_fit(current_words):
@@
-75,13
+102,14
@@
class TextBoxWidget(TextWidget):
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)
+ image_map = {}
rendered_lines = []
rendered_lines = []
- width, height = self.padding
, self.padding
- for line in self.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)
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)
+ width = max(width, line_rect.width + self.padding
* 2
)
height += line_rect.height
self.surface = pygame.surface.Surface((width, height),
height += line_rect.height
self.surface = pygame.surface.Surface((width, height),
@@
-93,6
+121,8
@@
class TextBoxWidget(TextWidget):
for line_surface in rendered_lines:
self.surface.blit(line_surface, (x, y))
y += line_surface.get_rect().height
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):
surface.blit(self.surface, self.rect)
def draw(self, surface):
surface.blit(self.surface, self.rect)