From 87df7cb62ec5c1d99bcfb816f4187b960610f358 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 04 Mar 2010 14:02:38 +0000 Subject: clean up of save as Logo code --- (limited to 'taexportlogo.py') diff --git a/taexportlogo.py b/taexportlogo.py index e646234..43093fd 100644 --- a/taexportlogo.py +++ b/taexportlogo.py @@ -1,4 +1,4 @@ -#Copyright (c) 2008-9, Walter Bender +#Copyright (c) 2008-10, Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal @@ -18,6 +18,8 @@ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. +IGNORE = ["hideblocks", "showblocks", "fullscreen", "savesvg", "savepicture"] + import math try: from sugar.datastore import datastore @@ -25,6 +27,9 @@ except: pass def save_logo(self, tw): + """ + We need to set up the Turtle Art color palette and color processing. + """ color_processing = "\ to tasetpalette :i :r :g :b :myshade \r\ make \"s ((:myshade - 50) / 50) \r\ @@ -73,60 +78,74 @@ setpencolor :color + 8 \r\ end \r\ \ make \"colors [ \ -100 0 0 100 5 0 100 10 0 100 15 0 100 20 0 100 25 0 100 30 0 100 35 0 100 40 0 100 45 0 \ -100 50 0 100 55 0 100 60 0 100 65 0 100 70 0 100 75 0 100 80 0 100 85 0 100 90 0 100 95 0 \ -100 100 0 90 100 0 80 100 0 70 100 0 60 100 0 50 100 0 40 100 0 30 100 0 20 100 0 10 100 0 \ -0 100 0 0 100 5 0 100 10 0 100 15 0 100 20 0 100 25 0 100 30 0 100 35 0 100 40 0 100 45 \ -0 100 50 0 100 55 0 100 60 0 100 65 0 100 70 0 100 75 0 100 80 0 100 85 0 100 90 0 100 95 \ -0 100 100 0 95 100 0 90 100 0 85 100 0 80 100 0 75 100 0 70 100 0 65 100 0 60 100 0 55 100 \ -0 50 100 0 45 100 0 40 100 0 35 100 0 30 100 0 25 100 0 20 100 0 15 100 0 10 100 0 5 100 \ -0 0 100 5 0 100 10 0 100 15 0 100 20 0 100 25 0 100 30 0 100 35 0 100 40 0 100 45 0 100 \ -50 0 100 55 0 100 60 0 100 65 0 100 70 0 100 75 0 100 80 0 100 85 0 100 90 0 100 95 0 100 \ -100 0 100 100 0 90 100 0 80 100 0 70 100 0 60 100 0 50 100 0 40 100 0 30 100 0 20 100 0 10] \r\ +100 0 0 100 5 0 100 10 0 100 15 0 100 20 0 \ +100 25 0 100 30 0 100 35 0 100 40 0 100 45 0 \ +100 50 0 100 55 0 100 60 0 100 65 0 100 70 0 \ +100 75 0 100 80 0 100 85 0 100 90 0 100 95 0 \ +100 100 0 90 100 0 80 100 0 70 100 0 60 100 0 \ +50 100 0 40 100 0 30 100 0 20 100 0 10 100 0 \ +0 100 0 0 100 5 0 100 10 0 100 15 0 100 20 0 \ +100 25 0 100 30 0 100 35 0 100 40 0 100 45 \ +0 100 50 0 100 55 0 100 60 0 100 65 0 100 70 \ +0 100 75 0 100 80 0 100 85 0 100 90 0 100 95 \ +0 100 100 0 95 100 0 90 100 0 85 100 0 80 100 \ +0 75 100 0 70 100 0 65 100 0 60 100 0 55 100 \ +0 50 100 0 45 100 0 40 100 0 35 100 0 30 100 \ +0 25 100 0 20 100 0 15 100 0 10 100 0 5 100 \ +0 0 100 5 0 100 10 0 100 15 0 100 20 0 100 25 \ +0 100 30 0 100 35 0 100 40 0 100 45 0 100 \ +50 0 100 55 0 100 60 0 100 65 0 100 70 0 100 \ +75 0 100 80 0 100 85 0 100 90 0 100 95 0 100 \ +100 0 100 100 0 90 100 0 80 100 0 70 100 0 60 \ +100 0 50 100 0 40 100 0 30 100 0 20 100 0 10] \r\ make \"shade 50 \r\ tasetshade :shade \r" bs = tw.just_blocks() code = "" - # these flags are used to trigger the prepending of additional procedures - random = 0 - fillscreen = 0 - setcolor = 0 - setxy = 0 - pensize = 0 - tastack = 0 - arc = 0 - heap = 0 - write = 0 + stack_count = 0 show = 0 - minus = 0 - image = 0 + + # These flags are used to trigger the prepending of additional procedures. + random = False + fillscreen = False + setcolor = False + setxy = False + pensize = False + arc = False + heap = False + write = False + minus = False + image = False + + """ + Walk through the code, substituting UCB Logo for Turtle Art primitives. + """ for b in bs: this_stack = "" data = walk_stack(self, tw, b) - # need to catch several special cases: - # stacks, random, setshade, et al. - stack = 0 - namedstack = 0 - namedbox = 0 - refstack = 0 - refbox = 0 + # We need to catch several special cases: stacks, random, etc. + stack = False + namedstack = False + namedbox = False + refstack = False + refbox = False myvar = "" for d in data: if type(d) == type((1,2)): (d,b) = d if type(d) is float: - if namedbox == 1: + if namedbox: myvar += str(d) myvar += " " - elif write == 1: + elif write: this_stack += "labelsize " this_stack += str(d) - write = 0 + write = False else: this_stack += str(d) elif show == 2: - # use title for Journal objects + # Use title for Journal objects if d[0:8] == '#smedia_': try: dsobject = datastore.get(d[8:]) @@ -138,37 +157,37 @@ tasetshade :shade \r" this_stack += str(d) show = 0 else: - # transalate some TA terms into UCB Logo - if namedstack == 1: + # Translate some Turtle Art primitives into UCB Logo + if namedstack: this_stack += "to " this_stack += d[2:].replace(" ","_") this_stack += "\r" - stack = 1 - namedstack = 0 - elif namedbox == 1: + stack = True + namedstack = False + elif namedbox: if d[0:2] == "#s": this_stack += "make \"" this_stack += d[2:].replace(" ","_") this_stack += " " this_stack += myvar - namedbox = 0 + namedbox = False myvar = "" else: myvar += d - elif refstack == 1: + elif refstack: this_stack += d[2:].replace(" ","_") this_stack += " " - refstack = 0 - elif refbox == 1: + refstack = False + elif refbox: this_stack += ":" this_stack += d[2:].replace(" ","_") - refbox = 0 + refbox = False elif d == "stack": - refstack = 1 + refstack = True elif d == "box": - refbox = 1 - elif d == "storein": - namedbox = 1 + refbox = True + elif d == "storeinbox": + namedbox = True elif d == "storeinbox1": this_stack += "make \"box1" elif d == "box1": @@ -180,7 +199,7 @@ tasetshade :shade \r" elif d == "shade": this_stack += ":shade" elif d == "setshade": - setcolor = 1 + setcolor = True this_stack += "tasetshade" elif d == "color": this_stack += "pencolor" @@ -188,68 +207,69 @@ tasetshade :shade \r" this_stack += " " elif d == "start": this_stack += "to start\r" - stack = 1 + stack = True elif d == "nop1": this_stack += "to stack1\r" - stack = 1 + stack = True elif d == "nop2": this_stack += "to stack2\r" - stack = 1 + stack = True elif d == "nop3": - namedstack = 1 + namedstack = True elif d == "stopstack": this_stack += "stop" elif d == "clean": this_stack += "clearscreen" elif d == "setxy": - setxy = 1 + setxy = True this_stack += "tasetxy" elif d == "color": this_stack += ":color" elif d == "plus": this_stack += "sum" elif d == "setcolor": - setcolor = 1 + setcolor = True this_stack += "tasetpencolor" elif d == "fillscreen": - fillscreen = 1 + fillscreen = True + setcolor = True this_stack += "tasetbackground" elif d == "random": - random = 1 + random = True this_stack += "tarandom" elif d == "pensize": - pensize = 1 + pensize = True this_stack += "tapensize" elif d == "arc": - arc = 1 + arc = True this_stack += "taarc" elif d == "pop": - heap = 1 + heap = True this_stack += "tapop" elif d == "push": - heap = 1 + heap = True this_stack += "tapush" elif d == "heap": - heap = 1 + heap = True this_stack += "taprintheap" elif d == "emptyheap": - heap = 1 + heap = True this_stack += "taclearheap" elif d == "kbinput": this_stack += "make \"keyboard readchar" elif d == "keyboard": this_stack += ":keyboard" elif d == 'insertimage': - image = 1 - elif image == 1: - # skip this arg + image = True + elif image: + # Skip this arg image = 2 elif image == 2: - # skip this arg - image = 0 + # Skip this arg + image = False elif d[0:2] == "#s": # output single characters as a string - if len(d[2:]) == 1: + if len(d[2:]): this_stack += "\"" this_stack += d[2:] # make a sentence out of everything else @@ -259,55 +279,53 @@ tasetshade :shade \r" this_stack += "\r" elif d == "write": this_stack += "label" - write = 1 - elif d == 'show': + write = True + elif d == 'show' or d == 'showaligned': this_stack += "label" show = 1 - elif d == "container": - if show == 1: - show = 2 elif d == "minus2": this_stack == "taminus" - minus = 1 - elif d == "hideblocks": + minus = True + elif d in IGNORE: this_stack += " " elif show == 1 and d[0:2] == "#s": this_stack += d[2:] - # need to handle templates somehow + # We don't handle depreciated 'template' blocks else: this_stack += d this_stack += " " if stack: - stack = 0 + stack = False # if it is not a stack, we need to add a "to ta#" label elif len(data) > 0: - this_stack = "to ta" + str(tastack) + "\r" + this_stack - tastack += 1 + this_stack = "to ta" + str(stack_count) + "\r" + this_stack + stack_count += 1 if len(data) > 0: code += this_stack code += "\rend\r" - # need to define some procedures - if minus: # minus only takes on arg + + # We need to define some additional procedures. + if minus: # Logo minus only takes one argument. code = "to taminus :y :x\routput sum :x minus :y\rend\r" + code if random: # to avoid negative numbers code = "to tarandom :min :max\r" + \ "output (random (:max - :min)) + :min\rend\r" +\ code - if fillscreen: # set shade than background color + if fillscreen: # Set shade than background color code = "to tasetbackground :color :shade\r" + \ - "tasetshade :shade\rsetbackground :color\rend" + \ + "tasetshade :shade\rsetbackground :color\rend\r" + \ code - if setcolor: # load palette + if setcolor: # Load the Turtle Art color palette. code = color_processing + code - if pensize: # return only first argument + if pensize: # Return only the first argument. code = "to tapensize\routput first round pensize\rend\r" + code - if setxy: # swap args and round args + if setxy: # Swap and round arguments code = "to tasetxy :x :y\rpenup\rsetxy :x :y\rpendown\rend\r" + code - if arc: # need to redefine this one all together + if arc: # Turtle Art 'arc' needs to be redefined. c = (2 * math.pi)/360 code = "to taarc :a :r\rrepeat round :a [right 1 forward (" +\ str(c) + " * :r)]\rend\r" + code - if heap: # add psuedo push and pop + if heap: # Add psuedo 'push' and 'pop' code = "to tapush :foo\rmake \"taheap fput :foo :taheap\rend\r" + \ "to tapop\rif emptyp :taheap [stop]\rmake \"tmp first :taheap\r" +\ "make \"taheap butfirst :taheap\routput :tmp\rend\r" + \ @@ -315,17 +333,14 @@ tasetshade :shade \r" "to taprintheap \rprint :taheap\rend\r" + \ "make \"taheap []\r" + code code = "window\r" + code -# print code return code def walk_stack(self, tw, blk): top = tw.find_top_block(blk) if blk == top: - # only walk the stack if the block is the top block code = tw.lc.run_blocks(top, tw.block_list.list, False) return code else: - # not top of stack, then return empty list return [] -- cgit v0.9.1