Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
143 changes: 103 additions & 40 deletions blocks_vertical/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,11 @@ Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_ = function() {
if (!wasRendered && this.getReturn) {
this.setInputsInline(true);
if (this.getReturn() === Blockly.PROCEDURES_CALL_TYPE_STATEMENT) {
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setPreviousStatement(true, "normal");
this.setNextStatement(true, "normal");
} else if (/*this.inputList.find(v => v.type == Blockly.NEXT_STATEMENT)*/false) {
this.setOutput(true, null);
this.setOutputShape(Blockly.OUTPUT_SHAPE_SQUARE)
} else {
if (this.getReturn() === Blockly.PROCEDURES_CALL_TYPE_BOOLEAN) {
this.setOutput(true, null);
Expand Down Expand Up @@ -298,8 +301,8 @@ Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_ = function() {
* @this Blockly.Block
*/
Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_ = function(connectionMap) {
// Split the proc into components, by %n, %b, %o, %a, and %s (ignoring escaped).
var procComponents = this.procCode_.split(/(?=[^\\]%[nboas])/);
// Split the proc into components, by %n, %b, %o, %a, %c, and %s (ignoring escaped).
var procComponents = this.procCode_.split(/(?=[^\\]%[nboasc])/);
procComponents = procComponents.map(function(c) {
return c.trim(); // Strip whitespace.
});
Expand All @@ -310,21 +313,26 @@ Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_ = function(connectionMap)
var labelText;
if (component.substring(0, 1) == '%') {
var argumentType = component.substring(1, 2);
if (!(argumentType == 'n' || argumentType == 'b' || argumentType == 'o' || argumentType == 'a' || argumentType == 's')) {
if (!['n', 'b', 'o', 'a', 's', 'c'].includes(argumentType)) {
throw new Error(
'Found an custom procedure with an invalid type: ' + argumentType);
}
labelText = component.substring(2).trim();

var id = this.argumentIds_[argumentCount];

var input = this.appendValueInput(id);
if (argumentType == 'b') {
input.setCheck('Boolean');
} else if (argumentType == 'o') {
input.setCheck('Object');
} else if (argumentType == 'a') {
input.setCheck('Array');
var input
if (argumentType === 'c') {
input = this.appendStatementInput(id).setCheck(this.type == 'procedures_prototype' ? "argumentReporterCommand" : "normal");
} else {
input = this.appendValueInput(id);
if (argumentType == 'b') {
input.setCheck('Boolean');
} else if (argumentType == 'o') {
input.setCheck('Object');
} else if (argumentType == 'a') {
input.setCheck('Array');
}
}
this.populateArgument_(argumentType, argumentCount, connectionMap, id,
input);
Expand Down Expand Up @@ -488,6 +496,8 @@ Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_ = function(
var blockType = 'argument_reporter_object';
} else if (argumentType == 'a') {
var blockType = 'argument_reporter_array';
} else if (argumentType == 'c') {
var blockType = 'argument_reporter_statement'
}
Blockly.Events.disable();
try {
Expand All @@ -498,6 +508,10 @@ Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_ = function(
newBlock.initSvg();
newBlock.render(false);
}
if (argumentType === 'c') {
newBlock.setPreviousStatement(true, 'argumentReporterCommand')
newBlock.setNextStatement(true, 'argumentReporterCommand')
}
} finally {
Blockly.Events.enable();
}
Expand Down Expand Up @@ -532,7 +546,8 @@ Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnCaller_ = function(type,
if (connectionMap && oldBlock) {
// Reattach the old block and shadow DOM.
connectionMap[input.name] = null;
oldBlock.outputConnection.connect(input.connection);
if (type == 'c') oldBlock.previousConnection.connect(input.connection);
else oldBlock.outputConnection.connect(input.connection);
if ((['s', 'n', 'b'].includes(type)) && this.generateShadows_) {
var shadowDom = oldShadow || this.buildShadowDom_(type);
input.connection.setShadowDom(shadowDom);
Expand Down Expand Up @@ -579,7 +594,8 @@ Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnPrototype_ = function(
}

// Attach the block.
input.connection.connect(argumentReporter.outputConnection);
if (type == 'c') input.connection.connect(argumentReporter.previousConnection);
else input.connection.connect(argumentReporter.outputConnection);
};

/**
Expand Down Expand Up @@ -621,7 +637,8 @@ Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnDeclaration_ = function(
}

// Attach the block.
input.connection.connect(argumentEditor.outputConnection);
if (type == 'c') input.connection.connect(argumentEditor.previousConnection)
else input.connection.connect(argumentEditor.outputConnection);
};

/**
Expand Down Expand Up @@ -649,6 +666,9 @@ Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_ = function(oldBlock,
if (type == 'a' && oldBlock.type == 'argument_reporter_array') {
return true;
}
if (type == 'c' && oldBlock.type == 'argument_reporter_statement') {
return true;
}
return false;
};

Expand Down Expand Up @@ -676,6 +696,8 @@ Blockly.ScratchBlocks.ProcedureUtils.createArgumentEditor_ = function(
var newBlock = this.workspace.newBlock('argument_editor_object');
} else if (argumentType == 'a') {
var newBlock = this.workspace.newBlock('argument_editor_array');
} else if (argumentType == 'c') {
var newBlock = this.workspace.newBlock('argument_editor_statement')
}
newBlock.setFieldValue(displayName, 'TEXT');
newBlock.setShadow(true);
Expand Down Expand Up @@ -721,6 +743,11 @@ Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_ = function() {
} else {
this.procCode_ += '%s';
}
} else if (input.type == Blockly.NEXT_STATEMENT) {
var target = input.connection.targetBlock();
this.displayNames_.push(target.getFieldValue('TEXT'));
this.argumentIds_.push(input.name);
this.procCode_ += '%c';
} else {
throw new Error(
'Unexpected input type on a procedure mutator root: ' + input.type);
Expand Down Expand Up @@ -801,6 +828,21 @@ Blockly.ScratchBlocks.ProcedureUtils.addArrayExternal = function() {
this.focusLastEditor_();
};

/**
* Externally-visible function to add a statement argument to the procedure
* declaration.
* @public
*/
Blockly.ScratchBlocks.ProcedureUtils.addStatementExternal = function() {
Blockly.WidgetDiv.hide(true);
this.procCode_ = this.procCode_ + ' %c';
this.displayNames_.push('statement');
this.argumentIds_.push('SUBSTACK' + Blockly.utils.genUid());
this.argumentDefaults_.push('');
this.updateDisplay_();
this.focusLastEditor_();
};

/**
* Externally-visible function to add a string/number argument to the procedure
* declaration.
Expand Down Expand Up @@ -853,18 +895,19 @@ Blockly.ScratchBlocks.ProcedureUtils.removeFieldCallback = function(field) {
return;
}
var inputNameToRemove = null;
const cannotRemove = (i) => i == 0 && this.inputList[1].type == Blockly.NEXT_STATEMENT
for (var n = 0; n < this.inputList.length; n++) {
var input = this.inputList[n];
if (input.connection) {
var target = input.connection.targetBlock();
if (target.getField(field.name) == field) {
if (cannotRemove(n)) return
inputNameToRemove = input.name;
}
} else {
for (var j = 0; j < input.fieldRow.length; j++) {
if (input.fieldRow[j] == field) {
inputNameToRemove = input.name;
}
if (input.fieldRow[0] == field) {
if (cannotRemove(n)) return
inputNameToRemove = input.name;
}
}
}
Expand Down Expand Up @@ -914,7 +957,8 @@ Blockly.ScratchBlocks.ProcedureUtils.updateArgumentReporterNames_ = function(pre
if ((block.type === 'argument_reporter_string_number' ||
block.type === 'argument_reporter_boolean' ||
block.type === 'argument_reporter_object' ||
block.type === 'argument_reporter_array'
block.type === 'argument_reporter_array' ||
block.type === 'argument_reporter_statement'
) &&
!block.isShadow()) { // Exclude arg reporters in the prototype block, which are shadows.
argReporters.push(block);
Expand Down Expand Up @@ -1079,6 +1123,7 @@ Blockly.Blocks['procedures_declaration'] = {
addBooleanExternal: Blockly.ScratchBlocks.ProcedureUtils.addBooleanExternal,
addObjectExternal: Blockly.ScratchBlocks.ProcedureUtils.addObjectExternal,
addArrayExternal: Blockly.ScratchBlocks.ProcedureUtils.addArrayExternal,
addStatementExternal: Blockly.ScratchBlocks.ProcedureUtils.addStatementExternal,
addStringNumberExternal: Blockly.ScratchBlocks.ProcedureUtils.addStringNumberExternal,
onChangeFn: Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_,
// For colour fixing of the fields when on the GUI side look at the GUI!!!.
Expand Down Expand Up @@ -1164,6 +1209,23 @@ Blockly.Blocks['argument_reporter_string_number'] = {
domToMutation: argumentReporterDomToMutation
};

Blockly.Blocks['argument_reporter_statement'] = {
init: function() {
this.jsonInit({ "message0": " %1",
"args0": [
{
"type": "field_label_serializable",
"name": "VALUE",
"text": ""
}
],
"extensions": ["colours_more", "shape_statement"]
});
},
mutationToDom: argumentReporterMutationToDom,
domToMutation: argumentReporterDomToMutation
};

Blockly.Blocks['argument_editor_boolean'] = {
init: function() {
this.jsonInit({ "message0": " %1",
Expand All @@ -1174,11 +1236,7 @@ Blockly.Blocks['argument_editor_boolean'] = {
"text": "foo"
}
],
"colour": Blockly.Colours.textField,
"colourSecondary": Blockly.Colours.textField,
"colourTertiary": Blockly.Colours.textField,
"colourQuaternary": Blockly.Colours.textField,
"extensions": ["output_boolean"]
"extensions": ["colours_more", "output_boolean"]
});
},
// Exist on declaration and arguments editors, with different implementations.
Expand All @@ -1195,11 +1253,7 @@ Blockly.Blocks['argument_editor_object'] = {
"text": "foo"
}
],
"colour": Blockly.Colours.textField,
"colourSecondary": Blockly.Colours.textField,
"colourTertiary": Blockly.Colours.textField,
"colourQuaternary": Blockly.Colours.textField,
"extensions": ["output_object"]
"extensions": ["colours_more", "output_object"]
});
},
// Exist on declaration and arguments editors, with different implementations.
Expand All @@ -1216,11 +1270,24 @@ Blockly.Blocks['argument_editor_array'] = {
"text": "foo"
}
],
"colour": Blockly.Colours.textField,
"colourSecondary": Blockly.Colours.textField,
"colourTertiary": Blockly.Colours.textField,
"colourQuaternary": Blockly.Colours.textField,
"extensions": ["output_array"]
"extensions": ["colours_more", "output_array"]
});
},
// Exist on declaration and arguments editors, with different implementations.
removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_
};

Blockly.Blocks['argument_editor_statement'] = {
init: function() {
this.jsonInit({ "message0": " %1",
"args0": [
{
"type": "field_input_removable",
"name": "TEXT",
"text": "foo"
}
],
"extensions": ["colours_more", "shape_statement"]
});
},
// Exist on declaration and arguments editors, with different implementations.
Expand All @@ -1237,11 +1304,7 @@ Blockly.Blocks['argument_editor_string_number'] = {
"text": "foo"
}
],
"colour": Blockly.Colours.textField,
"colourSecondary": Blockly.Colours.textField,
"colourTertiary": Blockly.Colours.textField,
"colourQuaternary": Blockly.Colours.textField,
"extensions": ["output_number", "output_string"]
"extensions": ["colours_more", "output_number", "output_string"]
});
},
// Exist on declaration and arguments editors, with different implementations.
Expand Down
8 changes: 4 additions & 4 deletions blocks_vertical/vertical_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Blockly.ScratchBlocks.VerticalExtensions.COLOUR_TEXTFIELD = function() {
*/
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_STATEMENT = function() {
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setPreviousStatement(true, "normal");
this.setNextStatement(true, "normal");
};

/**
Expand All @@ -89,7 +89,7 @@ Blockly.ScratchBlocks.VerticalExtensions.SHAPE_STATEMENT = function() {
*/
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_HAT = function() {
this.setInputsInline(true);
this.setNextStatement(true, null);
this.setNextStatement(true, "normal");
};

/**
Expand All @@ -101,7 +101,7 @@ Blockly.ScratchBlocks.VerticalExtensions.SHAPE_HAT = function() {
*/
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_END = function() {
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setPreviousStatement(true, "normal");
};

/**
Expand Down
8 changes: 5 additions & 3 deletions core/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ Blockly.Block.prototype.setFieldValue = function(newValue, name) {
Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
if (newBoolean) {
if (opt_check === undefined) {
opt_check = null;
opt_check = "normal";
}
if (!this.previousConnection) {
goog.asserts.assert(!this.outputConnection,
Expand Down Expand Up @@ -1065,7 +1065,7 @@ Blockly.Block.prototype.setPreviousStatement = function(newBoolean, opt_check) {
Blockly.Block.prototype.setNextStatement = function(newBoolean, opt_check) {
if (newBoolean) {
if (opt_check === undefined) {
opt_check = null;
opt_check = "normal";
}
if (!this.nextConnection) {
this.nextConnection = this.makeConnection_(Blockly.NEXT_STATEMENT);
Expand Down Expand Up @@ -1255,7 +1255,9 @@ Blockly.Block.prototype.appendValueInput = function(name) {
* @return {!Blockly.Input} The input object created.
*/
Blockly.Block.prototype.appendStatementInput = function(name) {
return this.appendInput_(Blockly.NEXT_STATEMENT, name);
let output = this.appendInput_(Blockly.NEXT_STATEMENT, name);
output.setCheck("normal")
return output
};

/**
Expand Down
Loading