com.google.blockly.android.ui.BlockGroup

Here are the examples of the java api class com.google.blockly.android.ui.BlockGroup taken from open source projects.

1. BlocklyController#bumpBlock()

View license
public void bumpBlock(Connection staticConnection, Connection impingingConnection) {
    Block rootBlock = impingingConnection.getBlock().getRootBlock();
    BlockGroup impingingBlockGroup = mHelper.getRootBlockGroup(rootBlock);
    int maxSnapDistance = mHelper.getMaxSnapDistance();
    int dx = (staticConnection.getPosition().x + maxSnapDistance) - impingingConnection.getPosition().x;
    int dy = (staticConnection.getPosition().y + maxSnapDistance) - impingingConnection.getPosition().y;
    rootBlock.setPosition(rootBlock.getPosition().x + dx, rootBlock.getPosition().y + dy);
    if (mWorkspaceView != null && impingingBlockGroup != null) {
        // Update UI
        impingingBlockGroup.bringToFront();
        impingingBlockGroup.updateAllConnectorLocations();
        mWorkspaceView.requestLayout();
    }
}

2. BlocklyController#addBlockFromTrash()

View license
/**
     * Moves a block (and the child blocks connected to it) from the trashed blocks (removing it
     * from the deleted blocks list), back to the workspace as a root block, including the
     * BlockGroup and other views in the TrashFragment.
     *
     * @param previouslyTrashedBlock The block in the trash to be moved back to the workspace.
     * @return The BlockGroup in the Workspace for the moved block.
     *
     * @throws IllegalArgumentException If {@code trashedBlock} is not found in the trashed blocks.
     */
public BlockGroup addBlockFromTrash(@NonNull Block previouslyTrashedBlock) {
    BlockGroup bg = mHelper.getParentBlockGroup(previouslyTrashedBlock);
    if (bg != null) {
        ViewParent parent = bg.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(bg);
        }
        bg.setTouchHandler(mTouchHandler);
    }
    mWorkspace.addBlockFromTrash(previouslyTrashedBlock);
    if (mWorkspaceView != null) {
        if (bg == null) {
            bg = mViewFactory.buildBlockGroupTree(previouslyTrashedBlock, mWorkspace.getConnectionManager(), mTouchHandler);
        }
        mWorkspaceView.addView(bg);
    }
    if (mTrashFragment != null) {
        mTrashFragment.onBlockRemovedFromTrash(previouslyTrashedBlock);
    }
    return bg;
}

3. BlocklyController#unlinkViews()

View license
/**
     * Recursively unlinks models from the views, and disconnects the view tree including clearing
     * the parent {@link BlockGroup}.
     *
     * @param block The root block of the tree to unlink.
     */
public void unlinkViews(Block block) {
    BlockView view = mHelper.getView(block);
    if (view == null) {
        // No view to unlink.
        return;
    }
    // Verify the block has no parent.  Only unlink complete block sequences.
    if (block.getParentBlock() != null) {
        throw new IllegalArgumentException("Expected unconnected/root block; only allowed to unlink complete block trees");
    }
    BlockGroup parentGroup = mHelper.getParentBlockGroup(block);
    if (parentGroup != null) {
        if (parentGroup.getChildAt(0) != mHelper.getView(block)) {
            // If it doesn't have a parent, this Block view should have been first.
            throw new IllegalStateException("BlockGroup does not match model");
        }
        parentGroup.unlinkModel();
    } else {
        if (view != null) {
            view.unlinkModel();
        }
    }
}

4. BlocklyController#connectAfter()

View license
/**
     * Connect a block after another block in the same block group.  Updates views as necessary.  If
     * the superior block already has a "next" block, splices the inferior block between the
     * superior block and its "next" block.
     * <p/>
     * Assumes that the inferior's previous connection is disconnected. Assumes that inferior's
     * blockGroup doesn't currently live at the root level.
     *
     * @param superior The {@link Block} after which the inferior block is connecting.
     * @param inferior The {@link Block} to be connected as the superior block's "next" block.
     */
