import pygame
+import pygame.locals as pgl
-from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE
+from naja.constants import FONT, FONT_SIZE, EIGHT_BIT_SCALE, PALETTE, KEYS
from naja.resources import resources
-from naja.resources.mutators import EIGHT_BIT, blender
+from naja.resources.mutators import EIGHT_BIT, R180, blender
from naja.utils import convert_colour
from naja.widgets.base import Widget
'WINTOKEN': ('glyphs/win.png', PALETTE.DARK_OLIVE),
'KEY': ('glyphs/key.png', None),
'MSB': ('glyphs/msb.png', None),
- 'REDKEY': ('glyphs/key.png', PALETTE.ORANGE),
- 'GREENKEY': ('glyphs/key.png', PALETTE.GREEN),
- 'BLUEKEY': ('glyphs/key.png', PALETTE.BLUE),
+ 'RED': ('glyphs/key.png', PALETTE.ORANGE),
+ 'GREEN': ('glyphs/key.png', PALETTE.GREEN),
+ 'BLUE': ('glyphs/key.png', PALETTE.BLUE),
+ 'CLOCKWISE': ('glyphs/clockwise.png', None),
+ 'ANTICLOCKWISE': ('glyphs/anticlockwise.png', None),
+
+ 'HEALTH_NOCOLOUR': ('glyphs/health.png', None),
+ 'WINTOKEN_NOCOLOUR': ('glyphs/win.png', None),
}
class TextWidget(Widget):
- def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
- colour=None):
- super(TextWidget, self).__init__(pos, size)
+ VIEW_PORT_DY = 50
+
+ def __init__(self, pos, text, fontname=None, fontsize=None,
+ colour=None, unselectable_colour=None, view_port=None,
+ centre=False):
+ super(TextWidget, self).__init__(pos)
self.text = text
self.fontname = fontname or FONT
self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
self.colour = convert_colour(colour or PALETTE.BLACK)
+ if unselectable_colour is not None:
+ unselectable_colour = convert_colour(unselectable_colour)
+ self.unselectable_colour = unselectable_colour
+ self.view_port = (
+ pygame.Rect((0, 0), view_port) if view_port is not None else None)
+ self.centre = centre
+ self.centre_pos = pos
def render_line(self, text):
- text_surf = self.font.render(text, True, self.colour)
+ colour = self.colour
+ if not self.is_selectable() and self.unselectable_colour is not None:
+ colour = self.unselectable_colour
+ text_surf = self.font.render(text, True, 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 update_size(self):
+ if self.view_port is not None:
+ self.size = self.view_port.size
+ else:
+ self.size = self.surface.get_rect().size
+ if self.centre:
+ self.pos = (self.centre_pos[0] - self.size[0] // 2,
+ self.centre_pos[1])
+
def prepare(self):
self.font = resources.get_font(self.fontname, self.fontsize)
self.surface = self.render_line(self.text)
- self.size = self.surface.get_rect().size
+ self.update_size()
+
+ def handle_event(self, ev):
+ if self.view_port is None:
+ return super(TextWidget, self).handle_event(ev)
+ if ev.type == pgl.KEYDOWN:
+ if ev.key in KEYS.DOWN:
+ self.view_port.move_ip(0, self.VIEW_PORT_DY)
+ if self.view_port.bottom > self.surface.get_rect().bottom:
+ self.view_port.bottom = self.surface.get_rect().bottom
+ return True
+ elif ev.key in KEYS.UP:
+ self.view_port.move_ip(0, -self.VIEW_PORT_DY)
+ if self.view_port.top < 0:
+ self.view_port.top = 0
+ return True
+ return super(TextWidget, self).handle_event(ev)
def draw(self, surface):
- surface.blit(self.surface, self.pos)
+ if self.view_port is None:
+ rect = self.rect
+ area = None
+ else:
+ rect = self.pos
+ area = self.view_port
+ surface.blit(self.surface, rect, area)
+ if self.view_port is not None:
+ self.draw_arrows(surface)
+
+ def draw_arrows(self, surface):
+ if self.view_port.top > 0:
+ up = resources.get_image('bits', 'arrow_on.png',
+ transforms=(EIGHT_BIT,))
+ icon_size = up.get_rect().height
+ pos = (self.pos[0] + self.view_port.width - icon_size, self.pos[1])
+ surface.blit(up, pos)
+ if self.view_port.bottom < self.surface.get_rect().bottom:
+ down = resources.get_image('bits', 'arrow_on.png',
+ transforms=(R180, EIGHT_BIT))
+ icon_size = down.get_rect().height
+ pos = (self.pos[0] + self.view_port.width - icon_size,
+ self.pos[1] + self.view_port.height - icon_size)
+ surface.blit(down, pos)
class TextBoxWidget(TextWidget):
def __init__(self, *args, **kwargs):
- self.padding = kwargs.pop('padding', 5)
+ self.padding = kwargs.pop('padding', 4)
self.border = kwargs.pop('border', 2)
self.bg_colour = convert_colour(kwargs.pop('bg_colour',
PALETTE.LIGHT_VIOLET))
subwords = word[1:-1].split(',')
if all(subword in MARKUP_MAP for subword in subwords):
return Glyph(word + suffix, subwords, suffix)
- elif word in MARKUP_MAP:
- return Glyph(word + suffix, [word], suffix)
return None
def _wrapped_lines(self, image_map):
def words_fit(words):
words_line = ' '.join(words)
- width = self.font.size(words_line)[0]
+ width = self.font.size(words_line)[0] * EIGHT_BIT_SCALE
if width < self.box_width:
return True
elif len(words) == 1:
line_count = 0
for line in self.text.splitlines():
+ line = line.strip()
+ if not line:
+ line_count += 1
+ yield line
+ continue
current_words = []
remaining_words = line.split()
while remaining_words:
word = glyph.markup_text
remaining_words.insert(0, word)
if current_words and words_fit(current_words):
+ line_count += 1
yield ' '.join(current_words)
def prepare(self):
self.surface = pygame.surface.Surface((width, height),
pygame.locals.SRCALPHA)
self.surface.fill(self.bg_colour)
- self.size = self.surface.get_rect().size
+ self.update_size()
x, y = self.padding, self.padding
for line_surface in rendered_lines:
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)