From ed2ca642409d1dd847565b248a764333e3b629ee Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 23 Jan 2010 20:16:44 +0000 Subject: label margins --- diff --git a/block.py b/block.py index 2dccaf4..60a9216 100644 --- a/block.py +++ b/block.py @@ -88,6 +88,8 @@ class Block: def _new_block_from_prototype(self, sprite_list, name, labels, colors, scale, x, y): + left, right = 0, 0 + if len(labels) == 0: print "new block: %s (%d %d)" % (name, x, y) else: @@ -136,6 +138,7 @@ class Block: self.docks = (('start', True, 0, 0), ('string', False, svg.docks[0][0], svg.docks[0][1]), ('flow', False, svg.docks[1][0], svg.docks[1][1])) + left, right = 0, svg.get_width()-svg.docks[0][0] elif name in BASIC_STYLE_TAIL: svg.expand(40,0) svg.set_tab(False) @@ -149,6 +152,7 @@ class Block: self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1]), ('flow', False, svg.docks[2][0], svg.docks[2][1])) + left, right = 0, svg.get_width()-svg.docks[1][0] elif name in BASIC_STYLE_2ARG: svg.expand(25,0) svg.set_innie([True,True]) @@ -157,17 +161,13 @@ class Block: ('number', False, svg.docks[1][0], svg.docks[1][1]), ('number', False, svg.docks[2][0], svg.docks[2][1]), ('flow', False, svg.docks[3][0], svg.docks[3][1])) + left, right = 0, svg.get_width()-svg.docks[1][0] elif name in BOX_STYLE: svg.expand(60,0) self._make_basic_box(sprite_list, svg, x, y) self.docks = (('number', True, svg.docks[0][0], svg.docks[0][1]), ('unavailable', False, 0, 0)) - elif name in BOX_STYLE_1ARG: - svg.expand(60,0) - svg.set_innie([True]) - self._make_basic_box(sprite_list, svg, x, y) - self.docks = (('number', True, svg.docks[0][0], svg.docks[0][1]), - ('string', False, svg.docks[1][0], svg.docks[1][1])) + left, right = svg.docks[1][0], 0 elif name in NUMBER_STYLE: svg.expand(0,0) svg.set_innie([True,True]) @@ -183,6 +183,7 @@ class Block: self.docks = (('number', True, svg.docks[2][0], svg.docks[2][1]), ('number', False, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1])) + left, right = svg.docks[2][0], 0 elif name in NUMBER_STYLE_1ARG: svg.expand(0,0) svg.set_innie([True]) @@ -192,6 +193,7 @@ class Block: self._make_basic_block(sprite_list, svg, x, y) self.docks = (('number', True, svg.docks[1][0], svg.docks[1][1]), ('number', False, svg.docks[0][0], svg.docks[0][1])) + left, right = svg.docks[1][0], svg.docks[1][0] elif name in NUMBER_STYLE_PORCH: svg.expand(0,0) svg.set_innie([True,True]) @@ -203,29 +205,35 @@ class Block: self.docks = (('number', True, svg.docks[2][0], svg.docks[2][1]), ('number', False, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1])) + left, right = svg.docks[2][0], svg.get_width()-svg.docks[0][0] elif name in COMPARE_STYLE: svg.expand(10,0) self._make_boolean_compare(sprite_list, svg, x, y) self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1]), ('number', False, svg.docks[2][0], svg.docks[2][1])) + left, right = svg.get_width()-svg.docks[2][0], 0 elif name in BOOLEAN_STYLE: svg.expand(10,0) self._make_boolean_and_or(sprite_list, svg, x, y) self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), ('bool', False, svg.docks[1][0], svg.docks[1][1]), ('bool', False, svg.docks[2][0], svg.docks[2][1])) + left, right = svg.get_width()-svg.docks[1][0], 0 elif name in NOT_STYLE: svg.expand(15,0) self._make_boolean_not(sprite_list, svg, x, y) self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), ('bool', False, svg.docks[1][0], svg.docks[1][1])) + right = svg.get_width()-svg.docks[1][0] + left = right elif name in FLOW_STYLE: svg.expand(25,0) svg.set_slot(True) self._make_basic_flow(sprite_list, svg, x, y) self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), - ('flow', False, svg.docks[0][0], svg.docks[0][1])) + ('flow', False, svg.docks[1][0], svg.docks[1][1])) + left, right = 0, svg.get_width()-svg.docks[1][0] elif name in FLOW_STYLE_1ARG: svg.expand(25,0) svg.set_slot(True) @@ -236,6 +244,7 @@ class Block: ('number', False, svg.docks[1][0], svg.docks[1][1]), ('flow', False, svg.docks[2][0], svg.docks[2][1]), ('flow', False, svg.docks[3][0], svg.docks[3][1])) + left, right = 0, svg.get_width()-svg.docks[1][0] elif name in FLOW_STYLE_BOOLEAN: svg.expand(25,0) svg.set_slot(True) @@ -246,6 +255,7 @@ class Block: ('bool', False, svg.docks[1][0], svg.docks[1][1]), ('flow', False, svg.docks[2][0], svg.docks[2][1]), ('flow', False, svg.docks[3][0], svg.docks[3][1])) + left, right = 0, svg.get_width()-svg.docks[1][0] else: svg.expand(40,0) self._make_basic_block(sprite_list, svg, x, y) @@ -254,8 +264,10 @@ class Block: print "don't know how to create a block for %s" % (name) print self.docks - print "w %s h %s" % (svg._width, svg._height) + print "w %d h %d" % (svg._width, svg._height) + print "l %d r %d" % (left, right) + self.spr.set_margins(left,0,right,0) # NEED TO PROCESS DEFAULTS if len(labels) > 0: if BLOCK_NAMES.has_key(name): diff --git a/constants.py b/constants.py index 803d665..6240242 100644 --- a/constants.py +++ b/constants.py @@ -36,7 +36,7 @@ BASIC_STYLE_HEAD_1ARG = ['def action'] BASIC_STYLE_TAIL = ['stop action'] BASIC_STYLE = ['clean', 'pen up', 'pen down', 'action 1', 'action 2', 'vspace', 'hide blocks'] -BASIC_STYLE_1ARG = ['forward', 'back', 'left', 'right', 'seth', 'show', +BASIC_STYLE_1ARG = ['forward', 'back', 'left', 'right', 'set heading', 'show', 'set scale', 'set pen size', 'set color', 'set shade', 'print', 'set text size', 'set text color', 'print', 'wait', 'store in box 1', 'store in box 2', 'wait', 'action'] @@ -44,25 +44,24 @@ BASIC_STYLE_2ARG = ['arc', 'set xy', 'fill screen', 'store in'] BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pen size', 'color', 'shade', 'text color', 'text size', 'box 1', 'box 2', 'string', 'left pos', 'scale', 'top pos', 'right pos', 'bottom pos', 'width', 'height'] -BOX_STYLE_1ARG = ['box'] NUMBER_STYLE = ['plus', 'multiply', 'random'] NUMBER_STYLE_PORCH = ['minus', 'divide', 'mod'] -NUMBER_STYLE_1ARG = ['square root'] +NUMBER_STYLE_1ARG = ['square root', 'box'] COMPARE_STYLE = ['greater than', 'less than', 'equal to'] BOOLEAN_STYLE = ['and', 'or'] NOT_STYLE = ['not'] FLOW_STYLE = ['forever', 'hspace'] FLOW_STYLE_1ARG = ['repeat'] FLOW_STYLE_BOOLEAN = ['if then'] -TURTLE_PALETTE = ['clean', 'forward', 'back', 'left', 'right', 'seth', 'show', - 'set scale', 'arc', 'scale', 'xcor', 'ycor', 'heading'] +TURTLE_PALETTE = ['clean', 'forward', 'back', 'left', 'right', 'set heading', + 'show', 'set scale', 'arc', 'scale', 'xcor', 'ycor', 'heading'] PEN_PALETTE = ['pen up','pen down', 'set pen size', 'set text size', 'set color', 'set shade', 'fill screen', 'pen size', 'text size', 'color', - 'shade'] # shade? + 'shade'] NUMBER_PALETTE = ['number', 'plus', 'minus', 'multiply', 'divide', 'mod', 'square root', 'random', 'greater than', 'less than', 'equal to', 'and', 'or', 'not', 'print'] -FLOW_PALETTE = ['wait', 'forever', 'repeat', 'if then', 'stop action', 'hspace' +FLOW_PALETTE = ['wait', 'forever', 'repeat', 'if then', 'stop action', 'hspace', 'vspace'] BLOCKS_PALETTE = ['start', 'def action 1', 'action 1', 'def action 2', 'action 2', 'def action', 'action', 'store in box 1', 'box 1', @@ -98,6 +97,7 @@ BLOCK_NAMES = {'clean':_('clean'), 'forward':_('forward'), 'back':_('back'), 'pen down':_('pen down'), 'set pen size':_('set pen size'), 'arc':_('arc'), 'set text size':_('set text size'), 'set color':_('set color'), 'set shade':_('set shade'), 'fill screen':_('fill screen'), + 'shade':_('shade'), 'pen size':_('pen size'), 'text size':_('text size'), 'color':_('color'), 'plus':'+', 'minus':'–', 'multiply':'×', 'divide':'/', 'mod':_('mod'), 'random':_('random'), 'square root':'√', 'less than':'<', @@ -125,7 +125,7 @@ PRIMITIVES = {'clean':'clean', 'forward':'forward', 'back':'back', 'arc':'arc', 'ycor':'ycor', 'heading':'heading', 'pen up':'penup', 'pen down':'pendown', 'set pen size':'setpensize', 'set text size':'settextsize', 'set color':'setcolor', - 'set shade':'setshade', 'fill screen':'fillscreen', + 'set shade':'setshade', 'fill screen':'fillscreen', 'shade':'shade', 'pen size':'pensize', 'text size':'textsize', 'color':'color', 'plus':'plus', 'minus':'minus', 'multiply':'product', 'divide':'division', 'mod':'mod', @@ -149,12 +149,12 @@ PRIMITIVES = {'clean':'clean', 'forward':'forward', 'back':'back', 'arc':'arc', # DEFAULTS = {'forward':[100], 'back':[100], 'left':[90], 'right':[90], - 'arc':[90,100], 'seth':[0], 'set scale':[33], 'show':[_('text')], + 'arc':[90,100], 'set heading':[0], 'set scale':[33], 'show':[_('text')], 'set pen size':[5], 'set text size':[32], 'set color':[0], 'set shade':[50], 'fill screen':[60,80], 'number':[100], 'random':[0,100], 'wait':[1], 'repeat':[4], 'set xy':[0,0], - 'store in':[_('box'),100], 'box':[_('my box')], 'def action':[_('action')], - 'action':[_('action')]} + 'store in':[_('my box'),100], 'box':[_('my box')], + 'def action':[_('action')], 'action':[_('action')]} # # 'dead key' Unicode dictionaries diff --git a/sprites.py b/sprites.py index 308766b..dbb5267 100644 --- a/sprites.py +++ b/sprites.py @@ -95,6 +95,7 @@ class Sprite: self._color = None self._width = 0 self._height = 0 + self._margins = [0,0,0,0] self.layer = 100 self.labels = [] self.set_image(image) @@ -148,6 +149,9 @@ class Sprite: self.labels[i] = str(new_label) self.inval() + def set_margins(self, l=0, t=0, r=0, b=0): + self._margins = [l,t,r,b] + def _extend_labels_array(self, i): if self._fd is None: self.set_font('Sans') @@ -205,20 +209,22 @@ class Sprite: return True def draw_label(self): + my_width = self._width-self._margins[0]-self._margins[2] + my_height = self._height-self._margins[1]-self._margins[3] for i in range(len(self.labels)): pl = self._sprites.canvas.create_pango_layout(str(self.labels[i])) self._fd.set_size(int(self._scale[i]*pango.SCALE)) pl.set_font_description(self._fd) w = pl.get_size()[0]/pango.SCALE - if w > self._width: + if w > my_width: if self._rescale[i] is True: - self._fd.set_size(int(self._scale[i]*pango.SCALE*\ - self._width/w)) + self._fd.set_size( + int(self._scale[i]*pango.SCALE*my_width/w)) pl.set_font_description(self._fd) w = pl.get_size()[0]/pango.SCALE else: j = len(self.labels[i])-1 - while(w > self._width and j > 0): + while(w > my_width and j > 0): pl = self._sprites.canvas.create_pango_layout( "…"+self.labels[i][len(self.labels[i])-j:]) self._fd.set_size(int(self._scale[i]*pango.SCALE)) @@ -226,18 +232,18 @@ class Sprite: w = pl.get_size()[0]/pango.SCALE j -= 1 if self._horiz_align[i] == "center": - x = int(self._x+(self._width-w)/2) + x = int(self._x+self._margins[0]+(my_width-w)/2) elif self._horiz_align[i] == 'left': - x = self._x + x = self._x+self._margins[0] else: # right - x = int(self._x+self._width-w) + 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._height-h)/2) + y = int(self._y+self._margins[1]+(my_height-h)/2) elif self._vert_align[i] == "top": - y = self._y + y = self._y+self._margins[1] else: # bottom - y = int(self._y+self._height-h) + y = int(self._y+self._height-h-self._margins[3]) self._sprites.gc.set_foreground(self._color) self._sprites.area.draw_layout(self._sprites.gc, x, y, pl) @@ -252,11 +258,15 @@ class Sprite: max = w return max + def label_area(self): + return((self._width-self._margin[0]-self._margin[2], + self._width-self._margin[1]-self._margin[3])) + def get_pixel(self, image, x, y): array = image.get_pixels() - offset = (y*image.get_width()+x)*4 - r,g,b,a = ord(array[offset]),ord(array[offset+1]),ord(array[offset+2]),\ - ord(array[offset+3]) - return (a<<24)+(b<<16)+(g<<8)+r - - + if array is not None: + offset = (y*image.get_width()+x)*4 + r,g,b,a = ord(array[offset]), ord(array[offset+1]),\ + ord(array[offset+2]), ord(array[offset+3]) + return (a<<24)+(b<<16)+(g<<8)+r + return 0 diff --git a/talogo.py b/talogo.py index 2436e94..0215f04 100644 --- a/talogo.py +++ b/talogo.py @@ -72,11 +72,11 @@ def run_blocks(lc, blk, blocks, run_flag): lc.stacks['stack1'] = None lc.stacks['stack2'] = None for b in blocks: - if b.name=='hat1': + if b.name=='def action 1': lc.stacks['stack1']= readline(lc,blocks_to_code(lc, b)) - if b.name=='hat2': + if b.name=='def action 2': lc.stacks['stack2']= readline(lc,blocks_to_code(lc, b)) - if b.name=='hat': + if b.name=='def action': if (b.connections[1]!=None): text=b.connections[1].spr.labels[0] lc.stacks['stack3'+text]= readline(lc,blocks_to_code(lc, b)) @@ -169,7 +169,8 @@ def setup_cmd(lc, str): lc.step = start_eval(lc, list) def start_eval(lc, list): - icall(lc, evline, list); yield True + icall(lc, evline, list) + yield True # turn off stop icon when execution is finished if hasattr(lc.tw,"activity"): lc.tw.activity.stop_button.set_icon("stopitoff") @@ -187,9 +188,12 @@ def evline(lc, list): yield True lc.tw.turtle.spr.set_layer(HIDE_LAYER) token = lc.iline[0] - if token==lc.symopar: token=lc.iline[1] - icall(lc, eval); yield True - if lc.procstop: break + if token is lc.symopar: + token=lc.iline[1] + icall(lc, eval) + yield True + if lc.procstop: + break if lc.iresult==None: continue raise logoerror(str(lc.iresult)) lc.iline = oldiline @@ -199,7 +203,7 @@ def evline(lc, list): def eval(lc, infixarg=False): token = lc.iline.pop(0) - if type(token) == lc.symtype: + if type(token) is lc.symtype: icall(lc, evalsym, token); yield True res = lc.iresult else: res = token @@ -215,23 +219,27 @@ def evalsym(lc, token): undefined_check(lc, token) oldcfun, oldarglist = lc.cfun, lc.arglist lc.cfun, lc.arglist = token, [] - if token.nargs==None: raise logoerror("#noinput") + if token.nargs is None: + raise logoerror("#noinput") for i in range(token.nargs): no_args_check(lc) - icall(lc, eval); yield True + icall(lc, eval) + yield True lc.arglist.append(lc.iresult) if lc.cfun.rprim: - if type(lc.cfun.fcn)==lc.listtype: - icall(lc, ufuncall, cfun.fcn); yield True + if type(lc.cfun.fcn) is lc.listtype: + icall(lc, ufuncall, cfun.fcn) + yield True else: - icall(lc, lc.cfun.fcn, *lc.arglist); yield True + icall(lc, lc.cfun.fcn, *lc.arglist) + yield True result = None else: result = lc.cfun.fcn(lc, *lc.arglist) lc.cfun, lc.arglist = oldcfun, oldarglist - if lc.arglist!=None and result==None: - raise logoerror("%s didn't output to %s" % \ - (oldcfun.name, lc.cfun.name)) + if lc.arglist is not None and result is None: + raise logoerror("%s didn't output to %s (arglist %s, result %s)" % \ + (oldcfun.name, lc.cfun.name, str(lc.arglist), str(result))) ireturn(lc, result) yield True @@ -240,15 +248,19 @@ def evalinfix(lc, firstarg): oldcfun, oldarglist = lc.cfun, lc.arglist lc.cfun, lc.arglist = token, [firstarg] no_args_check(lc) - icall(lc, eval,True); yield True + icall(lc, eval, True) + yield True lc.arglist.append(lc.iresult) result = lc.cfun.fcn(lc,*lc.arglist) lc.cfun, lc.arglist = oldcfun, oldarglist - ireturn (lc,result); yield True + ireturn (lc,result) + yield True def infixnext(lc): - if len(lc.iline)==0: return False - if type(lc.iline[0])!=lc.symtype: return False + if len(lc.iline)==0: + return False + if type(lc.iline[0]) is not lc.symtype: + return False return lc.iline[0].name in ['+', '-', '*', '/','%','and','or'] def debug_trace(lc, token): @@ -579,12 +591,13 @@ def lcNew(tw): lc.listtype = type([]) lc.symnothing = intern(lc, '%nothing%') lc.symopar = intern(lc, '(') + lc.iline, lc.cfun, lc.arglist, lc.ufun = None, None, None, None lc.istack = [] lc.stacks = {} - # lc.boxes = noKeyError({'box1': 0, 'box2': 0}) lc.boxes = {'box1': 0, 'box2': 0} lc.heap = [] + lc.keyboard = 0 lc.trace = 0 # flag for enabling debug output via showlabel lc.gplay = None @@ -594,8 +607,6 @@ def lcNew(tw): lc.body_height = int((lc.tw.turtle.height/60)*lc.tw.scale) lc.bullet_height = int((lc.tw.turtle.height/45)*lc.tw.scale) - lc.iline, lc.cfun, lc.arglist, lc.ufun = None, None, None, None - # this dictionary is used to define the relative size and postion of # template elements (w, h, x, y, dx, dy, dx1, dy1...) lc.templates = { diff --git a/tasetup.py b/tasetup.py index 611bdc0..efad82d 100644 --- a/tasetup.py +++ b/tasetup.py @@ -53,7 +53,7 @@ selectors = ( ('right','right','onearg',90), ('arc','arc','twoargs',90,100), ('set xy','setxy','twoargs',0,0), - ('seth','seth','onearg',0), + ('set heading','seth','onearg',0), ('show','show','onesarg',_('text')), ('set scale','setscale','onearg',33), ('show','show','onecarg','None'), -- cgit v0.9.1