Example #1
0
 def setpen(self, bool, share=True):
     """ Lower or raise the pen """
     self.pendown = bool
     self.tw.active_turtle.set_pen_state(bool)
     if self.tw.sharing() and share:
         self.tw.activity.send_event("p|%s" % \
             (data_to_string([self.tw.nick, bool])))
    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)
Example #3
0
    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)
Example #4
0
 def setpen(self, bool, share=True):
     ''' Lower or raise the pen '''
     self.pendown = bool
     self.tw.active_turtle.set_pen_state(bool)
     if self.tw.sharing() and share:
         event = 'p|%s' % (data_to_string([self._get_my_nick(), bool]))
         self.tw.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)
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    def forward(self, n, share=True):
        ''' Move the turtle forward.'''
        nn = n * self.tw.coord_scale
        self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255.,
                                   self.fgrgb[2] / 255.)
        if self.cr_svg is not None:
            debug_output('in forward', True)
            self.cr_svg.set_source_rgb(self.fgrgb[0] / 255.,
                                       self.fgrgb[1] / 255.,
                                       self.fgrgb[2] / 255.)
        oldx, oldy = self.xcor, self.ycor
        try:
            self.xcor += nn * sin(self.heading * DEGTOR)
            self.ycor += nn * cos(self.heading * DEGTOR)
        except (TypeError, ValueError):
            debug_output('bad value sent to %s' % (__name__),
                         self.tw.running_sugar)
            return
        if self.pendown:
            self.draw_line(oldx, oldy, self.xcor, self.ycor)

        self.move_turtle()

        if self.tw.sharing() and share:
            event = 'f|%s' % (data_to_string([self._get_my_nick(), int(n)]))
            self.tw.send_event(event)
        self.inval()
    def set_pen_state(self, pen_state=None, share=True):
        ''' Set the pen state (down==True) for this turtle. '''
        if pen_state is not None:
            self._pen_state = pen_state

        if self._turtles.turtle_window.sharing() and share:
            event = 'p|%s' % (data_to_string([self._turtles.turtle_window.nick,
                                              self._pen_state]))
            self._turtles.turtle_window.send_event(event)
Example #10
0
    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)
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
    def forward(self, distance, share=True):
        #print 'taturtle.py: def forward'
        scaled_distance = distance * self._turtles.turtle_window.coord_scale

        old = self.get_xy() #Projected Point
        old_3D = self.get_3Dpoint() #Actual Point

        #xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR)
        #ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR)

        xcor = old_3D[0] + scaled_distance * self._direction[0]
        ycor = old_3D[1] + scaled_distance * self._direction[1]
        zcor = old_3D[2] + scaled_distance * self._direction[2]

        width = self._turtles.turtle_window.width
        height = self._turtles.turtle_window.height
        
        old_point = Point3D(old_3D[0], old_3D[1], old_3D[2]) # Old point as Point3D object
        p = old_point.project(width, height, 512, 512) # Projected Old Point
        new_x, new_y = p.x, p.y
        pair1 = [new_x, new_y]
        pos1 = self._turtles.screen_to_turtle_coordinates(pair1)
        
        '''
        for i, val in enumerate(old_3D):
            if (abs(val) < 0.0001):
                old_3D[i] = 0.
            old_3D[i] = round(old_3D[i], 2)
        self._points.append([old_3D[0], old_3D[1], old_3D[2]])
        if (self._pen_state):
            self._points_penstate.append(1)
        else:
            self._points_penstate.append(0)
        '''

        self._3Dx, self._3Dy, self._3Dz = xcor, ycor, zcor
        self.store_data()

        new_point = Point3D(xcor, ycor, zcor) # New point as 3D object
        p = new_point.project(width, height, 512, 512) # Projected New Point
        new_x, new_y = p.x, p.y
        pair2 = [new_x, new_y]
        pos2 = self._turtles.screen_to_turtle_coordinates(pair2)
        #print 'new = ', new_point.x, new_point.y, new_point.z

        self._draw_line(pos1, pos2, True)
        #self.move_turtle((xcor, ycor))
        self.move_turtle((pos2[0], pos2[1]))

        if self._turtles.turtle_window.sharing() and share:
            event = 'f|%s' % (data_to_string([self._turtles.turtle_window.nick,
                                              int(distance)]))
            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)
