add highlighitng current block

This commit is contained in:
Silver Kuusik 2017-10-16 23:11:01 +02:00
parent 9f56770f09
commit e0e42bf283
2 changed files with 19 additions and 13 deletions

View File

@ -55,7 +55,7 @@
<table> <table>
<tr> <tr>
<td id="blocklyArea"></td> <td id="blocklyArea"></td>
<td id="blocklyCodeArea"><textarea id="blocklyCode"></textarea></td> <td id="blocklyCodeArea"><textarea id="blocklyCode" readonly></textarea></td>
</tr> </tr>
</table> </table>

View File

@ -7,6 +7,8 @@ var remoteControl = false;
var sumostart = false; var sumostart = false;
/* the sumorobot object */ /* the sumorobot object */
var sumorobot = null; var sumorobot = null;
/* Blockly workspace */
var workspace = null;
/* disable enable hotkeys */ /* disable enable hotkeys */
var hotkeys = true; var hotkeys = true;
/* the sumorobot code */ /* the sumorobot code */
@ -31,7 +33,7 @@ Sumorobot.prototype.connect = function() {
/* setup a timer to ping the robot */ /* setup a timer to ping the robot */
self.watchdogTimer = setInterval(function() { self.watchdogTimer = setInterval(function() {
/* send a ping to the robot */ /* send a ping to the robot */
console.log("ping the robot") //console.log("ping the robot")
self.send("ping"); self.send("ping");
}, 2000); }, 2000);
}; };
@ -46,7 +48,11 @@ Sumorobot.prototype.connect = function() {
/* when there is a message from the WebSocket */ /* when there is a message from the WebSocket */
this.websocket.onmessage = function(evt) { this.websocket.onmessage = function(evt) {
/* when scope is received */ /* when scope is received */
console.log(evt.data); if (evt.data.length == 20) {
workspace.highlightBlock(evt.data);
} else {
console.log(evt.data);
}
}; };
/* when there is an WebSocket error */ /* when there is an WebSocket error */
this.websocket.onerror = function(err) { this.websocket.onerror = function(err) {
@ -185,22 +191,22 @@ window.onload = function() {
}; };
Blockly.Sumorobot['sumorobot_delay'] = function(block) { Blockly.Sumorobot['sumorobot_delay'] = function(block) {
var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue('DELAY')) + ')\n'; var code = 'sumorobot.sleep(' + parseFloat(block.getFieldValue('DELAY')) + ', "' + block.id + '")\n';
return code; return code;
}; };
Blockly.Sumorobot['sumorobot_move'] = function(block) { Blockly.Sumorobot['sumorobot_move'] = function(block) {
var code = 'sumorobot.move(' + block.getFieldValue('MOVE') + ')\n'; var code = 'sumorobot.move(' + block.getFieldValue('MOVE') + ', "' + block.id + '")\n';
return code; return code;
}; };
Blockly.Sumorobot['sumorobot_enemy'] = function(block) { Blockly.Sumorobot['sumorobot_enemy'] = function(block) {
var code = 'sumorobot.is_enemy()'; var code = 'sumorobot.is_enemy("' + block.id + '")';
return [code, Blockly.Sumorobot.ORDER_ATOMIC]; return [code, Blockly.Sumorobot.ORDER_ATOMIC];
}; };
Blockly.Sumorobot['sumorobot_line'] = function(block) { Blockly.Sumorobot['sumorobot_line'] = function(block) {
var code = 'sumorobot.is_line(' + block.getFieldValue('LINE') + ')'; var code = 'sumorobot.is_line(' + block.getFieldValue('LINE') + ', "' + block.id + '")';
return [code, Blockly.Sumorobot.ORDER_ATOMIC]; return [code, Blockly.Sumorobot.ORDER_ATOMIC];
}; };
@ -208,7 +214,7 @@ window.onload = function() {
Blockly.Blocks.logic.HUE = '#44CC00'; Blockly.Blocks.logic.HUE = '#44CC00';
var blocklyArea = document.getElementById('blocklyArea'); var blocklyArea = document.getElementById('blocklyArea');
var blocklyDiv = document.getElementById('blocklyDiv'); var blocklyDiv = document.getElementById('blocklyDiv');
var workspace = Blockly.inject(blocklyDiv, { workspace = Blockly.inject(blocklyDiv, {
scrollbars: false, scrollbars: false,
media: 'media/', media: 'media/',
trashcan: true, trashcan: true,
@ -268,17 +274,17 @@ window.onload = function() {
/* only process change and move commands */ /* only process change and move commands */
if (event.type != Blockly.Events.CHANGE && event.type != Blockly.Events.MOVE) return; if (event.type != Blockly.Events.CHANGE && event.type != Blockly.Events.MOVE) return;
/* generate code from the used blocks */ /* generate code from the used blocks */
var code = Blockly.Sumorobot.workspaceToCode(workspace); sumocode = Blockly.Sumorobot.workspaceToCode(workspace);
/* show the code to the user */ /* show the code to the user, filter out block IDs */
document.getElementById("blocklyCode").value = code; document.getElementById("blocklyCode").value = sumocode.replace(/[,]?[ ]?"(.*?)"/g, "");
/* save the code to the local storage */ /* save the code to the local storage */
var xml = Blockly.Xml.workspaceToDom(workspace); var xml = Blockly.Xml.workspaceToDom(workspace);
localStorage.setItem("sumorobot.currentProgram", Blockly.Xml.domToText(xml)); localStorage.setItem("sumorobot.currentProgram", Blockly.Xml.domToText(xml));
/* if the if condition block is used */ /* if the if condition block is used */
if (code.indexOf("if") != -1) { if (sumocode.indexOf("if") != -1) {
/* disable the if condition block */ /* disable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>"); $("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>");
workspace.updateToolbox(document.getElementById("toolbox")); workspace.updateToolbox(document.getElementById("toolbox"));
@ -375,7 +381,7 @@ window.onload = function() {
/* start button listener */ /* start button listener */
$(".btn-start").click(function() { $(".btn-start").click(function() {
sumostart = true; sumostart = true;
sumorobot.send("start:" + document.getElementById("blocklyCode").value.replace(/sumorobot./g, "")); sumorobot.send("start:" + sumocode.replace(/sumorobot./g, ""));
}); });
/* stop button listener */ /* stop button listener */