def setxy(self, x, y, share=True, pendown=True): ''' Move turtle to position x,y ''' oldx, oldy = self.xcor, self.ycor x *= self.tw.coord_scale y *= self.tw.coord_scale try: self.xcor, self.ycor = x, y except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return if self.pendown and pendown: self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) if self.cr_svg is not None: self.cr_svg.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) self.draw_line(oldx, oldy, self.xcor, self.ycor) self.inval() self.move_turtle() if self.tw.sharing() and share: event = 'x|%s' % (data_to_string([self._get_my_nick(), [round_int(x), round_int(y)]])) self.tw.send_event(event)
def draw_text(self, label, x, y, size, w, share=True): ''' Draw text ''' self._turtles.turtle_window.canvas.draw_text( label, x, y, size, w, self._heading, self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'W|%s' % (data_to_string([self._turtles.turtle_window.nick, [label, round_int(x), round_int(y), round_int(size), round_int(w)]])) self._turtles.turtle_window.send_event(event)
def draw_text(self, label, x, y, size, w, share=True): ''' Draw text ''' self._turtles.turtle_window.canvas.draw_text( label, x, y, size, w, self._heading, self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'W|%s' % (data_to_string([self._turtles.turtle_window.nick, [label, round_int(x), round_int(y), round_int(size), round_int(w)]])) self._turtles.turtle_window.send_event(event)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' self._turtles.turtle_window.canvas.draw_pixbuf( pixbuf, a, b, x, y, w, h, self._heading) if self._turtles.turtle_window.sharing() and share: if self._turtles.turtle_window.running_sugar: tmp_path = get_path(self._turtles.turtle_window.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join( get_path(self._turtles.turtle_window.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() pos = self._turtles.screen_to_turtle_coordinates((x, y)) event = 'P|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(b), round_int(pos[0]), round_int(pos[1]), round_int(w), round_int(h), round_int(width), round_int(height), data]])) GObject.idle_add(self._turtles.turtle_window.send_event, event) os.remove(tmp_file)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' self._turtles.turtle_window.canvas.draw_pixbuf( pixbuf, a, b, x, y, w, h, self._heading) if self._turtles.turtle_window.sharing() and share: if self._turtles.turtle_window.running_sugar: tmp_path = get_path(self._turtles.turtle_window.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join( get_path(self._turtles.turtle_window.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() pos = self._turtles.screen_to_turtle_coordinates((x, y)) event = 'P|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(b), round_int(pos[0]), round_int(pos[1]), round_int(w), round_int(h), round_int(width), round_int(height), data]])) gobject.idle_add(self._turtles.turtle_window.send_event, event) os.remove(tmp_file)
def arc(self, a, r, share=True): ''' Draw an arc ''' if self._pen_state: self._turtles.turtle_window.canvas.set_source_rgb() if a < 0: pos = self.larc(-a, r) else: pos = self.rarc(a, r) self.move_turtle(pos) if self._turtles.turtle_window.sharing() and share: event = 'a|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(r)]])) self._turtles.turtle_window.send_event(event)
def arc(self, a, r, share=True): ''' Draw an arc ''' if self._pen_state: self._turtles.turtle_window.canvas.set_source_rgb() if a < 0: pos = self.larc(-a, r) else: pos = self.rarc(a, r) self.move_turtle(pos) if self._turtles.turtle_window.sharing() and share: event = 'a|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(r)]])) self._turtles.turtle_window.send_event(event)
def set_color(self, color=None, share=True): ''' Set the pen color for this turtle. ''' if isinstance(color, ColorObj): # See comment in tatype.py TYPE_BOX -> TYPE_COLOR color = color.color if color is None: color = self._pen_color # Special case for color blocks from CONSTANTS elif isinstance(color, Color): self.set_shade(color.shade, share) self.set_gray(color.gray, share) if color.color is not None: color = color.color else: color = self._pen_color self._pen_color = color self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'c|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_color)])) self._turtles.turtle_window.send_event(event)
def set_color(self, color=None, share=True): ''' Set the pen color for this turtle. ''' # Special case for color blocks if color is not None and color in COLORDICT: self.set_shade(COLORDICT[color][1], share) self.set_gray(COLORDICT[color][2], share) if COLORDICT[color][0] is not None: self.set_color(COLORDICT[color][0], share) color = COLORDICT[color][0] else: color = self._pen_color elif color is None: color = self._pen_color try: self._pen_color = color except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'c|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._pen_color)])) self._turtles.turtle_window.send_event(event)
def setcolor(self, c, share=True): ''' Set the pen color ''' # Special case for color blocks if c in COLORDICT: self.setshade(COLORDICT[c][1], share) self.setgray(COLORDICT[c][2], share) if COLORDICT[c][0] is not None: self.setcolor(COLORDICT[c][0], share) c = COLORDICT[c][0] else: c = self.color try: self.color = c except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_color(c) self.set_fgcolor() if self.tw.sharing() and share: event = 'c|%s' % (data_to_string([self._get_my_nick(), round_int(c)])) self.tw.send_event(event)
def set_color(self, color=None, share=True): ''' Set the pen color for this turtle. ''' # Special case for color blocks if color is not None and color in COLORDICT: self.set_shade(COLORDICT[color][1], share) self.set_gray(COLORDICT[color][2], share) if COLORDICT[color][0] is not None: self.set_color(COLORDICT[color][0], share) color = COLORDICT[color][0] else: color = self._pen_color elif color is None: color = self._pen_color try: self._pen_color = color except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'c|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_color)])) self._turtles.turtle_window.send_event(event)
def set_color(self, color=None, share=True): ''' Set the pen color for this turtle. ''' if isinstance(color, ColorObj): # See comment in tatype.py TYPE_BOX -> TYPE_COLOR color = color.color if color is None: color = self._pen_color # Special case for color blocks from CONSTANTS elif isinstance(color, Color): self.set_shade(color.shade, share) self.set_gray(color.gray, share) if color.color is not None: color = color.color else: color = self._pen_color self._pen_color = color self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'c|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._pen_color)])) self._turtles.turtle_window.send_event(event)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cr = gtk.gdk.CairoContext(self.canvas) cr.save() # center the rotation on the center of the image cr.translate(x + w / 2., y + h / 2.) cr.rotate(self.heading * DEGTOR) cr.translate(-x - w / 2., -y - h / 2.) cr.set_source_pixbuf(pixbuf, x, y) cr.rectangle(x, y, w, h) cr.fill() cr.restore() self.inval() if self.tw.saving_svg: if self.tw.running_sugar: # In Sugar, we embed the images inside the SVG tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) self.tw.svg_string += self.svg.image( x, y, w, h, path, image_to_base64(tmp_file, get_path(self.tw.activity, 'instance'))) os.remove(tmp_file) else: # In GNOME, we embed a path self.tw.svg_string += self.svg.image(x - self.width / 2, y, w, h, path) if self.tw.sharing() and share: if self.tw.running_sugar: tmp_path = get_path(self.tw.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() x, y = self.screen_to_turtle_coordinates(x, y) event = 'P|%s' % (data_to_string([ self._get_my_nick(), [ round_int(a), round_int(b), round_int(x), round_int(y), round_int(w), round_int(h), round_int(width), round_int(height), data ] ])) self.tw.send_event(event) os.remove(tmp_file)
def set_xy(self, x, y, share=True, pendown=True, dragging=False): old = self.get_xy() if dragging: xcor = x ycor = y else: xcor = x * self._turtles.turtle_window.coord_scale ycor = y * self._turtles.turtle_window.coord_scale self._draw_line(old, (xcor, ycor), pendown) self.move_turtle((xcor, ycor)) if self._turtles.turtle_window.sharing() and share: event = 'x|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(xcor), round_int(ycor)]])) self._turtles.turtle_window.send_event(event)
def set_xy(self, x, y, share=True, pendown=True, dragging=False): old = self.get_xy() if dragging: xcor = x ycor = y else: xcor = x * self._turtles.turtle_window.coord_scale ycor = y * self._turtles.turtle_window.coord_scale self._draw_line(old, (xcor, ycor), pendown) self.move_turtle((xcor, ycor)) if self._turtles.turtle_window.sharing() and share: event = 'x|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(xcor), round_int(ycor)]])) self._turtles.turtle_window.send_event(event)
def draw_text(self, label, x, y, size, w, share=True): ''' Draw text ''' w *= self.tw.coord_scale def _draw_text(cr, label, x, y, size, w, scale, heading, rgb): cc = pangocairo.CairoContext(cr) pl = cc.create_layout() fd = pango.FontDescription('Sans') fd.set_size(int(size * scale) * pango.SCALE) pl.set_font_description(fd) if type(label) == str or type(label) == unicode: pl.set_text(label.replace('\0', ' ')) elif type(label) == float or type(label) == int: pl.set_text(str(label)) else: pl.set_text(str(label)) pl.set_width(int(w) * pango.SCALE) cc.save() cc.translate(x, y) cc.rotate(heading * DEGTOR) cr.set_source_rgb(rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) cc.update_layout(pl) cc.show_layout(pl) cc.restore() _draw_text(self.canvas, label, x, y, size, w, self.tw.coord_scale, self.heading, self.fgrgb) self.inval() if self.cr_svg is not None: # and self.pendown: _draw_text(self.cr_svg, label, x, y, size, w, self.tw.coord_scale, self.heading, self.fgrgb) if self.tw.sharing() and share: event = 'W|%s' % (data_to_string([ self._get_my_nick(), [ label, round_int(x), round_int(y), round_int(size), round_int(w) ] ])) self.tw.send_event(event)
def set_heading(self, heading, share=True): ''' Set the turtle heading (one shape per 360/SHAPES degrees) ''' self._heading = heading self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def right(self, degrees, share=True): ''' Rotate turtle clockwise ''' self._heading += degrees self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def arc(self, a, r, share=True): ''' Draw an arc ''' if self._pen_state: self._turtles.turtle_window.canvas.set_source_rgb() try: if a < 0: pos = self.larc(-a, r) else: pos = self.rarc(a, r) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self.move_turtle(pos) if self._turtles.turtle_window.sharing() and share: event = 'a|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(r)]])) self._turtles.turtle_window.send_event(event)
def right(self, degrees, share=True): ''' Rotate turtle clockwise ''' self._heading += degrees self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def set_pen_size(self, pen_size=None, share=True): ''' Set the pen size for this turtle. ''' if pen_size is not None: self._pen_size = max(0, pen_size) self._turtles.turtle_window.canvas.set_pen_size( self._pen_size * self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'w|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_size)])) self._turtles.turtle_window.send_event(event)
def set_pen_size(self, pen_size=None, share=True): ''' Set the pen size for this turtle. ''' if pen_size is not None: self._pen_size = max(0, pen_size) self._turtles.turtle_window.canvas.set_pen_size( self._pen_size * self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'w|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_size)])) self._turtles.turtle_window.send_event(event)
def set_heading(self, heading, share=True): ''' Set the turtle heading (one shape per 360/SHAPES degrees) ''' self._heading = heading self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def set_xy(self, x, y, share=True, pendown=True, dragging=False): old = self.get_xy() try: if dragging: xcor = x ycor = y else: xcor = x * self._turtles.turtle_window.coord_scale ycor = y * self._turtles.turtle_window.coord_scale except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._draw_line(old, (xcor, ycor), pendown) self.move_turtle((xcor, ycor)) if self._turtles.turtle_window.sharing() and share: event = 'x|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(xcor), round_int(ycor)]])) self._turtles.turtle_window.send_event(event)
def set_shade(self, shade=None, share=True): ''' Set the pen shade for this turtle. ''' if shade is not None: self._pen_shade = shade self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 's|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_shade)])) self._turtles.turtle_window.send_event(event)
def set_shade(self, shade=None, share=True): ''' Set the pen shade for this turtle. ''' if shade is not None: self._pen_shade = shade self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 's|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_shade)])) self._turtles.turtle_window.send_event(event)
def arc(self, a, r, share=True): ''' Draw an arc ''' self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) if self.cr_svg is not None: self.cr_svg.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) try: if a < 0: self.larc(-a, r) else: self.rarc(a, r) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.move_turtle() if self.tw.sharing() and share: event = 'a|%s' % (data_to_string([self._get_my_nick(), [round_int(a), round_int(r)]])) self.tw.send_event(event)
def arc(self, a, r, share=True): ''' Draw an arc ''' if self._pen_state: self._turtles.turtle_window.canvas.set_source_rgb() try: if a < 0: pos = self.larc(-a, r) else: pos = self.rarc(a, r) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self.move_turtle(pos) if self._turtles.turtle_window.sharing() and share: event = 'a|%s' % (data_to_string([ self._turtles.turtle_window.nick, [round_int(a), round_int(r)] ])) self._turtles.turtle_window.send_event(event)
def set_xy(self, x, y, share=True, pendown=True, dragging=False): old = self.get_xy() try: if dragging: xcor = x ycor = y else: xcor = x * self._turtles.turtle_window.coord_scale ycor = y * self._turtles.turtle_window.coord_scale except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._draw_line(old, (xcor, ycor), pendown) self.move_turtle((xcor, ycor)) if self._turtles.turtle_window.sharing() and share: event = 'x|%s' % (data_to_string([ self._turtles.turtle_window.nick, [round_int(xcor), round_int(ycor)] ])) self._turtles.turtle_window.send_event(event)
def right(self, n, share=True): ''' Rotate turtle clockwise ''' try: self.heading += n except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: event = 'r|%s' % (data_to_string([self._get_my_nick(), round_int(self.heading)])) self.tw.send_event(event)
def setshade(self, s, share=True): ''' Set the color shade ''' try: self.shade = s except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_shade(s) self.set_fgcolor() if self.tw.sharing() and share: event = 's|%s' % (data_to_string([self._get_my_nick(), round_int(s)])) self.tw.send_event(event)
def draw_text(self, label, x, y, size, w, share=True): ''' Draw text ''' w *= self.tw.coord_scale def _draw_text(cr, label, x, y, size, w, scale, heading, rgb): cc = pangocairo.CairoContext(cr) pl = cc.create_layout() fd = pango.FontDescription('Sans') fd.set_size(int(size * scale) * pango.SCALE) pl.set_font_description(fd) if isinstance(label, (str, unicode)): pl.set_text(label.replace('\0', ' ')) elif isinstance(label, (float, int)): pl.set_text(str(label)) else: pl.set_text(str(label)) pl.set_width(int(w) * pango.SCALE) cc.save() cc.translate(x, y) cc.rotate(heading * DEGTOR) cr.set_source_rgb(rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) cc.update_layout(pl) cc.show_layout(pl) cc.restore() _draw_text(self.canvas, label, x, y, size, w, self.tw.coord_scale, self.heading, self.fgrgb) self.inval() if self.cr_svg is not None: # and self.pendown: _draw_text(self.cr_svg, label, x, y, size, w, self.tw.coord_scale, self.heading, self.fgrgb) if self.tw.sharing() and share: event = 'W|%s' % (data_to_string([self._get_my_nick(), [label, round_int(x), round_int(y), round_int(size), round_int(w)]])) self.tw.send_event(event)
def set_heading(self, heading, share=True): ''' Set the turtle heading (one shape per 360/SHAPES degrees) ''' try: self._heading = heading except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def right(self, degrees, share=True): ''' Rotate turtle clockwise ''' try: self._heading += degrees except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def update_label_value(self, name, value=None, label=None): """ Update the label of value blocks to reflect current value """ # If it is a named box, we need to match the label to the box if not self.tw.interactive_mode: return if self.tw.hide: return self.tw.display_coordinates() if value is None: for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0]) if name == 'box': argblk = block.connections[-2] dx = block.dx block.resize() if argblk is not None: # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0)) else: block.resize() elif self.update_values: if isinstance(value, float): valstring = str(round_int(value)).replace( '.', self.tw.decimal_point) else: valstring = str(value) for block in self.value_blocks_to_update[name]: if label is None: block.spr.set_label( block_names[name][0] + ' = ' + valstring) block.resize() else: argblk = block.connections[-2] # Only update if label matches if argblk is not None and argblk.spr.labels[0] == label: block.spr.set_label( block_names[name][0] + ' = ' + valstring) dx = block.dx block.resize() # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0))
def update_label_value(self, name, value=None, label=None): """ Update the label of value blocks to reflect current value """ # If it is a named box, we need to match the label to the box if not self.tw.interactive_mode: return if self.tw.hide: return self.tw.display_coordinates() if value is None: for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0]) if name == 'box': argblk = block.connections[-2] dx = block.dx block.resize() if argblk is not None: # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0)) else: block.resize() elif self.update_values: if isinstance(value, float): valstring = str(round_int(value)).replace( '.', self.tw.decimal_point) else: valstring = str(value) for block in self.value_blocks_to_update[name]: if label is None: block.spr.set_label(block_names[name][0] + ' = ' + valstring) block.resize() else: argblk = block.connections[-2] # Only update if label matches if argblk is not None and argblk.spr.labels[0] == label: block.spr.set_label(block_names[name][0] + ' = ' + valstring) dx = block.dx block.resize() # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0))
def right(self, degrees, share=True): ''' Rotate turtle clockwise ''' try: self._heading += degrees except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def set_heading(self, heading, share=True): ''' Set the turtle heading (one shape per 360/SHAPES degrees) ''' try: self._heading = heading except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._heading %= 360 self._update_sprite_heading() if self._turtles.turtle_window.sharing() and share: event = 'r|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._heading)])) self._turtles.turtle_window.send_event(event)
def set_pen_size(self, pen_size=None, share=True): ''' Set the pen size for this turtle. ''' if pen_size is not None: try: self._pen_size = max(0, pen_size) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_pen_size( self._pen_size * self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'w|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_size)])) self._turtles.turtle_window.send_event(event)
def update_label_value(self, name, value=None): """ Update the label of value blocks to reflect current value """ if self.tw.hide or not self.tw.interactive_mode: return if value is None: for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0]) block.resize() elif self.update_values: if type(value) == float: valstring = str(round_int(value)).replace( '.', self.tw.decimal_point) else: valstring = str(value) for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0] + ' = ' + valstring) block.resize()
def set_gray(self, gray=None, share=True): ''' Set the pen gray level for this turtle. ''' if gray is not None: self._pen_gray = gray if self._pen_gray < 0: self._pen_gray = 0 if self._pen_gray > 100: self._pen_gray = 100 self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'g|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_gray)])) self._turtles.turtle_window.send_event(event)
def set_shade(self, shade=None, share=True): ''' Set the pen shade for this turtle. ''' if shade is not None: try: self._pen_shade = shade except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 's|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_shade)])) self._turtles.turtle_window.send_event(event)
def set_gray(self, gray=None, share=True): ''' Set the pen gray level for this turtle. ''' if gray is not None: self._pen_gray = gray if self._pen_gray < 0: self._pen_gray = 0 if self._pen_gray > 100: self._pen_gray = 100 self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 'g|%s' % (data_to_string([self._turtles.turtle_window.nick, round_int(self._pen_gray)])) self._turtles.turtle_window.send_event(event)
def set_pen_size(self, pen_size=None, share=True): ''' Set the pen size for this turtle. ''' if pen_size is not None: try: self._pen_size = max(0, pen_size) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_pen_size( self._pen_size * self._turtles.turtle_window.coord_scale) if self._turtles.turtle_window.sharing() and share: event = 'w|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._pen_size)])) self._turtles.turtle_window.send_event(event)
def setpensize(self, ps, share=True): ''' Set the pen size ''' try: if ps < 0: ps = 0 self.pensize = ps except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_pen_size(ps) self.canvas.set_line_width(ps) if self.cr_svg is not None: self.cr_svg.set_line_width(ps) if self.tw.sharing() and share: event = 'w|%s' % (data_to_string([self._get_my_nick(), round_int(ps)])) self.tw.send_event(event)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' def _draw_pixbuf(cr, pixbuf, a, b, x, y, w, h, heading): # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cc = gtk.gdk.CairoContext(cr) cc.save() # center the rotation on the center of the image cc.translate(x + w / 2., y + h / 2.) cc.rotate(heading * DEGTOR) cc.translate(-x - w / 2., -y - h / 2.) cc.set_source_pixbuf(pixbuf, x, y) cc.rectangle(x, y, w, h) cc.fill() cc.restore() _draw_pixbuf(self.canvas, pixbuf, a, b, x, y, w, h, self.heading) self.inval() if self.cr_svg is not None: _draw_pixbuf(self.cr_svg, pixbuf, a, b, x, y, w, h, self.heading) if self.tw.sharing() and share: if self.tw.running_sugar: tmp_path = get_path(self.tw.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() x, y = self.screen_to_turtle_coordinates(x, y) event = 'P|%s' % (data_to_string([self._get_my_nick(), [round_int(a), round_int(b), round_int(x), round_int(y), round_int(w), round_int(h), round_int(width), round_int(height), data]])) gobject.idle_add(self.tw.send_event, event) os.remove(tmp_file)
def setgray(self, g, share=True): ''' Set the gray level ''' try: self.gray = g except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return if self.gray < 0: self.gray = 0 if self.gray > 100: self.gray = 100 self.set_fgcolor() self.tw.active_turtle.set_gray(self.gray) if self.tw.sharing() and share: event = 'g|%s' % (data_to_string([self._get_my_nick(), round_int(self.gray)])) self.tw.send_event(event)
def set_shade(self, shade=None, share=True): ''' Set the pen shade for this turtle. ''' if shade is not None: try: self._pen_shade = shade except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), self._turtles.turtle_window.running_sugar) return self._turtles.turtle_window.canvas.set_fgcolor(shade=self._pen_shade, gray=self._pen_gray, color=self._pen_color) if self._turtles.turtle_window.sharing() and share: event = 's|%s' % (data_to_string( [self._turtles.turtle_window.nick, round_int(self._pen_shade)])) self._turtles.turtle_window.send_event(event)
if self.tw.sharing() and share: self.tw.activity.send_event("f|%s" % \ (data_to_string([self.tw.nick, int(n)]))) def seth(self, n, share=True): """ Set the turtle heading. """ try: self.heading = n except TypeError, ValueError: _logger.debug("bad value sent to %s" % (__name__)) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: self.tw.activity.send_event("r|%s" % \ (data_to_string([self.tw.nick, round_int(self.heading)]))) def right(self, n, share=True): """ Rotate turtle clockwise """ try: self.heading += n except TypeError, ValueError: _logger.debug("bad value sent to %s" % (__name__)) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: self.tw.activity.send_event("r|%s" % \ (data_to_string([self.tw.nick, round_int(self.heading)]))) def arc(self, a, r, share=True):
self.inval() def seth(self, n, share=True): ''' Set the turtle heading. ''' try: self.heading = n except TypeError, ValueError: debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: event = 'r|%s' % (data_to_string( [self._get_my_nick(), round_int(self.heading)])) self.tw.send_event(event) def right(self, n, share=True): ''' Rotate turtle clockwise ''' try: self.heading += n except TypeError, ValueError: debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: event = 'r|%s' % (data_to_string( [self._get_my_nick(),