add disable if block when used once

This commit is contained in:
Silver Kuusik 2017-10-02 21:41:10 +02:00
parent 8cc77ff4bc
commit 8e147c485d

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;
/* disable enable hotkeys */
var hotkeys = true;
/* the sumorobot code */ /* the sumorobot code */
var sumocode = ""; var sumocode = "";
@ -16,7 +18,7 @@ var Sumorobot = function(wsUri) {
/* start connecting to the WebSocket */ /* start connecting to the WebSocket */
this.connect(); this.connect();
/* to ping the robot */ /* to ping the robot */
this.watchdogHandler = null; this.watchdogTimer = null;
}; };
Sumorobot.prototype.connect = function() { Sumorobot.prototype.connect = function() {
@ -27,7 +29,7 @@ Sumorobot.prototype.connect = function() {
this.websocket.onopen = function(evt) { this.websocket.onopen = function(evt) {
console.log("INFO websocket connected"); console.log("INFO websocket connected");
/* setup a timer to ping the robot */ /* setup a timer to ping the robot */
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");
@ -37,7 +39,7 @@ Sumorobot.prototype.connect = function() {
this.websocket.onclose = function(evt) { this.websocket.onclose = function(evt) {
console.log("INFO websocket disconnected"); console.log("INFO websocket disconnected");
/* clear the pinging */ /* clear the pinging */
clearInterval(watchdogTimer); clearInterval(self.watchdogTimer);
/* Try to recnnect to the sumorobot */ /* Try to recnnect to the sumorobot */
self.connect(); self.connect();
}; };
@ -61,6 +63,51 @@ Sumorobot.prototype.send = function(msg) {
}; };
window.onload = function() { window.onload = function() {
/* function to update the control panel */
function updateControlPanel() {
/* hide all buttons and text fields */
$(".robot-id, .robot-nr, .btn-robot-nr").hide();
/* show the first button and text field */
$(".robot-id:eq(0), .robot-nr:eq(0), .btn-robot-nr:eq(0)").show();
/* adjust the buttons and text fields to be in the middle */
$(".input-group, .btn-group-robot").css("width", "20%");
$(".input-group, .btn-group-robot").css("margin-left", "40%");
/* hide the robot add button */
$(".btn-robot-add").hide();
/* populate robots IDs and buttons */
for (var i = 0; i < 5; i++) {
var id = getLocalStorageItem("sumorobot.robotID" + i);
if (id) {
$(".robot-id:eq(" + i + ")").val(id);
$(".robot-id:eq(" + i + "), .robot-nr:eq(" + i + "), .btn-robot-nr:eq(" + i + ")").show();
$(".input-group, .btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
} else {
/* when no robots yet added */
if (i != 0) {
/* show the robot add button */
$(".btn-robot-add").show();
$(".btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
/* add click listener to the robot add button */
$(".btn-robot-add").click(function() {
$(".robot-id:eq(" + i + "), .robot-nr:eq(" + i + "), .btn-robot-nr:eq(" + i + ")").show();
$(".input-group, .btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
$(this).hide();
});
}
break;
}
}
}
/* load the control panel */
updateControlPanel();
/* remove previous and next statement from if block */ /* remove previous and next statement from if block */
Blockly.Blocks.controls_if.init = function() { Blockly.Blocks.controls_if.init = function() {
this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL); this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL);
@ -218,12 +265,26 @@ window.onload = function() {
/* on blockly code change */ /* on blockly code change */
function onCodeChanged(event) { function onCodeChanged(event) {
/* generate code from the blocks and show the code to the user */ /* generate code from the used blocks */
document.getElementById("blocklyCode").value = Blockly.Sumorobot.workspaceToCode(workspace); var code = Blockly.Sumorobot.workspaceToCode(workspace);
/* show the code to the user */
document.getElementById("blocklyCode").value = code;
/* 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 (code.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"));
} else {
/* enable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if'></block>");
workspace.updateToolbox(document.getElementById("toolbox"));
}
} }
/* add a change listener to Blockly */ /* add a change listener to Blockly */
@ -231,8 +292,8 @@ window.onload = function() {
/* key down event */ /* key down event */
$(document).keydown(function(e) { $(document).keydown(function(e) {
/* if the focused element is a textarea or text input, don't use hotkeys */ /* if the hotkeys are disabled or the focused element is a textarea or text input, don't use hotkeys */
if ($(e.target).is("textarea") || $(e.target).is("[type=text]")) return; if (hotkeys == false || $(e.target).is("textarea") || $(e.target).is("[type=text]")) return;
/* select the hotkey */ /* select the hotkey */
switch(e.which) { switch(e.which) {
case 32: // space bar case 32: // space bar
@ -273,7 +334,7 @@ window.onload = function() {
$(".btn-robot-nr:eq(4)").click(); $(".btn-robot-nr:eq(4)").click();
break; break;
case 67: // c case 67: // c
panel(); updateControlPanel();
$("#panel").toggle(); $("#panel").toggle();
break; break;
case 82: // r case 82: // r
@ -292,10 +353,10 @@ window.onload = function() {
/* key up event */ /* key up event */
$(document).keyup(function(e) { $(document).keyup(function(e) {
/* if the hotkeys are disabled or the focused element is a textarea or text input, don't use hotkeys */
if (hotkeys == false || $(e.target).is("textarea") || $(e.target).is("[type=text]")) return;
/* remove hover from buttons */ /* remove hover from buttons */
$('.btn').removeClass('hover'); $('.btn').removeClass('hover');
/* if the focused element is a textarea or text input, don't use hotkeys */
if ($(e.target).is("textarea") || $(e.target).is("[type=text]")) return;
/* if arrow keys */ /* if arrow keys */
if (e.which == 37 || e.which == 38 || e.which == 39 || e.which == 40) { if (e.which == 37 || e.which == 38 || e.which == 39 || e.which == 40) {
if (remoteControl) sumorobot.send("stop"); if (remoteControl) sumorobot.send("stop");
@ -304,11 +365,13 @@ window.onload = function() {
/* start button listener */ /* start button listener */
$(".btn-start").click(function() { $(".btn-start").click(function() {
sumostart = true;
sumorobot.send("start:" + document.getElementById("blocklyCode").value.replace(/sumorobot./g, "")); sumorobot.send("start:" + document.getElementById("blocklyCode").value.replace(/sumorobot./g, ""));
}); });
/* stop button listener */ /* stop button listener */
$(".btn-stop").click(function() { $(".btn-stop").click(function() {
sumostart = false;
sumorobot.send("stop"); sumorobot.send("stop");
}); });
@ -339,49 +402,6 @@ window.onload = function() {
$("#panel").hide(); $("#panel").hide();
}); });
function panel() {
/* hide all buttons and text fields */
$(".robot-id, .robot-nr, .btn-robot-nr").hide();
/* show the first button and text field */
$(".robot-id:eq(0), .robot-nr:eq(0), .btn-robot-nr:eq(0)").show();
/* adjust the buttons and text fields to be in the middle */
$(".input-group, .btn-group-robot").css("width", "20%");
$(".input-group, .btn-group-robot").css("margin-left", "40%");
/* hide the robot add button */
$(".btn-robot-add").hide();
/* populate robots IDs and buttons */
for (var i = 0; i < 5; i++) {
var id = getLocalStorageItem("sumorobot.robotID" + i);
if (id) {
$(".robot-id:eq(" + i + ")").val(id);
$(".robot-id:eq(" + i + "), .robot-nr:eq(" + i + "), .btn-robot-nr:eq(" + i + ")").show();
$(".input-group, .btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
} else {
/* when no robots yet added */
if (i != 0) {
/* show the robot add button */
$(".btn-robot-add").show();
$(".btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
/* add click listener to the robot add button */
$(".btn-robot-add").click(function() {
$(".robot-id:eq(" + i + "), .robot-nr:eq(" + i + "), .btn-robot-nr:eq(" + i + ")").show();
$(".input-group, .btn-group-robot").css("width", 20 + (i * 20) + "%");
$(".input-group, .btn-group-robot").css("margin-left", 40 - (i * 10) + "%");
$(this).hide();
});
}
break;
}
}
}
panel();
/* try to close if block bubble canvas */ /* try to close if block bubble canvas */
/*$(document).click(function(e) { /*$(document).click(function(e) {
var target = e.target; var target = e.target;