From 040275d412029fe5dcde82cd9b6ada041db43248 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 25 Jun 2013 03:29:06 +0000 Subject: more turtle-centric refactoring --- (limited to 'TurtleArt/taturtle.py') diff --git a/TurtleArt/taturtle.py b/TurtleArt/taturtle.py index 992af95..b6e2e68 100644 --- a/TurtleArt/taturtle.py +++ b/TurtleArt/taturtle.py @@ -19,16 +19,22 @@ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. -from random import uniform -from math import sin, cos, pi, sqrt +import os + import gtk +import gobject import cairo -from taconstants import (TURTLE_LAYER, DEFAULT_TURTLE_COLORS) -from tasprite_factory import (SVG, svg_str_to_pixbuf) -from tacanvas import (wrap100, COLOR_TABLE, COLORDICT) +from random import uniform +from math import sin, cos, pi, sqrt + +from taconstants import (TURTLE_LAYER, DEFAULT_TURTLE_COLORS, DEFAULT_TURTLE, + COLORDICT) +from tasprite_factory import SVG, svg_str_to_pixbuf +from tacanvas import wrap100, COLOR_TABLE from sprites import Sprite -from tautils import debug_output, data_to_string, round_int +from tautils import (debug_output, data_to_string, round_int, get_path, + image_to_base64) SHAPES = 36 DEGTOR = pi / 180. @@ -36,7 +42,7 @@ RTODEG = 180. / pi def generate_turtle_pixbufs(colors): - """ Generate pixbufs for generic turtles """ + ''' Generate pixbufs for generic turtles ''' shapes = [] svg = SVG() svg.set_scale(1.0) @@ -48,79 +54,148 @@ def generate_turtle_pixbufs(colors): class Turtles: - def __init__(self, sprite_list): - """ Class to hold turtles """ - self.dict = dict() - self.sprite_list = sprite_list - self.default_pixbufs = [] - - def get_turtle(self, k, append=False, colors=None): - """ Find a turtle """ - if k in self.dict: - return self.dict[k] + def __init__(self, turtle_window): + ''' Class to hold turtles ''' + self.turtle_window = turtle_window + self.sprite_list = turtle_window.sprite_list + self.width = turtle_window.width + self.height = turtle_window.height + self.dict = {} + self._default_pixbufs = [] + self._active_turtle = None + self._default_turtle_name = DEFAULT_TURTLE + + def get_turtle(self, turtle_name, append=False, colors=None): + ''' Find a turtle ''' + if turtle_name in self.dict: + return self.dict[turtle_name] elif not append: return None else: if colors is None: - Turtle(self, k) + Turtle(self, turtle_name) elif isinstance(colors, (list, tuple)): - Turtle(self, k, colors) + Turtle(self, turtle_name, colors) else: - Turtle(self, k, colors.split(',')) - return self.dict[k] + Turtle(self, turtle_name, colors.split(',')) + return self.dict[turtle_name] def get_turtle_key(self, turtle): - """ Find a turtle's name """ - for k in iter(self.dict): - if self.dict[k] == turtle: - return k + ''' Find a turtle's name ''' + for turtle_name in iter(self.dict): + if self.dict[turtle_name] == turtle: + return turtle_name return None def turtle_count(self): - """ How many turtles are there? """ + ''' How many turtles are there? ''' return(len(self.dict)) - def add_to_dict(self, k, turtle): - """ Add a new turtle """ - self.dict[k] = turtle + def add_to_dict(self, turtle_name, turtle): + ''' Add a new turtle ''' + self.dict[turtle_name] = turtle - def remove_from_dict(self, k): - """ Delete a turtle """ - if k in self.dict: - del(self.dict[k]) + def remove_from_dict(self, turtle_name): + ''' Delete a turtle ''' + if turtle_name in self.dict: + del(self.dict[turtle_name]) def show_all(self): - """ Make all turtles visible """ - for k in iter(self.dict): - self.dict[k].show() + ''' Make all turtles visible ''' + for turtle_name in iter(self.dict): + self.dict[turtle_name].show() def spr_to_turtle(self, spr): - """ Find the turtle that corresponds to sprite spr. """ - for k in iter(self.dict): - if spr == self.dict[k].spr: - return self.dict[k] + ''' Find the turtle that corresponds to sprite spr. ''' + for turtle_name in iter(self.dict): + if spr == self.dict[turtle_name].spr: + return self.dict[turtle_name] return None def get_pixbufs(self): - """ Get the pixbufs for the default turtle shapes. """ - if self.default_pixbufs == []: - self.default_pixbufs = generate_turtle_pixbufs( + ''' Get the pixbufs for the default turtle shapes. ''' + if self._default_pixbufs == []: + self._default_pixbufs = generate_turtle_pixbufs( ["#008000", "#00A000"]) - return(self.default_pixbufs) + return(self._default_pixbufs) + + def turtle_to_screen_coordinates(self, x, y): + ''' The origin of turtle coordinates is the center of the screen ''' + return self.width / 2.0 + x, self.invert_y_coordinate(y) + + def screen_to_turtle_coordinates(self, x, y): + ''' The origin of the screen coordinates is the upper left corner ''' + return x - self.width / 2.0, self.invert_y_coordinate(y) + + def invert_y_coordinate(self, y): + ''' Positive y goes up in turtle coordinates, down in sceeen + coordinates ''' + return self.height / 2.0 - y + + def reset_turtles(self): + for turtle_name in iter(self.dict): + self.set_turtle(turtle_name) + if not self._active_turtle.get_remote(): + self._active_turtle.set_color(0) + self._active_turtle.set_shade(50) + self._active_turtle.set_gray(100) + self._active_turtle.set_pen_size(5) + self._active_turtle.reset_shapes() + self._active_turtle.set_heading(0.0) + self._active_turtle.set_pen_state(False) + self._active_turtle.move_turtle((0.0, 0.0)) + self._active_turtle.set_pen_state(True) + self._active_turtle.set_fill(False) + self._active_turtle.hide() + self.set_turtle(self._default_turtle_name) + + def set_turtle(self, turtle_name, colors=None): + ''' Select the current turtle and associated pen status ''' + if turtle_name not in self.dict: + # if it is a new turtle, start it in the center of the screen + self._active_turtle = self.get_turtle(turtle_name, True, colors) + self._active_turtle.set_heading(0.0, False) + self._active_turtle.set_xy(0.0, 0.0, False, pendown=False) + self._active_turtle.set_pen_state(True) + elif colors is not None: + self._active_turtle = self.get_turtle(turtle_name, False) + self._active_turtle.set_turtle_colors(colors) + else: + self._active_turtle = self.get_turtle(turtle_name, False) + self._active_turtle.show() + tx, ty = self._active_turtle.get_xy() + self._active_turtle.set_color(share=False) + self._active_turtle.set_gray(share=False) + self._active_turtle.set_shade(share=False) + self._active_turtle.set_pen_size(share=False) + self._active_turtle.set_pen_state(share=False) + + def set_default_turtle_name(self, name): + self._default_turtle_name = name + + def get_default_turtle_name(self): + return self._default_turtle_name + + def set_active_turtle(self, active_turtle): + self._active_turtle = active_turtle + + def get_active_turtle(self): + return self._active_turtle class Turtle: - def __init__(self, turtle_window, turtles, key, turtle_colors=None): - """ The turtle is not a block, just a sprite with an orientation """ - self.tw = turtle_window - self.x = 0.0 - self.y = 0.0 + def __init__(self, turtles, turtle_name, turtle_colors=None): + ''' The turtle is not a block, just a sprite with an orientation ''' + self.turtles = turtles self.hidden = False self.shapes = [] self.custom_shapes = False self.type = 'turtle' - self.name = key + self.name = turtle_name + self.remote = False + self.x = 0.0 + self.y = 0.0 self.heading = 0.0 self.pen_shade = 50 self.pen_color = 0 @@ -131,15 +206,15 @@ class Turtle: self.pen_poly_points = [] self.label_block = None - self._prep_shapes(key, turtles, turtle_colors) + self._prep_shapes(turtle_name, self.turtles, turtle_colors) # Choose a random angle from which to attach the turtle label. if turtles.sprite_list is not None: - self.spr = Sprite(turtles.sprite_list, 0, 0, self.shapes[0]) + self.spr = Sprite(self.turtles.sprite_list, 0, 0, self.shapes[0]) angle = uniform(0, pi * 4 / 3.0) # 240 degrees w = self.shapes[0].get_width() r = w * 0.67 - # Restrict angle the the sides 30-150; 210-330 + # Restrict the angle to the sides: 30-150; 210-330 if angle > pi * 2 / 3.0: angle += pi / 2.0 # + 90 self.label_xy = [int(r * sin(angle)), @@ -150,7 +225,13 @@ class Turtle: int(r * cos(angle) + w / 2.0)] else: self.spr = None - turtles.add_to_dict(key, self) + self.turtles.add_to_dict(turtle_name, self) + + def set_remote(self): + self.remote = True + + def get_remote(self): + return self.remote def _prep_shapes(self, name, turtles=None, turtle_colors=None): # If the turtle name is an int, we'll use a palette color as the @@ -183,7 +264,7 @@ class Turtle: self.set_heading(self.heading, share=False) def set_shapes(self, shapes, i=0): - """ Reskin the turtle """ + ''' Reskin the turtle ''' n = len(shapes) if n == 1 and i > 0: # set shape[i] if i < len(self.shapes): @@ -193,7 +274,7 @@ class Turtle: else: # rotate shapes if n != 1: debug_output("%d images passed to set_shapes: ignoring" % (n), - self.tw.running_sugar) + self.turtles.turtle_window.running_sugar) if self.heading == 0.0: # rotate the shapes images = [] w, h = shapes[0].get_width(), shapes[0].get_height() @@ -202,11 +283,11 @@ class Turtle: surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, nw, nh) context = cairo.Context(surface) context = gtk.gdk.CairoContext(context) - context.translate(nw / 2., nh / 2.) + context.translate(nw / 2.0, nh / 2.0) context.rotate(i * 10 * pi / 180.) - context.translate(-nw / 2., -nh / 2.) - context.set_source_pixbuf(shapes[0], (nw - w) / 2., - (nh - h) / 2.) + context.translate(-nw / 2.0, -nh / 2.0) + context.set_source_pixbuf(shapes[0], (nw - w) / 2.0, + (nh - h) / 2.0) context.rectangle(0, 0, nw, nh) context.fill() images.append(surface) @@ -218,18 +299,18 @@ class Turtle: self.show() def reset_shapes(self): - """ Reset the shapes to the standard turtle """ + ''' Reset the shapes to the standard turtle ''' if self.custom_shapes: self.shapes = generate_turtle_pixbufs(self.colors) self.custom_shapes = False def set_heading(self, heading, share=True): - """ Set the turtle heading (one shape per 360/SHAPES degrees) """ + ''' 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.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return self.heading %= 360 @@ -240,13 +321,13 @@ class Turtle: except IndexError: self.spr.set_shape(self.shapes[0]) - if self.tw.sharing() and share: - event = 'r|%s' % (data_to_string([self.tw.nick, + if self.turtles.turtle_window.sharing() and share: + event = 'r|%s' % (data_to_string([self.turtles.turtle_window.nick, round_int(self.heading)])) - self.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_color(self, color=None, share=True): - """ Set the pen color for this turtle. """ + ''' 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) @@ -261,26 +342,26 @@ class Turtle: self.pen_color = color except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), - self.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return - self.tw.canvas.set_fgcolor(shade=self.pen_shade, - gray=self.pen_gray, - color=self.pen_color) + self.turtles.turtle_window.canvas.set_fgcolor(shade=self.pen_shade, + gray=self.pen_gray, + color=self.pen_color) - if self.tw.sharing() and share: - event = 'c|%s' % (data_to_string([self.tw.nick, + 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.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_gray(self, gray=None, share=True): - """ Set the pen gray level for this turtle. """ + ''' Set the pen gray level for this turtle. ''' if gray is not None: try: self.pen_gray = gray except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), - self.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return if self.pen_gray < 0: @@ -288,62 +369,60 @@ class Turtle: if self.pen_gray > 100: self.pen_gray = 100 - self.tw.canvas.set_fgcolor(shade=self.pen_shade, - gray=self.pen_gray, - color=self.pen_color) + self.turtles.turtle_window.canvas.set_fgcolor(shade=self.pen_shade, + gray=self.pen_gray, + color=self.pen_color) - if self.tw.sharing() and share: - event = 'g|%s' % (data_to_string([self.tw.nick, + 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.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_shade(self, shade=None, share=True): - """ Set the pen shade for this turtle. """ + ''' 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.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return - self.tw.canvas.set_fgcolor(shade=self.pen_shade, - gray=self.pen_gray, - color=self.pen_color) + self.turtles.turtle_window.canvas.set_fgcolor(shade=self.pen_shade, + gray=self.pen_gray, + color=self.pen_color) - if self.tw.sharing() and share: - event = 's|%s' % (data_to_string([self.tw.nick, + 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.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_pen_size(self, pen_size=None, share=True): - """ Set the pen size for this turtle. """ + ''' 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.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return - self.tw.canvas.set_pen_size(self.pen_size) + self.turtles.turtle_window.canvas.set_pen_size(self.pen_size) - if self.tw.sharing() and share: - event = 'w|%s' % (data_to_string([self.tw.nick, + 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.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_pen_state(self, pen_state=None, share=True): - """ Set the pen state (down==True) for this turtle. """ + ''' Set the pen state (down==True) for this turtle. ''' if pen_state is not None: self.pen_state = pen_state - self.tw.canvas.set_pen(self.pen_state) - - if self.tw.sharing() and share: - event = 'p|%s' % (data_to_string([self.tw.nick, + if self.turtles.turtle_window.sharing() and share: + event = 'p|%s' % (data_to_string([self.turtles.turtle_window.nick, self._pen_state])) - self.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_fill(self, state=False): self.pen_fill = state @@ -363,16 +442,16 @@ class Turtle: if len(self.poly_points) == 0: return - self.tw.canvas.fill_polygon(self.poly_points) + self.turtles.turtle_window.canvas.fill_polygon(self.poly_points) - if self.tw.sharing() and share: + if self.turtles.turtle_window.sharing() and share: shared_poly_points = [] for p in self.poly_points: shared_poly_points.append( - (self.tw.canvas.screen_to_turtle_coordinates(p[0], p[1]))) - event = 'F|%s' % (data_to_string([self.tw.nick, - shared_poly_points])) - self.tw.send_event(event) + (self.turtles.screen_to_turtle_coordinates(p[0], p[1]))) + event = 'F|%s' % (data_to_string( + [self.turtles.turtle_window.nick, shared_poly_points])) + self.turtles.turtle_window.send_event(event) self.poly_points = [] def hide(self): @@ -393,171 +472,149 @@ class Turtle: def move_turtle(self, pos=None): if pos is None: - x, y = self.tw.canvas.get_xy() - else: - x, y = self.tw.canvas.turtle_to_screen_coordinates(pos[0], pos[1]) - if self.tw.interactive_mode: - self.move((self.tw.canvas.cx + x - self.spr.rect.width / 2., - self.tw.canvas.cy + y - self.spr.rect.height / 2.)) + x, y = self.get_xy() else: - self.move((self.tw.canvas.cx + x, self.tw.canvas.cy + y)) + x, y = pos[0], pos[1] + + self.x, self.y = x, y + self.move((x, y)) def move(self, pos): - self.x, self.y = pos[0], pos[1] - if not self.hidden and self.spr is not None: - self.spr.move((int(pos[0]), int(pos[1]))) - if self.label_block is not None: - self.label_block.spr.move((int(pos[0] + self.label_xy[0]), - int(pos[1] + self.label_xy[1]))) - return(self.x, self.y) + # self.x, self.y = pos[0], pos[1] + x, y = self.turtles.turtle_to_screen_coordinates(pos[0], pos[1]) - def get_name(self): - return self.name + if self.turtles.turtle_window.interactive_mode: + x -= self.spr.rect.width / 2.0 + y -= self.spr.rect.height / 2.0 - def get_xy(self): + if not self.hidden and self.spr is not None: + self.spr.move((int(x), int(y))) + if self.label_block is not None: + self.label_block.spr.move((int(x + self.label_xy[0]), + int(y + self.label_xy[1]))) return(self.x, self.y) - def get_heading(self): - return(self.heading) - - def get_color(self): - return(self.pen_color) - - def get_gray(self): - return(self.pen_gray) - - def get_shade(self): - return(self.pen_shade) - - def get_pen_size(self): - return(self.pen_size) - - def get_pen_state(self): - return(self.pen_state) - - def get_fill(self): - return(self.pen_fill) - - def get_poly_points(self): - return(self.poly_points) - 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.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return self.heading %= 360 - if self.tw.sharing() and share: - event = 'r|%s' % (data_to_string([self.tw.nick, + if self.turtles.turtle_window.sharing() and share: + event = 'r|%s' % (data_to_string([self.turtles.turtle_window.nick, round_int(self.heading)])) - self.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def forward(self, distance, share=True): - scaled_distance = distance * self.tw.coord_scale + scaled_distance = distance * self.turtles.turtle_window.coord_scale - self.tw.canvas.set_rgb(self.tw.canvas.fgrgb[0] / 255., - self.tw.canvas.fgrgb[1] / 255., - self.tw.canvas.fgrgb[2] / 255.) + self.turtles.turtle_window.canvas.set_rgb( + self.turtles.turtle_window.canvas.fgrgb[0] / 255., + self.turtles.turtle_window.canvas.fgrgb[1] / 255., + self.turtles.turtle_window.canvas.fgrgb[2] / 255.) - oldx, oldy = self.tw.canvas.get_xy() + oldx, oldy = self.get_xy() try: xcor = oldx + scaled_distance * sin(self.heading * DEGTOR) ycor = oldy + scaled_distance * cos(self.heading * DEGTOR) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), - self.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return - self.tw.canvas.set_xy(xcor, ycor) if self.pen_state: - self.tw.canvas.draw_line(oldx, oldy, xcor, ycor) + x1, y1 = self.turtles.turtle_to_screen_coordinates(oldx, oldy) + x2, y2 = self.turtles.turtle_to_screen_coordinates(xcor, ycor) + self.turtles.turtle_window.canvas.draw_line(x1, y1, x2, y2) if self.pen_fill: if self.poly_points == []: - x, y = self.tw.canvas.turtle_to_screen_coordinates(oldx, oldy) + x, y = self.turtles.turtle_to_screen_coordinates(oldx, oldy) self.poly_points.append(('move', x, y)) - x, y = self.tw.canvas.turtle_to_screen_coordinates(xcor, ycor) + x, y = self.turtles.turtle_to_screen_coordinates(xcor, ycor) self.poly_points.append(('line', x, y)) self.move_turtle((xcor, ycor)) - if self.tw.sharing() and share: - event = 'f|%s' % (data_to_string([self.tw.nick, + if self.turtles.turtle_window.sharing() and share: + event = 'f|%s' % (data_to_string([self.turtles.turtle_window.nick, int(distance)])) - self.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def set_xy(self, x, y, share=True, pendown=True): - oldx, oldy = self.tw.canvas.get_xy() + oldx, oldy = self.get_xy() try: - xcor = x * self.tw.coord_scale - ycor = y * self.tw.coord_scale - self.tw.canvas.set_xy(x, y) + 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.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return if self.pen_state and pendown: - self.tw.canvas.set_rgb(self.tw.canvas.fgrgb[0] / 255., - self.tw.canvas.fgrgb[1] / 255., - self.tw.canvas.fgrgb[2] / 255.) - self.tw.canvas.draw_line(oldx, oldy, xcor, ycor) + self.turtles.turtle_window.canvas.set_rgb( + self.turtles.turtle_window.canvas.fgrgb[0] / 255., + self.turtles.turtle_window.canvas.fgrgb[1] / 255., + self.turtles.turtle_window.canvas.fgrgb[2] / 255.) + x1, y1 = self.turtles.turtle_to_screen_coordinates(oldx, oldy) + x2, y2 = self.turtles.turtle_to_screen_coordinates(xcor, ycor) + self.turtles.turtle_window.canvas.draw_line(x1, y1, x2, y2) if self.pen_fill: if self.poly_points == []: - x, y = self.tw.canvas.turtle_to_screen_coordinates(oldx, oldy) + x, y = self.turtles.turtle_to_screen_coordinates(oldx, oldy) self.poly_points.append(('move', x, y)) - x, y = self.tw.canvas.turtle_to_screen_coordinates(xcor, ycor) + x, y = self.turtles.turtle_to_screen_coordinates(xcor, ycor) self.poly_points.append(('line', x, y)) self.move_turtle((xcor, ycor)) - if self.tw.sharing() and share: - event = 'x|%s' % (data_to_string([self.tw.nick, + if self.turtles.turtle_window.sharing() and share: + event = 'x|%s' % (data_to_string([self.turtles.turtle_window.nick, [round_int(x), round_int(y)]])) - self.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def arc(self, a, r, share=True): ''' Draw an arc ''' if self.pen_state: - self.tw.canvas.set_rgb(self.tw.canvas.fgrgb[0] / 255., - self.tw.canvas.fgrgb[1] / 255., - self.tw.canvas.fgrgb[2] / 255.) + self.turtles.turtle_window.canvas.set_rgb( + self.turtles.turtle_window.canvas.fgrgb[0] / 255., + self.turtles.turtle_window.canvas.fgrgb[1] / 255., + self.turtles.turtle_window.canvas.fgrgb[2] / 255.) try: if a < 0: - self.larc(-a, r) + xcor, ycor = self.larc(-a, r) else: - self.rarc(a, r) + xcor, ycor = self.rarc(a, r) except (TypeError, ValueError): debug_output('bad value sent to %s' % (__name__), - self.tw.running_sugar) + self.turtles.turtle_window.running_sugar) return - xcor, ycor = self.tw.canvas.get_xy() self.move_turtle((xcor, ycor)) - if self.tw.sharing() and share: - event = 'a|%s' % (data_to_string([self.tw.nick, + 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.tw.send_event(event) + self.turtles.turtle_window.send_event(event) def rarc(self, a, r): ''' draw a clockwise arc ''' - r *= self.tw.coord_scale + r *= self.turtles.turtle_window.coord_scale if r < 0: r = -r a = -a - xcor, ycor = self.tw.canvas.get_xy() + xcor, ycor = self.get_xy() cx = xcor + r * cos(self.heading * DEGTOR) cy = ycor - r * sin(self.heading * DEGTOR) if self.pen_state: - x, y = self.tw.canvas.turtle_to_screen_coordinates(cx, cy) - self.tw.canvas.rarc(x, y, r, a, self.heading) + x, y = self.turtles.turtle_to_screen_coordinates(cx, cy) + self.turtles.turtle_window.canvas.rarc(x, y, r, a, self.heading) if self.pen_fill: - x, y = self.tw.canvas.turtle_to_screen_coordinates(x, y) + x, y = self.turtles.turtle_to_screen_coordinates(x, y) if self.poly_points == []: self.poly_points.append(('move', x, y)) self.poly_points.append(('rarc', x, y, r, @@ -565,24 +622,24 @@ class Turtle: (self.heading - 180 + a) * DEGTOR)) self.right(a, False) - self.tw.canvas.set_xy(cx - r * cos(self.heading * DEGTOR), - cy + r * sin(self.heading * DEGTOR)) + return cx - r * cos(self.heading * DEGTOR), \ + cy + r * sin(self.heading * DEGTOR) def larc(self, a, r): ''' draw a counter-clockwise arc ''' - r *= self.tw.coord_scale + r *= self.turtles.turtle_window.coord_scale if r < 0: r = -r a = -a - xcor, ycor = self.tw.canvas.get_xy() + xcor, ycor = self.get_xy() cx = xcor - r * cos(self.heading * DEGTOR) cy = ycor + r * sin(self.heading * DEGTOR) if self.pen_state: - x, y = self.tw.canvas.turtle_to_screen_coordinates(cx, cy) - self.tw.canvas.larc(x, y, r, a, self.heading) + x, y = self.turtles.turtle_to_screen_coordinates(cx, cy) + self.turtles.turtle_window.canvas.larc(x, y, r, a, self.heading) if self.pen_fill: - x, y = self.tw.canvas.turtle_to_screen_coordinates(x, y) + x, y = self.turtles.turtle_to_screen_coordinates(x, y) if self.poly_points == []: self.poly_points.append(('move', x, y)) self.poly_points.append(('larc', x, y, r, @@ -590,5 +647,86 @@ class Turtle: (self.heading - a) * DEGTOR)) self.right(-a, False) - self.tw.canvas.set_xy(cx + r * cos(self.heading * DEGTOR), - cy - r * sin(self.heading * DEGTOR)) + return cx + r * cos(self.heading * DEGTOR), \ + cy - r * sin(self.heading * DEGTOR) + + 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() + + x, y = self.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(x), round_int(y), + 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_text(self, label, x, y, size, w, share=True): + ''' Draw text ''' + w *= self.turtles.turtle_window.coord_scale + self.turtles.turtle_window.canvas.draw_text(label, x, y, size, w, + self.heading) + + 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 get_name(self): + return self.name + + def get_xy(self): + return(self.x, self.y) + + def get_heading(self): + return(self.heading) + + def get_color(self): + return(self.pen_color) + + def get_gray(self): + return(self.pen_gray) + + def get_shade(self): + return(self.pen_shade) + + def get_pen_size(self): + return(self.pen_size) + + def get_pen_state(self): + return(self.pen_state) + + def get_fill(self): + return(self.pen_fill) + + def get_poly_points(self): + return(self.poly_points) + + def get_pixel(self): + x, y = self.get_xy() + x, y = self.turtle_to_screen_coordinates(x, y) + return self.turtles.turtle_window.canvas.get_pixel(x, y) -- cgit v0.9.1