Example #15
0
    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)
Example #16
0
 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)
Example #17
0
    def forward(self, distance, share=True):
        scaled_distance = distance * self._turtles.turtle_window.coord_scale

        old = self.get_xy()
        xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR)
        ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR)

        self._draw_line(old, (xcor, ycor), True)
        self.move_turtle((xcor, ycor))

        if self._turtles.turtle_window.sharing() and share:
            event = 'f|%s' % (data_to_string([self._turtles.turtle_window.nick,
                                              int(distance)]))
            self._turtles.turtle_window.send_event(event)
Example #18
0
 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)
Example #19
0
    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)
Example #20
0
 def stop_fill(self):
     ''' Fill the polygon. '''
     self.fill = False
     if len(self.poly_points) == 0:
         return
     self.fill_polygon(self.poly_points)
     if self.tw.sharing():
         shared_poly_points = []
         for p in self.poly_points:
             shared_poly_points.append((self.screen_to_turtle_coordinates
                                        (p[0], p[1])))
             event = 'F|%s' % (data_to_string([self._get_my_nick(),
                                               shared_poly_points]))
         self.tw.send_event(event)
     self.poly_points = []
    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)
Example #23
0
    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
                ]
            ]))
            self.tw.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 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)
Example #26
0
    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)
Example #27
0
    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_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 stop_fill(self):
     ''' Fill the polygon. '''
     self.fill = False
     if len(self.poly_points) == 0:
         return
     self.fill_polygon(self.poly_points)
     if self.tw.sharing():
         shared_poly_points = []
         for p in self.poly_points:
             shared_poly_points.append(
                 (self.screen_to_turtle_coordinates(p[0], p[1])))
             event = 'F|%s' % (data_to_string(
                 [self._get_my_nick(), shared_poly_points]))
         self.tw.send_event(event)
     self.poly_points = []
     if self.tw.saving_svg:
         self.tw.svg_string += '</g>'
    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 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 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)
Example #33
0
 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)
Example #34
0
    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_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 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)
Example #37
0
 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)
Example #38
0
    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_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 stop_fill(self, share=True):
        self._pen_fill = False
        if len(self._poly_points) == 0:
            return

        self._turtles.turtle_window.canvas.fill_polygon(self._poly_points)

        if self._turtles.turtle_window.sharing() and share:
            shared_poly_points = []
            for p in self._poly_points:
                x, y = self._turtles.turtle_to_screen_coordinates((p[1], p[2]))
                if p[0] in ['move', 'line']:
                    shared_poly_points.append((p[0], x, y))
                elif p[0] in ['rarc', 'larc']:
                    shared_poly_points.append((p[0], x, y, p[3], p[4], p[5]))
                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 forward(self, distance, share=True):
        scaled_distance = distance * self._turtles.turtle_window.coord_scale

        old = self.get_xy()
        try:
            xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR)
            ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR)
        except (TypeError, ValueError):
            debug_output('bad value sent to %s' % (__name__),
                         self._turtles.turtle_window.running_sugar)
            return

        self._draw_line(old, (xcor, ycor), True)
        self.move_turtle((xcor, ycor))

        if self._turtles.turtle_window.sharing() and share:
            event = 'f|%s' % (data_to_string([self._turtles.turtle_window.nick,
                                              int(distance)]))
            self._turtles.turtle_window.send_event(event)
