From 52b0e32eb8f7eaa0cd905cb2cb38bcb8fdbcc42a Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 16 Jun 2012 14:07:26 +0000 Subject: more robust handling of renaming for stacks, boxes --- diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index ebe952c..4f25c5c 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -54,8 +54,14 @@ from taconstants import HORIZONTAL_PALETTE, VERTICAL_PALETTE, BLOCK_SCALE, \ CONSTANTS, EXPAND_SKIN, PROTO_LAYER from tapalette import palette_names, palette_blocks, expandable_blocks, \ block_names, content_blocks, default_values, special_names, block_styles, \ +<<<<<<< HEAD + help_strings, hidden_proto_blocks, string_or_number_args, \ + make_palette, palette_name_to_index +from talogo import LogoCode, primitive_dictionary, logoerror +======= help_strings, hidden_proto_blocks, string_or_number_args from talogo import LogoCode +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 from tacanvas import TurtleGraphics from tablock import Blocks, Block from taturtle import Turtles, Turtle @@ -67,7 +73,7 @@ from tautils import magnitude, get_load_name, get_save_name, data_from_file, \ collapsed, collapsible, hide_button_hit, show_button_hit, chooser, \ arithmetic_check, xy, find_block_to_run, find_top_block, journal_check, \ find_group, find_blk_below, data_to_string, find_start_stack, \ - get_hardware, debug_output, error_output, data_to_string + get_hardware, debug_output, error_output, data_to_string, convert from tasprite_factory import SVG, svg_str_to_pixbuf, svg_from_file from sprites import Sprites, Sprite @@ -173,6 +179,11 @@ class TurtleArtWindow(): self.coord_scale = 1 self.buddies = [] self.saved_string = '' +<<<<<<< HEAD + self._saved_action_name = '' + self._saved_box_name = '' +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 self.dx = 0 self.dy = 0 self.media_shapes = {} @@ -1189,6 +1200,18 @@ class TurtleArtWindow(): n -= 1 self.selected_blk.spr.set_label(str(n) + CURSOR) return True +<<<<<<< HEAD + elif self._action_name(self.selected_blk, hat=True): + if self.selected_blk.values[0] == _('action'): + self._new_stack_block(self.selected_blk.spr.labels[0]) + 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_box_block(self.selected_blk.spr.labels[0]) + self._update_box_names(self.selected_blk.spr.labels[0]) + +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 # Un-highlight any blocks in the stack grp = find_group(self.selected_blk) for blk in grp: @@ -1244,6 +1267,19 @@ class TurtleArtWindow(): 'block', blk.name)) > 0: self.showlabel('dupstack') return True +<<<<<<< HEAD + # If we autogenerated a stack prototype, we need + # to change its name from 'stack_foo' to 'stack' + elif blk.name[0:6] == 'stack_': + defaults = [blk.name[6:]] + name = 'stack' + # If we autogenerated a box prototype, we need + # to change its name from 'box_foo' to 'box' + elif blk.name[0:4] == 'box_': + defaults = [blk.name[4:]] + name = 'box' +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 # You cannot mix and match sensor blocks elif blk.name in ['sound', 'volume', 'pitch']: if len(self.block_list.get_similar_blocks( @@ -1327,6 +1363,88 @@ class TurtleArtWindow(): self._select_toolbar_button(spr) return True +<<<<<<< HEAD + def _update_action_names(self, name): + """ change the label on action blocks of the same name """ + if CURSOR in self._saved_action_name: + self._saved_action_name = \ + self._saved_action_name.replace(CURSOR, '') + if CURSOR in name: + name = name.replace(CURSOR, '') + for blk in self.just_blocks(): + if self._action_name(blk, hat=False): + if CURSOR in blk.spr.labels[0]: + blk.spr.labels[0] = \ + blk.spr.labels[0].replace(CURSOR, '') + if blk.spr.labels[0] == self._saved_action_name: + blk.spr.labels[0] = name + blk.values[0] = name + blk.spr.set_layer(BLOCK_LAYER) + self._change_proto_name(name, 'stack_%s' % (self._saved_action_name), + 'stack_%s' % (name)) + + def _update_box_names(self, name): + """ change the label on box blocks of the same name """ + if CURSOR in self._saved_box_name: + self._saved_box_name = \ + self._saved_box_name.replace(CURSOR, '') + if CURSOR in name: + name = name.replace(CURSOR, '') + for blk in self.just_blocks(): + if self._box_name(blk, storein=False): + 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._change_proto_name(name, 'box_%s' % (self._saved_box_name), + 'box_%s' % (name)) + + def _change_proto_name(self, name, old, new, palette='blocks'): + """ change the name of a proto block """ + for blk in self.just_protos(): + if blk.name == old: + blk.name = new + blk.spr.labels[0] = name + blk.spr.set_layer(PROTO_LAYER) + i = palette_name_to_index(palette) + if old in palette_blocks[i]: + j = palette_blocks[i].index(old) + palette_blocks[i][j] = new + return + + def _action_name(self, blk, hat=False): + """ is this a label for an action block? """ + if blk is None: + return False + if blk.name != 'string': # Ignoring int names + return False + if blk.connections[0] is None: + return False + if hat and blk.connections[0].name == 'hat': + return True + if not hat and blk.connections[0].name == 'stack': + return True + return False + + def _box_name(self, blk, storein=False): + """ is this a label for a storein block? """ + if blk is None: + return False + if blk.name != 'string': # Ignoring int names + return False + if blk.connections[0] is None: + return False + if storein and blk.connections[0].name == 'storein': + return True + if not storein and blk.connections[0].name == 'box': + return True + return False + +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 def _select_category(self, spr): """ Select a category from the toolbar """ i = self.selectors.index(spr) @@ -1401,6 +1519,32 @@ class TurtleArtWindow(): for gblk in group: gblk.spr.hide() +<<<<<<< HEAD + # if there was a named hat or storein, remove it from the proto palette + for gblk in group: + if gblk.name == 'hat' and \ + gblk.connections is not None and \ + gblk.connections[1] is not None and \ + gblk.connections[1].name == 'string' and \ + gblk.connections[1].values[0] != _('action'): + i = palette_name_to_index('blocks') + name = 'stack_%s' % (gblk.connections[1].values[0]) + if name in palette_blocks[i]: + palette_blocks[i].remove(name) + self.show_toolbar_palette(i, regenerate=True) + if gblk.name == 'storein' and \ + gblk.connections is not None and \ + gblk.connections[1] is not None and \ + gblk.connections[1].name == 'string' and \ + gblk.connections[1].values[0] != _('box'): + i = palette_name_to_index('blocks') + name = 'box_%s' % (gblk.connections[1].values[0]) + if name in palette_blocks[i]: + palette_blocks[i].remove(name) + self.show_toolbar_palette(i, regenerate=True) + +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 def _restore_all_from_trash(self): """ Restore all the blocks in the trash can. """ for blk in self.block_list.list: @@ -1473,6 +1617,8 @@ class TurtleArtWindow(): _('Share selected blocks')) if len(blk.spr.labels) > 0: self.saved_string = blk.spr.labels[0] + elif len(blk.values) > 0: + self.saved_string = blk.values[0] else: self.saved_string = '' @@ -2022,6 +2168,25 @@ class TurtleArtWindow(): if blk.name == 'number' or blk.name == 'string': self.saved_string = blk.spr.labels[0] +<<<<<<< HEAD + if self._action_name(blk, hat=True): + if CURSOR in self.saved_string: + self._saved_action_name = \ + self.saved_string.replace(CURSOR, '') + else: + self._saved_action_name = self.saved_string + else: + self._saved_action_name = '' + if self._box_name(blk, storein=True): + if CURSOR in self.saved_string: + self._saved_box_name = \ + self.saved_string.replace(CURSOR, '') + else: + self._saved_box_name = self.saved_string + else: + self._saved_box_name = '' +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 blk.spr.labels[0] += CURSOR if blk.name == 'number': bx, by = blk.spr.get_xy() @@ -2769,6 +2934,7 @@ class TurtleArtWindow(): s = self.selected_blk.spr.labels[0].replace(CURSOR, '') self.selected_blk.spr.set_label(s) self.selected_blk.values[0] = s.replace(RETURN, "\12") + self.saved_string = self.selected_blk.values[0] def load_python_code_from_file(self, fname=None, add_new_block=True): """ Load Python code from a file """ @@ -3423,6 +3589,84 @@ class TurtleArtWindow(): x, y = self._calc_image_offset('', blk.spr, w, h) blk.scale_image(x, y, w, h) +<<<<<<< HEAD + def _new_stack_block(self, name): + ''' Add a stack block to the 'blocks' palette ''' + if CURSOR in name: + name = name.replace(CURSOR, '') + if name == _('action'): + return + # Choose a palette for the new block. + palette = make_palette('blocks') + + # Create a new block prototype. + primitive_dictionary['stack'] = self._prim_stack + palette.add_block('stack_%s' % (name), + style='basic-style-1arg', + label=name, + string_or_number=True, + prim_name='stack', + logo_command='action', + default=name, + help_string=_('invokes named action stack')) + self.lc.def_prim('stack', 1, primitive_dictionary['stack'], True) + + # Regenerate the palette, which will now include the new block. + self.show_toolbar_palette(palette_name_to_index('blocks'), + regenerate=True) + + def _new_box_block(self, name): + ''' Add a box block to the 'blocks' palette ''' + if CURSOR in name: + name = name.replace(CURSOR, '') + if name == _('box'): + return + # Choose a palette for the new block. + palette = make_palette('blocks') + + # Create a new block prototype. + primitive_dictionary['box'] = self._prim_box + palette.add_block('box_%s' % (name), + style='number-style-1strarg', + label=name, + string_or_number=True, + prim_name='box', + default=name, + logo_command='box', + help_string=_('named variable (numeric value)')) + self.lc.def_prim('box', 1, + lambda self, x: primitive_dictionary['box'](x)) + + # 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: + if int(float(x)) == x: + x = int(x) + if 'stack3' + str(x) not in self.lc.stacks or \ + self.lc.stacks['stack3' + str(x)] is None: + raise logoerror("#nostack") + self.lc.icall(self.lc.evline, + self.lc.stacks['stack3' + str(x)][:]) + yield True + self.lc.procstop = False + self.lc.ireturn() + yield True + + def _prim_box(self, x): + """ Retrieve value from named box """ + if type(convert(x, float, False)) == float: + if int(float(x)) == x: + x = int(x) + try: + return self.lc.boxes['box3' + str(x)] + except KeyError: + raise logoerror("#emptybox") +======= +>>>>>>> 45bab2c428838e1f9425f0c538c1822e017a7136 def dock_dx_dy(block1, dock1n, block2, dock2n): """ Find the distance between the dock points of two blocks. """ -- cgit v0.9.1