def draw_button(self): (w, h) = self.size two_images = self.label.startswith("image:") and "|" in self.label if self.style.button_inverting: if self.pressed and not two_images: self.fill(self.style.button_color) else: self.fill(self.style.bg_color) pygame.draw.rect(self.surface, self.style.button_color, [0, 0, w, h], 1) else: self.fill(self.style.bg_color) if self.pressed and not two_images: color = self.style.button_pressed_color else: color = self.style.button_color rounding = self.style.button_rounding # draw two cross-pieces self.surface.fill( _color(color), ((rounding, 0), (w - rounding * 2, h))) self.surface.fill( _color(color), ((0, rounding), (w, h - rounding * 2))) # now do circles at the edges coords = [(x, y) for x in (rounding, w - rounding) for y in (rounding, h - rounding)] for pos in coords: pygame.draw.circle(self.surface, _color(color), pos, rounding)
def draw(self): # clear background self.fill(self.style.bg_color) # draw bounding box box = self.get_box_rect() pygame.draw.ellipse( self.surface, _color(self.style.radiobutton_color), box, 1) if self.pressed: pygame.draw.ellipse( self.surface, _color(self.style.radiobutton_color), box.inflate(-6, -6), 0) self.text(self.label, xy=box.inflate(6, 6).midright, align='left', color=self.style.radiobutton_text_color, font=self.style.radiobutton_text_font, font_size=self.style.radiobutton_text_font_size)
def draw(self): (w, h) = self.size # clear background self.fill(self.style.bg_color) # draw bounding box box = self.get_box_rect() pygame.draw.rect( self.surface, _color(self.style.checkbox_color), box.inflate(1, 1), 1) if self.value: self.line( box.topleft, box.bottomright, color=self.style.checkbox_color) self.line( box.bottomleft, box.topright, color=self.style.checkbox_color) self.text(self.label, xy=box.inflate(6, 6).midright, align='left', color=self.style.checkbox_text_color, font=self.style.checkbox_text_font, font_size=self.style.checkbox_text_font_size)
def image_with_text(string, color='grey', font=None, font_size=32, antialias=None): from tingbot.graphics import _font, _color font, antialias = _font(font, font_size, antialias) string = unicode(string) if antialias is None: antialias return Image(surface=font.render(string, antialias, _color(color)))
def draw(self): # clear background self.fill(self.style.bg_color) # draw bounding box box = self.get_box_rect() pygame.draw.ellipse(self.surface, _color(self.style.radiobutton_color), box, 1) if self.pressed: pygame.draw.ellipse(self.surface, _color(self.style.radiobutton_color), box.inflate(-6, -6), 0) self.text(self.label, xy=box.inflate(6, 6).midright, align='left', color=self.style.radiobutton_text_color, font=self.style.radiobutton_text_font, font_size=self.style.radiobutton_text_font_size)
def draw_groove(self): (w, h) = self.size if self.vertical: start = (w / 2, 0) end = (w / 2, h) width = w / 5 else: start = (0, h / 2) end = (w, h / 2) width = h / 5 pygame.draw.line(self.surface, _color(self.style.slider_line_color), start, end, width)
def draw(self): (w, h) = self.size self.draw_button() triangle_size = self.style.button_text_font_size / 2 self.text(self.selected[0], xy=(5, h / 2), size = (w-10-triangle_size,h), align="left", color=self.style.button_text_color, font=self.style.button_text_font, font_size=self.style.button_text_font_size) triangle_points = ((w-5, (h-triangle_size) / 2), (w - 5 - triangle_size/2, (h+triangle_size) / 2), (w - 5 - triangle_size, (h-triangle_size) / 2)) pygame.draw.polygon(self.surface, _color(self.style.button_text_color), triangle_points)
def draw(self): (w, h) = self.size # clear background self.fill(self.style.bg_color) # draw bounding box box = self.get_box_rect() pygame.draw.rect(self.surface, _color(self.style.checkbox_color), box.inflate(1, 1), 1) if self.value: self.line(box.topleft, box.bottomright, color=self.style.checkbox_color) self.line(box.bottomleft, box.topright, color=self.style.checkbox_color) self.text(self.label, xy=box.inflate(6, 6).midright, align='left', color=self.style.checkbox_text_color, font=self.style.checkbox_text_font, font_size=self.style.checkbox_text_font_size)
def text(self, string, xy=None, size=None, color='grey', align='center', font=None, font_size=32, antialias=None): """ render text to a specific area, will adjust font size to try and fit text into specified size returns a list of offsets for each letter """ string = unicode(string) if size is None: size = self.size if antialias is None: antialias for x in reversed(range(font_size*3/4,font_size)): font_obj, antialias= _font(font, x, antialias) rendered_size = font_obj.size(string) if (size[0]>rendered_size[0]) and (size[1]>rendered_size[1]): break else: # no break # couldn't fit with just font size shrinking. Try clipping and put an ellipsis on the end for x in range(3,len(string)): temp_string = string[:-x]+u'...' rendered_size = font_obj.size(temp_string) if (size[0]>rendered_size[0]): #success! string = temp_string break text_image = Image(surface=font_obj.render(string, antialias, _color(color))) self.image(text_image, xy, align=align) if string: total = 0 result = [] for metric in font_obj.metrics(string): total+=metric[4] result.append(total) return result else: return []
def text(self, string, xy=None, size=None, color="grey", align="center", font=None, font_size=32, antialias=None): """ render text to a specific area, will adjust font size to try and fit text into specified size returns a list of offsets for each letter """ string = unicode(string) if size is None: size = self.size if antialias is None: antialias for x in reversed(range(font_size * 3 / 4, font_size)): font_obj, antialias = _font(font, x, antialias) rendered_size = font_obj.size(string) if (size[0] > rendered_size[0]) and (size[1] > rendered_size[1]): break else: # no break # couldn't fit with just font size shrinking. Try clipping and put an ellipsis on the end for x in range(3, len(string)): temp_string = string[:-x] + u"..." rendered_size = font_obj.size(temp_string) if size[0] > rendered_size[0]: # success! string = temp_string break text_image = Image(surface=font_obj.render(string, antialias, _color(color))) self.image(text_image, xy, align=align) if string: total = 0 result = [] for metric in font_obj.metrics(string): total += metric[4] result.append(total) return result else: return []
def draw_handle(self): position = self.get_handle_position() size = self.get_handle_size() pygame.draw.circle(self.surface, _color(self.style.slider_handle_color), position, size[0] / 2)