private void connectAfter(Block superior, Block inferior) {
    // Get the relevant BlockGroups.  Either may be null if view is not initialized.
    BlockGroup superiorBlockGroup = mHelper.getParentBlockGroup(superior);
    BlockGroup inferiorBlockGroup = mHelper.getParentBlockGroup(inferior);
    Block remainderBlock = superior.getNextBlock();
    BlockGroup remainderGroup = null;
    // To splice between two blocks, just need another call to connectAfter.
    if (remainderBlock != null) {
        if (remainderBlock.isShadow()) {
            // If there was a shadow connected just remove it
            removeBlockTree(remainderBlock);
            remainderBlock = null;
        } else {
            // Disconnect the remainder and save it for later
            remainderGroup = (superiorBlockGroup == null) ? null : superiorBlockGroup.extractBlocksAsNewGroup(remainderBlock);
            superior.getNextConnection().disconnect();
        }
    }
    // Connect the new block to its parent
    connectAfter(superior, superiorBlockGroup, inferior, inferiorBlockGroup);
    // is considered in the workspace during connection checks.
    if (remainderBlock != null) {
        // Try to reconnect the remainder to the end of the new sequence. If the last block
        // has no next bump instead. Shadows will be replaced by the remainder.
        Block lastBlock = inferior.getLastBlockInSequence();
        if (lastBlock.getNextConnection() == null) {
            // Nothing to connect to.  Bump and add to root.
            addRootBlock(remainderBlock, remainderGroup, false);
            bumpBlock(inferior.getPreviousConnection(), remainderBlock.getPreviousConnection());
        } else {
            // Connect the remainder
            connectAfter(lastBlock, superiorBlockGroup, remainderBlock, remainderGroup);
        }
    }
}

5. BlocklyController#extractBlockAsRoot()

View license
/**
     * Takes a block, and adds it to the root blocks, disconnecting previous or output connections,
     * if previously connected.  No action if the block was already a root block.
     *
     * @param block {@link Block} to extract as a root block in the workspace.
     */
public void extractBlockAsRoot(Block block) {
    Block rootBlock = block.getRootBlock();
    if (block == rootBlock) {
        return;
    }
    boolean isPartOfWorkspace = mWorkspace.isRootBlock(rootBlock);
    BlockView bv = mHelper.getView(block);
    BlockGroup bg = (bv == null) ? null : (BlockGroup) bv.getParent();
    BlockGroup originalRootBlockGroup = (mWorkspaceView == null) ? null : mHelper.getRootBlockGroup(block);
    // Child block
    if (block.getParentConnection() != null) {
        Connection parentConnection = block.getParentConnection();
        Input in = parentConnection.getInput();
        if (in == null) {
            if (bg != null) {
                // Next block
                bg = bg.extractBlocksAsNewGroup(block);
            }
        } else {
            // Statement or value input
            // Disconnect view.
            InputView inView = in.getView();
            if (inView != null) {
                inView.setConnectedBlockGroup(null);
            }
        }
        parentConnection.disconnect();
        // If this is itself a shadow block the answer is 'no'.
        if (!block.isShadow() && parentConnection != null && parentConnection.getShadowBlock() != null) {
            Block shadowBlock = parentConnection.getShadowBlock();
            // We add the shadow as a root and then connect it so we properly add all the
            // connectors and views.
            addRootBlock(shadowBlock, null, true);
            connect(shadowBlock, parentConnection.getShadowConnection(), parentConnection);
        }
    }
    if (originalRootBlockGroup != null) {
        originalRootBlockGroup.requestLayout();
    }
    if (isPartOfWorkspace) {
        // Only add back to the workspace if the original tree is part of the workspace model.
        addRootBlock(block, bg, false);
    }
}

6. BlocklyController#addRootBlock()

View license
/**
     * Adds the provided block to the list of root blocks.  If the controller has an initialized
     * {@link WorkspaceView}, it will also create corresponding views.
     *
     * @param block The {@link Block} to add to the workspace.
     */
public BlockGroup addRootBlock(Block block) {
    if (block.getParentBlock() != null) {
        throw new IllegalArgumentException("New root block must not be connected.");
    }
    BlockGroup parentGroup = mHelper.getParentBlockGroup(block);
    return addRootBlock(block, parentGroup, /* is new BlockView? */
    parentGroup == null);
}

7. BlocklyController#connectAsInput()

View license
/**
     * Connect a block or block group to an input on another block and update views as necessary. If
     * the input was already connected, splice the child block or group in.
     *
     * @param parentConn The {@link Connection} on the superior block to connect to.  Must be an
     *                   input.
     * @param childConn The {@link Connection} on the inferior block.  Must be an output or previous
     *                  connection.
     */
