Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/block.py
diff options
context:
space:
mode:
authorWalter Bender <walter@walter-laptop.(none)>2010-01-21 22:04:23 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2010-01-21 22:04:23 (GMT)
commit90a696e3e66ad1b6a3865218fb19e1093f0337b8 (patch)
treeefe72763ddabfd0d7fe7ccdc741feec15a077ed6 /block.py
parentedce59893cdb5c317adc9f9ffbe2fdfca40f0891 (diff)
adding Turtle class
Diffstat (limited to 'block.py')
-rw-r--r--block.py138
1 files changed, 52 insertions, 86 deletions
diff --git a/block.py b/block.py
index 74b995c..c12a8e4 100644
--- a/block.py
+++ b/block.py
@@ -19,21 +19,17 @@
#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
from constants import *
import sprite_factory
import sprites
+from gettext import gettext as _
#
# A class for the list of blocks and everything they share in common
#
class Blocks:
- def __init__(self, sprite_list):
+ def __init__(self):
self.list = []
- self.sprites = sprite_list
def get_block(self, i):
if i < 0 or i > len(self.list)-1:
@@ -47,53 +43,47 @@ class Blocks:
def append_to_list(self,block):
self.list.append(block)
- def insert_in_list(self,block,i):
- if i < 0:
- self.list.insert(0, block)
- elif i > len(self.list)-1:
- self.list.append(block)
- else:
- self.list.insert(i, block)
-
def remove_from_list(self, block):
if block in self.list:
self.list.remove(block)
#
- # block and spr utilities
+ # sprite utilities
#
def spr_to_block(self, spr):
for b in self.list:
if spr == b.spr:
return b
+ return None
#
# A class for the individual blocks
#
class Block:
- def __init__(self, blocks, proto_name, x, y, labels=[],
+ def __init__(self, block_list, sprite_list, proto_name, x, y, labels=[],
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)
+ self.name = proto_name
+ self.docks = None
+ self.connections = None
+ self._new_block_from_prototype(sprite_list, proto_name, labels, colors,
+ scale, x, y)
+ block_list.append_to_list(self)
#
# TODO:
- # save arguments
- # dock and connection info
- # highlight image
# Logo code
# HTML code
# debug code
# etc.
- def _new_block_from_prototype(self, name, labels, colors, scale, x, y):
+ def _new_block_from_prototype(self, sprite_list, name, labels, colors,
+ scale, x, y):
if len(labels) == 0:
- print "%s (%d %d)" % (name, x, y)
+ print "new block: %s (%d %d)" % (name, x, y)
else:
- print "%s %s (%d %d)" % (name, labels[0], x, y)
+ print "new block: %s %s (%d %d)" % (name, labels[0], x, y)
svg = sprite_factory.SVG()
if name in TURTLE_PALETTE:
@@ -118,96 +108,72 @@ class Block:
svg.set_slot(True)
if name in BASIC_STYLE:
svg.expand(40,0)
- 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))
+ self._make_basic_block(sprite_list, svg, x, y)
+ self.docks = (('flow',True,37,5),('flow',False,37,39))
elif name in BASIC_STYLE_HEAD:
svg.expand(40,0)
svg.set_slot(False)
- 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))
+ self._make_basic_block(sprite_list, svg, x, y)
+ self.docks = (('start',True,37,0), ('flow',False,37,39))
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))
+ self._make_basic_block(sprite_list, svg, x, y)
+ self.docks = (('start',True,37,0), ('string',False,42,12),
+ ('flow',False,37,44))
elif name in BASIC_STYLE_TAIL:
svg.expand(40,0)
svg.set_tab(False)
- self._make_basic_block(svg, x, y)
+ self._make_basic_block(sprite_list, 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)
+ ex = 25
+ ey = 0
+ svg.expand(ex,ey)
svg.set_innie([True])
- 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))
+ self._make_basic_block(sprite_list, svg, x, y)
+ self.docks = (('flow',True,37,5), ('num',False,42+ex*scale,12),
+ ('flow',False,37,44+ey))
elif name in BASIC_STYLE_2ARG:
- svg.expand(20,0)
+ ex = 25
+ ey = 0
+ svg.expand(ex,ey)
svg.set_innie([True,True])
- 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))
+ self._make_basic_block(sprite_list, svg, x, y)
+ self.docks = (('flow',True,37,5), ('num',False,42+ex*scale,12),
+ ('num',False,42+ex*scale,54), ('flow',False,37,81))
elif name in BOX_STYLE:
svg.expand(50,0)
- 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))
+ self._make_basic_box(sprite_list, svg, x, y)
+ self.docks = (('num',True,0,12),('unavailable',False,105,12))
else:
svg.expand(40,0)
- self._make_basic_block(svg, x, y)
+ self._make_basic_block(sprite_list, svg, x, y)
print "don't know how to create a block for %s" % (name)
if len(labels) > 0:
- self.spr.set_label(labels[0])
+ self.spr.set_label(_(labels[0]))
for label in labels:
self.spr.set_label(label, labels.index(label))
self.type = 'block'
+ if DEFAULTS.has_key(name):
+ self.defaults = DEFAULTS[name]
+ else:
+ self.defaults = []
- def _make_basic_block(self, svg, x, y):
- self.shape = svg_str_to_pixbuf(svg.basic_block())
+ def _make_basic_block(self, sprite_list, svg, x, y):
+ self.shape = sprite_factory.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)
+ self.selected_shape =\
+ sprite_factory.svg_str_to_pixbuf(svg.basic_block())
+ self.spr = sprites.Sprite(sprite_list, x, y, self.shape)
- def _make_basic_box(self, svg, x, y):
- self.shape = svg_str_to_pixbuf(svg.basic_box())
+ def _make_basic_box(self, sprite_list, svg, x, y):
+ self.shape = sprite_factory.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
- self.spr = None
- self._new_turtle_from_prototype(orientation, scale)
- self.blocks.append_to_list(self)
- self.orientation = orientation
-
- def _new_turtle_from_prototype(self, orientation, scale):
- svg = sprite_factory.SVG()
- svg.set_scale(scale)
- svg.set_orientation(orientation)
- self.spr = sprites.Sprite(self.blocks.sprites, 0, 0,
- svg_str_to_pixbuf(svg.turtle()))
- self.type = 'turtle'
-
-#
-# Load pixbuf from SVG string
-#
-def svg_str_to_pixbuf(svg_string):
- pl = gtk.gdk.PixbufLoader('svg')
- pl.write(svg_string)
- pl.close()
- pixbuf = pl.get_pixbuf()
- return pixbuf
+ self.selected_shape = sprite_factory.svg_str_to_pixbuf(svg.basic_box())
+ self.spr = sprites.Sprite(sprite_list, x, y, self.shape)