Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast Inputs and Implicit Message Deletion #1302

Merged
merged 5 commits into from
Dec 27, 2017
Merged
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
6 changes: 6 additions & 0 deletions blocks_vertical/default_toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,14 @@ Blockly.Blocks.defaultToolbox = '<xml id="toolbox-categories" style="display: no
'<block type="event_whenbroadcastreceived" id="event_whenbroadcastreceived">'+
'</block>'+
'<block type="event_broadcast" id="event_broadcast">'+
'<value name="BROADCAST_INPUT">'+
'<shadow type="event_broadcast_menu"></shadow>'+
'</value>'+
'</block>'+
'<block type="event_broadcastandwait" id="event_broadcastandwait">'+
'<value name="BROADCAST_INPUT">'+
'<shadow type="event_broadcast_menu"></shadow>'+
'</value>'+
'</block>'+
'</category>'+
'<category name="Control" colour="#FFAB19" secondaryColour="#CF8B17">'+
Expand Down
12 changes: 4 additions & 8 deletions blocks_vertical/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ Blockly.Blocks['event_broadcast'] = {
"message0": "broadcast %1",
"args0": [
{
"type": "field_variable",
"name": "BROADCAST_OPTION",
"variableTypes": [Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE],
"variable": Blockly.Msg.DEFAULT_BROADCAST_MESSAGE_NAME
"type": "input_value",
"name": "BROADCAST_INPUT"
}
],
"category": Blockly.Categories.event,
Expand All @@ -197,10 +195,8 @@ Blockly.Blocks['event_broadcastandwait'] = {
"message0": "broadcast %1 and wait",
"args0": [
{
"type": "field_variable",
"name": "BROADCAST_OPTION",
"variableTypes": [Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE],
"variable": Blockly.Msg.DEFAULT_BROADCAST_MESSAGE_NAME
"type":"input_value",
"name":"BROADCAST_INPUT"
}
],
"category": Blockly.Categories.event,
Expand Down
41 changes: 21 additions & 20 deletions core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Blockly.FieldVariable = function(varname, opt_validator, opt_variableTypes) {
this.setValue(varname || '');
this.addArgType('variable');
this.variableTypes = opt_variableTypes;
this.variableType = (opt_variableTypes && opt_variableTypes.length == 1) ?
opt_variableTypes[0] : '';
};
goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);

Expand Down Expand Up @@ -84,14 +86,14 @@ Blockly.FieldVariable.prototype.initModel = function() {
// Check if there was exactly one element specified in the
// variableTypes list. This is the list that specifies which types of
// variables to include in the dropdown.
// If there is exactly one element specified, make the variable
// being created this specified type. Else, default behavior is to create
// a scalar variable
if (this.getVariableTypes_().length == 1) {
this.sourceBlock_.workspace.createVariable(this.getValue(),
this.getVariableTypes_()[0]);
} else {
this.sourceBlock_.workspace.createVariable(this.getValue());
this.sourceBlock_.workspace.createVariable(this.getValue(), this.variableType);
} else {
// Using shorter name for this constant
var broadcastMsgType = Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE;
var broadcastVars = this.sourceBlock_.workspace.getVariablesOfType(broadcastMsgType);
if (this.variableType == broadcastMsgType && broadcastVars.length != 0) {
broadcastVars.sort(Blockly.VariableModel.compareByName);

This comment was marked as abuse.

This comment was marked as abuse.

this.setValue(broadcastVars[0].name);
}
}
};
Expand Down Expand Up @@ -182,17 +184,13 @@ Blockly.FieldVariable.dropdownCreate = function() {
if (this.sourceBlock_) {
workspace = this.sourceBlock_.workspace;
}
var isBroadcastType = false;
if (workspace) {
var variableTypes = this.getVariableTypes_();
var variableModelList = [];
// Get a copy of the list, so that adding rename and new variable options
// doesn't modify the workspace's list.
for (var i = 0; i < variableTypes.length; i++) {
var variableType = variableTypes[i];
if (variableType == Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE){
isBroadcastType = true;
}
var variables = workspace.getVariablesOfType(variableType);
variableModelList = variableModelList.concat(variables);
}
Expand All @@ -204,10 +202,15 @@ Blockly.FieldVariable.dropdownCreate = function() {
}
}
}
var isBroadcastType = this.variableType == Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE;
// Ensure that the currently selected variable is an option.
// TODO (#1270): Remove isBroadcastType check here when flyout variables fixed.
if (createSelectedVariable && workspace && !isBroadcastType) {
var newVar = workspace.createVariable(name);
if (createSelectedVariable && workspace) {
var newVar = null;
if (isBroadcastType && workspace.isFlyout) {
newVar = workspace.targetWorkspace.createVariable(name, Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE);
} else {
newVar = workspace.createVariable(name);
}
variableModelList.push(newVar);
}
variableModelList.sort(Blockly.VariableModel.compareByName);
Expand All @@ -217,10 +220,7 @@ Blockly.FieldVariable.dropdownCreate = function() {
options[i] = [variableModelList[i].name, variableModelList[i].getId()];
}
if (isBroadcastType) {
// TODO (#1270): Re-enable create broadcast message dropdown in flyout when fixed.
if (!workspace.isFlyout) {
options.push([Blockly.Msg.NEW_BROADCAST_MESSAGE, Blockly.NEW_BROADCAST_MESSAGE_ID]);
}
options.push([Blockly.Msg.NEW_BROADCAST_MESSAGE, Blockly.NEW_BROADCAST_MESSAGE_ID]);
} else {
options.push([Blockly.Msg.RENAME_VARIABLE, Blockly.RENAME_VARIABLE_ID]);
if (Blockly.Msg.DELETE_VARIABLE) {
Expand Down Expand Up @@ -267,7 +267,8 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
thisField.setValue(newName);
}
};
Blockly.Variables.createVariable(workspace, setName, Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE);
var broadcastMsgWkspc = workspace.isFlyout ? workspace.targetWorkspace : workspace;
Blockly.Variables.createVariable(broadcastMsgWkspc, setName, Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ Blockly.WorkspaceSvg.prototype.createVariable = function(name, opt_type, opt_id)
opt_type, opt_id);
// For performance reasons, only refresh the the toolbox for new variables.
// Variables that already exist should already be there.
if (!variableInMap) {
if (!variableInMap && (opt_type != Blockly.BROADCAST_MESSAGE_VARIABLE_TYPE)) {
this.refreshToolboxSelection_();
}
return newVar;
Expand Down
3 changes: 0 additions & 3 deletions core/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,6 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
goog.asserts.assert(child.isShadow(),
'Shadow block not allowed non-shadow child.');
}
// Ensure this block doesn't have any variable inputs.
goog.asserts.assert(block.getVars().length == 0,
'Shadow blocks cannot have variable fields.');
block.setShadow(true);
}
return block;
Expand Down