# -*- coding: utf-8 -*- import pygame import Image path = 'assets/images/dialog/' class Globe(pygame.sprite.Sprite): """ a basic label properties: font: font to use text: text to display fgColor: foreground color bgColor: background color center: position of label's center size: (width, height) of label """ def __init__(self, size, text = '', pico='left', fontName = "freesansbold.ttf"): pygame.sprite.Sprite.__init__(self) self.font = pygame.font.Font(fontName, 20) self.size = size self.image = None self.mImageMaster = None self.text = text self.center = (0, 0) self.mPico = pico self.fgColor = (0, 0, 0) self.mup_left = Image.loadImage(path + 'esquina-up-left.png') self.mup_right = Image.loadImage(path + 'esquina-up-right.png') self.mdown_left = Image.loadImage(path + 'esquina-down-left.png') self.mdown_right = Image.loadImage(path + 'esquina-down-right.png') self.mup = Image.loadImage(path + 'borde-sin-sombra.png') self.mdown = Image.loadImage(path + 'borde-con-sombra.png') self.mcenter = Image.loadImage(path + 'fondo.png') self.mleft = pygame.transform.rotate(self.mup, 90) self.mright = pygame.transform.rotate(self.mdown, 90) self.mpico_der = Image.loadImage(path + 'pico-der.png') self.mpico_izq = Image.loadImage(path + 'pico-izq.png') self._create_image() self._update_image() def _create_image(self): h = self.size[0] v = self.size[1] horizontal = (h - 68) / 34 vertical = (v - 68) / 34 horizontal = horizontal + 2 vertical = vertical + 2 h = horizontal * 34 v = vertical * 34 + 40 self.image = pygame.Surface((h, v)) self.image.fill((255,255,255)) for i in range(horizontal): for j in range(vertical): x = i * 34 y = j * 34 if (i == 0): if (j == 0): self.image.blit(self.mup_left, (x, y)) elif (j==(vertical - 1)): self.image.blit(self.mdown_left, (x, y)) else: self.image.blit(self.mleft, (x, y)) elif (i == (horizontal - 1)): if (j == 0): self.image.blit(self.mup_right, (x, y)) elif (j == (vertical - 1)): self.image.blit(self.mdown_right, (x, y)) else: self.image.blit(self.mright, (x, y)) elif (j == 0): if not(i == 0) and not(i == (horizontal-1)): self.image.blit(self.mup, (x, y)) elif (j == (vertical -1)): if not(i == 0) and not(i == (horizontal-1)): self.image.blit(self.mdown, (x, y)) else: self.image.blit(self.mcenter, (x, y)) px = (h - 68) / 2 py = v - 74 if self.mPico == 'left': self.image.blit(self.mpico_izq, (px, py)) elif self.mPico == 'right': self.image.blit(self.mpico_der, (px, py)) self.image.set_colorkey((255,255,255)) self.mImageMaster = self.image.copy() def set_center(self, aCenter): self.center = aCenter def set_size(self, aSize): self.size = aSize self._create_image() self._update_image() def set_fgColor(self, afgColor): self.fgColor = afgColor self._update_image() def set_bgColor(self, abgColor): self.bgColor = abgColor self._update_image() def set_text(self, aText): self.text = aText self._update_image() def _update_image(self): self.image = self.mImageMaster.copy() y = 25 lines = self.text.split("\n") for l in lines: text = self.font.render(l.strip(), 1, self.fgColor) xPos = (self.image.get_width() - text.get_width())/2 self.image.blit(text, (xPos, y)) y = y + self.font.get_height() + 15 def update(self): self.rect = self.image.get_rect() self.rect.center = self.center