def get_width(string, font=None): t = Text(string) if font: t.font = font w = t.width from ursina import destroy destroy(t) return w
def clear(self): from ursina.ursinastuff import destroy to_destroy = [e for e in self.entities if not e.eternal] to_keep = [e for e in self.entities if e.eternal] for d in to_destroy: destroy(d) self.entities = to_keep from ursina import application application.sequences.clear()
def clear(self): from ursina.ursinastuff import destroy to_destroy = [e for e in self.entities if not e.eternal] to_keep = [e for e in self.entities if e.eternal] for d in to_destroy: try: print('destroying:', d.name) destroy(d) except Exception as e: print('failed to destroy entity', e) self.entities = to_keep from ursina import application application.sequences.clear()
def create_background(self, padding=size * 2, radius=size, color=ursina.color.black66): from ursina import Quad, destroy if self._background: destroy(self._background) self._background = Entity(parent=self, z=.01) if isinstance(padding, (int, float, complex)): padding = (padding, padding) w, h = self.width + padding[0], self.height + padding[1] self._background.x -= self.origin_x * self.width self._background.y -= self.origin_y * self.height self._background.model = Quad(radius=radius, scale=(w / self.scale_x, h / self.scale_y)) self._background.color = color
def text(self, text): self.raw_text = text # clear stuff from ursina.ursinastuff import destroy # needed to destroy inline images for img in self.images: destroy(img) self.images = [] for tn in self.text_nodes: tn.remove_node() self.text_nodes = [] # check if using tags if (not self.use_tags or self.text == self.start_tag or self.text == self.end_tag or not self.start_tag in text or not self.end_tag in text): self.create_text_section(text) self.align() return # parse tags text = self.start_tag + self.end_tag + str( text) # start with empty tag for alignment to work? sections = [] section = '' tag = self.start_tag + 'default' + self.end_tag temp_text_node = TextNode('temp_text_node') temp_text_node.setFont(self.font) x = 0 y = 0 i = 0 while i < len(text): char = text[i] if char == '\n': sections.append([section, tag, x, y]) section = '' y -= 1 x = 0 i += 1 elif char == self.start_tag: # find tag sections.append([section, tag, x, y]) x += temp_text_node.calcWidth(section) section = '' tag = '' for j in range(len(text) - i): tag += text[i + j] if text[i + j] == self.end_tag and len(tag) > 0: i += j + 1 break else: section += char i += 1 sections.append([section, tag, x, y]) for i, s in enumerate(sections): tag = s[1] # move the text after image one space right if tag.startswith(self.start_tag + 'image:'): for f in sections: if f[3] == s[3] and f[2] > s[2]: f[2] += .5 s[2] += .5 self.create_text_section(text=s[0], tag=s[1], x=s[2], y=s[3]) self.align()
def background(self, value): if value == True: self.create_background() elif self._background: from ursina.ursinastuff import destroy destroy(self._background)
def create_text_section(self, text, tag='', x=0, y=0): # print(text, tag) self.text_node = TextNode('t') self.text_node_path = self.attachNewNode(self.text_node) try: self.text_node.setFont(self._font) except: pass # default font if tag != '<>': tag = tag[1:-1] if tag.startswith('hsb('): # set color based on numbers tag = tag[4:-1] hsb_values = tuple(float(e.strip()) for e in tag.split(',')) self.current_color = color.color(*hsb_values) elif tag.startswith('rgb('): # set color based on numbers tag = tag[4:-1] rgb_values = (float(e.strip()) for e in tag.split(',')) self.current_color = color.rgba(*rgb_values) if tag.startswith('scale:'): scale = tag.split(':')[1] self.scale_override = float(scale) elif tag.startswith('image:'): texture_name = tag.split(':')[1] image = Entity( parent=self.text_node_path, name='inline_image', model='quad', texture=texture_name, color=self.current_color, # scale=self.scale_override, # position=(x*self.size*self.scale_override, y*self.size*self.line_height), origin=(.0, -.25), add_to_scene_entities=False, ) if not image.texture: destroy(image) else: self.images.append(image) else: if tag in self.text_colors: self.current_color = self.text_colors[tag] self.text_node_path.setScale(self.scale_override * self.size) self.text_node.setText(text) self.text_node.setTextColor(self.current_color) self.text_node.setPreserveTrailingWhitespace(True) # self.text_node_path.setPos( # x * self.size * self.scale_override, # 0, # (y * self.size * self.line_height) - .75 * self.size) self.text_node_path.setPos(x * self.size * self.scale_override, (y * self.size * self.line_height) - .75 * self.size, 0) self.text_nodes.append(self.text_node_path) return self.text_node