update block highlighting

This commit is contained in:
Silver Kuusik 2018-08-12 12:37:02 +02:00
parent 5a8e0a1127
commit 607825ed17

View File

@ -1,6 +1,6 @@
window.addEventListener("load", function() {
window.addEventListener('load', function() {
// To remember the control_if blockId
var controlBlockId = "";
var controlBlockId = '';
// Change the if block to be more cheerful
Blockly.Msg.LOGIC_HUE = '#44CC00';
@ -62,7 +62,7 @@ window.addEventListener("load", function() {
var target = e.target;
// When control_if block is in use
if (controlBlockId != "") {
if (controlBlockId != '') {
// 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')) {
@ -77,9 +77,9 @@ window.addEventListener("load", function() {
Blockly.Blocks['sumorobot_sleep'] = {
init: function() {
this.setColour("#E64C00");
this.setColour('#E64C00');
this.appendDummyInput()
.appendField("sleep")
.appendField('sleep')
.appendField(new Blockly.FieldTextInput('1000',
Blockly.FieldNumber.numberValidator), 'SLEEP');
this.setPreviousStatement(true);
@ -97,7 +97,7 @@ window.addEventListener("load", function() {
['move forward', 'FORWARD'],
['move backward', 'BACKWARD']
];
this.setColour("#E60000");
this.setColour('#E60000');
var dropdown = new Blockly.FieldDropdown(OPERATORS);
this.appendDummyInput().appendField(dropdown, 'MOVE');
this.setPreviousStatement(true);
@ -107,7 +107,7 @@ window.addEventListener("load", function() {
Blockly.Blocks['sumorobot_opponent'] = {
init: function() {
this.setColour("#0099E6");
this.setColour('#0099E6');
this.appendDummyInput().appendField('opponent');
this.setOutput(true, 'Boolean');
}
@ -119,7 +119,7 @@ window.addEventListener("load", function() {
['line left', 'LEFT'],
['line right', 'RIGHT']
];
this.setColour("#E6BF00");
this.setColour('#E6BF00');
var dropdown = new Blockly.FieldDropdown(OPERATORS);
this.appendDummyInput().appendField(dropdown, 'LINE');
this.setOutput(true, 'Boolean');
@ -127,29 +127,29 @@ window.addEventListener("load", function() {
};
Blockly.Python['sumorobot_sleep'] = function(block) {
var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue('SLEEP')) + ', "' + block.id + '")\n';
var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue('SLEEP')) + ');;' + block.id + '\n';
return code;
};
Blockly.Python['sumorobot_move'] = function(block) {
var code = 'sumorobot.move(' + block.getFieldValue('MOVE') + ', "' + block.id + '")\n';
var code = 'sumorobot.move(' + block.getFieldValue('MOVE') + ');;' + block.id + '\n';
return code;
};
Blockly.Python['sumorobot_opponent'] = function(block) {
var code = 'sumorobot.is_opponent("' + block.id + '")';
var code = 'sumorobot.is_opponent();;' + block.id + '\n';
return [code, Blockly.Python.ORDER_ATOMIC];
};
Blockly.Python['sumorobot_line'] = function(block) {
var code = 'sumorobot.is_line(' + block.getFieldValue('LINE') + ', "' + block.id + '")';
var code = 'sumorobot.is_line(' + block.getFieldValue('LINE') + ');;' + block.id + '\n';
return [code, Blockly.Python.ORDER_ATOMIC];
};
// Inject Blockly
var blocklyArea = document.getElementById('blocklyArea');
var blocklyDiv = document.getElementById('blocklyDiv');
var workspace = Blockly.inject(blocklyDiv, {
workspace = Blockly.inject(blocklyDiv, {
scrollbars: false,
media: 'assets/blockly/media/',
trashcan: true,
@ -183,14 +183,15 @@ window.addEventListener("load", function() {
Blockly.svgResize(workspace);
// Retrieve the blocks
var xml = Blockly.Xml.textToDom(getLocalStorageItem("sumorobot.blockly"));
var xml = Blockly.Xml.textToDom(getLocalStorageItem('sumorobot.blockly'));
// Resume the blocks
Blockly.Xml.domToWorkspace(xml, workspace);
// On Blockly code change
function onCodeChanged(event) {
// When the if condition block was created
if (event.type == Blockly.Events.CREATE && event.xml.getAttributeNode("type").nodeValue == "controls_if") {
if (event.type == Blockly.Events.CREATE &&
event.xml.getAttributeNode('type').nodeValue == 'controls_if') {
// Remember the control_if block id
controlBlockId = event.blockId;
// Get the control_if block object
@ -202,30 +203,32 @@ window.addEventListener("load", function() {
block.updateShape_();
}
// When the if condition block was removed
} else if (event.type == Blockly.Events.DELETE && event.oldXml.getAttributeNode("type").nodeValue == "controls_if") {
} else if (event.type == Blockly.Events.DELETE &&
event.oldXml.getAttributeNode('type').nodeValue == 'controls_if') {
// Remove the control_if block id
controlBlockId = "";
controlBlockId = '';
// Enable the if condition block
workspace.updateToolbox(document.getElementById("toolbox"));
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
sumorobot.setBlocklyCode(Blockly.Python.workspaceToCode(workspace));
// Show the code in the ace editor, filter out block IDs
readOnlyCodingEditor.setValue("\n" + sumorobot.getBlocklyCode().replace(/[,]?[ ]?"(.*?)"/g, ""));
readOnlyCodingEditor.setValue('\n' + sumorobot.getBlocklyCode().replace(/;;.{20}/g, ''));
readOnlyCodingEditor.clearSelection();
// Save the code to the local storage
var xml = Blockly.Xml.workspaceToDom(workspace);
localStorage.setItem("sumorobot.blockly", Blockly.Xml.domToText(xml));
localStorage.setItem('sumorobot.blockly', Blockly.Xml.domToText(xml));
// When control_if block is used
if (controlBlockId != "") {
if (controlBlockId != '') {
// Disable the if condition block
workspace.updateToolbox(document.getElementById("toolbox_no_if"));
workspace.updateToolbox(document.getElementById('toolbox_no_if'));
}
}
@ -237,7 +240,7 @@ window.addEventListener("load", function() {
var target = e.target;
// When control_if block is in use
if (controlBlockId != "") {
if (controlBlockId != '') {
// 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')) {