def __init__(self, renderer, font=None, text="", font_size=16, text_color=SDL_Color(255, 255, 255), background_color=SDL_Color(0, 0, 0)): self.renderer = renderer if font is None: font = FONTS.get_path("04B_20__.TTF") else: font = FONTS.get_path(font) self.font_manager = FontManager(font, bg_color=background_color) self._text = text self.font_size = font_size self.text_color = text_color self.background_color = background_color surface = self._create_surface() super(SoftSprite, self).__init__(surface, True)
def set_manager(self): self._manager = FM( font_path=self.font, size=self.font_size, color=self.color, bg_color=self.bg_color, )
def __init__(self, resources: Resources) -> None: self.ticks = -1 self.last_finish = -1 self.best_finish = -1 font = FontManager(resources.get_path("DejaVuSans.ttf"), size=28) white = Color(r=255, g=255, b=255) self.timer_textbox = TextBox(font, 50, 100, white) self.best_time_textbox = TextBox(font, 50, 150, white)
class Font: def __init__(self, paths): self._manager = FontManager(font_path=join(path, paths.pop(0)), size=14) for _font in paths: self._manager.add(font_path=join(path, _font), size=14) def set_weight(self, weight: Weight) -> None: self._manager.default_font = weight.string @property def weight(self) -> Weight: return Weight(string=self._manager.default_font) def set_size(self, size: int) -> None: self._manager.size = size @property def size(self) -> int: return self._manager.size def set_color(self, color: Color) -> None: self._manager.color = color @property def color(self) -> Color: manager_color = self._manager.color.__copy__() return Color( red=manager_color[0], green=manager_color[1], blue=manager_color[2], alpha=manager_color[3] ) def texture(self, text: str): texture = window.factory.from_text(text=text, fontmanager=self._manager) return texture
class SoftSprite(SoftwareSprite): def __init__(self, renderer, font=None, text="", font_size=16, text_color=SDL_Color(255, 255, 255), background_color=SDL_Color(0, 0, 0)): self.renderer = renderer if font is None: font = FONTS.get_path("04B_20__.TTF") else: font = FONTS.get_path(font) self.font_manager = FontManager(font, bg_color=background_color) self._text = text self.font_size = font_size self.text_color = text_color self.background_color = background_color surface = self._create_surface() super(SoftSprite, self).__init__(surface, True) def _create_surface(self): surface = self.font_manager.render(self._text) if surface is None: raise TTF_GetError() return surface def _update_surface(self): surface = self._create_surface() super(SoftSprite, self).__init__(surface, None) @property def text(self): return self._text @text.setter def text(self, value): if self._text == value: return self._text = value self._update_surface()
def __init__(self, paths): self._manager = FontManager(font_path=join(path, paths.pop(0)), size=14) for _font in paths: self._manager.add(font_path=join(path, _font), size=14)
class Text(Widget): def __init__( self, font="/usr/share/fonts/TTF/Vera.ttf", font_size=14, color=[255, 255, 255], bg_color=[0, 0, 0], ): # init parent super(Text, self).__init__() # set a default size self._font_size = font_size self._font = font self._color = Color(*color) self._bg_color = Color(*bg_color) # init the fontmanager self.set_manager() def set_manager(self): self._manager = FM( font_path=self.font, size=self.font_size, color=self.color, bg_color=self.bg_color, ) @property def font_size(self): return self._font_size @font_size.setter def font_size(self, font_size): self._font_size = int(font_size) self.set_manager() @property def font(self): return self._font @font.setter def font(self, font): self._font = font self.set_manager() @property def color(self): return self._color @color.setter def color(self, color): self._color = Color(*color) self.set_manager() @property def bg_color(self): return self._bg_color @bg_color.setter def bg_color(self, bg_color): self._bg_color = Color(*bg_color) self.set_manager() @property def text(self): return self._text @text.setter def text(self, text): self._text = text self._surface = self._manager.render(self._text) self.minWidth = self._surface.w self.width = self._surface.w self.minHeight = self._surface.h self.height = self._surface.h def createShaders(self): # set shaders self._shaders += t.shader_path("text/text.vsh") self._shaders += t.shader_path("text/text.fsh") # create buffers self._vertices = VBO(VERTEX_BUFFER) self._index = VBO(INDEX_BUFFER) self._vertices.create() self._index.create() # allocate buffers self._vertices.bind() self._vertices.allocate( self._mesh, len(self._mesh) * 4 ) self._vertices.release() # window self._index.bind() self._index.allocate( self._indices, len(self._indices) * 4 ) self._index.release() def draw(self, parent): GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glEnable(GL.GL_BLEND) self._shaders.bind() self._shaders.setUniformValue( "modelview", parent._widget_projection * self._model ) self._shaders.setUniformValue( "corner", self._corner, ) self._shaders.setUniformValue( "size", self._size, ) self._textures = parent.textures << [ ( self._surface, { "parameters": { "TEXTURE_MIN_FILTER": "LINEAR", "TEXTURE_MAG_FILTER": "LINEAR", "TEXTURE_WRAP_S": "CLAMP", "TEXTURE_WRAP_T": "CLAMP", } } ) ] self._shaders.setUniformValue( "texture0", self._textures[0], ) self._textures[0].activate() self._vertices.bind() self._shaders.enableAttributeArray("window") self._shaders.setAttributeBuffer( "window", self._mesh, ) self._vertices.release() self._index.bind() GL.glDrawElements( GL.GL_QUADS, self._npoints, GL.GL_UNSIGNED_INT, None ) self._index.release() self._shaders.disableAttributeArray("window") self._shaders.release() self._textures[0].release() def mouseEvent(self, event): pass