From 223c3f863c7a08129f4c9f8aa493c36dcdb98fe7 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 25 Jul 2012 13:22:44 +0000 Subject: add custom storein blocks on palette --- (limited to 'TurtleArt') diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index fc40478..7f8e156 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -1248,7 +1248,9 @@ class TurtleArtWindow(): self._update_action_names(self.selected_blk.spr.labels[0]) elif self._box_name(self.selected_blk, storein=True): if self.selected_blk.values[0] == _('my box'): + self._new_storein_block(self.selected_blk.spr.labels[0]) self._new_box_block(self.selected_blk.spr.labels[0]) + self._update_storein_names(self.selected_blk.spr.labels[0]) self._update_box_names(self.selected_blk.spr.labels[0]) # Un-highlight any blocks in the stack grp = find_group(self.selected_blk) @@ -1335,6 +1337,12 @@ class TurtleArtWindow(): elif blk.name[0:4] == 'box_': defaults = [blk.name[4:]] name = 'box' + # If we autogenerated a storein prototype, we need + # to change its name from 'storein_foo' to 'foo' + # and label[1] from foo to box + elif blk.name[0:8] == 'storein_': + defaults = [blk.name[8:], 100] + name = 'storein' # You cannot mix and match sensor blocks elif blk.name in ['sound', 'volume', 'pitch']: if len(self.block_list.get_similar_blocks( @@ -1458,7 +1466,29 @@ class TurtleArtWindow(): self._update_proto_name(name, 'box_%s' % (self._saved_box_name), 'box_%s' % (name), 'number-style-1strarg') - def _update_proto_name(self, name, old, new, style, palette='blocks'): + def _update_storein_names(self, name): + ''' change the label on storin blocks of the same name ''' + if type(name) in [float, int]: + return + if CURSOR in name: + name = name.replace(CURSOR, '') + if type(name) == 'unicode': + name = name.encode('ascii', 'replace') + for blk in self.just_blocks(): + if self._box_name(blk, storein=True): + if CURSOR in blk.spr.labels[0]: + blk.spr.labels[0] = \ + blk.spr.labels[0].replace(CURSOR, '') + if blk.spr.labels[0] == self._saved_box_name: + blk.spr.labels[0] = name + blk.values[0] = name + blk.spr.set_layer(BLOCK_LAYER) + self._update_proto_name(name, 'storein_%s' % (self._saved_box_name), + 'storein_%s' % (name), 'basic-style-2arg', + label=1) + + def _update_proto_name(self, name, old, new, style, palette='blocks', + label=0): ''' Change the name of a proto block ''' # The name change has to happen in multiple places: # (1) The proto block itself @@ -1473,8 +1503,10 @@ class TurtleArtWindow(): new = new.encode('ascii', 'replace') if old == new: + ''' debug_output('update_proto_name: %s == %s' % (old, new), self.running_sugar) + ''' return found = False @@ -1492,7 +1524,7 @@ class TurtleArtWindow(): for blk in self.palettes[i]: if blk.name == old: blk.name = new - blk.spr.labels[0] = name + blk.spr.labels[label] = name blk.spr.set_layer(PROTO_LAYER) blk.resize() break # Should only be one proto block by this name @@ -1601,23 +1633,51 @@ class TurtleArtWindow(): gblk.connections[1] is not None and \ gblk.connections[1].name == 'string': if gblk.name == 'hat': - name = 'stack_%s' % gblk.connections[1].values[0] - style = 'basic-style-1arg' - else: - name = 'box_%s' % gblk.connections[1].values[0] - style = 'number-style-1strarg' - i = palette_name_to_index('blocks') - if name in palette_blocks[i]: - palette_blocks[i].remove(name) - for blk in self.palettes[i]: - if blk.name == name: - blk.spr.hide() - self.palettes[i].remove(blk) - self.show_toolbar_palette(i, regenerate=True) - if name in block_styles[style]: - block_styles[style].remove(name) - if name in block_names: - del block_names[name] + self._remove_palette_blocks( + 'stack_%s' % gblk.connections[1].values[0], + 'basic-style-1arg') + else: # Only if it was the only one + remove = True + similars = self.block_list.get_similar_blocks( + 'block', 'storein') + for blk in similars: + if blk.connections is not None and \ + blk.connections[1] is not None and \ + blk.connections[1].name == 'string': + if blk.connections[1].values[0] == \ + gblk.connections[1].values[0]: + remove = False + similars = self.block_list.get_similar_blocks( + 'block', 'box') + for blk in similars: + if blk.connections is not None and \ + blk.connections[1] is not None and \ + blk.connections[1].name == 'string': + if blk.connections[1].values[0] == \ + gblk.connections[1].values[0]: + remove = False + if remove: + self._remove_palette_blocks( + 'box_%s' % gblk.connections[1].values[0], + 'number-style-1strarg') + self._remove_palette_blocks( + 'storein_%s' % gblk.connections[1].values[0], + 'basic-style-2arg') + + def _remove_palette_blocks(self, name, style, palette='blocks'): + ''' Remove blocks from palette and block, style lists ''' + i = palette_name_to_index('blocks') + if name in palette_blocks[i]: + palette_blocks[i].remove(name) + for blk in self.palettes[i]: + if blk.name == name: + blk.spr.hide() + self.palettes[i].remove(blk) + self.show_toolbar_palette(i, regenerate=True) + if name in block_styles[style]: + block_styles[style].remove(name) + if name in block_names: + del block_names[name] def _restore_all_from_trash(self): ''' Restore all the blocks in the trash can. ''' @@ -3417,6 +3477,8 @@ class TurtleArtWindow(): else: i = b[4][1] - len(self._process_block_data) name = self._extra_block_data[i][1][1] + if not self._find_proto_name('storein_%s' % (name), name): + self._new_storein_block(name) if not self._find_proto_name('box_%s' % (name), name): self._new_box_block(name) @@ -3564,7 +3626,7 @@ class TurtleArtWindow(): btype == 'loadblock2arg' or btype == 'loadblock3arg': blk.add_arg() if btype == 'myfunc3arg' or btype == 'userdefined3args' or \ - btype == 'loadblock3arg': + btype == 'loadblock3arg': blk.add_arg(False) if btype in PYTHON_SKIN: if self.nop == 'pythonloaded': @@ -3974,7 +4036,7 @@ class TurtleArtWindow(): name = name.replace(CURSOR, '') if type(name) == unicode: name = name.encode('ascii', 'replace') - if name == _('box'): + if name == _('my box'): return # Choose a palette for the new block. palette = make_palette('blocks') @@ -3996,6 +4058,38 @@ class TurtleArtWindow(): self.show_toolbar_palette(palette_name_to_index('blocks'), regenerate=True) + def _new_storein_block(self, name): + ''' Add a storin block to the 'blocks' palette ''' + if type(name) in [float, int]: + return + if CURSOR in name: + name = name.replace(CURSOR, '') + if type(name) == unicode: + name = name.encode('ascii', 'replace') + if name == _('my box'): + return + # Choose a palette for the new block. + palette = make_palette('blocks') + + # Create a new block prototype. + primitive_dictionary['setbox'] = self._prim_setbox + palette.add_block('storein_%s' % (name), + style='basic-style-2arg', + label=[_('store in'), name, _('value')], + string_or_number=True, + prim_name='storeinbox', + logo_command='storeinbox', + default=[name, 100], + help_string=_('stores numeric value in named \ +variable')) + self.lc.def_prim('storeinbox', 2, + lambda self, x, y: primitive_dictionary['setbox']( + 'box3', x, y)) + + # Regenerate the palette, which will now include the new block. + self.show_toolbar_palette(palette_name_to_index('blocks'), + regenerate=True) + def _prim_stack(self, x): ''' Process a named stack ''' if type(convert(x, float, False)) == float: @@ -4021,6 +4115,18 @@ class TurtleArtWindow(): except KeyError: raise logoerror("#emptybox") + def _prim_setbox(self, name, x, val): + """ Define value of named box """ + if x is not None: + if type(convert(x, float, False)) == float: + if int(float(x)) == x: + x = int(x) + self.lc.boxes[name + str(x)] = val + self.lc.update_label_value('box', val, label=x) + else: + self.lc.boxes[name] = val + self.lc.update_label_value(name, val) + def dock_dx_dy(self, block1, dock1n, block2, dock2n): ''' Find the distance between the dock points of two blocks. ''' # Cannot dock a block to itself -- cgit v0.9.1