From c223f72fc567756fa317450a3701657bcd1050c0 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 25 Jan 2010 16:56:33 +0000 Subject: toolbar palettes--a beginning --- diff --git a/block.py b/block.py index 293ee01..a6737bd 100644 --- a/block.py +++ b/block.py @@ -65,11 +65,10 @@ class Blocks: # A class for the individual blocks # class Block: - def __init__(self, block_list, sprite_list, name, x, y, labels=[], - colors=["#00FF00","#00A000"], scale=2.0): + def __init__(self, block_list, sprite_list, name, x, y, type='block', + labels=[], scale=2.0, colors=["#00FF00","#00A000"]): self.spr = None - self.shape = None - self.selected_shape = None + self.shapes = [] self.name = name self.colors = colors self.scale = scale @@ -78,12 +77,16 @@ class Block: self.defaults = [] self.content = None self.primitive = None - self._new_block_from_factory(sprite_list, labels, x, y) - self.type = 'block' - + self.type = type + self._font_size = [6.0, 4.5] self._left = 0 self._right = 0 + for i in range(len(self._font_size)): + self._font_size[i] *= self.scale + + self._new_block_from_factory(sprite_list, labels, x, y) + if DEFAULTS.has_key(self.name): self.defaults = DEFAULTS[self.name] @@ -101,15 +104,25 @@ class Block: # debug code # etc. - # We resize some blocks on the fly + # We need to resize some blocks on the fly. def resize(self): # make sure the label fits lw = self.spr.label_width() lwh = self.spr.label_area_dimensions() if lw > lwh[0]: - e = lw-lwh[0] + e = (lw-lwh[0]) self._make_block(e, self.svg) - self.spr.set_shape(self.selected_shape) + self.spr.set_shape(self.shapes[0]) + + # We may want to rescale blocks as well. + def rescale(self, scale): + for i in range(len(self._font_size)): + self._font_size[i] /= self.scale + self.scale = scale + for i in range(len(self._font_size)): + self._font_size[i] *= self.scale + self._make_block(e, self.svg) + self.spr.set_shape(self.shapes[0]) def _new_block_from_factory(self, sprite_list, labels, x, y): @@ -124,36 +137,41 @@ class Block: self.svg.set_slot(True) self._make_block(0, self.svg) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) + self.spr = sprites.Sprite(sprite_list, x, y, self.shapes[0]) self.spr.set_margins(self._left, self.svg.get_slot_depth(), self._right, self.svg.get_slot_depth()*2) - # if labels were passed, use them + # If labels were passed, use them; if len(labels) > 0: print labels for i, l in enumerate(labels): - self.spr.set_label(l,i) - if i == 1: # top - self.spr.set_label_attributes(9, True, "right", "top", i) - elif i == 2: # bottom - self.spr.set_label_attributes(9, True, "right", "bottom", i) - # otherwise use default values + 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.spr.set_label(l,i) - if i == 1: # top - self.spr.set_label_attributes(9, True, "right", "top", i) - elif i == 2: # bottom - self.spr.set_label_attributes(9, True, "right", "bottom", i) - # make sure labels fit + self._set_labels(i, l) + # and make sure the labels fit. self.resize() + def _set_labels(self, i, label): + if i == 1: # top + self.spr.set_label_attributes(int(self._font_size[1]+0.5), True, + "right", "top", i) + elif i == 2: # bottom + self.spr.set_label_attributes(int(self._font_size[1]+0.5), True, + "right", "bottom", i) + else: + self.spr.set_label_attributes(int(self._font_size[0]+0.5), True, + "center", "middle", i) + self.spr.set_label(label, i) + def _make_block(self, e, svg): self._set_colors(svg) self.svg.set_stroke_width(STANDARD_STROKE_WIDTH) self.svg.clear_docks() + self.shapes = [] if self.name in BASIC_STYLE: self._make_basic_style(e, svg) elif self.name in BASIC_STYLE_HEAD: @@ -191,20 +209,9 @@ class Block: print "don't know how to create a %s block" % (self.name) def _set_colors(self, svg): - if self.name in TURTLE_PALETTE: - self.colors = TURTLE_COLORS - elif self.name in PEN_PALETTE: - self.colors = PEN_COLORS - elif self.name in NUMBER_PALETTE: - self.colors = NUMBER_COLORS - elif self.name in BLOCKS_PALETTE: - self.colors = BLOCKS_COLORS - elif self.name in MISC_PALETTE: - self.colors = MISC_COLORS - elif self.name in FLOW_PALETTE: - self.colors = FLOW_COLORS - elif self.name in PORTFOLIO_PALETTE: - self.colors = PORTFOLIO_COLORS + for p in range(len(PALETTES)): + if self.name in PALETTES[p]: + self.colors = COLORS[p] self.svg.set_colors(self.colors) def _make_basic_style(self, e, svg): @@ -406,49 +413,49 @@ class Block: self._left, self._right = 0, self.svg.get_width()-self.svg.docks[1][0] def _make_basic_block(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.basic_block()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_block())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.basic_block()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_block())) def _make_basic_box(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.basic_box()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_box())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.basic_box()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_box())) def _make_basic_flow(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.basic_flow()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_flow())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.basic_flow()) + self.shapes.append(svg_str_to_pixbuf(self.svg.basic_flow())) def _make_boolean_compare(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.boolean_compare()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_compare())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.boolean_compare()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_compare())) def _make_boolean_and_or(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.boolean_and_or()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_and_or())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.boolean_and_or()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_and_or())) def _make_boolean_not(self, svg): - self.shape = svg_str_to_pixbuf(self.svg.boolean_not()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_not())) self.width = self.svg.get_width() self.height = self.svg.get_height() self.svg.set_stroke_width(SELECTED_STROKE_WIDTH) self.svg.set_stroke_color(SELECTED_COLOR) - self.selected_shape = svg_str_to_pixbuf(self.svg.boolean_not()) + self.shapes.append(svg_str_to_pixbuf(self.svg.boolean_not())) diff --git a/constants.py b/constants.py index 2af8c74..d71a441 100644 --- a/constants.py +++ b/constants.py @@ -28,9 +28,49 @@ STATUS_LAYER = 900 TOP_LAYER = 1000 # -# block definitions +# block palette categories # +TURTLE = 0 +PEN = 1 +NUMBER = 2 +FLOW = 3 +BLOCKS = 4 +MISC = 5 +PORTFOLIO = 6 + +PALETTES = [['clean', 'forward', 'back', 'left', 'right', 'arc', 'set xy', + 'show', 'set scale', 'set heading', 'scale', 'xcor', 'ycor', 'heading'], + ['pen up','pen down', 'set pen size', 'set text size', + 'set color', 'set shade', 'fill screen', 'pen size', 'text size', 'color', + 'shade'], + ['number', 'plus', 'minus', 'multiply', + 'divide', 'mod', 'square root', 'random', 'greater than', 'less than', + 'equal to', 'and', 'or', 'not', 'print'], + ['wait', 'forever', 'repeat', 'if then', 'stop action', 'hspace', + 'vspace'], + ['start', 'def action 1', 'action 1', 'def action 2', + 'action 2', 'def action', 'action', 'store in box 1', 'box 1', + 'store in box 2', 'box 2', 'store in', 'box', 'string'], + ['left pos', 'top pos', 'right pos', 'bottom pos', 'width', + 'height'], + ['hide blocks']] + +# +# block style attributes +# + +COLORS = [["#00FF00","#00A000"], ["#00FFFF","#00A0A0"], ["#FF00FF","#A000A0"], + ["#FFC000","#A08000"], ["#FFFF00","#A0A000"], ["#FF0000","#A0000"], + ["#0000FF","#0000FF"]] + +SELECTED_COLOR = "#0000FF" +SELECTED_STROKE_WIDTH = 1.5 +STANDARD_STROKE_WIDTH = 1.0 + +# +# block style definitions +# BASIC_STYLE_HEAD = ['start', 'def action 1', 'def action 2'] BASIC_STYLE_HEAD_1ARG = ['def action'] BASIC_STYLE_TAIL = ['stop action'] @@ -53,34 +93,6 @@ NOT_STYLE = ['not'] FLOW_STYLE = ['forever', 'hspace'] FLOW_STYLE_1ARG = ['repeat'] FLOW_STYLE_BOOLEAN = ['if then'] -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'] -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', - 'vspace'] -BLOCKS_PALETTE = ['start', 'def action 1', 'action 1', 'def action 2', - 'action 2', 'def action', 'action', 'store in box 1', 'box 1', - 'store in box 2', 'box 2', 'store in', 'box', 'string'] -MISC_PALETTE = ['left pos', 'top pos', 'right pos', 'bottom pos', 'width', - 'height'] -PORTFOLIO_PALETTE = ['hide blocks'] - -TURTLE_COLORS = ["#00FF00","#00A000"] -PEN_COLORS = ["#00FFFF","#00A0A0"] -NUMBER_COLORS = ["#FF00FF","#A000A0"] -BLOCKS_COLORS = ["#FFFF00","#A0A000"] -FLOW_COLORS = ["#FFC000","#A08000"] -MISC_COLORS = ["#FF0000","#A0000"] -PORTFOLIO_COLORS = ["#0000FF","#0000FF"] - -SELECTED_COLOR = "#0000FF" -SELECTED_STROKE_WIDTH = 2.0 -STANDARD_STROKE_WIDTH = 1.0 # # blocks that contain media @@ -187,5 +199,5 @@ NOISE_KEYS = ['Shift_L', 'Shift_R', 'Control_L', 'Caps_Lock', 'Pause', 'Left', 'Right', 'KP_Home', 'KP_End', 'KP_Up', 'Super_L', 'KP_Down', 'KP_Left', 'KP_Right', 'KP_Page_Down', 'Scroll_Lock', 'Page_Down', 'Page_Up'] -WHITE_SPACE = ['space','Tab'] +WHITE_SPACE = ['space','Tab','Return'] diff --git a/sprite_factory.py b/sprite_factory.py index cd15be0..7710429 100755 --- a/sprite_factory.py +++ b/sprite_factory.py @@ -236,7 +236,7 @@ class SVG: svg += self._rect(3, 3, 36, 36) self._fill, self._stroke = "#008000", "#008000" svg += self._turtle_body() - self._fill, self._stroke = "#00a000", "#00a000" + self._fill, self._stroke = "#00A000", "#00A000" svg += self._turtle_shell() self._fill, self._stroke = "#000000", "#000000" svg += self._circle(1.25,32.5,8) @@ -246,6 +246,13 @@ class SVG: # TODO: Add orientation return self._header() + svg + def palette(self, width, height): + self._fill, self._stroke = "#282828", "none" + self._width, self._height = width, height + svg = self._rect(width, height, 0, 0) + svg += self._footer() + return self._header() + svg + # # Utility methods # @@ -352,7 +359,7 @@ class SVG: "\n", @@ -401,20 +408,20 @@ class SVG: def _circle(self, r, cx, cy): return "%s%s%s%s%s%f%s%f%s%f%s" % ("") + cx, "\" cy=\"", cy, "\" />\n") def _rect(self, w, h, x, y): return "%s%s%s%s%s%f%s%f%s%f%s%f%s" % ("") + "\" height=\"", h,"\" x=\"", x, "\" y=\"", y, "\" />\n") def _turtle_body(self): return "%s%s%s%s%s" % ("") + self._stroke, "\" d=\"M 20,42 C 21,41 23,40 24,40 C 24,39 24,40 26,41 C 28,43 31,43 34,41 C 35,40 35,39 36,40 C 36,40 38,41 39,42 C 41,42 45,43 46,43 C 47,43 46,41 43,39 L 39,36 L 42,34 C 44,30 45,28 43,25 L 41,22 L 46,18 C 48,16 47.5,13.5 47,13 C 46.5,12.5 46,13 45,13 C 44,13 43.5,14 42.5,15 C 39.5,17 40,18 37,17 C 32,16 31.5,15 34.5,12 C 36.5,10 36,7 34,6 C 32,3 28,4 26,6 C 24,8 23,10 25,12 C 28,15 27,16 22,17 C 18,18 19,17 17,15 C 16,14 16,13 15,13 C 14,13 13,13 13,13 C 12,13 11,16 14,18 L 19,22 L 17,25 C 15,28 16,30 18,34 L 20,36 L 16,39 C 13,41 12,43 13,43 C 14,43 18,42 20,42 z M 30,18 C 32,18 36,19 38,20 C 40,22 39.5,25 39.5,28 C 39.5,30 40,32.5 38.5,35 C 37,36.5 36.5,37.5 35,38 C 33.5,38.5 31,39 30,39 C 28,39 26,39 25,38 C 23,37 22.5,37 21.5,35 C 20.5,33 20.5,30 20.5,28 C 20.5,25 20,22 22,20 C 24,19 27,18 30,18 z\" />\n") def _turtle_shell(self): return "%s%s%s%s%s" % ("") + self._stroke, "\" d=\"M 33,10 C 33,11 31.5,12 30,12 C 28,12 27,11 27,10 C 27,9 28,8 30,8 C 31.5,8 33,9 33,10 z\" />\n") def _check_min_max(self): if self._x < self._min_x: @@ -605,6 +612,14 @@ def close_file(f): def generator(datapath): svg0 = SVG() + f = open_file(datapath, "palette-test.svg") + svg_str = svg0.palette(600,75) + f.write(svg_str) + close_file(f) + + + """ + svg0 = SVG() f = open_file(datapath, "flow-test.svg") svg0.set_scale(1) svg0.expand(20,0) @@ -615,6 +630,7 @@ def generator(datapath): svg_str = svg0.basic_flow() f.write(svg_str) close_file(f) + """ svg1 = SVG() f = open_file(datapath, "blob-test.svg") @@ -627,6 +643,7 @@ def generator(datapath): f.write(svg_str) close_file(f) + """ svg2 = SVG() f = open_file(datapath, "box-test.svg") svg2.set_scale(1) @@ -664,6 +681,7 @@ def generator(datapath): svg_str = svg5.boolean_not() f.write(svg_str) close_file(f) + """ def main(): return 0 diff --git a/sprites.py b/sprites.py index 89d5682..13996bb 100644 --- a/sprites.py +++ b/sprites.py @@ -121,6 +121,9 @@ class Sprite: def get_xy(self): return (self._x, self._y) + def get_dimensions(self): + return (self._width, self._height) + def get_layer(self): return self.layer diff --git a/talogo.py b/talogo.py index 325f8bc..68f7f38 100644 --- a/talogo.py +++ b/talogo.py @@ -63,14 +63,6 @@ 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: - print " %s" % (b.name) - for c in b.connections: - if c == None: - print " None" - else: - print " %s" % (c.name) # user-defined stacks for x in lc.stacks.keys(): lc.stacks[x]= None @@ -83,24 +75,24 @@ def run_blocks(lc, blk, blocks, run_flag): if b.name=='def action 2': lc.stacks['stack2']= readline(lc,blocks_to_code(lc, b)) if b.name=='def action': - if (b.connections[1]!=None): + 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)) code = blocks_to_code(lc, blk) - if run_flag == True: + if run_flag is True: print "code: %s" % (code) setup_cmd(lc, code) else: return code def blocks_to_code(lc, blk): - if blk==None: + if blk is None: return ['%nothing%'] spr = blk.spr code = [] dock = blk.docks[0] if len(dock)>4: code.append(dock[4]) - if blk.primitive != None: + if blk.primitive is not None: code.append(blk.primitive) else: if blk.name=='number': @@ -116,18 +108,18 @@ def blocks_to_code(lc, blk): else: code.append('#s'+spr.labels[0]) elif blk.name=='journal': - if spr.ds_id != None: # TODO: put ds_id in blk + if spr.ds_id is not None: # TODO: put ds_id in blk code.append('#smedia_'+str(spr.ds_id)) else: code.append('#smedia_None') elif blk.name=='descriptionoff' or \ blk.name=='descriptionon': - if spr.ds_id != None: + if spr.ds_id is not None: code.append('#sdescr_'+str(spr.ds_id)) else: code.append('#sdescr_None') elif blk.name=='audiooff' or blk.name=='audio': - if spr.ds_id != None: + if spr.ds_id is not None: code.append('#saudio_'+str(spr.ds_id)) else: code.append('#saudio_None') @@ -139,7 +131,7 @@ def blocks_to_code(lc, blk): if len(dock)>4: for c in dock[4]: code.append(c) - if b != None: + if b is not None: code.extend(blocks_to_code(lc, b)) elif blk.docks[i][0] not in ['flow', 'unavailable']: print "appending nothing" @@ -242,7 +234,7 @@ def evalsym(lc, token): else: result = lc.cfun.fcn(lc, *lc.arglist) lc.cfun, lc.arglist = oldcfun, oldarglist - if lc.arglist != None and result == None: + if lc.arglist is not None and result == 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) @@ -262,7 +254,7 @@ def evalinfix(lc, firstarg): def infixnext(lc): if len(lc.iline)==0: return False - if type(lc.iline[0]) != lc.symtype: + if type(lc.iline[0]) is not lc.symtype: return False return lc.iline[0].name in ['+', '-', '*', '/','%','and','or'] @@ -298,11 +290,11 @@ def debug_trace(lc, token): return def undefined_check(lc, token): - if token.fcn != None: return False + if token.fcn is not None: return False raise logoerror("I don't know how to %s" % token.name) def no_args_check(lc): - if lc.iline and lc.iline[0]!=lc.symnothing : return + if lc.iline and lc.iline[0] is not lc.symnothing : return raise logoerror("#noinput") def prim_wait(lc,time): @@ -339,25 +331,25 @@ def prim_opar(lc,val): return val def prim_define(name, body): - if type(name) != symtype: name = intern(name) + if type(name) is not symtype: name = intern(name) name.nargs, name.fcn = 0, body name.rprim = True def prim_stack(lc,stri): if (not lc.stacks.has_key('stack3'+stri)) or \ - lc.stacks['stack3'+stri]==None: raise logoerror("#nostack") + lc.stacks['stack3'+stri] is None: raise logoerror("#nostack") icall(lc, evline, lc.stacks['stack3'+stri][:]); yield True lc.procstop = False ireturn(lc); yield True def prim_stack1(lc): - if lc.stacks['stack1']==None: raise logoerror("#nostack") + if lc.stacks['stack1'] is None: raise logoerror("#nostack") icall(lc, evline, lc.stacks['stack1'][:]); yield True lc.procstop = False ireturn(lc); yield True def prim_stack2(lc): - if lc.stacks['stack2']==None: raise logoerror("#nostack") + if lc.stacks['stack2'] is None: raise logoerror("#nostack") icall(lc, evline, lc.stacks['stack2'][:]); yield True lc.procstop = False ireturn(lc); yield True @@ -662,7 +654,7 @@ def box(lc,x): def loadmyblock(lc,x): # execute code inported from the Journal - if lc.tw.myblock != None: + if lc.tw.myblock is not None: y = myfunc_import(lc, lc.tw.myblock, x) else: raise logoerror("#nocode") @@ -680,7 +672,7 @@ def show_picture(lc, media, x, y, w, h): if media == "" or media[6:] == "": # raise logoerror("#nomedia") pass - elif media[6:] != "None": + elif media[6:] is not "None": try: dsobject = datastore.get(media[6:]) except: @@ -698,7 +690,7 @@ def show_picture(lc, media, x, y, w, h): play_dsobject(lc, dsobject, int(x), int(y), int(w), int(h)) else: pixbuf = get_pixbuf_from_journal(dsobject, int(w), int(h)) - if pixbuf != None: + if pixbuf is not None: draw_pixbuf(lc.tw.turtle, pixbuf, 0, 0, int(x), int(y), \ int(w), int(h)) dsobject.destroy() @@ -725,7 +717,7 @@ def show_description(lc, media, x, y, w, h): if media == "" or media[6:] == "": # raise logoerror("#nomedia") pass - elif media[6:] != "None": + elif media[6:] is not "None": try: dsobject = datastore.get(media[6:]) draw_text(lc.tw.turtle, \ @@ -940,7 +932,7 @@ def insert_image(lc, media, center): # convert from Turtle coordinates to screen coordinates x = lc.tw.turtle.width/2+int(lc.tw.turtle.xcor) y = lc.tw.turtle.height/2-int(lc.tw.turtle.ycor) - if center == True: + if center is True: x -= w/2 y -= h/2 if media[0:5] == 'media': @@ -974,12 +966,12 @@ def show(lc, string, center=False): elif string[0:6] == 'audio_': play_sound(lc, string) else: - if center == True: + if center is True: y -= lc.tw.textsize draw_text(lc.tw.turtle,string,x,y,lc.tw.textsize,lc.tw.turtle.width-x) elif type(string) == float or type(string) == int: string = round_int(string) - if center == True: + if center is True: y -= lc.tw.textsize draw_text(lc.tw.turtle,string,x,y,lc.tw.textsize,lc.tw.turtle.width-x) diff --git a/tawindow.py b/tawindow.py index effad0c..9fcbe52 100644 --- a/tawindow.py +++ b/tawindow.py @@ -45,6 +45,7 @@ from tasetup import * from talogo import * from taturtle import * from taproject import * +import sprite_factory try: from sugar.graphics.objectchooser import ObjectChooser except: @@ -131,8 +132,6 @@ class TurtleArtWindow(): self.sprite_list = sprites.Sprites(self.window, self.area, self.gc) self.selected_spr = None self.drag_group = None - """ - """ self.turtle = tNew(self,self.width,self.height) self.lc = lcNew(self) self.buddies = [] @@ -141,6 +140,12 @@ class TurtleArtWindow(): self.cartesian = False self.polar = False + """ + Selector-related stuff + """ + self.palette_spr = None + self.palettes = [] + self.selected_palette = None # # Public methods are called from the activity class @@ -232,6 +237,55 @@ class TurtleArtWindow(): self._run_stack(blk) return + """ + Show turtle palette + The new tasetup + Experiment with toolbar palette for block creation + """ + def show_toolbar_palette(self, n): + if self.palette_spr is None: + svg = sprite_factory.SVG() + self.palette_spr = sprites.Sprite(self.sprite_list, 0, 0, + sprite_factory.svg_str_to_pixbuf(svg.palette(self.width, 150))) + self.palette_spr.set_layer(CATEGORY_LAYER) + self.palette_spr.type = 'category' + if len(self.palettes) == 0: + for i in range(len(PALETTES)): + self.palettes.append([]) + + if n < 0 or n > len(PALETTES)-1: + return + + if self.selected_palette is not None: + print "out with the old %d" % (self.selected_palette) + for i in range(len(PALETTES[self.selected_palette])): + self.palettes[self.selected_palette][i].spr.set_layer( + HIDE_LAYER) + self.selected_palette = n + + if self.palettes[n] == []: + for i, name in enumerate(PALETTES[n]): + self.palettes[n].append(block.Block(self.block_list, + self.sprite_list, name, + 0, 0, 'selector', [], 1.5)) + self.palettes[n][i].spr.set_layer(TAB_LAYER) + self.palettes[n][i].spr.set_shape(self.palettes[n][i].shapes[0]) + # simple packing algorithm + x, y, max_width = 10, 75, 0 + for i in range(len(PALETTES[n])): + w, h = self.palettes[n][i].spr.get_dimensions() + if y+h > 150: + y = 75 + x += (max_width+10) + max_width = 0 + self.palettes[n][i].spr.move((int(x), int(y))) + y += h + if w > max_width: + max_width = w + else: + for blk in self.palettes[n]: + blk.spr.set_layer(CATEGORY_LAYER) + # # Internal methods # @@ -675,9 +729,13 @@ class TurtleArtWindow(): blk = self.block_list.spr_to_block(spr) if blk is not None: print "button press: found %s at (%d,%d)" % (blk.name, x, y) - # TODO: we can check here for type block vs type proto - self.selected_blk = blk - self._block_pressed(mask, x, y, blk) + if blk.type == 'block': + self.selected_blk = blk + self._block_pressed(mask, x, y, blk) + elif blk.type == 'selector': + blk.spr.set_shape(blk.shapes[1]) + self._new_block_from_category(blk.name, x, y+100) + blk.spr.set_shape(blk.shapes[0]) return True # Next, look for a turtle @@ -707,7 +765,7 @@ class TurtleArtWindow(): 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.selected_shape) + 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) @@ -726,7 +784,7 @@ class TurtleArtWindow(): if self.selected_blk.name == 'number': self._number_check() # Reset shape of the selected block - self.selected_blk.spr.set_shape(self.selected_blk.shape) + self.selected_blk.spr.set_shape(self.selected_blk.shapes[0]) self.selected_blk = None """ @@ -781,13 +839,13 @@ class TurtleArtWindow(): blk = self.drag_group[0] # Remove blocks by dragging them onto the category palette + # TODO: rethink when palette moves to toolbar if self.block_operation=='move' and self.category_spr.hit((x,y)): for b in self.drag_group: b.spr.hide() self.drag_group = None return # Pull a stack of new blocks off of the category palette. - # TODO: rethink when palette moves to toolbar if self.block_operation=='new': for b in self.drag_group: (bx, by) = b.spr.get_xy() @@ -903,24 +961,22 @@ class TurtleArtWindow(): if proto is None: return if proto is not 'hide': - self._new_block_from_category(proto, x, y) + self._new_block_from_category(proto.name, x, y) else: self.hideshow_palette(False) """ Make a new block. """ - def _new_block_from_category(self, proto, x, y): - if proto is None: - return True + def _new_block_from_category(self, name, x, y): # load alternative image of nop block if python code is loaded - if proto.name == 'nop' and self.nop == 'pythonloaded': + if name == 'nop' and self.nop == 'pythonloaded': pass # TODO: handle python-loaded case # newspr = Sprite(self,x-20,y-20,self.media_shapes['pythonloaded']) else: - newblk = block.Block(self.block_list, self.sprite_list, proto.name, - x-20, y-20, []) + newblk = block.Block(self.block_list, self.sprite_list, name, + x-20, y-20, 'block', []) newspr = newblk.spr newspr.set_layer(TOP_LAYER) self.dragpos = 20, 20 diff --git a/turtleart.py b/turtleart.py index fa45843..9b35f4b 100755 --- a/turtleart.py +++ b/turtleart.py @@ -53,6 +53,7 @@ Caveats: """ class TurtleMain(): def __init__(self): + self.i = 0 tw = None # make sure Sugar paths are present tapath = os.path.join(os.environ['HOME'],'.sugar','default', \ @@ -90,6 +91,10 @@ class TurtleMain(): menu = gtk.Menu() + menu_items = gtk.MenuItem(_("Turtle")) + menu.append(menu_items) + menu_items.connect("activate", self._do_turtle_cb) + menu_items.show() menu_items = gtk.MenuItem(_("Palette")) menu.append(menu_items) menu_items.connect("activate", self._do_palette_cb) @@ -144,6 +149,12 @@ class TurtleMain(): def _do_save_cb(self, widget): save_file(self.tw) + def _do_turtle_cb(self, widget): + self.tw.show_toolbar_palette(self.i) + self.i += 1 + if self.i > 6: + self.i = 0 + def _do_palette_cb(self, widget): if self.tw.palette == True: self.tw.hideshow_palette(False) -- cgit v0.9.1