Example #42
0
    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 forward(self, distance, share=True):
        scaled_distance = distance * self._turtles.turtle_window.coord_scale

        old = self.get_xy()
        try:
            xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR)
            ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR)
        except (TypeError, ValueError):
            debug_output('bad value sent to %s' % (__name__),
                         self._turtles.turtle_window.running_sugar)
            return

        self._draw_line(old, (xcor, ycor), True)
        self.move_turtle((xcor, ycor))

        if self._turtles.turtle_window.sharing() and share:
            event = 'f|%s' % (data_to_string(
                [self._turtles.turtle_window.nick,
                 int(distance)]))
            self._turtles.turtle_window.send_event(event)
Example #44
0
    def forward(self, distance, share=True):
        scaled_distance = distance * self._turtles.turtle_window.coord_scale

        old = self.get_xy() #Projected Point
        old_3D = self.get_3Dpoint() #Actual Point

        #xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR)
        #ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR)

        xcor = old_3D[0] + scaled_distance * self._direction[0]
        ycor = old_3D[1] + scaled_distance * self._direction[1]
        zcor = old_3D[2] + scaled_distance * self._direction[2]

        width = self._turtles.turtle_window.width
        height = self._turtles.turtle_window.height
        
        # Old point as Point3D object
        old_point = Point3D(old_3D[0], old_3D[1], old_3D[2])
        # Projected Old Point
        p = old_point.project(width, height, self._camera)
        new_x, new_y = p.x, p.y
        pair1 = [new_x, new_y]
        pos1 = self._turtles.screen_to_turtle_coordinates(pair1)
        
        self._3Dx, self._3Dy, self._3Dz = xcor, ycor, zcor
        self.store_data()

        new_point = Point3D(xcor, ycor, zcor) # New point as 3D object
        p = new_point.project(width, height, self._camera) # Projected New Point
        new_x, new_y = p.x, p.y
        pair2 = [new_x, new_y]
        pos2 = self._turtles.screen_to_turtle_coordinates(pair2)

        self._draw_line(pos1, pos2, True)
        self.move_turtle((pos2[0], pos2[1]))

        if self._turtles.turtle_window.sharing() and share:
            event = 'f|%s' % (data_to_string([self._turtles.turtle_window.nick,
                                              int(distance)]))
            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)
Example #46
0
 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 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 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_color(self, color=None, share=True):
        ''' Set the pen color for this turtle. '''
        if color is None:
            color = self._pen_color
        # Special case for color blocks
        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)
