From e0e42bf28307988b44c810327cf1766d8abe8390 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Mon, 16 Oct 2017 23:11:01 +0200 Subject: [PATCH] add highlighitng current block --- index.html | 2 +- sumorobot.js | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index d907ee5..8b98393 100755 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@ - +
diff --git a/sumorobot.js b/sumorobot.js index 40bef79..7e1a153 100755 --- a/sumorobot.js +++ b/sumorobot.js @@ -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(""); 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 */