def bicolor_text(text, font_size, color1, color2, space=0): import thorpy get_shadow = thorpy.graphics.get_shadow writer = Writer(size=font_size) width = writer.get_width(text) + (len(text)-1)*space height = writer.get_height() surface = pygame.Surface((width, height)) alpha_color = (50,100,150) surface.fill(alpha_color) surface.set_colorkey(alpha_color) x = 0 for letter in text: img = make_text(letter, font_size-20, color1).get_image() shad = get_shadow(img, shadow_radius=0, black=255, color_format="RGBA", alpha_factor=1., decay_mode="exponential", color=color2, sun_angle=45., vertical=True, angle_mode="flip", mode_value=(False, False)) size = shad.get_rect().inflate(6,6).size shad = pygame.transform.smoothscale(shad, size) img1 = shad img2 = img r1 = img1.get_rect() r1.left = x r2 = img2.get_rect() r2.center = r1.center surface.blit(img1, r1) surface.blit(img2, r2) x += img1.get_width() + space return surface
def __init__(self, text="", margin=None, writer=None): margin = style.INSERTWRITER_MARGIN if margin is None else margin OneLineText.__init__(self, text) self.margin = margin if not writer: self.writer = Writer() else: self.writer = writer
class _InsertWriter(OneLineText): def __init__(self, text="", margin=None, writer=None, finish=True): margin = style.INSERTWRITER_MARGIN if margin is None else margin OneLineText.__init__(self, text, finish=False) self.margin = margin if not writer: self.writer = Writer() else: self.writer = writer if finish: self.finish() def get_zone(self): return self.father.get_clip() # return self.father.get_fus_rect() def _is_small_enough(self, word): w = self.current_state.fusionner.title._writer.get_width(word) if w + self.margin >= self.get_zone().width: return False return True def refresh_img(self): """Refresh self's text. Returns -1 if the text is too large.""" text = self.father._inserted txt_img = self.writer.get_imgs(text)[0] if txt_img.get_size()[0] + self.margin >= self.get_zone().width: return -1 self.set_text(text) def _refresh_pos(self): zone = self.get_zone() y = (zone.height - self.writer.get_height()) / 2 self.set_topleft((zone.x + self.margin, zone.y + y)) def _get_cursor_pos(self): text = self.father._inserted[0:self.father._cursor_index] w = self.current_state.fusionner.title._writer.get_width(text) zone = self.get_zone() curs_height = self.father.cursor.get_fus_size()[1] y = zone.y + (zone.h - curs_height) / 2 return (zone.x + self.margin + w, y)
def __init__(self, files, size, writer=None, writer_hover=None, gap=None, x=None, folders=None, fold_img=None): gap = style.DIRVIEWER_GAP if gap is None else gap x = style.DIRVIEWER_X if x is None else x self.files = files self.size = size if writer: self.writer = writer else: self.writer = Writer() if writer_hover: self.writer_hover = writer_hover else: self.writer_hover = Writer(color=(255, 0, 0)) self.gap = gap self.elh = self.writer.get_height() self.pix_0 = 0 self.x = x self._hovered = -1 if folders: self.folders = folders else: self.folders = [] self.N = len(self.files) + len(self.folders) if fold_img: self.fold_img = fold_img elif self.folders: self.fold_img = load_image(filename=style.FOLDER_IMG, colorkey=style.FOLDER_IMG_COLORKEY) if fold_img and (self.x is None): self.x = self.fold_img.get_width() elif self.x is None: self.x = 25 self.folders_separated = True self.sort()
class _DirViewer(object): def __init__(self, files, size, writer=None, writer_hover=None, gap=None, x=None, folders=None, fold_img=None): gap = style.DIRVIEWER_GAP if gap is None else gap x = style.DIRVIEWER_X if x is None else x self.files = files self.size = size if writer: self.writer = writer else: self.writer = Writer() if writer_hover: self.writer_hover = writer_hover else: self.writer_hover = Writer(color=(255, 0, 0)) self.gap = gap self.elh = self.writer.get_height() self.pix_0 = 0 self.x = x self._hovered = -1 if folders: self.folders = folders else: self.folders = [] self.N = len(self.files) + len(self.folders) if fold_img: self.fold_img = fold_img elif self.folders: self.fold_img = load_image(filename=style.FOLDER_IMG, colorkey=style.FOLDER_IMG_COLORKEY) if fold_img and (self.x is None): self.x = self.fold_img.get_width() elif self.x is None: self.x = 25 self.folders_separated = True self.sort() def sort(self): if self.folders_separated: new_files = list(self.folders) for f in self.files: if not(f in self.folders): new_files.append(f) self.files = new_files def control(self): if self.pix_0 < 0: raise ValueError("DirViewer's pix_0 is negative" + str(self.pix_0)) elif self.get_n() - 1 > self.N: raise ValueError( "DirViewer's pix_0 is too large" + str(self.pix_0)) def get_n(self): """Returns index of the first text to be blitted""" n = float(self.pix_0 - self.gap) / (self.elh + self.gap) n = int(n + 1.) return max(n - 1, 0) def get_y(self, n): """Returns y-coord of the n-th text to be blitted""" return n * (self.gap + self.elh) - self.pix_0 def get_n_at_y(self, y): """Returns index of the element at coord y""" return int((y + self.pix_0) / (self.gap + self.elh)) def get_at_pix(self, x, y): """Returns the index of text in position (x, y)""" if x < 0 or x > self.size[0]: return None elif y < 0 or y > self.size[1]: return None else: return self.get_n_at_y(y) def get_txt_at_pix(self, x, y): index = self.get_at_pix(x, y) try: if index is not None: return self.files[index] else: return None except IndexError: return None def blit_on(self, surface, cursor, pos): """Blit the files texts on self.surface, not on browser!!""" i = self.get_n() y = self.get_y(i) _hovered = self.get_at_pix(cursor[0], cursor[1]) while y < self.size[1] and i < self.N: text = self.files[i] if i == _hovered: txt_img = self.writer_hover.get_imgs(text)[0] else: txt_img = self.writer.get_imgs(text)[0] if text in self.folders: surface.blit(self.fold_img, (pos[0] + 2, pos[1] + y)) surface.blit(txt_img, (pos[0] + self.x, pos[1] + y)) i += 1 y += self.elh + self.gap def get_real_size(self): w = self.size[0] L = len(self.files) h = L * self.elh + (L - 1) * self.gap return (w, h)