From 81adaceebc67c605c1576c0b2070f2ec543ef4e7 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 10 Feb 2011 21:21:15 +0000 Subject: cleaned up fill_polygon sharing --- (limited to 'TurtleArt') diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index ae01438..0c5f454 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -44,6 +44,23 @@ def wrap100(n): return n +def calc_poly_bounds(poly_points): + """ Calculate the minx, miny, width, height of polygon """ + minx = poly_points[0][0] + miny = poly_points[0][1] + maxx, maxy = minx, miny + for p in poly_points: + if p[0] < minx: + minx = p[0] + elif p[0] > maxx: + maxx = p[0] + if p[1] < miny: + miny = p[1] + elif p[1] > maxy: + maxy = p[1] + return(minx, miny, maxx - minx, maxy - miny) + + def calc_shade(c, s, invert=False): """ Convert a color to the current shade (lightness/darkness). """ # Assumes 16 bit input values @@ -149,30 +166,18 @@ class TurtleGraphics: self.fill = False if len(self.poly_points) == 0: return - minx = self.poly_points[0][0] - miny = self.poly_points[0][1] - maxx = minx - maxy = miny + self.fill_polygon(self.poly_points) + shared_poly_points = [] for p in self.poly_points: - if p[0] < minx: - minx = p[0] - elif p[0] > maxx: - maxx = p[0] - if p[1] < miny: - miny = p[1] - elif p[1] > maxy: - maxy = p[1] - w = maxx - minx - h = maxy - miny - self.fill_polygon(self.poly_points, minx, miny, w, h) + shared_poly_points.append((self.screen_to_turtle_coordinates( + p[0], p[1]))) event = "F|%s" % (data_to_string([self._get_my_nick(), - round_int(minx), round_int(miny), - round_int(w), round_int(h), - self.poly_points])) + shared_poly_points])) self._send_event(event, True) self.poly_points = [] - def fill_polygon(self, poly_points, minx, miny, w, h): + def fill_polygon(self, poly_points): + minx, miny, w, h = calc_poly_bounds(poly_points) self.canvas.images[0].draw_polygon(self.gc, True, poly_points) self.invalt(minx - self.pensize * self.tw.coord_scale / 2 - 3, miny - self.pensize * self.tw.coord_scale / 2 - 3, @@ -224,9 +229,9 @@ class TurtleGraphics: self.move_turtle() if self.tw.saving_svg and self.pendown: self.tw.svg_string += self.svg.new_path(oldx, - self.height / 2 - oldy) + self.invert_y_coordinate(oldy)) self.tw.svg_string += self.svg.line_to(self.xcor, - self.height / 2 - self.ycor) + self.invert_y_coordinate(self.ycor)) self.tw.svg_string += "\"\n" self.tw.svg_string += self.svg.style() event = "f|%s" % (data_to_string([self._get_my_nick(), int(n)])) @@ -286,8 +291,7 @@ class TurtleGraphics: oldx, oldy = self.xcor, self.ycor cx = self.xcor + r * cos(self.heading * DEGTOR) cy = self.ycor - r * sin(self.heading * DEGTOR) - x = self.width / 2 + int(cx - r) - y = self.height / 2 - int(cy + r) + x, y = self.turtle_to_screen_coordinates(int(cx - r), int(cy + r)) w = int(2 * r) h = w if self.pendown: @@ -302,9 +306,10 @@ class TurtleGraphics: self.ycor = cy + r * sin(self.heading * DEGTOR) if self.tw.saving_svg and self.pendown: self.tw.svg_string += self.svg.new_path(oldx, - self.height / 2 - oldy) + self.invert_y_coordinate(oldx)) self.tw.svg_string += self.svg.arc_to(self.xcor, - self.height / 2 - self.ycor, r, a, 0, s) + self.invert_y_coordinate(self.ycor), + r, a, 0, s) self.tw.svg_string += "\"\n" self.tw.svg_string += self.svg.style() @@ -319,8 +324,7 @@ class TurtleGraphics: oldx, oldy = self.xcor, self.ycor cx = self.xcor - r * cos(self.heading * DEGTOR) cy = self.ycor + r * sin(self.heading * DEGTOR) - x = self.width / 2 + int(cx - r) - y = self.height / 2 - int(cy + r) + x, y = self.turtle_to_screen_coordinates(int(cx - r), int(cy + r)) w = int(2 * r) h = w if self.pendown: @@ -336,9 +340,9 @@ class TurtleGraphics: self.ycor = cy - r * sin(self.heading * DEGTOR) if self.tw.saving_svg and self.pendown: self.tw.svg_string += self.svg.new_path(oldx, - self.height / 2 - oldy) + self.invert_y_coordinate(oldy)) self.tw.svg_string += self.svg.arc_to(self.xcor, - self.height / 2 - self.ycor, + self.invert_y_coordinate(self.ycor), r, a, 0, s) self.tw.svg_string += "\"\n" self.tw.svg_string += self.svg.style() @@ -512,20 +516,20 @@ class TurtleGraphics: # Outside of Sugar, we save a path self.tw.svg_string += self.svg.image(x - self.width / 2, y, w, h, path) - if self.tw.sharing(): - if self.tw.running_sugar: - tmp_path = get_path(self.tw.activity, 'instance') - else: - tmp_path = '/tmp' - data = image_to_base64(pixbuf, tmp_path) - height = pixbuf.get_height() - width = pixbuf.get_width() - event = "P|%s" % (data_to_string([self._get_my_nick(), + if self.tw.running_sugar: + tmp_path = get_path(self.tw.activity, 'instance') + else: + tmp_path = '/tmp' + data = image_to_base64(pixbuf, tmp_path) + print "sharing pixbuf from %s" % (tmp_path) + height = pixbuf.get_height() + width = pixbuf.get_width() + 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._send_event(event, share) + self._send_event(event, share) def draw_text(self, label, x, y, size, w, share=True): """ Draw text """ @@ -567,26 +571,36 @@ class TurtleGraphics: round_int(size), round_int(w)]])) self._send_event(event, share) + def turtle_to_screen_coordinates(self, x, y): + return self.width / 2 + x, self.invert_y_coordinate(y) + + def screen_to_turtle_coordinates(self, x, y): + return x - self.width / 2, self.invert_y_coordinate(y) + + def invert_y_coordinate(self, y): + return self.height / 2 - y + def draw_line(self, x1, y1, x2, y2): """ Draw a line """ - x1, y1 = self.width / 2 + int(x1), self.height / 2 - int(y1) - x2, y2 = self.width / 2 + int(x2), self.height / 2 - int(y2) + x1, y1 = self.turtle_to_screen_coordinates(x1, y1) + x2, y2 = self.turtle_to_screen_coordinates(x2, y2) if x1 < x2: - minx, maxx = x1, x2 + minx, maxx = int(x1), int(x2) else: - minx, maxx = x2, x1 + minx, maxx = int(x2), int(x1) if y1 < y2: - miny, maxy = y1, y2 + miny, maxy = int(y1), int(y2) else: - miny, maxy = y2, y1 + miny, maxy = int(y2), int(y1) w, h = maxx - minx, maxy - miny - self.canvas.images[0].draw_line(self.gc, x1, y1, x2, y2) + self.canvas.images[0].draw_line(self.gc, int(x1), int(y1), int(x2), + int(y2)) if self.fill and self.poly_points == []: - self.poly_points.append((x1, y1)) + self.poly_points.append((int(x1), int(y1))) if self.fill: - self.poly_points.append((x2, y2)) - self.invalt(minx - self.pensize * self.tw.coord_scale / 2 - 3, - miny - self.pensize * self.tw.coord_scale / 2 - 3, + self.poly_points.append((int(x2), int(y2))) + self.invalt(minx - int(self.pensize * self.tw.coord_scale / 2) - 3, + miny - int(self.pensize * self.tw.coord_scale / 2) - 3, w + self.pensize * self.tw.coord_scale + 6, h + self.pensize * self.tw.coord_scale + 6) @@ -596,8 +610,7 @@ class TurtleGraphics: def move_turtle(self): """ Move the turtle """ - x, y = self.width / 2 + int(self.xcor), \ - self.height / 2 - int(self.ycor) + x, y = self.turtle_to_screen_coordinates(self.xcor, self.ycor) self.tw.active_turtle.move( (int(self.cx + x - self.tw.active_turtle.spr.rect.width / 2), int(self.cy + y - self.tw.active_turtle.spr.rect.height / 2))) @@ -645,9 +658,8 @@ class TurtleGraphics: def get_pixel(self): """ Read the pixel at x, y """ if self.tw.interactive_mode: - return self.canvas.get_pixel( - (self.width / 2 + int(self.xcor), - self.height / 2 - int(self.ycor)), 0, self.tw.color_mode) + x, y = self.turtle_to_screen_coordinates(self.xcor, self.ycor) + return self.canvas.get_pixel(x, y, 0, self.tw.color_mode) else: return(-1, -1, -1, -1) @@ -662,10 +674,9 @@ class TurtleGraphics: self.tw.active_turtle = self.tw.turtles.get_turtle(k, False) self.tw.active_turtle.show() tx, ty = self.tw.active_turtle.get_xy() - self.xcor = -self.width / 2 + tx + \ - self.tw.active_turtle.spr.rect.width / 2 - self.ycor = self.height / 2 - ty - \ - self.tw.active_turtle.spr.rect.height / 2 + self.xcor, self.ycor = self.screen_to_turtle_coordinates(tx, ty) + self.xcor += self.tw.active_turtle.spr.rect.width / 2 + self.ycor -= self.tw.active_turtle.spr.rect.height / 2 self.heading = self.tw.active_turtle.get_heading() self.setcolor(self.tw.active_turtle.get_color(), False) self.setgray(self.tw.active_turtle.get_gray(), False) diff --git a/TurtleArt/tacollaboration.py b/TurtleArt/tacollaboration.py index e2e7662..ed29675 100644 --- a/TurtleArt/tacollaboration.py +++ b/TurtleArt/tacollaboration.py @@ -186,6 +186,8 @@ class Collaboration(): save_active_turtle = self._tw.active_turtle command, payload = event_message.split("|", 2) + print "event message %s" % command + _logger.debug("event message (%s)" % command) self._processing_methods[command](payload) # Restore active Turtle @@ -226,6 +228,8 @@ class Collaboration(): def _draw_pixbuf(self, payload): if len(payload) > 0: + print "trying to draw a shared pixbuf" + _logger.debug("(trying to draw a shared pixbuf)") [nick, [a, b, x, y, w, h, width, height, data]] =\ data_from_string(payload) if nick != self._tw.nick: @@ -233,7 +237,9 @@ class Collaboration(): tmp_path = get_path(self.tw.activity, 'instance') else: tmp_path = '/tmp' + print "creating %s" % (tmp_path) file_name = base64_to_image(data, tmp_path) + print "opening %s" % (file_path) pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(file_name, width, height) self._tw.canvas.draw_pixbuf(pixbuf, a, b, x, y, w, h, @@ -311,8 +317,13 @@ class Collaboration(): def _fill_polygon(self, payload): # Check to make sure that the poly_point array is passed properly if len(payload) > 0: - [nick, minx, miny, w, h, poly_points] = data_from_string(payload) - self._tw.canvas.fill_polygon(poly_points, minx, miny, w, h) + [nick, poly_points] = data_from_string(payload) + shared_poly_points = [] + for i in range(len(poly_points)): + shared_poly_points.append(( + self._tw.canvas.turtle_to_screen_coordinates( + poly_points[i][0], poly_points[i][1]))) + self._tw.canvas.fill_polygon(shared_poly_points) def _get_dictionary(self): d = {self._get_nick(): self._get_colors()} @@ -330,7 +341,6 @@ class Collaboration(): colors = self._activity.get_colors() if colors is None: colors = '#008000,#00A000' - _logger.debug(colors) return colors -- cgit v0.9.1