def _vertical_layout(self, x, y, blocks): ''' Position prototypes in a vertical palette. ''' row = [] row_w = 0 max_h = 0 for blk in blocks: if not blk.get_visibility(): continue w, h = get_stack_width_and_height(blk) if x + w > PALETTE_WIDTH: # Recenter row. dx = int((PALETTE_WIDTH - row_w) / 2) for r in row: for g in find_group(r): g.spr.move_relative((dx, 0)) g.spr.save_xy = (g.spr.save_xy[0] + dx, g.spr.save_xy[1]) row = [] row_w = 0 x = 4 y += int(max_h + 3) max_h = 0 row.append(blk) row_w += (4 + w) (bx, by) = blk.spr.get_xy() dx = int(x - bx) dy = int(y - by) for g in find_group(blk): g.spr.move_relative((dx, dy)) g.spr.save_xy = g.spr.get_xy() self._float_palette(g.spr) x += int(w + 4) if h > max_h: max_h = h # Recenter last row. dx = int((PALETTE_WIDTH - row_w) / 2) for r in row: for g in find_group(r): g.spr.move_relative((dx, 0)) g.spr.save_xy = (g.spr.save_xy[0] + dx, g.spr.save_xy[1]) return x, y, max_h
def _horizontal_layout(self, x, y, blocks): ''' Position prototypes in a horizontal palette. ''' offset = self._turtle_window.toolbar_offset max_w = 0 for blk in blocks: if not blk.get_visibility(): continue w, h = get_stack_width_and_height(blk) if y + h > PALETTE_HEIGHT + offset: x += int(max_w + 3) y = offset + 3 max_w = 0 (bx, by) = blk.spr.get_xy() dx = x - bx dy = y - by for g in find_group(blk): g.spr.move_relative((int(dx), int(dy))) g.spr.save_xy = g.spr.get_xy() self._float_palette(g.spr) y += int(h + 3) if w > max_w: max_w = w return x, y, max_w
def save_python(tw): """ Find all the action stacks and turn each into Python code """ all_blocks = tw.just_blocks() blocks_name = [] for block in all_blocks: blocks_name.append(block.name) if not 'start' in blocks_name: return None blocks_covered = set() tops_of_stacks = [] for block in all_blocks: if block not in blocks_covered: top = find_top_block(block) tops_of_stacks.append(top) block_stack = find_group(top) blocks_covered.update(set(block_stack)) snippets = [_SETUP_CODE_START] for block in tops_of_stacks: stack_name = get_stack_name(block) if stack_name: pythoncode = _action_stack_to_python(block, tw, name=stack_name) snippets.append(pythoncode) snippets.append(linesep) snippets.append(_SETUP_CODE_END) return "".join(snippets)
def save_python(tw): """ Find all the action stacks and turn each into Python code """ all_blocks = tw.just_blocks() blocks_name = [] for block in all_blocks: blocks_name.append(block.name) if 'start' not in blocks_name: return None blocks_covered = set() tops_of_stacks = [] for block in all_blocks: if block not in blocks_covered: top = find_top_block(block) tops_of_stacks.append(top) block_stack = find_group(top) blocks_covered.update(set(block_stack)) snippets = [_SETUP_CODE_START] for k in plugins_in_use: snippets.append('%s = None\n' % (k.lower(),)) snippets.append('\n') for block in tops_of_stacks: stack_name = get_stack_name(block) if stack_name: pythoncode = _action_stack_to_python(block, tw, name=stack_name) snippets.append(pythoncode) snippets.append(linesep) snippets.append(_SETUP_CODE_END) return ''.join(snippets)
def update_label_value(self, name, value=None, label=None): """ Update the label of value blocks to reflect current value """ # If it is a named box, we need to match the label to the box if not self.tw.interactive_mode: return if self.tw.hide: return self.tw.display_coordinates() if value is None: for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0]) if name == 'box': argblk = block.connections[-2] dx = block.dx block.resize() if argblk is not None: # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0)) else: block.resize() elif self.update_values: if isinstance(value, float): valstring = str(round_int(value)).replace( '.', self.tw.decimal_point) else: valstring = str(value) for block in self.value_blocks_to_update[name]: if label is None: block.spr.set_label(block_names[name][0] + ' = ' + valstring) block.resize() else: argblk = block.connections[-2] # Only update if label matches if argblk is not None and argblk.spr.labels[0] == label: block.spr.set_label(block_names[name][0] + ' = ' + valstring) dx = block.dx block.resize() # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0))
def update_label_value(self, name, value=None, label=None): """ Update the label of value blocks to reflect current value """ # If it is a named box, we need to match the label to the box if not self.tw.interactive_mode: return if self.tw.hide: return self.tw.display_coordinates() if value is None: for block in self.value_blocks_to_update[name]: block.spr.set_label(block_names[name][0]) if name == 'box': argblk = block.connections[-2] dx = block.dx block.resize() if argblk is not None: # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0)) else: block.resize() elif self.update_values: if isinstance(value, float): valstring = str(round_int(value)).replace( '.', self.tw.decimal_point) else: valstring = str(value) for block in self.value_blocks_to_update[name]: if label is None: block.spr.set_label( block_names[name][0] + ' = ' + valstring) block.resize() else: argblk = block.connections[-2] # Only update if label matches if argblk is not None and argblk.spr.labels[0] == label: block.spr.set_label( block_names[name][0] + ' = ' + valstring) dx = block.dx block.resize() # Move connections over... dx = (block.dx - dx) * self.tw.block_scale drag_group = find_group(argblk) for blk in drag_group: blk.spr.move_relative((dx, 0))
def layout(self, regenerate=False, show=True): ''' Layout prototypes in a palette. ''' offset = self._turtle_window.toolbar_offset buttons = self._turtle_window.palette_button orientation = self._turtle_window.orientation w = PALETTE_WIDTH h = PALETTE_HEIGHT if orientation == HORIZONTAL_PALETTE: x, y, max_w = self._horizontal_layout(_BUTTON_SIZE, offset + _MARGIN, self.blocks) if self._trash_palette(): blocks = [] # self.blocks[:] for blk in self._turtle_window.trash_stack: blocks.append(blk) x, y, max_w = self._horizontal_layout(x + max_w, y, blocks) w = x + max_w + _BUTTON_SIZE + _MARGIN if show: buttons[2].move((w - _BUTTON_SIZE, offset)) buttons[4].move((_BUTTON_SIZE, offset)) buttons[6].move((_BUTTON_SIZE, offset)) else: x, y, max_h = self._vertical_layout( _MARGIN, offset + _BUTTON_SIZE + _MARGIN, self.blocks) if self._trash_palette(): blocks = [] # self.blocks[:] for blk in self._turtle_window.trash_stack: blocks.append(blk) x, y, max_h = self._vertical_layout(x, y + max_h, blocks) h = y + max_h + _BUTTON_SIZE + _MARGIN - offset if show: buttons[2].move((PALETTE_WIDTH - _BUTTON_SIZE, offset)) buttons[3].move((0, offset + _BUTTON_SIZE)) buttons[5].move((0, offset + _BUTTON_SIZE)) self._make_background(0, offset, w, h, regenerate) if show: for blk in self.blocks: if blk.get_visibility(): blk.spr.set_layer(PROTO_LAYER) else: blk.spr.hide() buttons[2].save_xy = buttons[2].get_xy() self._float_palette(buttons[2]) self.backgrounds[orientation].set_layer(CATEGORY_LAYER) self.display_palette_shift_buttons() if self._trash_palette(): for blk in self._turtle_window.trash_stack: for gblk in find_group(blk): gblk.spr.set_layer(PROTO_LAYER) svg = SVG() self.backgrounds[orientation].set_shape( svg_str_to_pixbuf(svg.palette(w, h)))
def layout(self, regenerate=False, show=True): ''' Layout prototypes in a palette. ''' offset = self._turtle_window.toolbar_offset buttons = self._turtle_window.palette_button orientation = self._turtle_window.orientation w = PALETTE_WIDTH h = PALETTE_HEIGHT if orientation == HORIZONTAL_PALETTE: x, y, max_w = self._horizontal_layout( _BUTTON_SIZE, offset + _MARGIN, self.blocks) if self._trash_palette(): blocks = [] # self.blocks[:] for blk in self._turtle_window.trash_stack: blocks.append(blk) x, y, max_w = self._horizontal_layout(x + max_w, y, blocks) w = x + max_w + _BUTTON_SIZE + _MARGIN if show: buttons[2].move((w - _BUTTON_SIZE, offset)) buttons[4].move((_BUTTON_SIZE, offset)) buttons[6].move((_BUTTON_SIZE, offset)) else: x, y, max_h = self._vertical_layout( _MARGIN, offset + _BUTTON_SIZE + _MARGIN, self.blocks) if self._trash_palette(): blocks = [] # self.blocks[:] for blk in self._turtle_window.trash_stack: blocks.append(blk) x, y, max_h = self._vertical_layout(x, y + max_h, blocks) h = y + max_h + _BUTTON_SIZE + _MARGIN - offset if show: buttons[2].move((PALETTE_WIDTH - _BUTTON_SIZE, offset)) buttons[3].move((0, offset + _BUTTON_SIZE)) buttons[5].move((0, offset + _BUTTON_SIZE)) self._make_background(0, offset, w, h, regenerate) if show: for blk in self.blocks: if blk.get_visibility(): blk.spr.set_layer(PROTO_LAYER) else: blk.spr.hide() buttons[2].save_xy = buttons[2].get_xy() self._float_palette(buttons[2]) self.backgrounds[orientation].set_layer(CATEGORY_LAYER) self.display_palette_shift_buttons() if self._trash_palette(): for blk in self._turtle_window.trash_stack: for gblk in find_group(blk): gblk.spr.set_layer(PROTO_LAYER) svg = SVG() self.backgrounds[orientation].set_shape( svg_str_to_pixbuf(svg.palette(w, h)))
def hide(self): ''' Hide the palette. ''' for background in self.backgrounds: if background is not None: background.hide() for blk in self.blocks: blk.spr.hide() self._hide_palette_shift_buttons() if self._trash_palette(): for blk in self._turtle_window.trash_stack: for gblk in find_group(blk): gblk.spr.hide() self.visible = False
def move(self, x, y): ''' Move the palette. ''' buttons = self._turtle_window.palette_button for blk in self.blocks: blk.spr.move((x + blk.spr.save_xy[0], y + blk.spr.save_xy[1])) for button in buttons: button.move((x + button.save_xy[0], y + button.save_xy[1])) for spr in self.backgrounds: if spr is not None: spr.move((x + spr.save_xy[0], y + spr.save_xy[1])) if self._trash_palette(): for blk in self._turtle_window.trash_stack: for gblk in find_group(blk): gblk.spr.move((x + gblk.spr.save_xy[0], y + gblk.spr.save_xy[1]))
def save_python(tw): """ Find all the action stacks and turn each into Python code """ all_blocks = tw.just_blocks() blocks_covered = set() tops_of_stacks = [] for block in all_blocks: if block not in blocks_covered: top = find_top_block(block) tops_of_stacks.append(top) block_stack = find_group(top) blocks_covered.update(set(block_stack)) snippets = [_SETUP_CODE_START] for block in tops_of_stacks: stack_name = get_stack_name(block) if stack_name: pythoncode = _action_stack_to_python(block, tw, name=stack_name) snippets.append(pythoncode) snippets.append(linesep) snippets.append(_SETUP_CODE_END) return "".join(snippets)