diff --git a/sumorobot.js b/sumorobot.js index aae93d8..90e1112 100755 --- a/sumorobot.js +++ b/sumorobot.js @@ -220,6 +220,7 @@ window.onload = function() { /* change the if block to be more cheerful */ Blockly.Blocks.logic.HUE = '#44CC00'; + /* inject blockly */ var blocklyArea = document.getElementById('blocklyArea'); var blocklyDiv = document.getElementById('blocklyDiv'); workspace = Blockly.inject(blocklyDiv, { @@ -279,6 +280,36 @@ window.onload = function() { /* on blockly code change */ function onCodeChanged(event) { + /* the block */ + var block = workspace.getBlockById(event.blockId); + + /* if the if condition block was created */ + if (event.type == Blockly.Events.CREATE && block.type == "controls_if") { + /* automatically add the else statement input */ + block.elseCount_ = 1; + block.updateShape_(); + /* disable the if condition block */ + $("block[type=controls_if]").replaceWith(""); + workspace.updateToolbox(document.getElementById("toolbox")); + + /* set a click listener on the document */ + $(document).click(function(e) { + var target = e.target; + /* when the user clicks anywhere outside the mutator and not on the mutator icon */ + if (!$(target).is('.blocklyBubbleCanvas') && !$(target).parents().is('.blocklyBubbleCanvas')) { + if (!$(target).is('.blocklyIconGroup') && !$(target).parents().is('.blocklyIconGroup')) { + /* hide the mutator */ + block.mutator.setVisible(false); + } + } + }); + /* if the if condition block was removed */ + } else if (event.type == Blockly.Events.DELETE && event.oldXml.getAttributeNode("type").nodeValue == "controls_if") { + /* enable the if condition block */ + $("block[type=controls_if]").replaceWith(""); + workspace.updateToolbox(document.getElementById("toolbox")); + } + /* only process change and move commands */ if (event.type != Blockly.Events.CHANGE && event.type != Blockly.Events.MOVE) return; /* generate code from the used blocks */ @@ -290,17 +321,6 @@ window.onload = function() { /* save the code to the local storage */ var xml = Blockly.Xml.workspaceToDom(workspace); localStorage.setItem("sumorobot.currentProgram", Blockly.Xml.domToText(xml)); - - /* if the if condition block is used */ - if (sumocode.indexOf("if") != -1) { - /* disable the if condition block */ - $("block[type=controls_if]").replaceWith(""); - workspace.updateToolbox(document.getElementById("toolbox")); - } else { - /* enable the if condition block */ - $("block[type=controls_if]").replaceWith(""); - workspace.updateToolbox(document.getElementById("toolbox")); - } } /* add a change listener to Blockly */ @@ -473,13 +493,4 @@ window.onload = function() { /* load the Mixer stream */ $("#stream").html(''); - - /* try to close if block bubble canvas */ - /*$(document).click(function(e) { - var target = e.target; - if (!$(target).is('.blocklyBubbleCanvas') && !$(target).parents().is('.blocklyBubbleCanvas')) { - if (!$(target).is('.blocklyIconGroup') && !$(target).parents().is('.blocklyIconGroup')) - $('.blocklyBubbleCanvas').empty(); - } - });*/ }