""" text.text - letters, texts and scaling and coloring of fonts """ import pygame from random import randrange from .fonts import Fonts from display import Scaling, CGA_COLORS scaled_fonts = {} class LetterSprite(pygame.sprite.Sprite): """ Single letter at given properties hopefully from cache args: font_key: (font name, width to scale, color) ch: 0-255 character on cp473 color: 0-15 as in CGA palette """ def __init__(self, font_key, ch, pos): super().__init__() self.dead = True if font_key not in scaled_fonts: scaled_fonts[font_key]=[None for _ in range(256)] if scaled_fonts[font_key][ch] == None: font, w, color = font_key scaled_fonts[font_key][ch] = pygame.transform.scale_by( Fonts.fonts[font].get(ch), w/8 * Scaling.factor) scaled_fonts[font_key][ch].fill( CGA_COLORS[color], special_flags = pygame.BLEND_RGBA_MULT) self.image = scaled_fonts[font_key][ch] self.rect = self.image.get_rect().move(pos) self.direction = ( Scaling.factor * (1_000 - randrange(2_000)), Scaling.factor * (1_000 - randrange(2_000))) def update(self, dt = 0, explode = 0, **kwargs): if explode and dt: self.rect = pygame.Rect( self.rect.x + self.direction[0] * dt, self.rect.y + self.direction[1] * dt, self.rect.w, self.rect.h) self.direction = ( self.direction[0] * 0.95, self.direction[1] * 0.95 + 0.3) class TextPage(pygame.sprite.Group): """ Creates sprite group out of given text and parameters args: text Just text. \xe0 - \xef to cahnge color on cga palette pos Position of right top corner in internal cooordinates size Single character size (w,h) grid Space for a character (w,h) font Font loaded in Fonts.fonts dict """ def __init__( self, text, pos = (0,0), size = (8_000,16_000), grid = None, font = 'lcd'): super().__init__() if grid == None: grid = size color = 0xf col, row = 0, 0 x, y = pos w, h = grid for ch_txt in text: if ch_txt == '\n': row += 1 col = 0 continue if ch_txt == '\t': col = (col + 4) % 4 continue ch = ord(ch_txt) if ch in range(0xe0,0xf0): color = ch - 0xe0 continue font_key = (font, size[0], color) sprite_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) self.add(LetterSprite(font_key, ch, sprite_pos)) col += 1