From d175bf2d8e11135173b64bf2b7756b95cd079fc2 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 20 Sep 2010 23:34:07 +0000 Subject: added reskin turtle block --- diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 1997d1b..e51c4d0 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -135,7 +135,7 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right', ['kbinput', 'push', 'printheap', 'keyboard', 'pop', 'clearheap', 'myfunc1arg', 'userdefined', 'addturtle', 'comment', 'print', 'cartesian', 'width', 'height', 'polar', 'sandwichtop_no_label', - 'sandwichbottom', 'readpixel', 'see'], + 'sandwichbottom', 'readpixel', 'see', 'skin'], ['journal', 'audio', 'description', 'hideblocks', 'showblocks', 'fullscreen', 'savepix', 'savesvg', 'picturelist', 'picture1x1a', 'picture1x1', 'picture2x2', 'picture2x1', @@ -196,7 +196,7 @@ BASIC_STYLE_1ARG = ['forward', 'back', 'left', 'right', 'seth', 'show', 'image', 'setscale', 'setpensize', 'setcolor', 'setshade', 'print', 'showaligned', 'settextsize', 'settextcolor', 'print', 'wait', 'storeinbox1', 'savepix', 'storeinbox2', 'wait', 'stack', 'push', 'nop', 'addturtle', 'comment', - 'savesvg', 'setgray'] + 'savesvg', 'setgray', 'skin'] BASIC_STYLE_VAR_ARG = ['userdefined', 'userdefined2args', 'userdefined3args'] BULLET_STYLE = ['templatelist', 'list'] BASIC_STYLE_2ARG = ['arc', 'setxy', 'fillscreen', 'storein', 'write'] @@ -383,6 +383,7 @@ BLOCK_NAMES = { 'show':[_('show')], 'showblocks':[_('show blocks')], 'showaligned':[_('show aligned')], + 'skin':[_('turtle shell')], 'sqrt':['√'], 'stack':[_('action')], 'stack1':[_('action 1')], @@ -526,6 +527,7 @@ PRIMITIVES = { 'show':'show', 'showblocks':'showblocks', 'showaligned':'showaligned', + 'skin':'skin', 'sqrt':'sqrt', 'stack':'stack', 'stack1':'stack1', @@ -609,6 +611,7 @@ DEFAULTS = { 'setxy':[0, 0], 'show':[_('text')], 'showaligned':[_('text')], + 'skin':[None], 'stack':[_('action')], 'storeinbox1':[100], 'storeinbox2':[100], @@ -638,7 +641,7 @@ STRING_OR_NUMBER_ARGS = ['plus2', 'equal2', 'less2', 'greater2', 'box', 'userdefined2args', 'userdefined3args', 'storein'] CONTENT_ARGS = ['show', 'showaligned', 'push', 'storein', 'storeinbox1', - 'storeinbox2'] + 'storeinbox2', 'skin'] # # Status blocks @@ -838,6 +841,7 @@ HELP_STRINGS = { 'shade':_("holds current pen shade"), 'show':_("draws text or show media from the Journal"), 'showblocks':_("restores hidden blocks"), + 'skin':_("put a shell on the turtle"), 'sqrt':_("calculates square root"), 'stack1':_("invokes Action 1 stack"), 'stack2':_("invokes Action 2 stack"), diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index d485d22..a9b13dc 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -348,6 +348,7 @@ class LogoCode: 'show':[1, lambda self, x: self.show(x, True)], 'showaligned':[1,lambda self, x: self.show(x, False)], 'showblocks':[0, lambda self: self.tw.showblocks()], + 'skin':[1, lambda self, x: self.reskin(x)], 'sound':[1, lambda self, x: self.play_sound(x)], 'sqrt':[1, lambda self, x: tasqrt(x)], 'stack1':[0, self.prim_stack1, True], @@ -1071,6 +1072,14 @@ class LogoCode: """ Set scale used by media object display """ self.scale = x + def reskin(self, media): + """ Reskin the turtle with an image from a file """ + w = int(self.scale) + h = int(self.scale) + pixbuf = self.show_picture(media, 0, 0, w, h, False) + if pixbuf is not None: + self.tw.active_turtle.set_shapes([pixbuf]) + def show(self, string, center=False): """ Show is the general-purpose media-rendering block. """ # convert from Turtle coordinates to screen coordinates @@ -1138,7 +1147,7 @@ class LogoCode: else: play_audio(self, audio[6:]) - def show_picture(self, media, x, y, w, h): + def show_picture(self, media, x, y, w, h, show=True): """ Image file from Journal """ if media == "" or media[6:] == "": pass @@ -1179,10 +1188,12 @@ class LogoCode: self.filepath = None self.tw.showlabel('nofile', media[6:]) print "Couldn't open media object %s" % (media[6:]) - if pixbuf is not None: + if pixbuf is not None and show: self.tw.canvas.draw_pixbuf(pixbuf, 0, 0, int(x), int(y), int(w), int(h), self.filepath) + else: + return pixbuf def show_description(self, media, x, y, w, h): """ Description field from Journal """ diff --git a/TurtleArt/taturtle.py b/TurtleArt/taturtle.py index 5272cd6..91e7dfc 100644 --- a/TurtleArt/taturtle.py +++ b/TurtleArt/taturtle.py @@ -24,12 +24,19 @@ from tasprite_factory import SVG, svg_str_to_pixbuf from tacanvas import wrap100, color_table from sprites import Sprite +import logging +_logger = logging.getLogger('turtleart-activity') + + +SHAPES = 36 + + def generate_turtle_pixbufs(colors): """ Generate pixbufs for generic turtles """ shapes = [] svg = SVG() svg.set_scale(1.0) - for i in range(36): + for i in range(SHAPES): svg.set_orientation(i*10) shapes.append(svg_str_to_pixbuf(svg.turtle(colors))) return shapes @@ -143,10 +150,25 @@ class Turtle: self.spr = None turtles.add_to_dict(key, self) + def set_shapes(self, shapes): + """ Reskin the turtle """ + n = len(shapes) + if n == SHAPES: + self.shapes = shapes[:] + else: + if n != 1: + _logger.debug("%d images passed to set_shapes: ignoring" % (n)) + images = [shapes[0]] + for i in range(3): + images.append(images[i].rotate_simple(90)) + for i in range(SHAPES): + j = (i + 4) % SHAPES + self.shapes[j] = images[int(j/9) % 4] + def set_heading(self, heading): - """ Set the turtle heading (and shape: one per 10 degrees) """ + """ Set the turtle heading (one shape per 360/SHAPES degrees) """ self.heading = heading - i = (int(self.heading+5)%360)/10 + i = (int(self.heading+5)%360)/(360 / SHAPES) if not self.hidden and self.spr is not None: try: self.spr.set_shape(self.shapes[i]) -- cgit v0.9.1