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>
<tr>
<td id="blocklyArea"></td>
<td id="blocklyCodeArea"><textarea id="blocklyCode"></textarea></td>
<td id="blocklyCodeArea"><textarea id="blocklyCode" readonly></textarea></td>
</tr>
</table>

View File

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