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 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): 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 draw(self): scl=min(self.width,self.height) self.scl=scl btn_siz=min(22/scl,0.05) #work in normalized units ui.concat_ctm(ui.Transform.scale(scl,scl)) #origin at center ui.concat_ctm(ui.Transform.translation(.5,.5)) ui.set_color('#d0d0d0') o = ui.Path.oval(-.5+btn_siz, -.5+btn_siz, 1-2*btn_siz, 1-2*btn_siz) o.line_width=2/scl o.stroke() if self.image: self.image.draw(-.5+2*btn_siz, -.5+2*btn_siz, 1-4*btn_siz, 1-4*btn_siz) #rotate by angle ui.concat_ctm(ui.Transform.rotation(self.a)) ui.set_color(self.tint_color if self.tint_color else '#1aa1b5') arc = ui.Path() arc.move_to(.5-btn_siz, 0) ang = -(self.a+pi) arc.add_arc(0, 0, .5-btn_siz, 0, ang, False) arc.line_width=2/scl arc.stroke() # center origin at button ui.concat_ctm(ui.Transform.translation(.5-btn_siz,0)) #optional: to keep images upright #ui.concat_ctm(ui.Transform.rotation(-self.a)) p=ui.Path.oval(-btn_siz,-btn_siz,2*btn_siz,2*btn_siz) p.fill()
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 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 get_rotated_icon(named_icon_name, wh=32, degree=0): ''' help from @omz https://forum.omz-software.com/topic/3180/understanding-ui-transform-rotation ''' r = ui.Rect(0, 0, wh, wh) img = ui.Image.named(named_icon_name) with ui.ImageContext(wh, wh) as ctx: ui.concat_ctm(ui.Transform.translation(*r.center())) ui.concat_ctm(ui.Transform.rotation(math.radians(degree))) ui.concat_ctm(ui.Transform.translation(*r.center() * -1)) img.draw() return ctx.get_image()
def draw(self): scl = min(self.width, self.height) self.scl = scl btn_siz = min(22 / scl, 0.05) #work in normalized units ui.concat_ctm(ui.Transform.scale(scl, scl)) #origin at center ui.concat_ctm(ui.Transform.translation(.5, .5)) ui.set_color('#1aa1b5') o = ui.Path.oval(-.5 + btn_siz, -.5 + btn_siz, 1 - 2 * btn_siz, 1 - 2 * btn_siz) o.line_width = 2 / scl o.stroke() #rotate by angle ui.concat_ctm(ui.Transform.rotation(self.a)) # center origin at button ui.concat_ctm(ui.Transform.translation(.5 - btn_siz, 0)) #optional: to keep images upright #ui.concat_ctm(ui.Transform.rotation(-self.a)) p = ui.Path.oval(-btn_siz, -btn_siz, 2 * btn_siz, 2 * btn_siz) p.fill()
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, **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)
# 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