Adding ability to create a new broadcast message using the dropdown option. Broadcast messages are now typed variables with the variable type as 'broadcast_msg'. Needed to do some extra work to translate variables from blocks to XML correctly because the default broadcast messages were getting added as new variables with the incorrect type upon trying to move any of the broadcast message related blocks from the toolbar into the workspace because it was trying to create a new variable with a different type than already existed. Currently, broadcast message names conflict with variable names, and I need to make it so that isn't so. Additionally, need to update the source block's message name after creation of a new message via the dropdown.

This commit is contained in:
Karishma Chadha 2017-11-16 11:45:59 -05:00
parent b5714adc01
commit 9ddf8a79a9
6 changed files with 70 additions and 27 deletions

View file

@ -315,14 +315,8 @@ 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_OPTION">'+
'<shadow type="event_broadcast_menu"></shadow>'+
'</value>'+
'</block>'+
'<block type="event_broadcastandwait" id="event_broadcastandwait">'+
'<value name="BROADCAST_OPTION">'+
'<shadow type="event_broadcast_menu"></shadow>'+
'</value>'+
'</block>'+
'</category>'+
'<category name="Control" colour="#FFAB19" secondaryColour="#CF8B17">'+

View file

@ -77,13 +77,10 @@ Blockly.Blocks['event_whenbroadcastreceived'] = {
"message0": "when I receive %1",
"args0": [
{
"type": "field_dropdown",
"type": "field_variable",
"name": "BROADCAST_OPTION",
"options": [
['message1', 'message1'],
['message2', 'message2'],
['new message', 'new message']
]
"variableTypes": ["broadcast_msg"],
"variable": "message1"
}
],
"category": Blockly.Categories.event,
@ -153,13 +150,10 @@ Blockly.Blocks['event_broadcast_menu'] = {
"message0": "%1",
"args0": [
{
"type": "field_dropdown",
"type": "field_variable",
"name": "BROADCAST_OPTION",
"options": [
['message1', 'message1'],
['message2', 'message2'],
['new message', 'new message']
]
"variableTypes":["broadcast_msg"],
"variable":"message1"
}
],
"colour": Blockly.Colours.event.secondary,
@ -181,8 +175,10 @@ Blockly.Blocks['event_broadcast'] = {
"message0": "broadcast %1",
"args0": [
{
"type": "input_value",
"name": "BROADCAST_OPTION"
"type": "field_variable",
"name": "BROADCAST_OPTION",
"variableTypes": ["broadcast_msg"],
"variable": "message1"
}
],
"category": Blockly.Categories.event,
@ -201,8 +197,10 @@ Blockly.Blocks['event_broadcastandwait'] = {
"message0": "broadcast %1 and wait",
"args0": [
{
"type": "input_value",
"name": "BROADCAST_OPTION"
"type": "field_variable",
"name": "BROADCAST_OPTION",
"variableTypes": ["broadcast_msg"],
"variable":"message1"
}
],
"category": Blockly.Categories.event,

View file

@ -326,3 +326,12 @@ Blockly.RENAME_VARIABLE_ID = 'RENAME_VARIABLE_ID';
* @const {string}
*/
Blockly.DELETE_VARIABLE_ID = 'DELETE_VARIABLE_ID';
/**
* String for use in the dropdown created in field_variable,
* specifically for broadcast messages.
* This string indicates that this option in the dropdown is 'New message...'
* and if selected, should trigger the prompt to create a new message.
* @const {string}
*/
Blockly.NEW_MESSAGE_ID = 'NEW_MESSAGE_ID';

View file

@ -81,7 +81,18 @@ Blockly.FieldVariable.prototype.initModel = function() {
// For instance, some blocks in the toolbox have variable dropdowns filled
// in by default.
if (!this.sourceBlock_.isInFlyout) {
this.sourceBlock_.workspace.createVariable(this.getValue());
// 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 default 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());
}
}
};
@ -171,6 +182,7 @@ Blockly.FieldVariable.dropdownCreate = function() {
if (this.sourceBlock_) {
workspace = this.sourceBlock_.workspace;
}
var isBroadcastType = false;
if (workspace) {
var variableTypes = this.getVariableTypes_();
var variableModelList = [];
@ -178,6 +190,9 @@ Blockly.FieldVariable.dropdownCreate = function() {
// doesn't modify the workspace's list.
for (var i = 0; i < variableTypes.length; i++) {
var variableType = variableTypes[i];
if (variableType === 'broadcast_msg'){
isBroadcastType = true;
}
var variables = workspace.getVariablesOfType(variableType);
variableModelList = variableModelList.concat(variables);
}
@ -200,10 +215,14 @@ Blockly.FieldVariable.dropdownCreate = function() {
// Set the uuid as the internal representation of the variable.
options[i] = [variableModelList[i].name, variableModelList[i].getId()];
}
options.push([Blockly.Msg.RENAME_VARIABLE, Blockly.RENAME_VARIABLE_ID]);
if (Blockly.Msg.DELETE_VARIABLE) {
options.push([Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
Blockly.DELETE_VARIABLE_ID]);
if (isBroadcastType) {
options.push([Blockly.Msg.NEW_MESSAGE, Blockly.NEW_MESSAGE_ID]);
} else {
options.push([Blockly.Msg.RENAME_VARIABLE, Blockly.RENAME_VARIABLE_ID]);
if (Blockly.Msg.DELETE_VARIABLE) {
options.push([Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
Blockly.DELETE_VARIABLE_ID]);
}
}
return options;
};
@ -237,6 +256,10 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
// Delete variable.
workspace.deleteVariable(this.getText());
return;
} else if (id == Blockly.NEW_MESSAGE_ID) {
Blockly.Variables.createVariable(workspace, null, 'broadcast_msg');
// TODO update sourceblock's variable name...
return;
}
// Call any validation function, and allow it to override.

View file

@ -116,6 +116,21 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
if (variable) {
container.setAttribute('id', variable.getId());
container.setAttribute('variabletype', variable.type);
} else {
// Above works well for untyped variables, but we need to correctly
// set the type for blocks that exist by default in the toolbox
// (e.g. broadcast messages)
// TODO figure out if we ever need to do something where there's
// more than one element in variableTypes field
// must check that field is an instance of FieldVariable because
// FieldVariableGetter doesn't have getVariableTypes_ function
if (field instanceof Blockly.FieldVariable) {
var variableTypes = field.getVariableTypes_();
if (variableTypes.length === 1) {
container.setAttribute('variabletype', variableTypes[0]);
}
}
}
}
element.appendChild(container);

View file

@ -148,6 +148,10 @@ Blockly.Msg.CANNOT_DELETE_VARIABLE_PROCEDURE = 'Can\'t delete the variable "%1"
/// dropdown choice - Delete the currently selected variable.
Blockly.Msg.DELETE_VARIABLE = 'Delete the "%1" variable';
// Broadcast Message creation
/// dropdown choice - Create a new message.
Blockly.Msg.NEW_MESSAGE = 'New message...';
// Colour Blocks.
/// url - Information about colour.
Blockly.Msg.COLOUR_PICKER_HELPURL = 'https://en.wikipedia.org/wiki/Color';