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=1, # position=(x*self.size*self.scale_override, y*self.size*self.line_height), origin=(0, -.3), ignore=True, ) if not image.texture: destroy(image) else: self.images.append(image) # self.text_node.remove_node() # self.text_node = 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_nodes.append(self.text_node_path) return self.text_node
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: print('default font') pass # default font # tags if not tag: tag = self.tag if tag in self.text_colors: lightness = color.to_hsv(self.color_tag)[2] self.color_tag = self.text_colors[tag] if not tag == self.start_tag + 'default' + self.end_tag: if lightness > .8: self.color_tag = color.tint(self.color_tag, .2) else: self.color_tag = color.tint(self.color_tag, -.3) self.text_node.setTextColor(self.color_tag) if tag.startswith(self.start_tag + 'scale:'): scale = tag.split(':')[1] self.scale_override = scale = float(scale[:-1]) elif tag.startswith(self.start_tag + 'image:'): texture_name = tag.split(':')[1].replace(self.end_tag, '') image = Entity( parent=self.text_node_path, name='inline_image', model='quad', texture=texture_name, color=self.color_tag, scale=1, # position=(x*self.size*self.scale_override, y*self.size*self.line_height), origin=(0, -.3), ignore=True, ) if not image.texture: destroy(image) else: self.images.append(image) self.text_node_path.setScale(self.scale_override * self.size) if not text: return self.text_node.setText(text) 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_nodes.append(self.text_node_path) return self.text_node
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 update(self): try: color_saturation = 1 - self.health / 100 except AttributeError: self.health = 100 color_saturation = 1 - self.health / 100 self.color = ursina.color.color(0, color_saturation, 1) if self.health <= 0: ursina.destroy(self)
def update(self): self.position += self.velocity * ursina.time.dt hit_info = self.intersects() if hit_info.hit: if not self.slave: for entity in hit_info.entities: if isinstance(entity, Enemy): entity.health -= self.damage self.network.send_health(entity) ursina.destroy(self)
def get_vertices(entity, relative_to=None): if relative_to is None: return entity.model.vertices vertices = list() temp_entity = Entity(parent=entity, ignore=True) for v in entity.model.vertices: temp_entity.position = v vertices.append(temp_entity.get_position(relative_to)) from ursina import destroy destroy(temp_entity) return vertices
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 input(self, key): if self.hovered: if key == 'left mouse down': if not self.parent_game.started: self.parent_game.started = True if len(self.phoneme_store.phonemes): if self.position[2] == ARENA_DEPTH: if not self.text: phoneme = self.phoneme_store.phonemes.pop(-1) voxel = Voxel( self.phoneme_store, self.parent_game, position=self.position + mouse.normal, texture=self.phoneme_store.textures.get( phoneme, 'white_cube'), text=phoneme, highlight_color=color.white) self.parent_game.voxels.append(voxel) self.parent_game.score -= 1 self.phoneme_store.test_positions.append( (self.position[0], phoneme)) self.play_sound(phoneme) logger.debug(phoneme) if not len(self.phoneme_store.phonemes): invoke(self.check_win, delay=.5) else: self.play_sound(self.text) else: self.play_sound(self.text) if key == 'right mouse down': if self.text: if not self.parent_game.correct: self.phoneme_store.phonemes.append(self.text) self.parent_game.voxels.remove(self) if (self.position[0], self.text ) in self.phoneme_store.test_positions: self.phoneme_store.test_positions.remove( (self.position[0], self.text)) destroy(self) else: self.play_sound(self.text)
def destroy_all(self): for v in self.voxels: destroy(v) self.voxels = []
def background(self, value): if value == True: self.create_background() elif self._background: destroy(self._background)
def combine(entity, analyze=False, auto_destroy=True, ignore=[]): verts = list() tris = list() norms = list() uvs = list() cols = list() to_destroy = list() o = 0 for e in scene.entities: if e in ignore: continue if e.has_ancestor(entity) or e == entity: if not hasattr( e, 'model') or e.model == None or e.scripts or e.eternal: continue if not e.model.vertices: continue if analyze: print('combining:', e) verts += get_vertices(e, entity) if not e.model.triangles: new_tris = [i for i in range(len(e.model.vertices))] else: new_tris = list() for t in e.model.triangles: if isinstance(t, int): new_tris.append(t) elif len(t) == 3: new_tris.extend(t) elif len(t) == 4: # turn quad into tris new_tris.extend([t[0], t[1], t[2], t[2], t[3], t[0]]) new_tris = [t + o for t in new_tris] new_tris = [(new_tris[i], new_tris[i + 1], new_tris[i + 2]) for i in range(0, len(new_tris), 3)] o += len(e.model.vertices) tris += new_tris # if e.model.normals: # norms += e.model.normals if e.model.uvs: uvs += e.model.uvs if e.model.colors: # if has vertex colors cols.extend(e.model.colors) else: cols.extend((e.color, ) * len(e.model.vertices)) if auto_destroy and e != entity: to_destroy.append(e) if auto_destroy: from ursina import destroy [destroy(e) for e in to_destroy] entity.model = Mesh(vertices=verts, triangles=tris, normals=norms, uvs=uvs, colors=cols, mode='triangle') print('combined') # entity.model = Mesh(vertices=verts, mode='triangle') # entity.flatten_strong() if analyze: render.analyze() return entity.model
def text(self, text): self.raw_text = text # clear stuff for img in self.images: destroy(img) self.images.clear() for tn in self.text_nodes: tn.remove_node() self.text_nodes.clear() # 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 alignemnt to work? sections = list() 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 combine(combine_parent, analyze=False, auto_destroy=True, ignore=[]): verts = [] tris = [] norms = [] uvs = [] cols = [] to_destroy = [] o = 0 for e in scene.entities: if e in ignore: continue if e.has_ancestor(combine_parent) or e == combine_parent: if not hasattr( e, 'model') or e.model == None or e.scripts or e.eternal: continue if not hasattr(e.model, 'vertices') or not e.model.vertices: e.model = load_model(e.model.name, use_deepcopy=True) e.origin = e.origin if not e.model: continue if analyze: print('combining:', e) vertex_to_world_matrix = e.model.getTransform( combine_parent).getMat() verts += [ vertex_to_world_matrix.xformPoint(v) for v in e.model.vertices ] if not e.model.triangles: new_tris = [i for i in range(len(e.model.vertices))] else: new_tris = list() for t in e.model.triangles: if isinstance(t, int): new_tris.append(t) elif len(t) == 3: new_tris.extend(t) elif len(t) == 4: # turn quad into tris new_tris.extend([t[0], t[1], t[2], t[2], t[3], t[0]]) new_tris = [t + o for t in new_tris] new_tris = [(new_tris[i], new_tris[i + 1], new_tris[i + 2]) for i in range(0, len(new_tris), 3)] o += len(e.model.vertices) tris += new_tris # if e.model.normals: # norms += e.model.normals if e.model.uvs: uvs += e.model.uvs if e.model.colors: # if has vertex colors cols.extend( [Color(*vcol) * e.color for vcol in e.model.colors]) else: cols.extend((e.color, ) * len(e.model.vertices)) if auto_destroy and e != combine_parent: to_destroy.append(e) if auto_destroy: from ursina import destroy [destroy(e) for e in to_destroy] combine_parent.model = Mesh(vertices=verts, triangles=tris, normals=norms, uvs=uvs, colors=cols, mode='triangle') print('combined') # entity.model = Mesh(vertices=verts, mode='triangle') # entity.flatten_strong() if analyze: render.analyze() return combine_parent.model