From 853c5a909ad06a2040db8b8facf81fa15b4d71da Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 23 Jan 2010 21:33:11 +0000 Subject: expandable blocks :) --- diff --git a/block.py b/block.py index 60a9216..be829e3 100644 --- a/block.py +++ b/block.py @@ -65,19 +65,33 @@ class Blocks: # A class for the individual blocks # class Block: - def __init__(self, block_list, sprite_list, proto_name, x, y, labels=[], + def __init__(self, block_list, sprite_list, name, x, y, labels=[], colors=["#00FF00","#00A000"], scale=2.0): self.spr = None self.shape = None self.selected_shape = None - self.name = proto_name + self.name = name self.docks = None self.connections = None self.defaults = [] self.content = None self.primitive = None - self._new_block_from_prototype(sprite_list, proto_name, labels, colors, + self._new_block_from_factory(sprite_list, name, labels, colors, scale, x, y) + self.type = 'block' + + self._left = 0 + self._right = 0 + + if DEFAULTS.has_key(name): + self.defaults = DEFAULTS[name] + + if name in CONTENT_BLOCKS: + self.content = name + + if PRIMITIVES.has_key(name): + self.primitive = PRIMITIVES[name] + block_list.append_to_list(self) # # TODO: @@ -86,9 +100,8 @@ class Block: # debug code # etc. - def _new_block_from_prototype(self, sprite_list, name, labels, colors, - scale, x, y): - left, right = 0, 0 + def _new_block_from_factory(self, sprite_list, name, labels, colors, + scale, x, y): if len(labels) == 0: print "new block: %s (%d %d)" % (name, x, y) @@ -96,20 +109,6 @@ class Block: print "new block: %s %s (%d %d)" % (name, labels[0], x, y) svg = SVG() - if name in TURTLE_PALETTE: - svg.set_colors(TURTLE_COLORS) - elif name in PEN_PALETTE: - svg.set_colors(PEN_COLORS) - elif name in NUMBER_PALETTE: - svg.set_colors(NUMBER_COLORS) - elif name in BLOCKS_PALETTE: - svg.set_colors(BLOCKS_COLORS) - elif name in MISC_PALETTE: - svg.set_colors(MISC_COLORS) - elif name in FLOW_PALETTE: - svg.set_colors(FLOW_COLORS) - elif name in PORTFOLIO_PALETTE: - svg.set_colors(PORTFOLIO_COLORS) svg.set_scale(scale) svg.set_gradiant(True) svg.set_innie([False]) @@ -117,226 +116,292 @@ class Block: svg.set_tab(True) svg.set_slot(True) + self._make_block(name, 0, svg) + self.spr = sprites.Sprite(sprite_list, x, y, self.shape) + + + self.spr.set_margins(self._left, 0, self._right, 0) + if BLOCK_NAMES.has_key(name): + self.spr.set_label(BLOCK_NAMES[name]) + # make sure the label fits + lw = self.spr.label_width() + lwh = self.spr.label_area_dimensions() + if lw > lwh[0]: + e = lw-lwh[0] + self._make_block(name, e, svg) + self.spr.set_shape(self.shape) + + """ + # NEED TO ADD EXTRA LABELS + if len(labels) > 0: + if BLOCK_NAMES.has_key(name): + self.spr.set_label(BLOCK_NAMES[name]) + for i, label in enumerate(labels): + if i > 0: + self.spr.set_label(label, labels[i]) + """ + + def _make_block(self, name, e, svg): + self._set_colors(name, svg) + svg.set_stroke_width(STANDARD_STROKE_WIDTH) + svg.clear_docks() if name in BASIC_STYLE: - svg.expand(40,0) - self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), - ('flow', False,svg.docks[1][0], svg.docks[1][1])) + self._make_basic_style(e, svg) elif name in BASIC_STYLE_HEAD: - svg.expand(40,0) - svg.set_slot(False) - svg.set_cap(True) - self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('start', True, 0, 0), - ('flow', False, svg.docks[0][0], svg.docks[0][1])) + self._make_basic_style_head(e, svg) elif name in BASIC_STYLE_HEAD_1ARG: - svg.expand(40,0) - svg.set_innie([True]) - svg.set_slot(False) - svg.set_cap(True) - self._make_basic_block(sprite_list, svg, x, y) - 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] + self._make_basic_style_head_1arg(e, svg) elif name in BASIC_STYLE_TAIL: - svg.expand(40,0) - svg.set_tab(False) - self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), - ('unavailable', False, 0, 0)) + self._make_basic_style_tail(e, svg) elif name in BASIC_STYLE_1ARG: - svg.expand(25,0) - svg.set_innie([True]) - self._make_basic_block(sprite_list, svg, x, y) - 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] + self._make_basic_style_1arg(e, svg) elif name in BASIC_STYLE_2ARG: - svg.expand(25,0) - svg.set_innie([True,True]) - self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('flow', 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]), - ('flow', False, svg.docks[3][0], svg.docks[3][1])) - left, right = 0, svg.get_width()-svg.docks[1][0] + self._make_basic_style_1arg(e, svg) 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)) - left, right = svg.docks[1][0], 0 + self._make_box_style(e, svg) elif name in NUMBER_STYLE: - svg.expand(0,0) - svg.set_innie([True,True]) - svg.set_outie(True) - svg.set_tab(False) - svg.set_slot(False) - self._make_basic_block(sprite_list, svg, x, y) - """ - NOTE: - The "outie" is added last, so the dock order in the NUMBER_STYLE - needs to be modified. - """ - 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 + self._make_number_style(e, svg) elif name in NUMBER_STYLE_1ARG: - svg.expand(0,0) - svg.set_innie([True]) - svg.set_outie(True) - svg.set_tab(False) - svg.set_slot(False) - 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] + self._make_number_style_1arg(e, svg) elif name in NUMBER_STYLE_PORCH: - svg.expand(0,0) - svg.set_innie([True,True]) - svg.set_outie(True) - svg.set_tab(False) - svg.set_slot(False) - svg.set_porch(True) - self._make_basic_block(sprite_list, svg, x, y) - 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] + self._make_number_style_porch(e, svg) 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 + self._make_compare_style(e, svg) 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 + self._make_boolean_style(e, svg) 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 + self._make_not_style(e, svg) 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[1][0], svg.docks[1][1])) - left, right = 0, svg.get_width()-svg.docks[1][0] + self._make_flow_style(e, svg) elif name in FLOW_STYLE_1ARG: - svg.expand(25,0) - svg.set_slot(True) - svg.set_tab(True) - svg.set_innie([True]) - self._make_basic_flow(sprite_list, svg, x, y) - 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]), - ('flow', False, svg.docks[3][0], svg.docks[3][1])) - left, right = 0, svg.get_width()-svg.docks[1][0] + self._make_flow_style_1arg(e, svg) elif name in FLOW_STYLE_BOOLEAN: - svg.expand(25,0) - svg.set_slot(True) - svg.set_tab(True) - svg.set_boolean(True) - self._make_basic_flow(sprite_list, svg, x, y) - self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), - ('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] + self._make_flow_style_boolean(e, svg) else: - svg.expand(40,0) - self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), - ('flow', False,svg.docks[1][0], svg.docks[1][1])) + self._make_basic_style(e, svg) print "don't know how to create a block for %s" % (name) print self.docks print "w %d h %d" % (svg._width, svg._height) - print "l %d r %d" % (left, right) + print "l %d r %d" % (self._left, self._right) - self.spr.set_margins(left,0,right,0) - # NEED TO PROCESS DEFAULTS - if len(labels) > 0: - if BLOCK_NAMES.has_key(name): - self.spr.set_label(BLOCK_NAMES[name]) - for i, label in enumerate(labels): - if i > 0: - self.spr.set_label(label, labels[i]) + def _set_colors(self, name, svg): + if name in TURTLE_PALETTE: + svg.set_colors(TURTLE_COLORS) + elif name in PEN_PALETTE: + svg.set_colors(PEN_COLORS) + elif name in NUMBER_PALETTE: + svg.set_colors(NUMBER_COLORS) + elif name in BLOCKS_PALETTE: + svg.set_colors(BLOCKS_COLORS) + elif name in MISC_PALETTE: + svg.set_colors(MISC_COLORS) + elif name in FLOW_PALETTE: + svg.set_colors(FLOW_COLORS) + elif name in PORTFOLIO_PALETTE: + svg.set_colors(PORTFOLIO_COLORS) - self.type = 'block' + def _make_basic_style(self, e, svg): + svg.expand(40+e, 0) + self._make_basic_block(svg) + self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), + ('flow', False,svg.docks[1][0], svg.docks[1][1])) + self._left, self._right = 0, 0 - if DEFAULTS.has_key(name): - self.defaults = DEFAULTS[name] + def _make_basic_style_head(self, e, svg): + svg.expand(40+e, 0) + svg.set_slot(False) + svg.set_cap(True) + self._make_basic_block(svg) + self.docks = (('start', True, 0, 0), + ('flow', False, svg.docks[0][0], svg.docks[0][1])) + self._left, self._right = 0, 0 - if name in CONTENT_BLOCKS: - self.content = name + def _make_basic_style_head_1arg(self, e, svg): + svg.expand(40+e, 0) + svg.set_innie([True]) + svg.set_slot(False) + svg.set_cap(True) + self._make_basic_block(svg) + 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])) + self._left, self._right = 0, svg.get_innie_size() - if PRIMITIVES.has_key(name): - self.primitive = PRIMITIVES[name] + def _make_basic_style_tail(self, e, svg): + svg.expand(40+e, 0) + svg.set_tab(False) + self._make_basic_block(svg) + self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), + ('unavailable', False, 0, 0)) + + def _make_basic_style_1arg(self, e, svg): + svg.expand(25+e, 0) + svg.set_innie([True]) + self._make_basic_block(svg) + 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])) + self._left, self._right = 0, svg.get_innie_size() + + def _make_basic_style_2arg(self, e, svg): + svg.expand(25+e, 0) + svg.set_innie([True,True]) + self._make_basic_block(svg) + self.docks = (('flow', 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]), + ('flow', False, svg.docks[3][0], svg.docks[3][1])) + self._left, self._right = 0, svg.get_width()-svg.docks[1][0] - def _make_basic_block(self, sprite_list, svg, x, y): + def _make_box_style(self, e, svg): + svg.expand(60+e, 0) + self._make_basic_box(svg) + self.docks = (('number', True, svg.docks[0][0], svg.docks[0][1]), + ('unavailable', False, 0, 0)) + self._left, self._right = svg.docks[1][0], 0 + + def _make_number_style(self, e, svg): + svg.expand(e, 0) + svg.set_innie([True,True]) + svg.set_outie(True) + svg.set_tab(False) + svg.set_slot(False) + self._make_basic_block(svg) + """ + NOTE: + The "outie" is added last, so the dock order in the NUMBER_STYLE + needs to be modified. + """ + 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])) + self._left, self._right = svg.docks[2][0], 0 + + def _make_number_style_1arg(self, e, svg): + svg.expand(e, 0) + svg.set_innie([True]) + svg.set_outie(True) + svg.set_tab(False) + svg.set_slot(False) + self._make_basic_block(svg) + self.docks = (('number', True, svg.docks[1][0], svg.docks[1][1]), + ('number', False, svg.docks[0][0], svg.docks[0][1])) + self._left, self._right = svg.docks[1][0], svg.docks[1][0] + + def _make_number_style_porch(self, e, svg): + svg.expand(e, 0) + svg.set_innie([True,True]) + svg.set_outie(True) + svg.set_tab(False) + svg.set_slot(False) + svg.set_porch(True) + self._make_basic_block(svg) + 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])) + self._left, self._right = svg.docks[2][0], svg.get_width()-svg.docks[0][0] + + def _make_compare_style(self, e, svg): + svg.expand(10+e,0) + self._make_boolean_compare(svg) + 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])) + self._left, self._right = svg.get_width()-svg.docks[2][0], 0 + + def _make_boolean_style(self, e, svg): + svg.expand(10+e,0) + self._make_boolean_and_or(svg) + 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])) + self._left, self._right = svg.get_width()-svg.docks[1][0], 0 + + def _make_not_style(self, e, svg): + svg.expand(15+e, 0) + self._make_boolean_not(svg) + self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), + ('bool', False, svg.docks[1][0], svg.docks[1][1])) + self._right = svg.get_width()-svg.docks[1][0] + self._left = self._right + + def _make_flow_style(self, e, svg): + svg.expand(25+e, 0) + svg.set_slot(True) + self._make_basic_flow(svg) + self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), + ('flow', False, svg.docks[1][0], svg.docks[1][1])) + self._left, self._right = 0, svg.get_width()-svg.docks[1][0] + + def _make_flow_style_1arg(self, e, svg): + svg.expand(25+e, 0) + svg.set_slot(True) + svg.set_tab(True) + svg.set_innie([True]) + self._make_basic_flow(svg) + 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]), + ('flow', False, svg.docks[3][0], svg.docks[3][1])) + self._left, self._right = 0, svg.get_width()-svg.docks[1][0] + + def _make_flow_style_boolean(self, e, svg): + svg.expand(25+e, 0) + svg.set_slot(True) + svg.set_tab(True) + svg.set_boolean(True) + self._make_basic_flow(svg) + self.docks = (('flow', True, svg.docks[0][0], svg.docks[0][1]), + ('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])) + self._left, self._right = 0, svg.get_width()-svg.docks[1][0] + + def _make_basic_block(self, svg): self.shape = svg_str_to_pixbuf(svg.basic_block()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.basic_block()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) - def _make_basic_box(self, sprite_list, svg, x, y): + def _make_basic_box(self, svg): self.shape = svg_str_to_pixbuf(svg.basic_box()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.basic_box()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) - def _make_basic_flow(self, sprite_list, svg, x, y): + def _make_basic_flow(self, svg): self.shape = svg_str_to_pixbuf(svg.basic_flow()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.basic_flow()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) - def _make_boolean_compare(self, sprite_list, svg, x, y): + def _make_boolean_compare(self, svg): self.shape = svg_str_to_pixbuf(svg.boolean_compare()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.boolean_compare()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) - def _make_boolean_and_or(self, sprite_list, svg, x, y): + def _make_boolean_and_or(self, svg): self.shape = svg_str_to_pixbuf(svg.boolean_and_or()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.boolean_and_or()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) - def _make_boolean_not(self, sprite_list, svg, x, y): + def _make_boolean_not(self, svg): self.shape = svg_str_to_pixbuf(svg.boolean_not()) self.width = svg.get_width() self.height = svg.get_height() svg.set_stroke_width(SELECTED_STROKE_WIDTH) svg.set_stroke_color(SELECTED_COLOR) self.selected_shape = svg_str_to_pixbuf(svg.boolean_not()) - self.spr = sprites.Sprite(sprite_list, x, y, self.shape) diff --git a/constants.py b/constants.py index 6240242..2618839 100644 --- a/constants.py +++ b/constants.py @@ -80,6 +80,7 @@ PORTFOLIO_COLORS = ["#0000FF","#0000FF"] SELECTED_COLOR = "#0000FF" SELECTED_STROKE_WIDTH = 2.0 +STANDARD_STROKE_WIDTH = 1.0 # # blocks that contain media diff --git a/sprite_factory.py b/sprite_factory.py index 6ebd74e..1bfb1b1 100755 --- a/sprite_factory.py +++ b/sprite_factory.py @@ -256,6 +256,12 @@ class SVG: def get_height(self): return self._height + def get_innie_size(self): + return (self._innie_x1+self._innie_x2)*self._scale + + def clear_docks(self): + self.docks = [] + def set_scale(self, scale=1): self._scale = scale diff --git a/sprites.py b/sprites.py index dbb5267..8c64a0f 100644 --- a/sprites.py +++ b/sprites.py @@ -258,9 +258,9 @@ 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 label_area_dimensions(self): + return((self._width-self._margins[0]-self._margins[2], + self._width-self._margins[1]-self._margins[3])) def get_pixel(self, image, x, y): array = image.get_pixels() -- cgit v0.9.1