use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.
the class BlockLink method connect.
/**
* This method actually connects the two blocks stored in this BlockLink object.
*/
public void connect() {
/* Make sure to disconnect any connections that are going to be overwritten
* by this new connection. For example, if inserting a block between two
* others, make sure to break that original link.*/
if (socket.hasBlock()) {
// save the ID of the block previously attached to (in) this
// socket. This is used by insertion rules to re-link the replaced
// block to the newly-inserted block.
lastPlugBlockID = socket.getBlockID();
// break the link between the socket block and the block in that socket
Block plugBlock = workspace.getEnv().getBlock(lastPlugBlockID);
BlockConnector plugBlockPlug = BlockLinkChecker.getPlugEquivalent(plugBlock);
if (plugBlockPlug != null && plugBlockPlug.hasBlock()) {
Block socketBlock = workspace.getEnv().getBlock(plugBlockPlug.getBlockID());
BlockLink link = BlockLink.getBlockLink(workspace, plugBlock, socketBlock, plugBlockPlug, socket);
link.disconnect();
// don't tell the block about the disconnect like we would normally do, because
// we don't actually want it to have a chance to remove any expandable sockets
// since the inserted block will be filling whatever socket was vacated by this
// broken link.
// NOTIFY WORKSPACE LISTENERS OF DISCONNECTION (not sure if this is great because the connection is immediately replaced)
workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(socketBlock.getBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED));
}
}
if (plug.hasBlock()) {
// after any insertion-esq links were broken above
throw new RuntimeException("trying to link a plug that's already connected somewhere.");
}
// actually form the connection
plug.setConnectorBlockID(socketBlockID);
socket.setConnectorBlockID(plugBlockID);
// notify renderable block of connection so it can redraw with stretching
RenderableBlock socketRB = workspace.getEnv().getRenderableBlock(socketBlockID);
socketRB.blockConnected(socket, plugBlockID);
if (clickSound != null) {
// System.out.println("playing click sound");
clickSound.play();
}
}
use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.
the class BlockStub method disconnectBlock.
/**
* Disconnect the given block from us. Must have a valid id.
*/
private void disconnectBlock(Long id) {
Block b2 = workspace.getEnv().getBlock(id);
BlockConnector conn2 = b2.getConnectorTo(getBlockID());
BlockConnector conn = getConnectorTo(id);
BlockLink link = BlockLink.getBlockLink(workspace, this, b2, conn, conn2);
RenderableBlock rb = workspace.getEnv().getRenderableBlock(link.getSocketBlockID());
link.disconnect();
rb.blockDisconnected(link.getSocket());
workspace.notifyListeners(new WorkspaceEvent(workspace, rb.getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED));
}
use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.
the class FactoryManager method addDynamicBlocks.
public void addDynamicBlocks(Collection<RenderableBlock> blocks, String drawer) {
// find canvas
for (FactoryCanvas canvas : this.dynamicCanvases) {
if (canvas.getName().equals(drawer)) {
for (RenderableBlock block : blocks) {
if (block == null || Block.NULL.equals(block.getBlockID())) {
continue;
}
canvas.addBlock(block);
workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
}
canvas.layoutBlocks();
return;
}
}
this.printError("Drawer not found: " + drawer);
return;
}
use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.
the class FactoryManager method addStaticBlocks.
/**
* Add blocks to drawer if drawer can be found. Add graphically
* and alos throw event. Do nothing if no drawer if specified
* name is found.
*
* @param blocks
* @param drawer
*/
public void addStaticBlocks(Collection<RenderableBlock> blocks, String drawer) {
// find canvas
for (FactoryCanvas canvas : this.staticCanvases) {
if (canvas.getName().equals(drawer)) {
for (RenderableBlock block : blocks) {
if (block == null || Block.NULL.equals(block.getBlockID())) {
continue;
}
canvas.addBlock(block);
workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
}
canvas.layoutBlocks();
return;
}
}
this.printError("Drawer not found: " + drawer);
return;
}
use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.
the class FactoryManager method addSubsetBlocks.
/**
* Adds the specified RenderableBlocks to the drawer with the specified drawerName. Do nothing
* if the drawer with the specified drawerName does not exist.
* @param blocks Collection of RenderableBlocks to the drawer with name: drawerName
* @param drawerName String name of the drawer to add blocks to
*/
public void addSubsetBlocks(Collection<RenderableBlock> blocks, String drawerName) {
// find canvas //TODO generalize the following code to one method where you pass in the blocks, drawername, and canvas types
for (FactoryCanvas canvas : this.subsetCanvases) {
if (canvas.getName().equals(drawerName)) {
for (RenderableBlock block : blocks) {
if (block == null || Block.NULL.equals(block.getBlockID())) {
continue;
}
canvas.addBlock(block);
workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
}
canvas.layoutBlocks();
return;
}
}
this.printError("Drawer not found: " + drawerName);
return;
}
Aggregations