projects
/
naja.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Strip underscory stuff from JSON
[naja.git]
/
naja
/
widgets
/
text.py
diff --git
a/naja/widgets/text.py
b/naja/widgets/text.py
index 6862b968907ec7132a15bc1a01f509e63d446e45..1cf15d0bcf4601c72d52eb81d871ecedf8b454ec 100644
(file)
--- a/
naja/widgets/text.py
+++ b/
naja/widgets/text.py
@@
-22,6
+22,9
@@
MARKUP_MAP = {
'BLUE': ('glyphs/key.png', PALETTE.BLUE),
'CLOCKWISE': ('glyphs/clockwise.png', None),
'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None),
'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),
+ 'COUNTDOWN': ('glyphs/countdown_4.png', PALETTE.DARK_VIOLET),
'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
@@
-41,7
+44,8
@@
class TextWidget(Widget):
def __init__(self, pos, text, fontname=None, fontsize=None,
colour=None, unselectable_colour=None, view_port=None,
def __init__(self, pos, text, fontname=None, fontsize=None,
colour=None, unselectable_colour=None, view_port=None,
- centre=False):
+ centre=False, border=0, border_colour=PALETTE.GREY,
+ padding=2):
super(TextWidget, self).__init__(pos)
self.text = text
super(TextWidget, self).__init__(pos)
self.text = text
@@
-55,6
+59,9
@@
class TextWidget(Widget):
pygame.Rect((0, 0), view_port) if view_port is not None else None)
self.centre = centre
self.centre_pos = pos
pygame.Rect((0, 0), view_port) if view_port is not None else None)
self.centre = centre
self.centre_pos = pos
+ self.border = border
+ self.border_colour = convert_colour(border_colour)
+ self.padding = padding
def render_line(self, text):
colour = self.colour
def render_line(self, text):
colour = self.colour
@@
-66,6
+73,13
@@
class TextWidget(Widget):
text_surf, (text_rect.width * EIGHT_BIT_SCALE,
text_rect.height * EIGHT_BIT_SCALE))
text_surf, (text_rect.width * EIGHT_BIT_SCALE,
text_rect.height * EIGHT_BIT_SCALE))
+ def render_border(self, surface):
+ if not self.border or not self.border_colour:
+ return
+ rect = surface.get_rect()
+ rect = rect.inflate(- self.border / 2, - self.border / 2)
+ pygame.draw.rect(surface, self.border_colour, rect, self.border)
+
def update_size(self):
if self.view_port is not None:
self.size = self.view_port.size
def update_size(self):
if self.view_port is not None:
self.size = self.view_port.size
@@
-78,6
+92,7
@@
class TextWidget(Widget):
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)
self.surface = self.render_line(self.text)
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)
self.surface = self.render_line(self.text)
+ self.render_border(self.surface)
self.update_size()
def handle_event(self, ev):
self.update_size()
def handle_event(self, ev):
@@
-125,13
+140,11
@@
class TextWidget(Widget):
class TextBoxWidget(TextWidget):
def __init__(self, *args, **kwargs):
class TextBoxWidget(TextWidget):
def __init__(self, *args, **kwargs):
- self.padding = kwargs.pop('padding', 4)
- self.border = kwargs.pop('border', 2)
self.bg_colour = convert_colour(kwargs.pop('bg_colour',
PALETTE.LIGHT_VIOLET))
self.bg_colour = convert_colour(kwargs.pop('bg_colour',
PALETTE.LIGHT_VIOLET))
- self.border_colour = convert_colour(kwargs.pop('border_colour',
- PALETTE.BLACK))
self.box_width = kwargs.pop('box_width', 0)
self.box_width = kwargs.pop('box_width', 0)
+ self.full_width = kwargs.pop('full_width', True)
+ kwargs.setdefault('padding', 4)
super(TextBoxWidget, self).__init__(*args, **kwargs)
super(TextBoxWidget, self).__init__(*args, **kwargs)
@@
-160,6
+173,8
@@
class TextBoxWidget(TextWidget):
suffix = word[-1]
word = word[:-1]
suffix = word[-1]
word = word[:-1]
+ if not word:
+ return None
if word[0] == '{' and word[-1] == '}':
subwords = word[1:-1].split(',')
if all(subword in MARKUP_MAP for subword in subwords):
if word[0] == '{' and word[-1] == '}':
subwords = word[1:-1].split(',')
if all(subword in MARKUP_MAP for subword in subwords):
@@
-219,6
+234,9
@@
class TextBoxWidget(TextWidget):
width = max(width, line_rect.width + self.padding * 2)
height += line_rect.height
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)
self.surface = pygame.surface.Surface((width, height),
pygame.locals.SRCALPHA)
self.surface.fill(self.bg_colour)
@@
-226,7
+244,12
@@
class TextBoxWidget(TextWidget):
x, y = self.padding, self.padding
for line_surface in rendered_lines:
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():
self.surface.blit(img, pos)
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)
+
+ self.render_border(self.surface)