forked from marva/sumorobot-web
add disable if block when used once
This commit is contained in:
parent
8cc77ff4bc
commit
8e147c485d
126
sumorobot.js
126
sumorobot.js
@ -7,6 +7,8 @@ var remoteControl = false;
|
||||
var sumostart = false;
|
||||
/* the sumorobot object */
|
||||
var sumorobot = null;
|
||||
/* disable enable hotkeys */
|
||||
var hotkeys = true;
|
||||
/* the sumorobot code */
|
||||
var sumocode = "";
|
||||
|
||||
@ -16,7 +18,7 @@ var Sumorobot = function(wsUri) {
|
||||
/* start connecting to the WebSocket */
|
||||
this.connect();
|
||||
/* to ping the robot */
|
||||
this.watchdogHandler = null;
|
||||
this.watchdogTimer = null;
|
||||
};
|
||||
|
||||
Sumorobot.prototype.connect = function() {
|
||||
@ -27,7 +29,7 @@ Sumorobot.prototype.connect = function() {
|
||||
this.websocket.onopen = function(evt) {
|
||||
console.log("INFO websocket connected");
|
||||
/* setup a timer to ping the robot */
|
||||
watchdogTimer = setInterval(function() {
|
||||
self.watchdogTimer = setInterval(function() {
|
||||
/* send a ping to the robot */
|
||||
console.log("ping the robot")
|
||||
self.send("ping");
|
||||
@ -37,7 +39,7 @@ Sumorobot.prototype.connect = function() {
|
||||
this.websocket.onclose = function(evt) {
|
||||
console.log("INFO websocket disconnected");
|
||||
/* clear the pinging */
|
||||
clearInterval(watchdogTimer);
|
||||
clearInterval(self.watchdogTimer);
|
||||
/* Try to recnnect to the sumorobot */
|
||||
self.connect();
|
||||
};
|
||||
@ -61,6 +63,51 @@ Sumorobot.prototype.send = function(msg) {
|
||||
};
|
||||
|
||||
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 */
|
||||
Blockly.Blocks.controls_if.init = function() {
|
||||
this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL);
|
||||
@ -218,12 +265,26 @@ window.onload = function() {
|
||||
|
||||
/* on blockly code change */
|
||||
function onCodeChanged(event) {
|
||||
/* generate code from the blocks and show the code to the user */
|
||||
document.getElementById("blocklyCode").value = Blockly.Sumorobot.workspaceToCode(workspace);
|
||||
/* generate code from the used blocks */
|
||||
var code = Blockly.Sumorobot.workspaceToCode(workspace);
|
||||
|
||||
/* show the code to the user */
|
||||
document.getElementById("blocklyCode").value = code;
|
||||
|
||||
/* 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) {
|
||||
/* 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 */
|
||||
@ -231,8 +292,8 @@ window.onload = function() {
|
||||
|
||||
/* key down event */
|
||||
$(document).keydown(function(e) {
|
||||
/* 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 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;
|
||||
/* select the hotkey */
|
||||
switch(e.which) {
|
||||
case 32: // space bar
|
||||
@ -273,7 +334,7 @@ window.onload = function() {
|
||||
$(".btn-robot-nr:eq(4)").click();
|
||||
break;
|
||||
case 67: // c
|
||||
panel();
|
||||
updateControlPanel();
|
||||
$("#panel").toggle();
|
||||
break;
|
||||
case 82: // r
|
||||
@ -292,10 +353,10 @@ window.onload = function() {
|
||||
|
||||
/* key up event */
|
||||
$(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 */
|
||||
$('.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 (e.which == 37 || e.which == 38 || e.which == 39 || e.which == 40) {
|
||||
if (remoteControl) sumorobot.send("stop");
|
||||
@ -304,11 +365,13 @@ window.onload = function() {
|
||||
|
||||
/* start button listener */
|
||||
$(".btn-start").click(function() {
|
||||
sumostart = true;
|
||||
sumorobot.send("start:" + document.getElementById("blocklyCode").value.replace(/sumorobot./g, ""));
|
||||
});
|
||||
|
||||
/* stop button listener */
|
||||
$(".btn-stop").click(function() {
|
||||
sumostart = false;
|
||||
sumorobot.send("stop");
|
||||
});
|
||||
|
||||
@ -339,49 +402,6 @@ window.onload = function() {
|
||||
$("#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 */
|
||||
/*$(document).click(function(e) {
|
||||
var target = e.target;
|
||||
|
Loading…
Reference in New Issue
Block a user