diff options
-rw-r--r-- | block.py | 71 | ||||
-rwxr-xr-x | block_factory.py | 381 | ||||
-rw-r--r-- | sprites.py | 244 |
3 files changed, 696 insertions, 0 deletions
diff --git a/block.py b/block.py new file mode 100644 index 0000000..e944ecc --- /dev/null +++ b/block.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +#Copyright (c) 2010 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 pygtk +pygtk.require('2.0') +import gtk +import gobject +import pango + +# +# A class for the list of blocks and everything they share in common +# +class Blocks: + def __init__(self): + self.list = [] + + def get_block(self, i): + if i < 0 or i > len(self.list)-1: + return(None) + else: + return(self.list[i]) + + def length_of_list(self): + return(len(self.list)) + + def append_to_list(self,spr): + self.list.append(spr) + + def insert_in_list(self,spr,i): + if i < 0: + self.list.insert(0, spr) + elif i > len(self.list)-1: + self.list.append(spr) + else: + self.list.insert(i, spr) + + def remove_from_list(self,spr): + if spr in self.list: + self.list.remove(spr) + +# +# A class for the individual blocks +# +class Block: + def __init__(self, blocks, prototype_style, color=["#00A000","#00FF00"], scale=1.0): + self.blocks = blocks + self._new_block_from_prototype(prototype_style, color, scale) + self.blocks.append_to_list(self) + + def _new_block_from_prototype(self, style, color, scale): + pass + + diff --git a/block_factory.py b/block_factory.py new file mode 100755 index 0000000..62eaa5b --- /dev/null +++ b/block_factory.py @@ -0,0 +1,381 @@ +#!/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 = 0 + self._slot = True + self._tab = True + self._slot_x = 12 + self._slot_y = 4 + 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_box(self): + self.set_outie(True) + svg = self._new_path(self._stroke_width/2.0+self._innie_x1+\ + self._innie_x2, self._stroke_width/2.0) + svg += self._rline_to(self._expand_x, 0) + svg += self._rline_to(0, 2*self._innie_y2+self._expand_y) + svg += self._rline_to(-self._expand_x, 0) + svg += self._rline_to(0, -self._expand_y) + svg += self._do_outie() + svg += self._close_path() + self._calculate_w_h() + svg += self._style() + svg += self._footer() + 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 + + # + # 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" % ( + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", + "<!-- Created with Python -->\n", + "<svg\n", + " xmlns:svg=\"http://www.w3.org/2000/svg\"\n", + " xmlns=\"http://www.w3.org/2000/svg\"\n", + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"", + " version=\"1.1\"\n", + " width=\"", self._width, "\"\n", + " height=\"", self._height, "\">\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" % ( + " <defs>\n <linearGradient\n id=\"linearGradient1234\">\n", + " <stop\n style=\"stop-color:#ffffff;stop-opacity:1;\"\n", + " offset=\"0\" />\n", + " <stop\n style=\"stop-color:", self._fill, + ";stop-opacity:1;\"\n", + " offset=\"1\" />\n", + " </linearGradient>\n", + " <linearGradient\n xlink:href=\"#linearGradient1234\"\n", + " id=\"linearGradient5678\"\n", + " x1=\"0\"\n", + " y1=\"", self._max_y-self._min_y, "\"\n", + " x2=\"", self._max_x-self._min_x, "\"\n", + " y2=\"", self._max_y-self._min_y, "\"\n", + " gradientUnits=\"userSpaceOnUse\" />\n </defs>\n") + else: + return "" + + def _transform(self): + return "%s%.1f%s%.1f%s" % ( + "<g\n transform=\"scale(",self._scale,",",self._scale,")\">\n") + + def _footer(self): + return " </g>\n</svg>\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): + 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 90 0 1 %.1f %.1f " % ( + self._radius, self._radius, x, y) + + def _rarc_to(self, sign_x, sign_y): + 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) + + 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 " <path d=\"m%.1f %.1f " % (x, y) + + def _close_path(self): + return "z\"\n" + + def _do_slot(self): + if self._slot is True: + return "%s%s%s" % ( + self._rline_to(0, self._slot_y), + self._rline_to(self._slot_x, 0), + self._rline_to(0, -self._slot_y)) + else: + return self._rline_to(self._slot_x, 0) + + def _do_tab(self): + if self._tab is True: + return "%s%s%s%s%s" % ( + self._rline_to(-self._stroke_width, 0), + self._rline_to(0, self._slot_y), + self._rline_to(-self._slot_x+2*self._stroke_width, 0), + self._rline_to(0, -self._slot_y), + self._rline_to(-self._stroke_width, 0)) + else: + return self._rline_to(-self._slot_x, 0) + + def _do_innie(self): + return "%s%s%s%s%s%s%s" % ( + self._rline_to(-self._innie_x1, 0), + self._rline_to(0, -self._innie_y1), + self._rline_to(-self._innie_x2, 0), + self._rline_to(0, self._innie_y2+2*self._innie_y1), + self._rline_to(self._innie_x2, 0), + self._rline_to(0, -self._innie_y1), + self._rline_to(self._innie_x1, 0)) + + def _do_outie(self): + if self._outie is not True: + return self._rline_to(0, -self._innie_y2) + return "%s%s%s%s%s%s%s%s%s" % ( + self._rline_to(0, -self._stroke_width), + self._rline_to(-self._innie_x1-2*self._stroke_width, 0), + self._rline_to(0, self._innie_y1), + self._rline_to(-self._innie_x2+2*self._stroke_width, 0), + self._rline_to(0, + -self._innie_y2-2*self._innie_y1+2*self._stroke_width), + self._rline_to(self._innie_x2-2*self._stroke_width, 0), + self._rline_to(0, self._innie_y1), + self._rline_to(self._innie_x1+2*self._stroke_width, 0), + self._rline_to(0, -self._stroke_width)) + + def _do_porch(self): + return "%s%s%s" % ( + self._rline_to(0, self._porch_y), + self._rline_to(self._porch_x-self._radius, 0), + self._rarc_to(1, 1)) + + def _calculate_w_h(self): + self._width = (self._max_x-self._min_x+self._stroke_width)*\ + self._scale + self._height = (self._max_y-self._min_y+self._stroke_width)*\ + self._scale + + def _calculate_x_y(self): + if self._outie is True: + return(self._stroke_width/2.0+self._innie_x1+self._innie_x2, + self._radius+self._stroke_width/2.0) + else: + return(self._stroke_width/2.0, self._radius+self._stroke_width/2.0) + +# +# Command-line tools for testing +# + +def open_file(datapath, filename): + return file(os.path.join(datapath, filename), "w") + +def close_file(f): + f.close() + +def generator(datapath): + svg = SVG() + f = open_file(datapath, "blob-test.svg") + svg.set_scale(1.5) + svg.expand(20,0) + svg.set_innie([True]) + svg.set_gradiant(True) + svg_str = svg.basic_block() + f.write(svg_str) + close_file(f) + + f = open_file(datapath, "box-test.svg") + svg.set_scale(1.5) + svg.expand(20,10) + svg.set_colors(["#FFA000","#A08000"]) + svg.set_gradiant(True) + svg_str = svg.basic_box() + f.write(svg_str) + close_file(f) + +def main(): + return 0 + +if __name__ == "__main__": + generator(os.path.abspath('.')) + main() + + diff --git a/sprites.py b/sprites.py new file mode 100644 index 0000000..e737057 --- /dev/null +++ b/sprites.py @@ -0,0 +1,244 @@ +# -*- coding: utf-8 -*- + +#Copyright (c) 2007-8, Playful Invention Company. +#Copyright (c) 2008-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 pygtk +pygtk.require('2.0') +import gtk +import gobject +import pango + +# +# A class for the list of sprites and everything they share in common +# +class Sprites: + def __init__(self, canvas): + self.canvas = canvas + self.area = self.canvas.window + self.gc = self.area.new_gc() + self.cm = self.gc.get_colormap() + self.list = [] + + def get_sprite(self, i): + if i < 0 or i > len(self.list)-1: + return(None) + else: + return(self.list[i]) + + def length_of_list(self): + return(len(self.list)) + + def append_to_list(self,spr): + self.list.append(spr) + + def insert_in_list(self,spr,i): + if i < 0: + self.list.insert(0, spr) + elif i > len(self.list)-1: + self.list.append(spr) + else: + self.list.insert(i, spr) + + def remove_from_list(self,spr): + if spr in self.list: + self.list.remove(spr) + + def find_sprite(self, pos): + list = self.list + list.reverse() + for spr in list: + if spr.hit(pos): return spr + return None + + def redraw_sprites(self): + for spr in self.list: + spr.draw() + +# +# A class for the individual sprites +# +class Sprite: + def __init__(self, sprites, x, y, image): + self.sprites = sprites + self.x = x + self.y = y + self.layer = 100 + self.labels = [] + self.scale = [24] + self.rescale = [True] + self.horiz_align = ["center"] + self.vert_align = ["middle"] + self.fd = None + self.bold = False + self.italic = False + self.color = None + self.set_image(image) + self.sprites.append_to_list(self) + + def set_image(self, image): + self.image = image + if isinstance(self.image,gtk.gdk.Pixbuf): + self.width = self.image.get_width() + self.height = self.image.get_height() + else: + self.width, self.height = self.image.get_size() + + def move(self, pos): + self.inval() + self.x,self.y = pos + self.inval() + + def set_shape(self, image): + self.inval() + self.set_image(image) + self.inval() + + def set_layer(self, layer): + self.sprites.remove_from_list(self) + self.layer = layer + for i in range(self.sprites.length_of_list()): + if layer < self.sprites.get_sprite(i).layer: + self.sprites.insert_in_list(self, i) + self.inval() + return + self.sprites.append_to_list(self) + self.inval() + + def set_label(self, new_label, i=0): + self._extend_labels_array(i) + if type(new_label) is str or type(new_label) is unicode: + # pango doesn't like nulls + self.labels[i] = new_label.replace("\0"," ") + else: + self.labels[i] = str(new_label) + if self.fd is None: + self.set_font('Sans') + if self.color is None: + self.color = self.sprites.cm.alloc_color('black') + self.inval() + + def _extend_labels_array(self, i): + while len(self.labels) < i+1: + self.labels.append(" ") + self.scale.append(self.scale[0]) + self.rescale.append(self.rescale[0]) + self.horiz_align.append(self.horiz_align[0]) + self.vert_align.append(self.vert_align[0]) + + def set_font(self, font): + self.fd = pango.FontDescription(font) + + def set_label_color(self, rgb): + self.color = self.sprites.cm.alloc_color(rgb) + + def set_label_attributes(self, scale, rescale=True, horiz_align="center", + vert_align="middle", i=0): + self._extend_labels_array(i) + self.scale[i] = scale + self.rescale[i] = rescale + self.horiz_align[i] = horiz_align + self.vert_align[i] = vert_align + + def hide(self): + self.inval() + self.sprites.remove_from_list(self) + + def inval(self): + self.sprites.area.invalidate_rect( + gtk.gdk.Rectangle(self.x,self.y,self.width,self.height), False) + + def draw(self): + if isinstance(self.image,gtk.gdk.Pixbuf): + self.sprites.area.draw_pixbuf( + self.sprites.gc, self.image, 0, 0, self.x, self.y) + else: + self.sprites.area.draw_drawable( + self.sprites.gc, self.image, 0, 0, self.x, self.y, -1, -1) + if len(self.labels) > 0: + self.draw_label() + + def hit(self, pos): + x, y = pos + if x < self.x: + return False + if x > self.x+self.width: + return False + if y < self.y: + return False + if y > self.y+self.height: + return False + return True + + def draw_label(self): + for i in range(len(self.labels)): + pl = self.sprites.canvas.create_pango_layout(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 self.rescale[i] is True: + self.fd.set_size(int(self.scale[i]*pango.SCALE*\ + self.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): + 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)) + pl.set_font_description(self.fd) + w = pl.get_size()[0]/pango.SCALE + j -= 1 + if self.horiz_align[i] == "center": + x = int(self.x+(self.width-w)/2) + elif self.horiz_align[i] == 'left': + x = self.x + else: # right + x = int(self.x+self.width-w) + h = pl.get_size()[1]/pango.SCALE + if self.vert_align[i] == "middle": + y = int(self.y+(self.height-h)/2) + elif self.vert_align[i] == "top": + y = self.y + else: # bottom + y = int(self.y+self.height-h) + self.sprites.gc.set_foreground(self.color) + self.sprites.area.draw_layout(self.sprites.gc, x, y, pl) + + def label_width(self): + max = 0 + for i in range(len(self.labels)): + pl = self.sprites.canvas.create_pango_layout(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 > max: + max = w + return max + + 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 |