From 09812f9d4e7c60923920ae7ea3746bd7cbb4b195 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 05 Nov 2011 19:11:54 +0000 Subject: myriad of small patches to get non-interactive mode working again --- diff --git a/TurtleArt/sprites.py b/TurtleArt/sprites.py index 6f5695a..a67046f 100644 --- a/TurtleArt/sprites.py +++ b/TurtleArt/sprites.py @@ -173,6 +173,7 @@ class Sprite: self.layer = 100 self.labels = [] self.images = [] + self.surfaces = [] self._dx = [] # image offsets self._dy = [] self.type = None @@ -183,6 +184,7 @@ class Sprite: ''' Add an image to the sprite. ''' while len(self.images) < i + 1: self.images.append(None) + self.surfaces.append(None) self._dx.append(0) self._dy.append(0) self.images[i] = image @@ -323,18 +325,25 @@ class Sprite: if cr is None: print 'sprite.draw: no Cairo context.' return + # Fix me: Cache cairo surfaces for i, img in enumerate(self.images): - if isinstance(img, gtk.gdk.Pixbuf): + if self.surfaces[i] is not None: + cr.set_source_surface(self.surfaces[i], + self.rect.x + self._dx[i], + self.rect.y + self._dy[i]) + elif isinstance(img, gtk.gdk.Pixbuf): cr.set_source_pixbuf(img, self.rect.x + self._dx[i], self.rect.y + self._dy[i]) - cr.rectangle(self.rect.x + self._dx[i], - self.rect.y + self._dy[i], - self.rect.width, - self.rect.height) - cr.fill() + # self.surfaces[i] = cr.get_target() else: print 'sprite.draw: source not a pixbuf (%s)' % (type(img)) + cr.rectangle(self.rect.x + self._dx[i], + self.rect.y + self._dy[i], + self.rect.width, + self.rect.height) + cr.fill() + if len(self.labels) > 0: self.draw_label(cr) diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index f2d0f62..03ddc05 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -381,8 +381,9 @@ class Block: self.width = copy_block.width self.height = copy_block.height self.shapes[0] = copy_block.shapes[0] - self.spr = sprites.Sprite(sprite_list, x, y, self.shapes[0]) - self.spr._margins = copy_block.spr._margins[:] + if sprite_list is not None: + self.spr = sprites.Sprite(sprite_list, x, y, self.shapes[0]) + self.spr._margins = copy_block.spr._margins[:] if len(copy_block.shapes) > 1: self.shapes[1] = copy_block.shapes[1] self.docks = copy_block.docks[:] @@ -414,15 +415,19 @@ class Block: for i, n in enumerate(block_names[self.name]): self._set_labels(i, n) - if copy_block is None: + if copy_block is None and self.spr is not None: if self.spr.label_width() > self.spr.label_safe_width(): self.resize() def _set_margins(self): + if self.spr is None: + return self.spr.set_margins(self.svg.margins[0], self.svg.margins[1], self.svg.margins[2], self.svg.margins[3]) def _set_label_attributes(self): + if self.spr is None: + return if self.name in content_blocks: n = len(self.values) if n == 0: @@ -451,6 +456,8 @@ class Block: True, 'center', 'middle', i) def _set_labels(self, i, label): + if self.spr is None: + return self.spr.set_label(label, i) def _make_block(self, svg): diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index c025d5a..5800f8f 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -237,10 +237,7 @@ class TurtleGraphics: if self.tw.sharing() and share: event = 'f|%s' % (data_to_string([self._get_my_nick(), int(n)])) self.tw.send_event(event) - self.tw.window.queue_draw_area(0, - 0, - self.width, - self.height) + self.inval() def seth(self, n, share=True): ''' Set the turtle heading. ''' @@ -531,28 +528,23 @@ class TurtleGraphics: def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' - # Fix me: rotate image - - r = sqrt(x * x + y * y) - if x != 0: - angle = atan(y / x) # initial angle relative to the origin - else: - angle = 0. - angle += self.heading * DEGTOR # add in heading - nx = cos(angle) * r - ny = sin(angle) * r - - debug_output('x,y: %f,%f r: %f, a: %f, nx,y: %f,%f' % (x, y, r, angle, - nx, ny), True) + ### These really only need to be calculated once + ox, oy = self.turtle_to_screen_coordinates(-w / 2., h / 2.) + r = sqrt(self.width * self.width + self.height * self.height) / 2 + a = atan(self.width / float(self.height)) + cx = ox - cos(a) * r + cy = oy - sin(a) * r + ### # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cr = gtk.gdk.CairoContext(self.canvas) cr.save() - cr.translate(-x, -y) # move to origin - cr.rotate(self.heading * DEGTOR) # rotate - cr.translate(x, y) # move back + cr.rotate(self.heading * DEGTOR) + # Fix me: offset of rotated image + nx = cx + cos(a - self.heading * DEGTOR) * r + ny = cy + sin(a - self.heading * DEGTOR) * r + cr.translate(nx-x, ny-y) cr.set_source_pixbuf(pixbuf, x, y) - # To do: reposition rectangle based on angle of rotation cr.rectangle(x, y, w, h) cr.fill() cr.restore() @@ -660,9 +652,12 @@ class TurtleGraphics: def move_turtle(self): ''' Move the turtle ''' 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.))) + if self.tw.interactive_mode: + 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.))) + else: + self.tw.active_turtle.move((int(self.cx + x), int(self.cy + y))) def get_color_index(self, r, g, b, a=0): ''' Find the closest palette entry to the rgb triplet ''' @@ -737,8 +732,9 @@ class TurtleGraphics: self.tw.active_turtle.show() tx, ty = self.tw.active_turtle.get_xy() 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. + if self.tw.interactive_mode: + 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/tawindow.py b/TurtleArt/tawindow.py index 2a0a80d..e4a6435 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -97,23 +97,11 @@ class TurtleArtWindow(): self.running_sugar = True else: self.running_sugar = False - self.area = self.window.window - if self.area is not None: - self.gc = self.area.new_gc() - else: - # We lose... - debug_output('drawable area is None... punting', - self.running_sugar) - exit() self._setup_events() - elif type(canvas_window) == gtk.gdk.Pixmap: + else: self.interactive_mode = False self.window = canvas_window self.running_sugar = False - if self.window is not None: - self.gc = self.window.new_gc() - else: - debug_output("bad win type %s" % (type(canvas_window)), False) if self.running_sugar: from sugar import profile @@ -219,7 +207,8 @@ class TurtleArtWindow(): self.sprite_list = None self.canvas = TurtleGraphics(self, self.width, self.height) - self.sprite_list.set_cairo_context(self.canvas.canvas) + if self.interactive_mode: + self.sprite_list.set_cairo_context(self.canvas.canvas) self.turtles = Turtles(self.sprite_list) if self.nick is None: @@ -579,7 +568,8 @@ class TurtleArtWindow(): def inval_all(self): """ Force a refresh """ - self.window.queue_draw_area(0, 0, self.width, self.height) + if self.interactive_mode: + self.window.queue_draw_area(0, 0, self.width, self.height) def hideshow_palette(self, state): """ Hide or show palette """ @@ -2819,7 +2809,7 @@ class TurtleArtWindow(): blk.spr.set_layer(BLOCK_LAYER) if check_dock: blk.connections = 'check' - if blk.spr.labels[0] is not None and \ + if self.running_sugar and blk.spr.labels[0] is not None and \ blk.name not in ['', ' ', 'number', 'string']: if blk.spr.labels[0] not in self.used_block_list: self.used_block_list.append(blk.spr.labels[0]) diff --git a/turtleart.py b/turtleart.py index ab5e7b7..9afc238 100755 --- a/turtleart.py +++ b/turtleart.py @@ -84,11 +84,13 @@ class TurtleMain(): self._plugins = [] if self.output_png: + # Fix me: We need to create a cairo surface to work with pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, gtk.gdk.screen_width(), gtk.gdk.screen_height()) - self.canvas, mask = pixbuf.render_pixmap_and_mask() - self._build_window() + # self.canvas, mask = pixbuf.render_pixmap_and_mask() + self.canvas = pixbuf + self._build_window(interactive=False) self._draw_and_quit() else: self._read_initial_pos() @@ -175,13 +177,19 @@ class TurtleMain(): self.tw.load_start(self.ta_file) self.tw.lc.trace = 0 self.tw.run_button(0) - self.tw.save_as_image(self.ta_file, self.canvas) + self.tw.save_as_image(self.ta_file) - def _build_window(self): + def _build_window(self, interactive=True): ''' Initialize the TurtleWindow instance. ''' - win = self.canvas.get_window() - cr = win.cairo_create() - surface = cr.get_target() + if interactive: + win = self.canvas.get_window() + cr = win.cairo_create() + surface = cr.get_target() + else: + img_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, + 1024, 768) + cr = cairo.Context(img_surface) + surface = cr.get_target() self.turtle_canvas = surface.create_similar( cairo.CONTENT_COLOR, gtk.gdk.screen_width() * 2, gtk.gdk.screen_height() * 2) -- cgit v0.9.1