From edce59893cdb5c317adc9f9ffbe2fdfca40f0891 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 21 Jan 2010 16:23:34 +0000 Subject: added selection mask --- diff --git a/block.py b/block.py index 02fcaf4..74b995c 100644 --- a/block.py +++ b/block.py @@ -75,6 +75,8 @@ class Block: colors=["#00FF00","#00A000"], scale=2.0): self.blocks = blocks self.spr = None + self.shape = None + self.selected_shape = None self._new_block_from_prototype(proto_name, labels, colors, scale, x, y) self.blocks.append_to_list(self) # @@ -102,66 +104,66 @@ class Block: 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]) + svg.set_outie(False) + svg.set_tab(True) + svg.set_slot(True) if name in BASIC_STYLE: svg.expand(40,0) - svg.set_innie([False]) - svg.set_outie(False) - svg.set_tab(True) - svg.set_slot(True) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_block())) + self._make_basic_block(svg, x, y) + self.docks = (('flow',True,37,5),('flow',False,37,44)) print "created new basic block: %s" % (str(self.spr)) elif name in BASIC_STYLE_HEAD: svg.expand(40,0) - svg.set_innie([False]) - svg.set_outie(False) - svg.set_tab(True) svg.set_slot(False) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_block())) + self._make_basic_block(svg, x, y) + self.docks = (('start',True,50,0), ('flow',False,49,55)) + print "created new basic block head: %s" % (str(self.spr)) + elif name in BASIC_STYLE_HEAD_1ARG: + svg.expand(40,0) + svg.set_slot(False) + self._make_basic_block(svg, x, y) + self.docks = (('start',True,50,0), ('string',False,21,38), + ('flow',False,75,75)) print "created new basic block head: %s" % (str(self.spr)) elif name in BASIC_STYLE_TAIL: svg.expand(40,0) - svg.set_innie([False]) - svg.set_outie(False) svg.set_tab(False) - svg.set_slot(True) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_block())) + self._make_basic_block(svg, x, y) + self.docks = (('flow',True,37,5),('unavailable',False,0,0)) print "created new basic block tail: %s" % (str(self.spr)) elif name in BASIC_STYLE_1ARG: svg.expand(20,0) svg.set_innie([True]) - svg.set_outie(False) - svg.set_tab(True) - svg.set_slot(True) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_block())) + self._make_basic_block(svg, x, y) + self.docks = (('flow',True,37,5), ('num',False,74,21), + ('flow',False,37,44)) print "created new basic block 1 arg: %s" % (str(self.spr)) elif name in BASIC_STYLE_2ARG: svg.expand(20,0) svg.set_innie([True,True]) - svg.set_outie(False) - svg.set_tab(True) - svg.set_slot(True) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_block())) + self._make_basic_block(svg, x, y) + self.docks = (('flow',True,37,5), ('num',False,74,21), + ('num',False,74,58), ('flow',False,37,81)) print "created new basic block 2 args: %s" % (str(self.spr)) elif name in BOX_STYLE: svg.expand(50,0) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_box())) + self._make_basic_box(svg, x, y) + self.docks = (('num',True,0,12),('numend',False,105,12)) print "created new box block: %s" % (str(self.spr)) else: - svg.expand(50,0) - self.spr = sprites.Sprite(self.blocks.sprites, x, y, - svg_str_to_pixbuf(svg.basic_box())) + svg.expand(40,0) + self._make_basic_block(svg, x, y) print "don't know how to create a block for %s" % (name) - return - if len(labels) > 0: self.spr.set_label(labels[0]) for label in labels: @@ -169,6 +171,20 @@ class Block: self.type = 'block' + def _make_basic_block(self, svg, x, y): + self.shape = svg_str_to_pixbuf(svg.basic_block()) + 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(self.blocks.sprites, x, y, self.shape) + + def _make_basic_box(self, svg, x, y): + self.shape = svg_str_to_pixbuf(svg.basic_box()) + 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(self.blocks.sprites, x, y, self.shape) + class Turtle: def __init__(self, blocks, orientation=0, scale=1.0): self.blocks = blocks diff --git a/block_factory.py b/block_factory.py deleted file mode 100755 index 061d624..0000000 --- a/block_factory.py +++ /dev/null @@ -1,542 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -#Copyright (c) 2009,10 Walter Bender - -#Permission is hereby granted, free of charge, to any person obtaining a copy -#of this software and associated documentation files (the "Software"), to deal -#in the Software without restriction, including without limitation the rights -#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -#copies of the Software, and to permit persons to whom the Software is -#furnished to do so, subject to the following conditions: - -#The above copyright notice and this permission notice shall be included in -#all copies or substantial portions of the Software. - -#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -#THE SOFTWARE. - -import os -from gettext import gettext as _ - -class SVG: - def __init__(self): - self._x = 0 - self._y = 0 - self._min_x = 0 - self._min_y = 0 - self._max_x = 0 - self._max_y = 0 - self._width = 0 - self._height = 0 - self._scale = 1 - self._radius = 8 - self._stroke_width = 1 - self._innie = [False] - self._outie = False - self._innie_x1 = 4 - self._innie_y1 = 3 - self._innie_x2 = 4 - self._innie_y2 = 4 - self._innie_spacer = 9 - self._slot = True - self._tab = True - self._bool = False - self._slot_x = 12 - self._slot_y = 2 - self._porch = False - self._porch_x = self._innie_x1+self._innie_x2+4*self._stroke_width - self._porch_y = self._innie_y1+self._innie_y2+4*self._stroke_width - self._expand_x = 0 - self._expand_y = 0 - self._fill = "#00FF00" - self._stroke = "#00A000" - self._gradiant = False - - def basic_block(self): - (x, y) = self._calculate_x_y() - svg = self._new_path(x, y) - svg += self._rarc_to(1, -1) - svg += self._do_slot() - svg += self._rline_to(self._expand_x, 0) - xx = self._x - svg += self._rarc_to(1, 1) - for i in range(len(self._innie)): - if self._innie[i] is True: - svg += self._do_innie() - if i==0 and self._porch is True: - svg += self._do_porch() - elif len(self._innie)-1 > i: - svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer) - svg += self._rline_to(0, self._expand_y) - svg += self._rarc_to(-1, 1) - svg += self._line_to(xx, self._y) - svg += self._rline_to(-self._expand_x, 0) - svg += self._do_tab() - svg += self._rarc_to(-1, -1) - svg += self._rline_to(0, -self._expand_y) - if True in self._innie: - svg += self._line_to(x, - self._radius+self._innie_y2+self._stroke_width) - svg += self._do_outie() - svg += self._close_path() - self._calculate_w_h() - svg += self._style() - svg += self._footer() - return self._header() + svg - - def basic_flow(self): - (x, y) = self._calculate_x_y() - svg = self._new_path(x, y) - svg += self._rarc_to(1, -1) - svg += self._do_slot() - svg += self._rline_to(self._expand_x, 0) - xx = self._x - svg += self._rarc_to(1, 1) - for i in range(len(self._innie)): - if self._innie[i] is True: - svg += self._do_innie() - svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer) - if self._bool is True: - svg += self._rline_to(0,self._radius/2.0) - svg += self._do_boolean() - svg += self._rline_to(0,self._radius/2.0) - svg += self._rline_to(self._radius+self._slot_x, 0) - svg += self._rarc_to(1,1) - svg += self._rline_to(-self._radius,0) - svg += self._do_tab() - svg += self._rline_to(-self._radius, 0) - svg += self._rline_to(0, self._expand_y) - svg += self._rarc_to(-1, 1) - svg += self._line_to(xx, self._y) - svg += self._rline_to(-self._expand_x, 0) - svg += self._do_tab() - svg += self._rarc_to(-1, -1) - svg += self._rline_to(0, -self._expand_y) - if True in self._innie: - svg += self._line_to(x, - self._radius+self._innie_y2+self._stroke_width) - svg += self._do_outie() - svg += self._close_path() - self._calculate_w_h() - svg += self._style() - svg += self._footer() - return self._header() + svg - - def basic_box(self): - self.set_outie(True) - x = self._stroke_width/2.0+self._innie_x1+self._innie_x2 - svg = self._new_path(x, self._stroke_width/2.0) - svg += self._rline_to(self._expand_x, 0) - svg += self._rline_to(0, 2*self._radius+self._innie_y2+self._expand_y) - svg += self._rline_to(-self._expand_x, 0) - svg += self._line_to(x, self._radius+self._innie_y2+\ - self._stroke_width/2.0) - svg += self._do_outie() - svg += self._close_path() - self._calculate_w_h() - svg += self._style() - svg += self._footer() - return self._header() + svg - - def boolean_and_or(self): - svg = self._start_boolean(self._stroke_width/2.0, - self._radius*5.5+self._stroke_width/2.0+\ - self._innie_y2+self._innie_spacer) - svg += self._rline_to(0,-self._radius*3.5-self._innie_y2-\ - self._innie_spacer-self._stroke_width) - svg += self._rarc_to(1, -1) - svg += self._rline_to(self._radius/2.0+self._expand_x, 0) - svg += self._rline_to(0,self._radius/2.0) - svg += self._do_boolean() - svg += self._rline_to(0,self._radius*1.5+self._innie_y2+\ - self._innie_spacer) - svg += self._do_boolean() - svg += self._rline_to(0,self._radius/2.0) - svg += self._end_boolean() - return self._header() + svg - - def boolean_not(self): - svg = self._start_boolean(self._stroke_width/2.0, - self._radius*2.0+self._stroke_width/2.0) - svg += self._rline_to(0,-self._stroke_width) - svg += self._rarc_to(1, -1) - svg += self._rline_to(self._radius/2.0+self._expand_x, 0) - svg += self._rline_to(0,self._radius/2.0) - svg += self._do_boolean() - svg += self._rline_to(0,self._radius/2.0) - svg += self._end_boolean() - return self._header() + svg - - def boolean_compare(self): - yoffset = self._radius*2+2*self._innie_y2+\ - self._innie_spacer+self._stroke_width/2.0 - if self._porch is True: - yoffset += self._porch_y - svg = self._start_boolean(self._stroke_width/2.0, yoffset) - yoffset = -2*self._innie_y2-self._innie_spacer-self._stroke_width - if self._porch is True: - yoffset -= self._porch_y - svg += self._rline_to(0, yoffset) - svg += self._rarc_to(1, -1) - svg += self._rline_to(self._radius/2.0+self._expand_x, 0) - svg += self._rline_to(0,self._radius) - xx = self._x - svg += self._do_innie() - if self._porch is True: - svg += self._do_porch() - else: - svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer) - svg += self._do_innie() - svg += self._rline_to(0, self._radius) - svg += self._line_to(xx, self._y) - svg += self._end_boolean() - return self._header() + svg - - # - # Utility methods - # - - def set_scale(self, scale=1): - self._scale = scale - - def expand(self, w=0, h=0): - self._expand_x = w - self._expand_y = h - - def set_stroke_width(self, stroke_width=1.5): - self._stroke_width = stroke_width - self._calc_porch_params() - - def set_colors(self, colors=["#00FF00","#00A000"]): - self._fill = colors[0] - self._stroke = colors[1] - - def set_gradiant(self, flag=False): - self._gradiant = flag - - def set_innie(self, innie_array=[False]): - self._innie = innie_array - - def set_outie(self, flag=False): - self._outie = flag - - def set_slot(self, flag=True): - self._slot = flag - - def set_tab(self, flag=True): - self._tab = flag - - def set_porch(self, flag=False): - self._porch = flag - - def set_boolean(self, flag=False): - self._bool = flag - - # - # Exotic methods - # - - def set_radius(self, radius=8): - self._radius = radius - - def set_innie_params(self, x1=4, y1=3, x2=4, y2=4): - self._innie_x1 = x1 - self._innie_y1 = y1 - self._innie_x2 = x2 - self._innie_y2 = y2 - self._calc_porch_params() - - def set_innie_spacer(self, innie_spacer = 0): - self._innie_spacer = innie_spacer - - def set_slot_params(self, x=12, y=4): - self._slot_x = x - self._slot_y = y - - def _calc_porch_params(self): - self._porch_x = self._innie_x1+self._innie_x2+4*self._stroke_width - self._porch_y = self._innie_y1+self._innie_y2+4*self._stroke_width - - # - # SVG helper methods - # - - def _header(self): - return "%s%s%s%s%s%s%s%s%.1f%s%s%.1f%s%s%s" % ( - "\n", - "\n", - "\n", - self._defs(), - self._transform()) - - def _defs(self): - if self._gradiant is True: - return "%s%s%s%s%s%s%s%s%s%s%s%s%.1f%s%s%.1f%s%s%.1f%s%s" % ( - " \n \n", - " \n", - " \n", - " \n", - " \n \n") - else: - return "" - - def _transform(self): - return "%s%.1f%s%.1f%s" % ( - "\n") - - def _footer(self): - return " \n\n" - - def _style(self): - if self._gradiant is True: - fill = "url(#linearGradient5678)" - else: - fill = self._fill - return "%s%s;%s%s;%s%.1f;%s%s" % ( - " style=\"fill:",fill, - "fill-opacity:1;stroke:",self._stroke, - "stroke-width:",self._stroke_width, - "stroke-linecap:square;", - "stroke-opacity:1;\" />\n") - - def _check_min_max(self): - if self._x < self._min_x: - self._min_x = self._x - if self._y < self._min_y: - self._min_y = self._y - if self._x > self._max_x: - self._max_x = self._x - if self._y > self._max_y: - self._max_y = self._y - - def _line_to(self, x, y): - if self._x == x and self._y == y: - return "" - else: - self._x = x - self._y = y - self._check_min_max() - return "L %.1f %.1f " % (x, y) - - def _rline_to(self, dx, dy): - if dx == 0 and dy == 0: - return "" - else: - return self._line_to(self._x+dx, self._y+dy) - - def _arc_to(self, x, y, a=90, l=0, s=1): - if self._radius == 0: - return self._line_to(x, y) - else: - self._x = x - self._y = y - self._check_min_max() - return "A %.1f %.1f %.1f %d %d %.1f %.1f " % ( - self._radius, self._radius, a, l, s, x, y) - - def _rarc_to(self, sign_x, sign_y, a=90, l=0, s=1): - if self._radius == 0: - return "" - else: - x = self._x + sign_x*self._radius - y = self._y + sign_y*self._radius - return self._arc_to(x, y, a, l, s) - - def _new_path(self, x, y): - self._min_x = x - self._min_y = y - self._max_x = x - self._max_y = y - self._x = x - self._y = y - return "