Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions block-lexical-variables/src/blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// [lyn, 10/24/13] need to reconstruct first input

// Body of procedure
const bodyInput = this.inputList[this.inputList.length - 1];
const stackInput = this.getInput('STACK');
const returnInput = this.getInput('RETURN');

// stop rendering until block is recreated
const savedRendered = this.rendered;
Expand All @@ -193,7 +194,7 @@ Blockly.Blocks['procedures_defnoreturn'] = {
// necessary)

// Only args and body are left
const oldArgCount = this.inputList.length - 1;
const oldArgCount = this.inputList.length - (stackInput ? 1 : 0) - (returnInput ? 1 : 0);
if (oldArgCount > 0) {
const paramInput0 = this.getInput('VAR0');
if (paramInput0) { // Yes, they were vertical
Expand Down Expand Up @@ -242,7 +243,20 @@ Blockly.Blocks['procedures_defnoreturn'] = {
}

// put the last two arguments back
this.inputList = this.inputList.concat(bodyInput);
const wantStack = (this.bodyInputName === 'STACK') || !!this.stackEnabled_;
if (wantStack) {
if (stackInput) {
this.inputList = this.inputList.concat(stackInput);
} else if (this.bodyInputName !== 'STACK') {
// defreturn with STACK enabled but missing -> create it
this.appendStatementInput('STACK')
.appendField(Blockly.Msg['LANG_PROCEDURES_DEFNORETURN_DO']);
}
}

if (returnInput) {
this.inputList = this.inputList.concat(returnInput);
}

this.rendered = savedRendered;
// [lyn, 10/28/13] I thought this rerendering was unnecessary. But I was
Expand Down Expand Up @@ -574,13 +588,16 @@ Blockly.Blocks['procedures_defreturn'] = {
Blockly.Msg['LANG_PROCEDURES_DEFRETURN_PROCEDURE'], this);
this.createHeader(legalName);
this.horizontalParameters = true; // horizontal by default
this.appendStatementInput('STACK')
.appendField(Blockly.Msg['LANG_PROCEDURES_DOTHENRETURN_DO']);
this.appendInputFromRegistry('indented_input', 'RETURN')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField(Blockly.Msg['LANG_PROCEDURES_DEFRETURN_RETURN']);
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField(Blockly.Msg['LANG_PROCEDURES_DEFRETURN_RETURN']);
this.setMutator(new Blockly.icons.MutatorIcon(['procedures_mutatorarg'], this));
this.setTooltip(Blockly.Msg['LANG_PROCEDURES_DEFRETURN_TOOLTIP']);
this.arguments_ = [];
this.warnings = [{name: 'checkEmptySockets', sockets: ['RETURN']}];
this.stackEnabled_ = true;
},
createHeader: function(procName) {
return this.appendDummyInput('HEADER')
Expand All @@ -595,10 +612,48 @@ Blockly.Blocks['procedures_defreturn'] = {
parameterFlydown: Blockly.Blocks.procedures_defnoreturn.parameterFlydown,
setParameterOrientation:
Blockly.Blocks.procedures_defnoreturn.setParameterOrientation,
mutationToDom: Blockly.Blocks.procedures_defnoreturn.mutationToDom,
domToMutation: Blockly.Blocks.procedures_defnoreturn.domToMutation,
decompose: Blockly.Blocks.procedures_defnoreturn.decompose,
compose: Blockly.Blocks.procedures_defnoreturn.compose,
mutationToDom: function () {
const m = Blockly.Blocks.procedures_defnoreturn.mutationToDom.call(this);
m.setAttribute('stack_enabled', this.stackEnabled_ ? 'true' : 'false');
return m;
},
domToMutation: function (xml) {
Blockly.Blocks.procedures_defnoreturn.domToMutation.call(this, xml);

this.stackEnabled_ = xml.getAttribute('stack_enabled') === 'true';
const hasStack = !!this.getInput('STACK');
if (this.stackEnabled_ && !hasStack) {
this.appendStatementInput('STACK')
.appendField(Blockly.Msg['LANG_PROCEDURES_DOTHENRETURN_DO']);
if (this.getInput('RETURN')) this.moveInputBefore('STACK', 'RETURN');
} else if (!this.stackEnabled_ && hasStack) {
this.removeInput('STACK');
}
},
decompose: function (workspace) {
const containerBlock =
Blockly.Blocks.procedures_defnoreturn.decompose.call(this, workspace);

const cb = containerBlock.getField('STACK_ENABLED');
if (cb) cb.setValue(this.stackEnabled_ ? 'TRUE' : 'FALSE');

return containerBlock;
},
compose: function (containerBlock) {
const cb = containerBlock.getField('STACK_ENABLED');
this.stackEnabled_ = cb ? cb.getValue() === 'TRUE' : false;

const hasStack = !!this.getInput('STACK');
if (this.stackEnabled_ && !hasStack) {
this.appendStatementInput('STACK')
.appendField(Blockly.Msg['LANG_PROCEDURES_DOTHENRETURN_DO']);
if (this.getInput('RETURN')) this.moveInputBefore('STACK', 'RETURN');
} else if (!this.stackEnabled_ && hasStack) {
this.removeInput('STACK');
}

Blockly.Blocks.procedures_defnoreturn.compose.call(this, containerBlock);
},
dispose: Blockly.Blocks.procedures_defnoreturn.dispose,
getProcedureDef: Blockly.Blocks.procedures_defnoreturn.getProcedureDef,
getDeclaredVars: Blockly.Blocks.procedures_defnoreturn.getDeclaredVars,
Expand All @@ -621,7 +676,7 @@ Blockly.Blocks['procedures_mutatorcontainer'] = {
// this.setColour(Blockly.PROCEDURE_CATEGORY_HUE);
this.setStyle('procedure_blocks');
this.appendDummyInput()
.appendField(Blockly.Msg['LANG_PROCEDURES_MUTATORCONTAINER_TITLE']);
.appendField(Blockly.Msg['LANG_PROCEDURES_MUTATORCONTAINER_TITLE']);
this.appendStatementInput('STACK');
this.setTooltip(Blockly.Msg['LANG_PROCEDURES_MUTATORCONTAINER_TOOLTIP']);
this.contextMenu = false;
Expand All @@ -630,6 +685,22 @@ Blockly.Blocks['procedures_mutatorcontainer'] = {
// [lyn. 11/24/12] Set procBlock associated with this container.
setProcBlock: function(procBlock) {
this.procBlock_ = procBlock;
const isDefReturn = !!procBlock && procBlock.type === 'procedures_defreturn';

if (this.getInput('ENABLE_STACK')) this.removeInput('ENABLE_STACK');

if (isDefReturn) {
const row = this.appendDummyInput('ENABLE_STACK')
.appendField(Blockly.Msg['LANG_PROCEDURES_DEFRETURN_ENABLE_STACK'])
.appendField(new Blockly.FieldCheckbox('false'), Blockly.Msg['LANG_PROCEDURES_DEFRETURN_STACK_ENABLE_FIELD'])
.appendField(Blockly.Msg['LANG_PROCEDURES_MUTATORCONTAINER_STACK']);
row.init();

const cb = this.getField('STACK_ENABLED');
if (cb) cb.setValue(procBlock.stackEnabled_ ? 'TRUE' : 'FALSE');
}

if (this.rendered) this.render();
},
// [lyn. 11/24/12] Set procBlock associated with this container.
// Invariant: should not be null, since only created as mutator for a
Expand Down
2 changes: 2 additions & 0 deletions block-lexical-variables/src/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Blockly.Msg['LANG_PROCEDURES_DEFRETURN_RETURN'] = 'result';
Blockly.Msg['LANG_PROCEDURES_DEFRETURN_COLLAPSED_PREFIX'] = 'to ';
Blockly.Msg['LANG_PROCEDURES_DEFRETURN_TOOLTIP'] =
'A procedure returning a result value.';
Blockly.Msg['LANG_PROCEDURES_DEFRETURN_ENABLE_STACK'] = 'allow statements'
Blockly.Msg['LANG_PROCEDURES_DEFRETURN_STACK_ENABLE_FIELD'] = 'STACK_ENABLED'
Blockly.Msg['LANG_PROCEDURES_DEF_DUPLICATE_WARNING'] =
'Warning:\nThis procedure has\nduplicate inputs.';
Blockly.Msg['LANG_PROCEDURES_CALLNORETURN_CALL'] = 'call ';
Expand Down