Example #50
0
class TurtleGraphics:
    ''' A class for the Turtle graphics canvas '''
    def __init__(self, tw, width, height):
        ''' Create a sprite to hold the canvas. '''
        self.tw = tw
        self.width = width
        self.height = height

        # Build a cairo.Context from a cairo.XlibSurface
        self.canvas = cairo.Context(self.tw.turtle_canvas)
        cr = gtk.gdk.CairoContext(self.canvas)
        cr.set_line_cap(1)  # Set the line cap to be round
        self.cr_svg = None  # Surface used for saving to SVG
        self.cx = 0
        self.cy = 0
        self.fgrgb = [255, 0, 0]
        self.bgrgb = [255, 248, 222]
        self.textsize = 48  # deprecated
        self.shade = 0
        self.pendown = False
        self.xcor = 0
        self.ycor = 0
        self.heading = 0
        self.pensize = 5
        self.color = 0
        self.gray = 100
        self.fill = False
        self.poly_points = []

    def setup_svg_surface(self):
        ''' Set up a surface for saving to SVG '''
        if self.tw.running_sugar:
            svg_surface = cairo.SVGSurface(
                os.path.join(get_path(self.tw.activity, 'instance'),
                             'output.svg'), self.width, self.height)
        else:
            svg_surface = cairo.SVGSurface(
                os.path.join(os.getcwd(), 'output.svg'), self.width,
                self.height)
        self.cr_svg = cairo.Context(svg_surface)
        self.cr_svg.set_line_cap(1)  # Set the line cap to be round

    def start_fill(self):
        ''' Start accumulating points of a polygon to fill. '''
        self.fill = True
        self.poly_points = []

    def stop_fill(self):
        ''' Fill the polygon. '''
        self.fill = False
        if len(self.poly_points) == 0:
            return
        self.fill_polygon(self.poly_points)
        if self.tw.sharing():
            shared_poly_points = []
            for p in self.poly_points:
                shared_poly_points.append(
                    (self.screen_to_turtle_coordinates(p[0], p[1])))
                event = 'F|%s' % (data_to_string(
                    [self._get_my_nick(), shared_poly_points]))
            self.tw.send_event(event)
        self.poly_points = []

    def fill_polygon(self, poly_points):
        ''' Draw the polygon... '''
        def _fill_polygon(cr, poly_points):
            cr.new_path()
            for i, p in enumerate(poly_points):
                if p[0] == 'move':
                    cr.move_to(p[1], p[2])
                elif p[0] == 'rarc':
                    cr.arc(p[1], p[2], p[3], p[4], p[5])
                elif p[0] == 'larc':
                    cr.arc_negative(p[1], p[2], p[3], p[4], p[5])
                else:  # line
                    cr.line_to(p[1], p[2])
            cr.close_path()
            cr.fill()

        _fill_polygon(self.canvas, poly_points)
        self.inval()
        if self.cr_svg is not None:
            _fill_polygon(self.cr_svg, poly_points)

    def clearscreen(self, share=True):
        '''Clear the canvas and reset most graphics attributes to defaults.'''
        def _clearscreen(cr):
            cr.move_to(0, 0)
            self.bgrgb = [255, 248, 222]
            cr.set_source_rgb(self.bgrgb[0] / 255., self.bgrgb[1] / 255.,
                              self.bgrgb[2] / 255.)
            cr.rectangle(0, 0, self.width * 2, self.height * 2)
            cr.fill()

        _clearscreen(self.canvas)
        self.inval()
        if self.cr_svg is not None:
            _clearscreen(self.cr_svg)

        self.setpensize(5, share)
        self.setgray(100, share)
        self.setcolor(0, share)
        self.setshade(50, share)
        self.fill = False
        self.poly_points = []
        for turtle_key in iter(self.tw.turtles.dict):
            # Don't reset remote turtles
            if not self.tw.remote_turtle(turtle_key):
                self.set_turtle(turtle_key)
                self.tw.active_turtle.set_color(0)
                self.tw.active_turtle.set_shade(50)
                self.tw.active_turtle.set_gray(100)
                self.tw.active_turtle.set_pen_size(5)
                self.tw.active_turtle.reset_shapes()
                self.seth(0, share)
                self.setpen(False, share)
                self.setxy(0, 0, share)
                self.setpen(True, share)
                self.tw.active_turtle.hide()
        self.set_turtle(self.tw.default_turtle_name)

    def forward(self, n, share=True):
        ''' Move the turtle forward.'''
        nn = n * self.tw.coord_scale
        self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255.,
                                   self.fgrgb[2] / 255.)
        if self.cr_svg is not None:
            debug_output('in forward', True)
            self.cr_svg.set_source_rgb(self.fgrgb[0] / 255.,
                                       self.fgrgb[1] / 255.,
                                       self.fgrgb[2] / 255.)
        oldx, oldy = self.xcor, self.ycor
        try:
            self.xcor += nn * sin(self.heading * DEGTOR)
            self.ycor += nn * cos(self.heading * DEGTOR)
        except TypeError, ValueError:
            debug_output('bad value sent to %s' % (__name__),
                         self.tw.running_sugar)
            return
        if self.pendown:
            self.draw_line(oldx, oldy, self.xcor, self.ycor)

        self.move_turtle()

        if self.tw.sharing() and share:
            event = 'f|%s' % (data_to_string([self._get_my_nick(), int(n)]))
            self.tw.send_event(event)
        self.inval()
