From 071077470caf28dde2e7033f16b7431e594c13ec Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sun, 27 Feb 2011 18:35:35 +0000 Subject: enab;e setting defaults --- diff --git a/pysamples/load_block.py b/pysamples/load_block.py index 8fb9546..566ed47 100644 --- a/pysamples/load_block.py +++ b/pysamples/load_block.py @@ -30,39 +30,55 @@ def myblock(lc, blkname): # ########################################################################### - from taconstants import BLOCK_NAMES, PRIMITIVES + from taconstants import BLOCK_NAMES, PRIMITIVES, CONTENT_BLOCKS, \ + SPECIAL_NAMES from tautils import find_group - def new_block(lc, blkname, x, y): + def make_block(lc, name, x, y, defaults): + lc.tw._new_block(name, x + 20, y + 20, defaults) + + # Find the block we just created and attach it to a stack. + lc.tw.drag_group = None + spr = lc.tw.sprite_list.find_sprite((x + 20, y + 20)) + if spr is not None: + blk = lc.tw.block_list.spr_to_block(spr) + if blk is not None: + lc.tw.drag_group = find_group(blk) + lc.tw._snap_to_dock() + + # Disassociate new block from mouse. + lc.tw.drag_group = None + return blk.height + + def find_block(lc, blkname, x, y, defaults=None): """ Create a new block. It is a bit more work than just calling _new_block(). We need to: (1) translate the label name into the internal block name; (2) 'dock' the block onto a stack where appropriate; and (3) disassociate the new block from the mouse. """ + print blkname for name in BLOCK_NAMES: # Translate label name into block/prim name. - if blkname in BLOCK_NAMES[name] and PRIMITIVES[name] == name: - lc.tw._new_block(name, x + 20, y + 20) - - # Find the block we just created and attach it to a stack. - lc.tw.drag_group = None - spr = lc.tw.sprite_list.find_sprite((x + 20, y + 20)) - if spr is not None: - blk = lc.tw.block_list.spr_to_block(spr) - if blk is not None: - lc.tw.drag_group = find_group(blk) - lc.tw._snap_to_dock() - - # Disassociate new block from mouse. - lc.tw.drag_group = None - return blk.height - return 0 + if blkname in BLOCK_NAMES[name]: + if (name in PRIMITIVES and PRIMITIVES[name] == name) or \ + name in CONTENT_BLOCKS: + return make_block(lc, name, x, y, defaults) + for name in SPECIAL_NAMES: + # Translate label name into block/prim name. + if blkname in SPECIAL_NAMES[name]: + return make_block(lc, name, x, y, defaults) + return -1 - # Place the block at the active turtle (x, y). + # Place the block at the active turtle (x, y) and + # push height to stack. x, y = lc.tw.active_turtle.get_xy() if type(blkname) == type([]): - for name in blkname: - y += int(new_block(lc, name, x, y)) + name = blkname[0] + value = blkname[1:] + dy = int(find_block(lc, name, x, y, value)) else: - new_block(lc, blkname, x, y) + name = blkname + dy = int(find_block(lc, name, x, y)) + if dy != -1: + lc.heap.append(dy) -- cgit v0.9.1