Skip to content

Commit

Permalink
Issue OpenRoberta/openroberta-lab#1525: enhance variable declaration …
Browse files Browse the repository at this point in the history
…blocks to be able to change their positioning and adopt the callers accordingly
  • Loading branch information
bjost2s committed Dec 14, 2024
1 parent e467527 commit 8d1674b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 44 deletions.
35 changes: 19 additions & 16 deletions blockly_compressed.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions blocks/mbedControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Blockly.Blocks['mbedControls_start'] = {
this.declare_ = (xmlElement.getAttribute('declare') != 'false');
if (this.declare_) {
this.appendStatementInput('ST');
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
}
},
/**
Expand All @@ -102,7 +102,7 @@ Blockly.Blocks['mbedControls_start'] = {
if (!this.declare_) {
this.appendStatementInput('ST');
// making sure only declarations can connect to the statement list
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
this.declare_ = true;
}
var vd = this.workspace.newBlock('robGlobalVariables_declare');
Expand Down
6 changes: 3 additions & 3 deletions blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ Blockly.Blocks['robProcedures_defnoreturn'] = {
var stackConnectionTarget = this.getInput('STACK').connection.targetConnection;
this.removeInput('STACK');
this.appendStatementInput('ST');
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationLocal');
this.appendStatementInput('STACK').appendField(Blockly.Msg.PROCEDURES_DEFNORETURN_DO);
if (stackConnectionTarget) {
this.getInput('STACK').connection.connect(stackConnectionTarget);
Expand All @@ -889,7 +889,7 @@ Blockly.Blocks['robProcedures_defnoreturn'] = {
this.removeInput('STACK');
this.appendStatementInput('ST');
// making sure only declarations can connect to the statement list
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationLocal');
this.appendStatementInput('STACK').appendField(Blockly.Msg.PROCEDURES_DEFNORETURN_DO);
if (stackConnectionTarget) {
this.getInput('STACK').connection.connect(stackConnectionTarget);
Expand Down Expand Up @@ -1056,7 +1056,7 @@ Blockly.Blocks['robProcedures_defreturn'] = {
this.removeInput('STACK');
this.appendStatementInput('ST');
// making sure only declarations can connect to the statement list
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationLocal');
this.appendStatementInput('STACK').appendField(Blockly.Msg.PROCEDURES_DEFNORETURN_DO);
this.appendValueInput('RETURN').
setAlign(Blockly.ALIGN_RIGHT).
Expand Down
8 changes: 4 additions & 4 deletions blocks/robControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Blockly.Blocks['robControls_start'] = {
this.declare_ = (xmlElement.getAttribute('declare') != 'false');
if (this.declare_) {
this.appendStatementInput('ST');
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
}
},
/**
Expand All @@ -117,7 +117,7 @@ Blockly.Blocks['robControls_start'] = {
if (!this.declare_) {
this.appendStatementInput('ST');
// making sure only declarations can connect to the statement list
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
this.declare_ = true;
}
var vd = this.workspace.newBlock('robGlobalVariables_declare');
Expand Down Expand Up @@ -212,7 +212,7 @@ Blockly.Blocks['robControls_start_ardu'] = {
this.declare_ = (xmlElement.getAttribute('declare') != 'false');
if (this.declare_) {
this.appendStatementInput('ST');
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
}
},
/**
Expand All @@ -228,7 +228,7 @@ Blockly.Blocks['robControls_start_ardu'] = {
if (!this.declare_) {
this.appendStatementInput('ST');
// making sure only declarations can connect to the statement list
this.getInput('ST').connection.setCheck('declaration_only');
this.getInput('ST').connection.setCheck('declarationGlobal');
this.declare_ = true;
}
var vd = this.workspace.newBlock('robGlobalVariables_declare');
Expand Down
61 changes: 44 additions & 17 deletions blocks/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
appendField(this.declType_, 'TYPE').
appendField(Blockly.RTL ? '\u2192' : '\u2190').
setCheck('Number');
this.setPreviousStatement(true, 'declaration_only');
this.setPreviousStatement(true, 'declarationGlobal');
//this.setTooltip(Blockly.Msg.VARIABLES_GLOBAL_DECLARE_TOOLTIP);
this.setMutatorMinus(new Blockly.MutatorMinus(['robGlobalVariables_declare']));
this.setMovable(false);
this.setMovable(true);
this.setDeletable(false);
this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
this.contextMenudeclarationType_ = 'variables_get';
this.declarationType_ = 'Number';
this.nextStatement_ = false;
this.setNextStatement(false);
this.nextStatement_ = true;
this.setNextStatement(true, 'declarationGlobal');
//this.setHelp(new Blockly.Help(Blockly.Msg.VARIABLE_GLOBAL_HELP));
},
/**
Expand Down Expand Up @@ -346,7 +346,7 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
},
setNext: function(next) {
this.nextStatement_ = next;
this.setNextStatement(next, 'declaration_only');
this.setNextStatement(next, 'declarationGlobal');
},
getType: function() {
return this.declarationType_;
Expand All @@ -365,7 +365,7 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
* @this Blockly.Block
*/
getVars: function() {
return [this.getFieldValue('VAR')];
return this.inTask ? [this.getFieldValue('VAR')] : [];
},
/**
* Update the shape, if minus is pressed or if type has changed.
Expand All @@ -381,8 +381,6 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
this.unplug(true, true);
if (!!parent && (parent.type.indexOf('Controls_start') !== -1) && !nextBlock) {
parent.updateShape_(num);
} else if (!!parent && !nextBlock) {
parent.setNext(false);
}
Blockly.Variables.deleteAll(this.getFieldValue('VAR'));
this.dispose();
Expand Down Expand Up @@ -421,7 +419,7 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
break;
default:
block = this.workspace.newBlock('robColour_picker');
}
}
} else if (option === 'Image') {
switch (this.workspace.device){
case 'microbit':
Expand All @@ -437,7 +435,7 @@ Blockly.Blocks['robGlobalVariables_declare'] = {
block = this.workspace.newBlock('image_image');
break;
default:
}
}
} else if (option === 'Connection') {
block = this.workspace.newBlock('logic_null');
}
Expand Down Expand Up @@ -477,15 +475,16 @@ Blockly.Blocks['robLocalVariables_declare'] = {
appendField(nameField, 'VAR').
appendField(':').
appendField(this.declType_, 'TYPE');
this.setPreviousStatement(true, 'declaration_only');
this.setPreviousStatement(true, 'declarationLocal');
this.setTooltip(Blockly.Msg.VARIABLES_LOCAL_DECLARE_TOOLTIP);
this.setMutatorMinus(new Blockly.MutatorMinus(this));
this.setMovable(false);
this.setMovable(true);
this.setDeletable(false);
this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET;
this.contextMenudeclarationType_ = 'variables_get';
this.nextStatement_ = false;
this.setNextStatement(true, 'declarationLocal');
this.declarationType_ = 'Number';
this.connected = true;
},
/**
* Initialization of the block has completed, clean up anything that may be
Expand Down Expand Up @@ -526,7 +525,10 @@ Blockly.Blocks['robLocalVariables_declare'] = {
* @param {Element} XML storage element.
* @this Blockly.Block
*/
setNext: Blockly.Blocks['robGlobalVariables_declare'].setNext,
setNext: function(next) {
this.nextStatement_ = next;
this.setNextStatement(next, 'declarationLocal');
},
getType: function() {
return this.declarationType_;
},
Expand All @@ -544,7 +546,7 @@ Blockly.Blocks['robLocalVariables_declare'] = {
* @this Blockly.Block
*/
getVars: function() {
return [this.getFieldValue('VAR')];
return this.inTask ? [this.getFieldValue('VAR')] : [];
},
/**
* Update the shape, if minus is pressed or if type has changed..
Expand All @@ -563,8 +565,6 @@ Blockly.Blocks['robLocalVariables_declare'] = {
this.unplug(true, true);
if (!!parent && (parent.type == 'robProcedures_defnoreturn' || parent.type == 'robProcedures_defreturn') && !nextBlock) {
parent.updateShape_(num);
} else if (!!parent && !nextBlock) {
parent.setNextStatement(false);
}
this.dispose();
}
Expand All @@ -579,6 +579,33 @@ Blockly.Blocks['robLocalVariables_declare'] = {
Blockly.Variables.updateType(this.getFieldValue('VAR'), option);
Blockly.Procedures.updateCallers(this.getFieldValue('VAR'), option, Blockly.mainWorkspace, 0);
},
onchange : function(evt) {
if (!this.workspace || evt.type != Blockly.Events.MOVE || Blockly.hasClass_(this.svgGroup_,'blocklyDragging')) {
// Block has been deleted or is in move
return;
}
var surroundParent = this.getSurroundParent();
if (surroundParent) {
if (this.connected) {
if (this.surroundParentName != surroundParent.getFieldValue('NAME')) {
Blockly.Procedures.updateCallers(this.getFieldValue('VAR'), this.declarationType_, this.workspace, 1, surroundParent.getFieldValue('NAME'));
Blockly.Procedures.updateCallers(this.getFieldValue('VAR'), this.declarationType_, this.workspace, -1, this.surroundParentName);
}
this.surroundParentName = surroundParent.getFieldValue('NAME');
} else {
this.connected = true;
Blockly.Procedures.updateCallers(this.getFieldValue('VAR'), this.declarationType_, this.workspace, 1);
this.surroundParentName = surroundParent.getFieldValue('NAME');
}
} else {
if (this.connected) {
this.connected = false;
Blockly.Procedures.updateCallers(this.getFieldValue('VAR'), this.declarationType_, this.workspace, -1, this.surroundParentName);
Blockly.Variables.deleteAll(this.getFieldValue('VAR'));
this.surroundParentName = surroundParent.getFieldValue('NAME');
}
}
},
contextMenuType_: 'variables_get',
customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
};
4 changes: 3 additions & 1 deletion core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,9 @@ Blockly.Connection.prototype.targetBlock = function() {
Blockly.Connection.prototype.checkType_ = function(otherConnection) {
if (!this.check_ || !otherConnection.check_) {
// special case for variable declarations
if ((this.check_ == 'declaration_only' && !otherConnection.check_) || (!this.check_ && otherConnection.check_ == 'declaration_only'))
if ((this.check_ == 'declarationGlobal' && !otherConnection.check_) || (!this.check_ && otherConnection.check_ == 'declarationGlobal'))
return false;
if ((this.check_ == 'declarationLocal' && !otherConnection.check_) || (!this.check_ && otherConnection.check_ == 'declarationLocal'))
return false;
// special case for botnroll startblock
if ((this.check_ == 'botnroll' && !otherConnection.check_) || (!this.check_ && otherConnection.check_ ==
Expand Down
2 changes: 2 additions & 0 deletions core/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ Blockly.Variables.getProcedureName = function(name) {
return surroundParent.getFieldValue('NAME');
} else if (surroundParent && (surroundParent.type == 'robControls_start' || surroundParent.type == 'robControls_start_ardu')) {
return 'global';
} else if (!surroundParent && blocks[i].surroundParentName) {
return blocks[i].surroundParentName;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion msg/json/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@metadata": {
"author": "Ellen Spertus <[email protected]>",
"lastupdated": "2024-11-04 14:33:51.984184",
"lastupdated": "2024-12-14 08:45:05.222531",
"locale": "en",
"messagedocumentation" : "qqq"
},
Expand Down

0 comments on commit 8d1674b

Please sign in to comment.