我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用ui.Path()。
def touch_moved(self, data): if self.dragging_waypoint: self.w.center = data.location return if self.flood_filling: if Vector(data.location).distance_to(self.touch_start) < 10: return else: self.flood_fill(self.control.edit_menu['flood_fill']) (w, h) = self.img.size with ui.ImageContext(w,h) as ctx: self.img.draw() blend_mode = ui.BLEND_CLEAR if self.digging else ui.BLEND_NORMAL ui.set_blend_mode(blend_mode) ui.set_color('black') (x, y) = data.location #* self.multiplier (px, py) = data.prev_location #* self.multiplier path = ui.Path() path.move_to(px, py) path.line_to(x, y) path.line_width = 30 #* self.multiplier #if self.digging else 1 path.line_cap_style = ui.LINE_CAP_ROUND path.stroke() self.img = ctx.get_image() #self.set_needs_display()
def load_actual(self, burn_waypoints_in=False): # Clear old waypoints for wp in self.edit_view.waypoints: self.edit_view.remove_subview(wp) self.edit_view.waypoints = [] # Load image img_filename = self.filename iv = ui.ImageView(frame=self.bg.bounds) iv.image = ui.Image(img_filename) self.edit_view.img = snapshot(iv) self.multiplier = self.edit_view.img.size[1]/self.edit_view.height # Load settings json_filename = img_filename[:-3]+'json' if os.path.exists(json_filename): with open(json_filename) as fp: settings = json.load(fp) if isinstance(settings, dict): locations = settings['waypoint_locations'] self.bg.image = ui.Image.named(settings['bg_filename']) self.edit_view.bg_filename = settings['bg_filename'] else: locations = settings self.bg.image = ui.Image.named('backgrounds/caves.jpg') self.edit_view.bg_filename = None for loc in locations: wp = self.edit_view.add_waypoint() wp.center = loc if burn_waypoints_in: with ui.ImageContext(self.edit_view.width, self.edit_view.height) as ctx: self.edit_view.img.draw() ui.set_blend_mode(ui.BLEND_CLEAR) ui.set_color('black') for wp in self.edit_view.waypoints: (x,y) = wp.center path = ui.Path.oval(x-15, y-15, 30, 30) path.fill() self.edit_view.img = ctx.get_image()
def draw(self): if self.tracking: ui.set_color('black') ui.fill_rect(0, 0, self.width, self.height) else: base_color = tuple([self.color[i] for i in range(3)]) opacity_increment = 1.0/(len(self.current_move)+1) # 0.002 alpha_incremental = 1.0 - self.animate_counter*opacity_increment if self.animate_counter > 0: for i in range(1, self.animate_counter): alpha_actual = max(0, alpha_incremental) self.draw_segment(base_color + (alpha_actual,), self.current_move[i-1], self.current_move[i]) alpha_incremental += opacity_increment if len(self.waypoints_hit_this_turn) > 0: (hit_distance, waypoint_index) = self.waypoints_hit_this_turn[0] if self.animate_counter >= hit_distance: sound.play_effect('digital:PowerUp1') self.waypoints[waypoint_index].background_color = '#6cd655' self.waypoints_hit_this_turn = self.waypoints_hit_this_turn[1:] if self.animation_state == 'sploding': splode_alpha = 1.0-self.splosion_counter*0.15 splode_radius = self.splosion_counter * 4 splode_color = base_color + (splode_alpha,) (x,y) = self.current_move[-1] path = ui.Path.oval(x-splode_radius,y-splode_radius,2*splode_radius,2*splode_radius) ui.set_color(splode_color) path.fill() # pos1 = self.current_move[self.animate_counter - 2] # pos2 = self.current_move[self.animate_counter - 1] # angle = (Vector(pos2) - Vector(pos1)).degrees #print(self.animate_counter, angle)
def new_path(self, path_color, line_width=3): ui.set_color(path_color) path = ui.Path() path.line_width = line_width path.line_cap_style = ui.LINE_CAP_ROUND return path
def get_polygon_path(points): """ Get a ui.Path object that connects a list of points with straight lines to form a closed figure """ p = ui.Path() # Move to first point p.move_to(*points[0]) # Begin path, drawing line to the rest for point in points[1:]: p.line_to(*point) # close the shape p.close() return p
def get_regular_polygon_path(*args, **kwargs): """ Get a ui.Path for a regular polygon. See 'get_regular_polygon_points' for description of arguments """ return get_polygon_path(get_regular_polygon_points(*args, **kwargs))
def __init__(self): self.p = ui.Path() # Stores all arcs, curves, and lines in the path. They are stored as # [start_point, function_args, end_point] self.components = [] # Keeps track of the current position internally. Only updated manually # and is therefore used to track old values self._position = (0, 0) # Copy docstring self.__doc__ == ui.Path.__doc__ # Wrapper methods
def copy_path(path): """ Make a copy of a ui.Path and return it. Preserves all data. """ new = ui.Path() new.append_path(path) # Copy over the attributes new.line_cap_style = path.line_cap_style new.line_join_style = path.line_join_style new.line_width = path.line_width return new
def demo_ProgressPathView(): import math import random p = ui.Path() p.move_to(20, 20) p.line_to(480, 20) p.line_to(480, 250) p.add_arc(250, 250, 230, 0, math.radians(110)) p.add_curve(50, 450, 20, 250, 480, 250) p.close() # This makes the end look nicer ppv = ui2.ProgressPathView(p) view = ui.View(background_color="white") view.add_subview(ppv) view.width = view.height = ppv.width = ppv.height = 500 view.present("sheet", hide_title_bar=True) def advance(): """Advance by a random amount and repeat.""" pg = ppv.progress + random.random() / 20 if pg < 1: ppv.progress = pg ui.delay(advance, random.random() / 2) else: ppv.progress = 1 advance()
def draw(self): # redraw button def darken(color): return tuple([0.5*x for x in color]) #set up button size to fit. padding=10 textsize=ui.measure_string(string=self.title,max_width=0,font=self.font,alignment=ui.ALIGN_CENTER) #draw border ui.set_color(self.border_color) path = ui.Path.rounded_rect(0, 0, self.width, self.height,self.corner_radius) path.line_width=self.border_width path.stroke() #fill button, depending on touch state if self.touched: if self.doing_longtouch: ui.set_color('blue') else: ui.set_color('grey') else : ui.set_color(self.bg_color) path.fill() # fill corner darker, if has children if self.flow.subviews: corner = ui.Path() corner.move_to(self.width-1.5*self.corner_radius,0) corner.line_to(self.width,0) corner.line_to(self.width,1.5*self.corner_radius) corner.line_to(self.width-1.5*self.corner_radius,0) ui.set_color(darken(darken(self.bg_color))) corner.stroke() corner.fill() # draw title, center vertically, horiz rect=list(self.bounds) rect[1]=(rect[3]-textsize[1])/2 #vert center rect[2]=max(rect[2],textsize[0]+10) ui.draw_string(self.title, rect=tuple(rect), font=self.font, color=self.tint_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_WORD_WRAP) if textsize[0]>self.bounds[2]: self.width=textsize[0]+10