Search in sources :

Example 1 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class RenderableBlock method redrawFromTop.

/**
 * Redraws this RenderableBlock along with the RenderableBlocks
 * after it, which include after and socket blocks.  In other words,
 * this method redraws the stack of blocks that begin with this.
 * NOTE: this is inefficient, should only use this if needed
 * NOTE: Must call this after loading of blocks to update the socket
 * dimensions of this and set the isLoading flag to false
 */
public void redrawFromTop() {
    if (GraphicsEnvironment.isHeadless()) {
        return;
    }
    isLoading = false;
    for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(getBlock())) {
        if (socket.hasBlock()) {
            // loop through all the afters of the connected block
            long curBlockID = socket.getBlockID();
            // TODO: this is a patch, but we need to fix the root of the problem!
            if (workspace.getEnv().getRenderableBlock(curBlockID) == null) {
                System.out.println("does not exist yet, block: " + curBlockID);
                continue;
            }
            workspace.getEnv().getRenderableBlock(curBlockID).redrawFromTop();
            // add dimension to the mapping
            this.getConnectorTag(socket).setDimension(calcDimensionOfSocket(socket));
        } else {
            this.getConnectorTag(socket).setDimension(null);
        }
    }
    // reform shape with new socket dimension
    reformBlockShape();
    // next time, redraw with new positions and moving children blocks
    clearBufferedImage();
}
Also used : BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 2 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class RenderableBlock method stopDragging.

/**
 * This method is called when this RenderableBlock is plugged into another RenderableBlock that has finished dragging.
 * @param widget the WorkspaceWidget where this RenderableBlock is being dropped.
 */
public static void stopDragging(RenderableBlock renderable, WorkspaceWidget widget) {
    if (!renderable.dragging) {
        throw new RuntimeException("dropping without prior dragging?");
    }
    // notify children
    for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(renderable.getBlock())) {
        if (socket.hasBlock()) {
            stopDragging(renderable.getWorkspace().getEnv().getRenderableBlock(socket.getBlockID()), widget);
        }
    }
    // drop this block on its widget (if w is null it'll throw an exception)
    widget.blockDropped(renderable);
    // stop rendering as transparent
    renderable.dragging = false;
    // move comment
    if (renderable.hasComment()) {
        if (renderable.getParentWidget() != null) {
            renderable.comment.setParent(renderable.getParentWidget().getJComponent(), 0);
        } else {
            renderable.comment.setParent(null, renderable.getBounds());
        }
        renderable.comment.setConstrainComment(true);
        renderable.comment.setLocation(renderable.comment.getLocation());
        renderable.comment.getArrow().updateArrow();
    }
}
Also used : BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 3 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class RenderableBlock method synchronizeLabelsAndSockets.

/**
 * Updates all the labels within this block.  Returns true if this update found any changed labels; false otherwise
 * @return true if this update found any changed labels; false otherwise.
 */
private boolean synchronizeLabelsAndSockets() {
    boolean blockLabelChanged = getBlock().getBlockLabel() != null && !blockLabel.getText().equals(getBlock().getBlockLabel());
    boolean pageLabelChanged = getBlock().getPageLabel() != null && !pageLabel.getText().equals(getBlock().getPageLabel());
    boolean socketLabelsChanged = false;
    // are consistent.
    for (int i = 0; i < getBlock().getNumSockets(); i++) {
        BlockConnector socket = getBlock().getSocketAt(i);
        ConnectorTag tag = this.getConnectorTag(socket);
        if (tag != null) {
            if (tag.getLabel() != null) {
                if (!tag.getLabel().getText().equals(socket.getLabel())) {
                    socketLabelsChanged = synchronizeSockets();
                    break;
                }
            }
        }
        if (!socket.isLabelEditable()) {
            socketLabelsChanged = synchronizeSockets();
            break;
        }
    }
    if (blockLabelChanged) {
        blockLabel.setText(getBlock().getBlockLabel());
    }
    if (pageLabelChanged) {
        pageLabel.setText(getBlock().getPageLabel());
    }
    if (blockLabelChanged || pageLabelChanged || socketLabelsChanged || commentLabelChanged) {
        reformBlockShape();
        commentLabelChanged = false;
    }
    if (BlockLinkChecker.hasPlugEquivalent(getBlock())) {
        BlockConnector plug = BlockLinkChecker.getPlugEquivalent(getBlock());
        Block plugBlock = workspace.getEnv().getBlock(plug.getBlockID());
        if (plugBlock != null) {
            if (plugBlock.getConnectorTo(blockID) == null) {
                throw new RuntimeException("one-sided connection from " + getBlock().getBlockLabel() + " to " + workspace.getEnv().getBlock(blockID).getBlockLabel());
            }
            workspace.getEnv().getRenderableBlock(plug.getBlockID()).updateSocketSpace(plugBlock.getConnectorTo(blockID), blockID, true);
        }
    }
    return false;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point)

