diff options
-rw-r--r-- | block.py | 27 | ||||
-rw-r--r-- | constants.py | 9 | ||||
-rw-r--r-- | sprites.py | 4 | ||||
-rw-r--r-- | talogo.py | 20 | ||||
-rw-r--r-- | taproject.py | 174 | ||||
-rw-r--r-- | tawindow.py | 54 |
6 files changed, 159 insertions, 129 deletions
@@ -47,10 +47,10 @@ class Blocks: if block in self.list: self.list.remove(block) - def print_list(self): + def print_list(self, block_type=None): for i, block in enumerate(self.list): - print "%d: %s" % (i, block.name) - + if block_type is None or block_type == block.type: + print "%d: %s" % (i, block.name) # # sprite utilities @@ -65,6 +65,12 @@ class Blocks: # A class for the individual blocks # class Block: + # + # TODO: + # Logo code + # HTML code + # debug code + # etc. def __init__(self, block_list, sprite_list, name, x, y, type='block', labels=[], scale=2.0, colors=["#00FF00","#00A000"]): self.spr = None @@ -82,6 +88,9 @@ class Block: self._left = 0 self._right = 0 + if OLD_NAMES.has_key(self.name): + self.name = OLD_NAMES[self.name] + for i in range(len(self._font_size)): self._font_size[i] *= self.scale @@ -97,12 +106,6 @@ class Block: self.primitive = PRIMITIVES[self.name] block_list.append_to_list(self) - # - # TODO: - # Logo code - # HTML code - # debug code - # etc. # We need to resize some blocks on the fly. def resize(self): @@ -126,7 +129,7 @@ class Block: def _new_block_from_factory(self, sprite_list, labels, x, y): - print "new block: %s (%d %d)" % (self.name, x, y) + # print "new block: %s (%d %d)" % (self.name, x, y) self.svg = SVG() self.svg.set_scale(self.scale) @@ -144,12 +147,10 @@ class Block: # If labels were passed, use them; if len(labels) > 0: - print labels for i, l in enumerate(labels): self._set_labels(i, l) # otherwise use default values; elif BLOCK_NAMES.has_key(self.name): - print BLOCK_NAMES[self.name] for i, l in enumerate(BLOCK_NAMES[self.name]): self._set_labels(i, l) # and make sure the labels fit. @@ -206,7 +207,7 @@ class Block: self._make_flow_style_boolean(e, svg) else: self._make_basic_style(e, svg) - print "don't know how to create a %s block" % (self.name) + print ">>>>> I don't know how to create a %s block" % (self.name) def _set_colors(self, svg): for p in range(len(PALETTES)): diff --git a/constants.py b/constants.py index 1f6e114..2251885 100644 --- a/constants.py +++ b/constants.py @@ -144,6 +144,15 @@ BLOCK_NAMES = {'clean':[_('clean')], 'forward':[_('forward')], 'scale':[_('scale')]} # +# Legacy names +# +OLD_NAMES = {'setxy':'set xy', 'storeinbox1':'store in box 1', + 'setpensize':'set pen size', 'setshade':'set shade', + 'plus2':'plus', 'division2':'divide','box1':'box 1', + 'box2':'box 2', 'storeinbox2':'store in box 2', + 'division':'divide', 'setcolor':'set color'} + +# # Logo primitives # @@ -237,14 +237,14 @@ class Sprite: if self._horiz_align[i] == "center": x = int(self._x+self._margins[0]+(my_width-w)/2) elif self._horiz_align[i] == 'left': - x = self._x+self._margins[0] + x = int(self._x+self._margins[0]) else: # right x = int(self._x+self._width-w-self._margins[2]) h = pl.get_size()[1]/pango.SCALE if self._vert_align[i] == "middle": y = int(self._y+self._margins[1]+(my_height-h)/2) elif self._vert_align[i] == "top": - y = self._y+self._margins[1] + y = int(self._y+self._margins[1]) else: # bottom y = int(self._y+self._height-h-self._margins[3]) self._sprites.gc.set_foreground(self._color) @@ -63,21 +63,27 @@ class logoerror(Exception): return repr(self.value) def run_blocks(lc, blk, blocks, run_flag): + print "run blocks: %s" % (blk.name) + for b in blocks: + if b is None: + print " None" + else: + print " %s" % b.name # user-defined stacks for x in lc.stacks.keys(): - lc.stacks[x]= None + lc.stacks[x] = None # two built-in stacks lc.stacks['stack1'] = None lc.stacks['stack2'] = None for b in blocks: - if b.name=='def action 1': - lc.stacks['stack1']= readline(lc,blocks_to_code(lc, b)) + if b.name == 'def action 1': + lc.stacks['stack1'] = readline(lc,blocks_to_code(lc, b)) if b.name=='def action 2': - lc.stacks['stack2']= readline(lc,blocks_to_code(lc, b)) - if b.name=='def action': + lc.stacks['stack2'] = readline(lc,blocks_to_code(lc, b)) + if b.name == 'def action': if (b.connections[1] is not None): - text=b.connections[1].spr.labels[0] - lc.stacks['stack3'+text]= readline(lc,blocks_to_code(lc, b)) + text = b.connections[1].spr.labels[0] + lc.stacks['stack3'+text] = readline(lc,blocks_to_code(lc, b)) code = blocks_to_code(lc, blk) if run_flag is True: print "code: %s" % (code) diff --git a/taproject.py b/taproject.py index ac8d165..2242288 100644 --- a/taproject.py +++ b/taproject.py @@ -62,71 +62,71 @@ shape_dict = {'journal':'texton', \ def new_project(tw): stop_logo(tw) - for b in blocks(tw): b.hide() + for b in tw._just_blocks(): + b.spr.hide() tw.turtle.canvas.set_layer(CANVAS_LAYER) clearscreen(tw.turtle) tw.save_file_name = None def load_file(tw, create_new_project=True): fname = get_load_name(tw) - if fname==None: return - if fname[-3:]=='.ta': fname=fname[0:-3] + if fname==None: + return + if fname[-3:]=='.ta': + fname=fname[0:-3] load_files(tw,fname+'.ta', create_new_project) if create_new_project is True: tw.save_file_name = os.path.basename(fname) +# +# We try to maintain read-compatibility with all versions of Turtle Art. +# Try pickle first; then two different versions of json. +# def load_files(tw, ta_file, create_new_project=True): - # ignoring the png_file even if it is present + # Just open the .ta file, ignoring any .png file that might be present. f = open(ta_file, "r") try: - data = pickle.load(f) # old-style data format + data = pickle.load(f) except: - # print "reading saved json data" - f.seek(0) # rewind necessary because of pickle.load + # Rewind necessary because of failed pickle.load attempt + f.seek(0) text = f.read() - if _old_Sugar_system is True: - listdata = json.read(text) - else: - io = StringIO(text) - listdata = jload(io) - print "load files: %s" % (listdata) - # listdata = jdecode(text) - data = tuplify(listdata) # json converts tuples to lists + data = _json_read(text) f.close() if create_new_project is True: new_project(tw) - read_data(tw,data) + read_data(tw, data) -def get_load_name(tw): - dialog = gtk.FileChooserDialog("Load...", None, - gtk.FILE_CHOOSER_ACTION_OPEN, - (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN, gtk.RESPONSE_OK)) - dialog.set_default_response(gtk.RESPONSE_OK) - return do_dialog(tw,dialog) - -# unpack serialized data sent across a share -def load_string(tw,text): +def _json_read(text): if _old_Sugar_system is True: listdata = json.read(text) else: io = StringIO(text) listdata = jload(io) - data = tuplify(listdata) # json converts tuples to lists + print "load files: %s" % (listdata) + # json converts tuples to lists, so we need to convert back, + return tuplify(listdata) + +def get_load_name(tw): + dialog = gtk.FileChooserDialog("Load...", None, + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, gtk.RESPONSE_OK)) + dialog.set_default_response(gtk.RESPONSE_OK) + return do_dialog(tw, dialog) + +# Unpack serialized data sent across a share. +def load_string(tw, text): + data = _json_read(text) new_project(tw) - read_data(tw,data) + read_data(tw, data) -# unpack sserialized data from the clipboard -def clone_stack(tw,text): - if _old_Sugar_system is True: - listdata = json.read(text) - else: - io = StringIO(text) - listdata = jload(io) - data = tuplify(listdata) # json converts tuples to lists - # read_stack(tw,data) - read_data(tw,data) +# Unpack sserialized data from the clipboard. +def clone_stack(tw, text): + data = _json_read(text) + read_data(tw, data) -# paste stack from the clipboard +# Paste stack from the clipboard. # TODO: rebase on read data def read_stack(tw,data): clone = [] @@ -144,42 +144,67 @@ def tuplify(t): return t return tuple(map(tuplify, t)) -def read_data(tw,data): - sprs = [] +def read_data(tw, data): + print "data is %d elements long" % (len(data)) + # First we create the blocks + blocks = [] + t = 0 for b in data: - if b[1]=='turtle': - load_turtle(tw,b) - else: spr = load_spr(tw, b); sprs.append(spr) - for i in range(len(sprs)): + print b + if b[1] == 'turtle': + load_turtle(tw, b) + print "we founf a turtle" + t = 1 + else: + blk = load_block(tw, b); blocks.append(blk) + print "we have %d blocks and %d turtles" % (len(blocks), t) + # Then we make the connections + for i in range(len(blocks)): + print "connections for %s are:" % (blocks[i].name) cons=[] for c in data[i][4]: - if c==None: cons.append(None) - else: cons.append(sprs[c]) - sprs[i].connections = cons # phasing out - tw.block_list.spr_to_block(sprs[i]).connections = cons - -def load_spr(tw,b): + if c is None: + cons.append(None) + print " None" + else: + cons.append(blocks[c]) + print " %s" % (blocks[c].name) + blocks[i].connections = cons + # Then we need to adjust the x,y positions, as block sizes may have changed + for b in blocks: + (sx, sy) = b.spr.get_xy() + for i, c in enumerate(b.connections): + if c is not None: + bdock = b.docks[i] + for j in range(len(c.docks)): + if c.connections[j] == b: + cdock = c.docks[j] + nx, ny = sx+bdock[2]-cdock[2], sy+bdock[3]-cdock[3] + c.spr.move((nx, ny)) + +def load_block(tw, b): + # A blook is saved as: (i, (btype, label), x, y, (c0,... cn)) media = None - btype, label = b[1],None + btype, label = b[1], None if type(btype) == type((1,2)): btype, label = btype + if label is None: + labels = [] + else: + labels = [label] + + print labels + """ if btype == 'title': # for backward compatibility btype = 'string' if btype == 'journal' or btype == 'audiooff' or btype == 'descriptionoff': media = label label = None - try: - proto = tw.protodict[btype] - except KeyError: - print "swapping in a forward block for %s" % (btype) - proto = tw.protodict['forward'] + """ blk = block.Block(tw.block_list, tw.sprite_list, btype, b[2]+tw.turtle.cx, - b[3]+tw.turtle.cy, [label]) - spr = blk.spr - spr.type = 'block' # phasing out - spr.proto = proto # phasing out - # if label is not None: spr.set_label(label) # phasing out + b[3]+tw.turtle.cy, 'block', labels) + """ if media is not None and media not in nolabel: try: dsobject = datastore.get(media) @@ -194,10 +219,11 @@ def load_spr(tw,b): except: if hasattr(spr,"ds_id"): print "couldn't open dsobject (" + str(spr.ds_id) + ")" - spr.set_layer(BLOCK_LAYER) - return spr + """ + blk.spr.set_layer(BLOCK_LAYER) + return blk -def load_turtle(tw,b): +def load_turtle(tw, b): id, name, xcor, ycor, heading, color, shade, pensize = b setxy(tw.turtle, xcor, ycor) seth(tw.turtle, heading) @@ -205,10 +231,16 @@ def load_turtle(tw,b): setshade(tw.turtle, shade) setpensize(tw.turtle, pensize) -def load_pict(tw,fname): - pict = gtk.gdk.pixbuf_new_from_file(fname) - tw.turtle.canvas.image.draw_pixbuf(tw.turtle.gc, pict, 0, 0, 0, 0) +# start a new project with a start brick +def load_start(tw): + clone_stack(tw,"%s%s%s" % ("[[0,[\"start\",\"", _("start"), + "\"],250,30,[null,null]]]")) +# +# Everything below is suspect +# mix and match of old and new +# procced with caution +# def save_file(tw): if tw.save_folder is not None: tw.load_save_folder = tw.save_folder fname = get_save_name(tw) @@ -352,10 +384,6 @@ def do_dialog(tw,dialog): dialog.destroy() return result -# phasing out -def blocks(tw): return [spr for spr in tw.sprite_list.list \ - if spr.type == 'block'] - def findgroup(blk, block_list): group=[blk.spr] for spr2 in blk.connections[1:]: @@ -368,8 +396,4 @@ def find_top_block(blk, block_list): blk = block_list.spr_to_block(blk.connections[0]) return blk -# start a new project with a start brick -def load_start(tw): - clone_stack(tw,"%s%s%s" % ("[[0,[\"start\",\"", _("start"), - "\"],250,30,[null,null]]]")) diff --git a/tawindow.py b/tawindow.py index acc7a96..d61dd6e 100644 --- a/tawindow.py +++ b/tawindow.py @@ -178,7 +178,7 @@ class TurtleArtWindow(): change the icon for user-defined blocks after Python code is loaded """ def set_userdefined(self): - for blk in self.block_list.list: + for blk in self._just_blocks(): if blk.name == 'nop': blk.set_image(self.media_shapes['pythonloaded']) self.nop = 'pythonloaded' @@ -188,12 +188,12 @@ class TurtleArtWindow(): """ def hideshow_button(self): if self.hide is False: - for blk in self.block_list.list: + for blk in self._just_blocks(): blk.spr.set_layer(HIDE_LAYER) self._hide_palette() self.hide = True else: - for blk in self.block_list.list: + for blk in self._just_blocks(): blk.spr.set_layer(BLOCK_LAYER) self.show_palette() self.hide = False @@ -232,14 +232,14 @@ class TurtleArtWindow(): if self._running_sugar(): self.activity.recenter() # Look for a 'start' block - for blk in self.block_list.list: + for blk in self._just_blocks(): if self._find_start_stack(blk): self.step_time = time print "running stack starting from %s" % (blk.name) self._run_stack(blk) return # If there is no 'start' block, run stacks that aren't 'def action' - for blk in self.blocks_list.list: + for blk in self._just_blocks(): if self._find_block_to_run(blk): self.step_time = time print "running stack starting from %s" % (blk.name) @@ -255,6 +255,8 @@ class TurtleArtWindow(): if hide_palette_spr is True: self.palette_spr.set_layer(HIDE_LAYER) self.selected_palette = None + for i in range(len(PALETTES)): + self.selectors[i].set_layer(HIDE_LAYER) def show_toolbar_palette(self, n, init_only=False): # TODO: make graphical selector buttons @@ -281,7 +283,7 @@ class TurtleArtWindow(): self.selectors[i].type = 'selector' self.selectors[i].set_layer(TAB_LAYER) w, h = self.selectors[i].get_dimensions() - x += (w+5) + x += int(w+5) if self.palette_spr is None: svg = sprite_factory.SVG() self.palette_spr = sprites.Sprite(self.sprite_list, 0, 0, @@ -292,12 +294,16 @@ class TurtleArtWindow(): if len(self.palettes) == 0: for i in range(len(PALETTES)): self.palettes.append([]) - + if init_only is True: return if self.selected_palette is not None: self.hide_toolbar_palette(False) + + for i in range(len(PALETTES)): + self.selectors[i].set_layer(TAB_LAYER) + self.selected_palette = n self.selectors[n].set_shape(self.selector_shapes[n][1]) self.selected_selector = self.selectors[n] @@ -315,10 +321,10 @@ class TurtleArtWindow(): w, h = self.palettes[n][i].spr.get_dimensions() if y+h > PALETTE_HEIGHT: y = ICON_SIZE+5 - x += (max_width+5) + x += int(max_width+5) max_width = 0 self.palettes[n][i].spr.move((int(x), int(y))) - y += (h+5) + y += int(h+5) if w > max_width: max_width = w else: @@ -367,7 +373,6 @@ class TurtleArtWindow(): """ Select a category. - TODO: move to toolbar """ def _select_category(self, spr): i = self.selectors.index(spr) @@ -377,13 +382,6 @@ class TurtleArtWindow(): self.selected_selector.set_shape(self.selector_shapes[j][0]) self.selected_selector = spr self.show_toolbar_palette(i) - """ - if hasattr(self, 'current_category'): - self.current_category.set_shape(self.current_category.offshape) - spr.set_shape(spr.onshape) - self.current_category = spr - self.category_spr.set_shape(spr.group) - """ """ Hide the palette. @@ -457,7 +455,7 @@ class TurtleArtWindow(): self.block_operation = 'move' # First, check to see if we are dragging or rotating a turtle. if self.selected_turtle is not None: - type,dragx,dragy = self.dragpos + type, dragx, dragy = self.dragpos (sx, sy) = self.selected_turtle.spr.get_xy() if type == 'move': if mdx != 0 or mdy != 0: @@ -809,30 +807,19 @@ class TurtleArtWindow(): return True elif spr.type == 'selector': self._select_category(spr) - """ - elif spr.type == 'selbutton': - self._select_category(spr) - elif spr.type == 'category': - self._block_selector_pressed(x,y) - """ """ Block pressed """ def _block_pressed(self, mask, x, y, blk): if blk is not None: - print "in block_pressed: %s" % (blk.name) - print "0. marking block %s as selected" % (blk.name) blk.spr.set_shape(blk.shapes[1]) - print "1. disconnecting block %s from those above it" % (blk.name) self._disconnect(blk) - print "2. creating drag_group with %s" % (blk.name) self.drag_group = self._find_group(blk) - print "drag_group: %s" % (self._print_blk_list(self.drag_group)) - for blk in self.drag_group: - blk.spr.set_layer(TOP_LAYER) (sx, sy) = blk.spr.get_xy() self.dragpos = x-sx, y-sy + for blk in self.drag_group: + blk.spr.set_layer(TOP_LAYER) """ Unselect block @@ -947,7 +934,7 @@ class TurtleArtWindow(): my_block = self.drag_group[0] d = 200 for my_dockn in range(len(my_block.docks)): - for i, your_block in enumerate(self.block_list.list): + for i, your_block in enumerate(self._just_blocks()): # don't link to a block to which you're already connected if your_block in self.drag_group: continue @@ -1012,6 +999,9 @@ class TurtleArtWindow(): run_blocks(self.lc, top, self._just_blocks(), True) gobject.idle_add(doevalstep, self.lc) + """ + Filter out 'proto' blocks + """ def _just_blocks(self): just_blocks_list = [] for b in self.block_list.list: |