Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2012-07-22 16:00:04 (GMT)
committer Walter Bender <walter.bender@gmail.com>2012-07-22 16:00:04 (GMT)
commite819f8968fc798e8c6a431a9f9a9836cb54e95a1 (patch)
treeb53218b0ba92021209ed305bd4f368729037ec51 /plugins
parent4a22be29826c721bdb25ca013ff1b8ad86d609a3 (diff)
sync with changes for v152
Diffstat (limited to 'plugins')
-rw-r--r--plugins/turtle_blocks_extras/turtle_blocks_extras.py190
1 files changed, 169 insertions, 21 deletions
diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
index 88d4a60..5871be1 100644
--- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py
+++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
@@ -22,14 +22,16 @@ import os
from gettext import gettext as _
from plugins.plugin import Plugin
-from TurtleArt.tapalette import make_palette, define_logo_function
+from TurtleArt.tapalette import make_palette, define_logo_function, \
+ block_names, block_primitives, special_names, content_blocks, \
+ palette_name_to_index, palette_names
from TurtleArt.talogo import primitive_dictionary, logoerror, \
media_blocks_dictionary
from TurtleArt.taconstants import DEFAULT_SCALE, ICON_SIZE, CONSTANTS, \
MEDIA_SHAPES, SKIN_PATHS, BLOCKS_WITH_SKIN, PYTHON_SKIN, \
- PREFIX_DICTIONARY, VOICES
+ PREFIX_DICTIONARY, VOICES, MACROS
from TurtleArt.tautils import convert, round_int, debug_output, get_path, \
- data_to_string
+ data_to_string, find_group, image_to_base64
from TurtleArt.tajail import myfunc, myfunc_import
@@ -114,6 +116,15 @@ boolean operators from Numbers palette'))
help_string=_('do-until-True operator that uses \
boolean operators from Numbers palette'))
+ primitive_dictionary['clamp'] = self._prim_clamp
+ palette.add_block('sandwichclamp',
+ style='clamp-style-collapsible',
+ label=' ',
+ special_name=_('top'),
+ prim_name='clamp',
+ help_string=_('top of a collapsible stack'))
+ self.tw.lc.def_prim('clamp', 1, primitive_dictionary['clamp'], True)
+
def _media_palette(self):
palette = make_palette('media',
@@ -671,15 +682,6 @@ module found in the Journal'))
label=_('turtle shell'),
help_string=_("put a custom 'shell' on the turtle"))
- primitive_dictionary['clamp'] = self._prim_clamp
- palette.add_block('sandwichclamp',
- style='clamp-style-collapsible',
- label=' ',
- special_name=_('top'),
- prim_name='clamp',
- help_string=_('top of a collapsible stack'))
- self.tw.lc.def_prim('clamp', 1, primitive_dictionary['clamp'], True)
-
palette.add_block('sandwichclampcollapsed',
hidden=True,
style='clamp-style-collapsed',
@@ -688,6 +690,49 @@ module found in the Journal'))
special_name=_('top'),
help_string=_('top of a collapsed stack'))
+ primitive_dictionary['loadblock'] = self._prim_load_block
+ palette.add_block('loadblock',
+ style='basic-style-var-arg',
+ label=_('load'),
+ prim_name='loadblock',
+ default=_('forward'),
+ help_string=_('loads a block'))
+ self.tw.lc.def_prim('loadblock', 1,
+ lambda self, x: primitive_dictionary['loadblock'](x))
+
+ palette.add_block('loadblock2arg',
+ style='basic-style-var-arg',
+ hidden=True,
+ label=_('load'),
+ prim_name='loadblock2',
+ string_or_number=True,
+ default=[_('forward'), 100],
+ help_string=_('loads a block'))
+ self.tw.lc.def_prim('loadblock2', 2,
+ lambda self, x, y: primitive_dictionary['loadblock']([x, y]))
+
+ palette.add_block('loadblock3arg',
+ style='basic-style-var-arg',
+ hidden=True,
+ label=_('load'),
+ string_or_number=True,
+ prim_name='loadblock3',
+ default=[_('setxy'), 0, 0],
+ help_string=_('loads a block'))
+ self.tw.lc.def_prim('loadblock3', 3,
+ lambda self, x, y, z: primitive_dictionary['loadblock']([x, y, z]))
+
+ primitive_dictionary['loadpalette'] = self._prim_load_palette
+ palette.add_block('loadpalette',
+ style='basic-style-1arg',
+ string_or_number=True,
+ label=_('palette'),
+ prim_name='loadpalette',
+ default=_('turtle'),
+ help_string=_('selects a palette'))
+ self.tw.lc.def_prim('loadpalette', 1,
+ lambda self, x: primitive_dictionary['loadpalette'](x))
+
def _portfolio_palette(self):
palette = make_palette('portfolio',
@@ -1103,15 +1148,6 @@ bullets'))
self.tw.lc.heap.append(val)
self.tw.lc.update_label_value('pop', val)
- '''
- def _prim_keyboard_num(self):
- """ Return a number when a number is typed. """
- if self.tw.lc.keyboard < 48 or self.tw.lc.keyboard > 57:
- return -1
- else:
- return self.tw.lc.keyboard - 48
- '''
-
def _prim_readpixel(self):
""" Read r, g, b, a from the canvas and push b, g, r to the stack """
r, g, b, a = self.tw.canvas.get_pixel()
@@ -1157,6 +1193,24 @@ bullets'))
if pen_state:
self.tw.canvas.setpen(True)
+ if self.tw.sharing():
+ if self.tw.running_sugar:
+ tmp_path = get_path(self.tw.activity, 'instance')
+ else:
+ tmp_path = '/tmp'
+ tmp_file = os.path.join(get_path(self.tw.activity, 'instance'),
+ 'tmpfile.png')
+ pixbuf.save(tmp_file, 'png', {'quality': '100'})
+ data = image_to_base64(tmp_file, tmp_path)
+ height = pixbuf.get_height()
+ width = pixbuf.get_width()
+ event = 'R|%s' % (data_to_string([self.tw.nick,
+ [round_int(width),
+ round_int(height),
+ data]]))
+ gobject.idle_add(self.tw.send_event, event)
+ os.remove(tmp_file)
+
def _prim_save_picture(self, name):
""" Save canvas to file as PNG """
self.tw.save_as_image(name)
@@ -1403,6 +1457,100 @@ bullets'))
self.tw.lc.ireturn()
yield True
+ def _prim_load_block(self, blkname):
+ ''' Load a block on to the canvas '''
+ # Place the block at the active turtle (x, y) and move the turtle
+ # into position to place the next block in the stack.
+ # TODO: Add expandable argument
+ x, y = self.tw.active_turtle.get_xy()
+ if type(blkname) == type([]):
+ name = blkname[0]
+ if len(blkname) > 1:
+ value = blkname[1:]
+ dy = int(self._find_block(name, x, y, value))
+ else:
+ dy = int(self._find_block(name, x, y))
+ else:
+ name = blkname
+ if name == 'delete':
+ for blk in self.tw.just_blocks():
+ if blk.status == 'load block':
+ blk.type = 'trash'
+ blk.spr.hide()
+ dy = 0
+ else:
+ dy = int(self._find_block(name, x, y))
+
+ # Reposition turtle to end of flow
+ self.tw.canvas.ycor -= dy
+ self.tw.canvas.move_turtle()
+
+ def _make_block(self, name, x, y, defaults):
+ if defaults is None:
+ self.tw._new_block(name, x, y, defaults)
+ else:
+ for i, v in enumerate(defaults):
+ if type(v) == float and int(v) == v:
+ defaults[i] = int(v)
+ self.tw._new_block(name, x, y, defaults)
+
+ # Find the block we just created and attach it to a stack.
+ self.tw.drag_group = None
+ spr = self.tw.sprite_list.find_sprite((x, y))
+ if spr is not None:
+ blk = self.tw.block_list.spr_to_block(spr)
+ if blk is not None:
+ self.tw.drag_group = find_group(blk)
+ for b in self.tw.drag_group:
+ b.status = 'load block'
+ self.tw._snap_to_dock()
+
+ # Disassociate new block from mouse.
+ self.tw.drag_group = None
+ return blk.docks[-1][3] # * self.tw.scale
+
+ def _find_block(self, blkname, x, y, defaults=None):
+ """ Create a new block. It is a bit more work than just calling
+ _new_block(). We need to:
+ (1) translate the label name into the internal block name;
+ (2) 'dock' the block onto a stack where appropriate; and
+ (3) disassociate the new block from the mouse. """
+ for name in block_names:
+ # Translate label name into block/prim name.
+ if blkname in block_names[name]:
+ if (name in block_primitives and \
+ block_primitives[name] == name) or \
+ name in content_blocks:
+ return self._make_block(name, x, y, defaults)
+ elif blkname in block_names:
+ return self._make_block(blkname, x, y, defaults)
+ for name in special_names:
+ # Translate label name into block/prim name.
+ if blkname in special_names[name]:
+ return self._make_block(name, x, y, defaults)
+ # Check for a macro
+ if blkname in MACROS:
+ self.tw.new_macro(blkname, x, y)
+ return 0 # Fix me: calculate flow position
+ # Block not found
+ raise logoerror("#syntaxerror")
+ return -1
+
+ def _prim_load_palette(self, arg):
+ ''' Select a palette '''
+ if type(arg) in [int, float]:
+ if int(arg) < 0 or int(arg) > len(palette_names):
+ raise logoerror("#syntaxerror")
+ else:
+ self.tw.show_toolbar_palette(int(arg))
+ else:
+ if type(arg) == unicode:
+ arg = arg.encode('ascii', 'replace')
+ if arg in palette_names:
+ self.tw.show_toolbar_palette(palette_name_to_index(arg))
+ else:
+ raise logoerror("#syntaxerror")
+
# Deprecated blocks
def _prim_t1x1(self, title, media):