private void connectAsInput(Connection parentConn, Connection childConn) {
    InputView parentInputView = parentConn.getInputView();
    Block child = childConn.getBlock();
    BlockGroup childBlockGroup = mHelper.getParentBlockGroup(child);
    Connection previousTargetConnection = null;
    if (parentConn.isConnected()) {
        previousTargetConnection = parentConn.getTargetConnection();
        // If there was a shadow block here delete it from the hierarchy and forget about it.
        if (previousTargetConnection.getBlock().isShadow()) {
            removeBlockTree(previousTargetConnection.getBlock());
            previousTargetConnection = null;
        } else {
            // Otherwise just disconnect for now
            parentConn.disconnect();
            if (parentInputView != null) {
                parentInputView.setConnectedBlockGroup(null);
            }
        }
    }
    // Connect the new block to its parent.
    parentConn.connect(childConn);
    // Try to reconnect the old block at the end.
    if (previousTargetConnection != null) {
        Block previousTargetBlock = previousTargetConnection.getBlock();
        // Traverse the tree to ensure it doesn't branch. We only reconnect if there's a
        // single place it could be reconnected to. The previousTarget will replace a shadow if
        // one was present.
        Connection lastInputConnection = child.getLastUnconnectedInputConnection();
        if (lastInputConnection == null) {
            // Bump and add back to root.
            BlockGroup previousTargetGroup = mHelper.getParentBlockGroup(previousTargetBlock);
            addRootBlock(previousTargetBlock, previousTargetGroup, false);
            bumpBlock(parentConn, previousTargetConnection);
        } else {
            // Connect the previous part
            connectAsInput(lastInputConnection, previousTargetConnection);
        }
    }
    if (mWorkspaceView != null && parentInputView != null) {
        if (childBlockGroup == null) {
            childBlockGroup = mViewFactory.buildBlockGroupTree(child, mWorkspace.getConnectionManager(), mTouchHandler);
        }
        parentInputView.setConnectedBlockGroup(childBlockGroup);
    }
}

8. InputView#measureConnectedBlockGroup()

Project: blockly-android
Source File: InputView.java
View license
// Measure only blocks connected to this input.
private void measureConnectedBlockGroup(int widthMeasureSpec, int heightMeasureSpec) {
    final boolean inputsInline = mInput.getBlock().getInputsInline();
    BlockGroup groupToMeasure = mConnectedGroup;
    if (groupToMeasure != null) {
        // There is a block group connected to this input - measure it and add its size
        // to this InputView's size.
        groupToMeasure.measure(widthMeasureSpec, heightMeasureSpec);
        mConnectedGroupWidth = groupToMeasure.getMeasuredWidth();
        mConnectedGroupHeight = groupToMeasure.getMeasuredHeight();
        // Only add space for decorations around Statement and inline Value inputs.
        switch(mInputType) {
            case Input.TYPE_VALUE:
                {
                    if (inputsInline) {
                        // Inline Value input - add space for connector that is enclosing connected
                        // block(s).
                        mConnectedGroupWidth += mPatchManager.mInlineInputTotalPaddingX;
                        mConnectedGroupHeight += mPatchManager.mInlineInputTotalPaddingY;
                    }
                    break;
                }
            case Input.TYPE_STATEMENT:
                {
                    // Statement input - add space for top and bottom of C-connector.
                    mConnectedGroupHeight += mPatchManager.mStatementTopThickness + mPatchManager.mStatementBottomThickness;
                    break;
                }
            default:
                {
                // Nothing to do for other types of inputs.
                }
        }
    } else {
        // There's nothing connected to this input - use the size of the empty connectors.
        mConnectedGroupWidth = emptyConnectorWidth(inputsInline);
        mConnectedGroupHeight = emptyConnectorHeight(inputsInline);
    }
}

9. InputView#layoutChild()

Project: blockly-android
Source File: InputView.java
View license
/**
     * If there is a child connected to this Input, then layout the child in the correct place.
     */
private void layoutChild() {
    BlockGroup groupToLayout = mConnectedGroup;
    if (groupToLayout != null) {
        // Compute offset of child relative to InputView. By default, align top of fields and
        // input, and shift right by left padding plus field width.
        int topOffset = 0;
        int leftOffset = mFieldLayoutWidth + mPatchManager.mBlockStartPadding;
        switch(mInputType) {
            case Input.TYPE_VALUE:
                {
                    if (mInput.getBlock().getInputsInline()) {
                        topOffset += mBlockTopPadding + mPatchManager.mInlineInputTopPadding;
                        leftOffset += mPatchManager.mInlineInputStartPadding;
                    } else {
                        // The child block overlaps the parent block slightly at the connector, by
                        // the width of the extruding output connector.
                        leftOffset += mPatchManager.mBlockEndPadding + mPatchManager.mValueInputWidth - mPatchManager.mOutputConnectorWidth;
                    }
                    break;
                }
            case Input.TYPE_STATEMENT:
                {
                    topOffset += mPatchManager.mStatementTopThickness;
                    leftOffset += mPatchManager.mStatementInputPadding;
                    break;
                }
            default:
        }
        final int width = groupToLayout.getMeasuredWidth();
        final int height = groupToLayout.getMeasuredHeight();
        if (mHelper.useRtl()) {
            leftOffset = getMeasuredWidth() - leftOffset - width;
        }
        groupToLayout.layout(leftOffset, topOffset, leftOffset + width, topOffset + height);
    }
}