use of edu.mit.blocks.codeblocks.BlockLink in project openblocks by mikaelhg.
the class BlockUtilities method makeRenderable.
public static RenderableBlock makeRenderable(Workspace workspace, BlockNode node, WorkspaceWidget widget) {
// genusName may not be null
String genusName = node.getGenusName();
RenderableBlock renderable = BlockUtilities.getBlock(workspace, genusName, node.getLabel());
if (renderable == null) {
throw new RuntimeException("No children block exists for this genus: " + genusName);
}
// assume not null
Block block = workspace.getEnv().getBlock(renderable.getBlockID());
widget.blockDropped(renderable);
for (int i = 0; i < node.getChildren().size(); i++) {
BlockConnector socket = block.getSocketAt(i);
BlockNode child = node.getChildren().get(i);
RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
Block childBlock = workspace.getEnv().getBlock(childRenderable.getBlockID());
// link blocks
BlockLink link;
if (childBlock.hasPlug()) {
link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
} else if (childBlock.hasBeforeConnector()) {
link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getBeforeConnector());
} else {
link = null;
}
// assume link is not null
link.connect();
workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
}
if (node.getAfterNode() != null) {
// assume has after connector
BlockConnector socket = block.getAfterConnector();
BlockNode child = node.getAfterNode();
RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
Block childBlock = workspace.getEnv().getBlock(childRenderable.getBlockID());
// link blocks
BlockLink link;
if (childBlock.hasPlug()) {
link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
} else if (childBlock.hasBeforeConnector()) {
link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getBeforeConnector());
} else {
link = null;
}
// assume link is not null
link.connect();
workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
}
return renderable;
}
Aggregations