Example 4 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class RenderableBlock method moveConnectedBlocks.

// ////////////////////////////////
// MOVEMENT OF CONNECTED BLOCKS //
// ////////////////////////////////
/**
 * Aligns all RenderableBlocks plugged into this one with the current location of this RenderableBlock.
 * These RenderableBlocks to move include blocks connected at sockets and the after connector.
 */
public void moveConnectedBlocks() {
    if (DEBUG) {
        System.out.println("move connected blocks of this: " + this);
    }
    // if this hasn't been added anywhere, asking its location will break stuff
    if (getParent() == null) {
        return;
    }
    Block b = workspace.getEnv().getBlock(blockID);
    Point socketLocation;
    Point plugLocation;
    RenderableBlock rb;
    Point myScreenOffset = getLocation();
    Point otherScreenOffset;
    for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(b)) {
        socketLocation = getSocketPixelPoint(socket);
        if (socket.hasBlock()) {
            rb = workspace.getEnv().getRenderableBlock(socket.getBlockID());
            // needs to be found and fixed!!
            if (rb == null) {
                System.out.println("Block doesn't exist yet: " + socket.getBlockID());
                continue;
            }
            plugLocation = rb.getSocketPixelPoint(BlockLinkChecker.getPlugEquivalent(workspace.getEnv().getBlock(socket.getBlockID())));
            otherScreenOffset = SwingUtilities.convertPoint(rb.getParent(), rb.getLocation(), getParent());
            otherScreenOffset.translate(-rb.getX(), -rb.getY());
            rb.setLocation((int) Math.round((float) myScreenOffset.getX() + socketLocation.getX() - (float) otherScreenOffset.getX() - plugLocation.getX()), (int) Math.round((float) myScreenOffset.getY() + socketLocation.getY() - (float) otherScreenOffset.getY() - plugLocation.getY()));
            rb.moveConnectedBlocks();
        }
    }
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point)

Example 5 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class BlockUtilities method makeNodeWithChildren.

public static BlockNode makeNodeWithChildren(Workspace workspace, Long blockID) {
    if (isNullBlockInstance(workspace, blockID)) {
        return null;
    }
    Block block = workspace.getEnv().getBlock(blockID);
    String genus = block.getGenusName();
    String parentGenus = block instanceof BlockStub ? ((BlockStub) block).getParentGenus() : null;
    String label;
    if (!block.labelMustBeUnique() || block instanceof BlockStub) {
        label = block.getBlockLabel();
    } else {
        label = null;
    }
    BlockNode node = new BlockNode(genus, parentGenus, label);
    for (BlockConnector socket : block.getSockets()) {
        if (socket.hasBlock()) {
            node.addChild(makeNodeWithStack(workspace, socket.getBlockID()));
        }
    }
    return node;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockStub(edu.mit.blocks.codeblocks.BlockStub) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Aggregations

BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)26 Block (edu.mit.blocks.codeblocks.Block)14 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)9 Point (java.awt.Point)9 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)8 BlockLink (edu.mit.blocks.codeblocks.BlockLink)4 ArrayList (java.util.ArrayList)3 BlockStub (edu.mit.blocks.codeblocks.BlockStub)2 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Dimension (java.awt.Dimension)2 Workspace (edu.mit.blocks.workspace.Workspace)1 Component (java.awt.Component)1 Container (java.awt.Container)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Point2D (java.awt.geom.Point2D)1 JComponent (javax.swing.JComponent)1