def draw(self): s = ui.Path.rect(100, 100, 200, 200) with ui.GState(): ui.set_color('deeppink') s.fill() with ui.GState(): ui.concat_ctm(ui.Transform.rotation(.45)) ui.set_color('red') s.fill()
def _fill_and_stroke(self, p, c, fill_c, alpha, alpha_overrides): if fill_c is not None: with ui.GState(): if len(fill_c) == 3 or alpha_overrides: ui.set_color((fill_c[0], fill_c[1], fill_c[2], alpha)) else: ui.set_color((fill_c[0], fill_c[1], fill_c[2], fill_c[3])) #ctx.fill_preserve() p.fill() with ui.GState(): ui.set_color(c) p.stroke()
def make_shape(self): ln_width = 30 with ui.GState(): r = ui.Rect(*self.bounds).inset(ln_width / 2, ln_width /2) s = ui.Path.oval(*r) s.line_width = ln_width with ui.GState(): s2 = ui.Path.rect(*s.bounds) s2.line_width = .5 s.append_path(s2) s.close() s.stroke() return s
def draw(self): s = ui.Path.rect(100, 100, 200, 200) with ui.GState(): ui.set_color('deeppink') s.fill() with ui.GState(): # Move the origin (0, 0) to the center of the rectangle: ui.concat_ctm(ui.Transform.translation(200, 200)) # Rotate the coordinate system: ui.concat_ctm(ui.Transform.rotation(.45)) # Move the origin back, so that the rectangle's coordinates are valid: ui.concat_ctm(ui.Transform.translation(-200, -200)) ui.set_color('red') s.fill()
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): font = 'Menlo', prop.get_size_in_points() * 1.1 w, h = ui.measure_string(s, font=font, alignment=ui.ALIGN_RIGHT, line_break_mode=ui.LB_WORD_WRAP) y -= h if True or angle: with ui.GState(): ui.concat_ctm(ui.Transform.translation(x, y)) ui.concat_ctm( ui.Transform.rotation(-angle * matplotlib.numpy.pi / 180.)) ui.draw_string(s, rect=(0, 0, 0, 0), font=font, color='black', alignment=ui.ALIGN_RIGHT, line_break_mode=ui.LB_WORD_WRAP) else: ui.draw_string(s, rect=(x, y, 0, 0), font=font, color='black', alignment=ui.ALIGN_RIGHT, line_break_mode=ui.LB_WORD_WRAP)
def draw(self): r = ui.Rect(*self.bounds).inset(*self.circle_inset) oval = ui.Path.oval(r.x, r.y, r.width, r.height) c = ui.parse_color(self.circle_color) nc = (c[0], c[1], c[2], self.circle_alpha) # color with alpha pattern = '9' * len(self.text) text_rect = ui.Rect(0, 0, r.width * .8, r.height * .6) text_rect.center(r.center()) fs = self.get_max_fontsize(text_rect, pattern, self.font_name, self.inset_rect) with ui.GState(): ui.set_color(nc) oval.fill() ui.draw_string(self.text, rect=text_rect, font=(self.font_name, fs), color=self.text_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_TRUNCATE_TAIL) j = ui.Path.rect(*text_rect) ui.set_color((0, 0, 0, .5)) j.fill()
def draw(self): # just to see the path when testing... s = ui.Path.oval(*self.cir_rect) with ui.GState(): ui.set_color(css_clr_to_rgba('lime', .4)) s.line_width = 1 s.stroke()
def draw_face(self, diameter, border, font_size): with ui.ImageContext(diameter, diameter) as ctx: ui.set_color('white') circle = ui.Path.oval(border / 2, border / 2, diameter - border, diameter - border) circle.line_width = border - 1 circle.fill() ui.set_color('silver') with ui.GState(): ui.set_shadow((0, 0, 0, 0.35), 0, 1, 5.0) circle.stroke() angle = (-pi / 2) + (pi * 2) / 12.0 for i in range(1, 13): number = str(i) x = diameter / 2 + cos(angle) * (diameter / 2 - font_size * 1.2) y = diameter / 2 + sin(angle) * (diameter / 2 - font_size * 1.2) font = ('HelveticaNeue-UltraLight', font_size) w, h = ui.measure_string(number, font=font) rect = (x - 50, y - h / 2, 100, h) ui.draw_string(number, rect=rect, font=font, alignment=ui.ALIGN_CENTER) angle += (pi * 2.0) / 12.0 return ctx.get_image()
def draw(self): r = self.r #(100, 100, 400, 400) with ui.GState(): ui.set_color('teal') s = ui.Path.oval(*r) s.fill() self.text_class.draw_text()
def get_diamond(color): with ui.ImageContext(100, 100) as ctx: ui.set_color(color) with ui.GState(): ui.concat_ctm(ui.Transform.rotation(45/180*math.pi)) a = 0.1 ui.fill_rect((0.5+a)/math.sqrt(2)*100, (a-0.5)/math.sqrt(2)*100, math.sqrt(2)*(0.5-a)*100, math.sqrt(2)*(0.5-a)*100) im = ctx.get_image() return im
def draw(self): with ui.GState(): ui.set_color(self.shape_bg_color) shape = ui.Path.oval(*self.bounds) shape.fill() ui.set_color(self.tint_color) img = ui.Image.named(self.image_name) img_rect = ui.Rect(*self.bounds).inset(*self.image_margin) if img: img.with_rendering_mode(ui.RENDERING_MODE_TEMPLATE).draw(*img_rect)
def fill_oval(self, xorg, yorg, width, height, color=(1, 1, 1)): w, h = self.whWorld(width, height) x, y = self.xyWorld(xorg, yorg) with ui.GState(): ui.set_color(color) #ui.set_blend_mode(ui.BLEND_NORMAL) oval = ui.Path.oval(x, y + h, w, -h) # prevent neg height #rect.eo_fill_rule=True #rect.close() oval.fill() self.path.append_path(oval)
def fill_rect(self, xorg, yorg, width, height, color=(1, 1, 1)): w, h = self.whWorld(width, height) x, y = self.xyWorld(xorg, yorg) logger.debug('fill rect xy:%s, wh:%s with:%s' % ((x, y + h), (w, -h), color)) with ui.GState(): ui.set_color(color) ui.set_blend_mode(ui.BLEND_NORMAL) rect = ui.Path.rect(x, y + h, w, -h) # prevent neg height rect.eo_fill_rule = True rect.close() rect.fill() self.path.append_path(rect)
def update_texture(self): if self._suspend_updates or not self.path: return if self.shadow: shadow_color = self.shadow[0] shadow_offset_x = self.shadow[1] shadow_offset_y = self.shadow[2] shadow_radius = self.shadow[3] else: shadow_offset_x = 0 shadow_offset_y = 0 shadow_radius = 0 shadow_left = shadow_radius - shadow_offset_x shadow_right = shadow_radius + shadow_offset_x shadow_top = shadow_radius - shadow_offset_y shadow_bottom = shadow_radius + shadow_offset_y lw = self.path.line_width path_bounds = self.path.bounds w = max( 1, math.ceil(path_bounds.w + abs(shadow_left) + abs(shadow_right)) + lw) h = max( 1, math.ceil(path_bounds.h + abs(shadow_top) + abs(shadow_bottom)) + lw) with ui.ImageContext(w, h) as ctx: ui.concat_ctm( ui.Transform.translation( lw / 2 + max(0, shadow_left) - path_bounds.x, lw / 2 + max(0, shadow_top) - path_bounds.y)) ui.set_color(self.fill_color) with ui.GState(): if self.shadow: ui.set_shadow(shadow_color, shadow_offset_x, shadow_offset_y, shadow_radius) self.path.fill() if self.path.line_width > 0: ui.set_color(self.stroke_color) self.path.stroke() img = ctx.get_image() self.texture = Texture(img)
def draw_string(self): color = 'black' # uiのメソッドによるフォント描画 with ui.GState(): if self.is_enable_title is True: ui.set_shadow('gray', 1, 1, 0) ui.draw_string(self.string_A_D, rect=(10, 10, 300, 20), font=('HiraMinProN-W6', 20), color=color) ui.draw_string(self.string_A_D_s, rect=(10, 30, 300, 10), font=('HiraMinProN-W6', 10), color=color) ui.draw_string(self.string_C_D, rect=(350, 10, 300, 20), font=('HiraMinProN-W6', 20), color=color) ui.draw_string(self.string_C_D_s, rect=(350, 30, 300, 10), font=('HiraMinProN-W6', 10), color=color) ui.draw_string(self.string_T_D, rect=(10, 150, 300, 20), font=('HiraMinProN-W6', 20), color=color) ui.draw_string(self.string_T_D_s, rect=(10, 170, 300, 10), font=('HiraMinProN-W6', 10), color=color) ui.draw_string(self.string_E_D, rect=(350, 135, 300, 20), font=('HiraMinProN-W6', 20), color=color) ui.draw_string(self.string_E_D_s, rect=(350, 155, 300, 10), font=('HiraMinProN-W6', 10), color=color) for character in self.characters: if character.is_enable_name is True: ui.draw_string(character.name_string, rect=(character.name_x, character.name_y, 300, 15), font=('HiraMinProN-W6', 15), color=color)
def clear(self, uFrame=None): #self.fill_rect(*self.xyUser(), *self.whUser(), (1,1,1)) #return if uFrame: x, y = self.xyWorld(uFrame[0], uFrame[1]) w, h = self.whWorld(uFrame[2], uFrame[3]) else: w, h = self.whWorld() x, y = self.xyWorld() self.draw_recorded() # clears path with ui.GState(): ui.set_color('white') path = ui.Path.rect(x, y, w, h) #path.eo_fill_rule=False #path.close() #path.draw() path.fill()
def draw(self): ''' called by iOS whenever set_needs_display is called, or whenever os decides it is needed, for instance view rotates''' # set color to red ui.set_color((1, 0, 0)) # create a copy of self.pts, and append the current point drawpts = self.pts if drawpts: # move path to first point: pth = ui.Path() pth.move_to(*drawpts[0]) p0 = drawpts[0] for p in drawpts[1:]: # for each point after the first, draw a line from the previous point, compute length vector L # draw the line segment pth.line_to(*p) # compute point which is halfway along line between the # start/end point. L is the vector pointing from start to # finish. H is the Point at the halfway, referenced back to # global origin L = (ui.Point(*p) - ui.Point(*p0)) P0 = ui.Point(*p0) H = P0 + L / 2 # halfway point # put text at the halfway point, containing length ofmsegment * # scale ui.draw_string('%3.2f' % abs(L * self.scale.value / 10), (H.x, H.y, 0, 0), color='red') # set starting point for next line segment p0 = p pth.stroke() # draw the path if len(drawpts) > 2: # 'close' the path to show area computed with ui.GState(): pth = ui.Path() pth.move_to(*drawpts[-1]) pth.line_to(*drawpts[0]) pth.set_line_dash([2, 3]) ui.set_color((1, .5, .5, .5)) pth.stroke() # create a 10 pixel circle at last entered point ui.Path.oval(drawpts[-1][0] - 5, drawpts[-1][1] - 5, 10, 10).fill() # show circles for previously entered points. smaller and lighter ui.set_color((1, .5, .5)) for p in drawpts[0:-1]: ui.Path.oval(p[0] - 3, p[1] - 3, 6, 6).fill()
def draw_text(self, **kwargs): if kwargs: self.do_kwargs(**kwargs) r = ui.Rect(*self.rect).inset(*self.margin).translate(*self.origin) my_center = r.center() font_size = self.font_size if not font_size: result = self.get_max_fontsize() r1 = ui.Rect(*result[1]) font_size = result[0] else: w, h = ui.measure_string(self.text, max_width=0, font=(self.font_name, font_size), alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_TRUNCATE_TAIL) r1 = ui.Rect(0, 0, w, h) r1.center(my_center) with ui.GState(): if self.rotate: ''' help from @omz https://forum.omz-software.com/topic/3180/understanding-ui-transform-rotation ''' ui.concat_ctm(ui.Transform.translation(*r1.center())) ui.concat_ctm(ui.Transform.rotation(math.radians(self.rotate))) ui.concat_ctm(ui.Transform.translation(*r1.center() * -1)) if self.use_shadow: ui.set_shadow(*self.shadow_params) ui.draw_string(self.text, rect=r1, font=(self.font_name, font_size), color=self.text_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_TRUNCATE_TAIL)
def text_in_circle(r, text, text_color = 'white', circle_color = 'teal', circle_alpha = 1.0, font_name = 'Arial Rounded MT Bold', inset_percent = 0): ''' text_in_circle - * denotes a param ============== *r-ui.Rect or tuple (0, 0, 0, 0) - the bounding rect for the circle. *text-text to draw in the circle *text_color-color of the text drawn inside the circle *circle_color-color of the circle *circle_alpha-alpha setting applied to circle color. Note, did this for a reason. easier to use string names for colors! *font_name-the font used to render the *text *inset_percent-reduces *r by a percentage for l,t,w,h for possible better placement of the text inside the circle. a.k.a margin RETURNS - a rendered uiImage ''' # this inner function does not need to be here, was just to keep it # all together def get_max_fontsize(r, text, font_name, inset_rect = ui.Rect()): r1 = ui.Rect(*r).inset(*inset_rect) for i in xrange(5, 1000): w, h = ui.measure_string(text, max_width=0, font=(font_name, i), alignment=ui.ALIGN_LEFT, line_break_mode=ui.LB_TRUNCATE_TAIL) if w > r1.width or h > r1.height: return (i - 1, ui.Rect(r1.x, r1.y, w, h)) # tuple or Rect? tried issubclass but had problems if not type(r) is ui.Rect: r = ui.Rect(*r) inset_rect = ui.Rect(r.x * inset_percent, r.y * inset_percent, r.width * inset_percent, r.height * inset_percent) with ui.ImageContext(r.width, r.height) as ctx: oval = ui.Path.oval(r.x, r.y, r.width, r.height) c = ui.parse_color(circle_color) nc = (c[0], c[1], c[2], circle_alpha) # color with alpha fs, dest_r = get_max_fontsize(r, text, font_name, inset_rect = inset_rect) dest_r.center(r.center()) with ui.GState(): ui.set_color(nc) oval.fill() ui.draw_string(text, rect=dest_r, font=(font_name, fs), color=text_color, alignment=ui.ALIGN_LEFT, line_break_mode=ui.LB_TRUNCATE_TAIL) return ctx.get_image()
def stroke_bounds(self): with ui.GState(): s = self.get_bounds_shape() s.line_width = self.line_width s.stroke()
def draw(self): # 水面 if not self.surface is None: surface = self.surface a_x, a_y, a_z = surface.corner_A() b_x, b_y, b_z = surface.corner_B() c_x, c_y, c_z = surface.corner_C() d_x, d_y, d_z = surface.corner_D() a_x, a_y = self.projection(a_x, a_y, a_z) b_x, b_y = self.projection(b_x, b_y, b_z) c_x, c_y = self.projection(c_x, c_y, c_z) d_x, d_y = self.projection(d_x, d_y, d_z) path_s = ui.Path() path_s.move_to(a_x, a_y) path_s.line_to(b_x, b_y) path_s.line_to(c_x, c_y) path_s.line_to(d_x, d_y) path_s.line_to(a_x, a_y) ui.set_color(surface.color) path_s.fill() # uiのメソッドによるフォント描画 with ui.GState(): ui.set_shadow('gray', 10, 10, 0) ui.draw_string(self.string_S, rect=(130, 130, 300, 40), font=('HiraMinProN-W6', 30), color='black') ui.draw_string(self.string_D, rect=(130, 290, 300, 40), font=('HiraMinProN-W6', 30), color='black') ui.draw_string(self.string_G, rect=(130, 450, 300, 40), font=('HiraMinProN-W6', 30), color='black') # 文字を描画する for character in self.characters: # [[x,y,z,type,flag],[],...],[] contours = character.glyph(self.height) # フォントを描画する for points in contours: if len(points) < 1: continue # 座標点、制御点を描画する # line,curve 黒塗り # control 白抜き if self.is_Fill is False: self.draw_glyph_points(points) path = ui.Path() # 最初のglyph座標 if points[0][4] is True: p_x, p_y = self.projection(points[0][0], points[0][1], points[0][2]) path.move_to(p_x, p_y) else: continue controls = [] # 制御点を格納する # 2番目以降のglyph座標 for i in range(1, len(points)): if points[i][4] is True: self.draw_glyph(points[i], controls, path) # 最後のglyph座標 self.draw_glyph(points[0], controls, path) ui.set_color('black') if self.is_Fill is True: path.fill() else: path.stroke()
def fill_bounds(self): with ui.GState(): s = self.get_bounds_shape() s.fill()
def draw(self): s = ui.Path.oval(*self.cir_rect) with ui.GState(): ui.set_color(css_clr_to_rgba('lime', .4)) s.line_width = 1 s.stroke()
# coding: utf-8 # https://forum.omz-software.com/topic/1942/drop-shadow-behind-ui-view import ui with ui.ImageContext(100, 100) as ctx: ui.set_shadow('blue', 5, 5, 2) with ui.GState(): ui.concat_ctm(ui.Transform.rotation(0.78)) ui.draw_string(' Rotated text') ui.draw_string('Not rotated') ctx.get_image().show() #============================== class shadowview(ui.View): def draw(self): path = ui.Path.rect(0, 0, self.width-10,self.height-10) ui.set_color((0.9,0.9,0.9,1.0)) ui.set_shadow("black",0,0,10) path.fill() #============================== class shadowview(ui.View): '''A class for a ui.View that has a shadow behind it. This is accomplished by: 1. Draw the background 2. Redraw with a shadow, but set clipping so only the edge of the shadow
def fill(self): with ui.GState(): ui.set_color(self.f_clr) self.shape.line_width = self.line_width self.shape.fill()
def stroke(self): with ui.GState(): ui.set_color(self.s_clr) ui.set_shadow('darkgray', .5, .5, 8) self.shape.line_width = self.line_width self.shape.stroke()