Example #51
0
def save_html(self, tw, embed_flag=True):
    """ Either save the canvas and code or pictures to HTML """

    if embed_flag:
        HTML_GLUE['img'] = ('<img width="400" height="300" alt=' + \
                                 '"Image" src="data:image/png;base64,\n',
                                 '"/>\n')
        HTML_GLUE['img2'] = ('<img alt="Image" src="data:image/png;' + \
                                  'base64,\n', '"/>\n')
    """
    If there are saved_pictures, put them into a .html; otherwise,
    save a screendump and the turtle project code.
    """
    htmlcode = ''
    if len(tw.saved_pictures) > 0:
        # saved_picture list is a collection of tuples of either the
        # image_file or the containing dsobject and an SVG flag
        for i, (image, svg_flag) in enumerate(tw.saved_pictures):
            htmlcode += HTML_GLUE['slide'][0] + str(i)
            htmlcode += HTML_GLUE['slide'][1] + \
                    HTML_GLUE['div'][0] + \
                    HTML_GLUE['h1'][0]
            if tw.running_sugar:
                from sugar.datastore import datastore
                dobject = datastore.get(image)  # dsobject.object_id
                image_file = dobject.file_path
            else:
                image_file = image
            if embed_flag:
                f = open(image_file, 'r')
                imgdata = f.read()
                f.close()
                if svg_flag:
                    tmp = imgdata
                else:
                    imgdata = image_to_base64(
                        image_file, get_path(tw.activity, 'instance'))
                    tmp = HTML_GLUE['img2'][0]
                    tmp += imgdata
                    tmp += HTML_GLUE['img2'][1]
            else:
                if svg_flag:
                    f = open(image_file, 'r')
                    imgdata = f.read()
                    f.close()
                    tmp = imgdata
                else:
                    tmp = HTML_GLUE['img3'][0]
                    tmp += image_file
                    tmp += HTML_GLUE['img3'][1]
            htmlcode += tmp + \
                    HTML_GLUE['h1'][1] + \
                    HTML_GLUE['div'][1]
    else:
        if embed_flag:
            tmp_file = os.path.join(get_path(tw.activity, 'instance'),
                                    'tmpfile.png')
            save_picture(self.tw.canvas, tmp_file)
            imgdata = image_to_base64(tmp_file,
                                      get_path(tw.activity, 'instance'))
        else:
            imgdata = os.path.join(self.tw.load_save_folder, 'image')
            self.tw.save_as_image(imgdata)
        htmlcode += (HTML_GLUE['img'][0] + imgdata + \
                 HTML_GLUE['img'][1])
        htmlcode += HTML_GLUE['div'][0]
        htmlcode += escape(
            data_to_string(tw.assemble_data_to_save(False, True)))
        htmlcode += HTML_GLUE['div'][1]

    if tw.running_sugar:
        title = _('Turtle Art') + ' ' + tw.activity.metadata['title']
    else:
        title = _('Turtle Art')

    header = HTML_GLUE['doctype'] + \
            HTML_GLUE['html'][0]
    style = HTML_GLUE['style'][0] + \
            HTML_GLUE['style'][1]
    if len(tw.saved_pictures) > 0:
        if tw.saved_pictures[0][1]:
            header = HTML_GLUE['html_svg'][0]
            style = COMMENT

    return header + \
           HTML_GLUE['head'][0] + \
           HTML_GLUE['meta'] + \
           HTML_GLUE['title'][0] + \
           title + \
           HTML_GLUE['title'][1] + \
           style + \
           HTML_GLUE['head'][1] + \
           HTML_GLUE['body'][0] + \
           htmlcode + \
           HTML_GLUE['body'][1] + \
           HTML_GLUE['html'][1]
Example #52
0
            self.tw.send_event(event)
        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(