mirror of
https://github.com/google/blockly.git
synced 2026-01-14 20:37:10 +01:00
variables_dynamic
This commit is contained in:
137
blocks/variables_dynamic.js
Normal file
137
blocks/variables_dynamic.js
Normal file
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Variable blocks for Blockly.
|
||||
|
||||
* This file is scraped to extract a .json file of block definitions. The array
|
||||
* passed to defineBlocksWithJsonArray(..) must be strict JSON: double quotes
|
||||
* only, no outside references, no functions, no trailing commas, etc. The one
|
||||
* exception is end-of-line comments, which the scraper will remove.
|
||||
* @author duzc2dtw@gmail.com (Du Tian Wei)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Constants.VariablesDynamic');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
* Common HSV hue for all blocks in this category.
|
||||
* Should be the same as Blockly.Msg.VARIABLES_DYNAMIC_HUE.
|
||||
* @readonly
|
||||
*/
|
||||
Blockly.Constants.VariablesDynamic.HUE = 310;
|
||||
|
||||
Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
|
||||
// Block for variable getter.
|
||||
{
|
||||
"type": "variables_get_dynamic",
|
||||
"message0": "%1",
|
||||
"args0": [{
|
||||
"type": "field_variable",
|
||||
"name": "VAR",
|
||||
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
|
||||
}],
|
||||
"output": null,
|
||||
"colour": "%{BKY_VARIABLES_DYNAMIC_HUE}",
|
||||
"helpUrl": "%{BKY_VARIABLES_GET_HELPURL}",
|
||||
"tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}",
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"]
|
||||
},
|
||||
// Block for variable setter.
|
||||
{
|
||||
"type": "variables_set_dynamic",
|
||||
"message0": "%{BKY_VARIABLES_SET}",
|
||||
"args0": [{
|
||||
"type": "field_variable",
|
||||
"name": "VAR",
|
||||
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}"
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "VALUE"
|
||||
}
|
||||
],
|
||||
"previousStatement": null,
|
||||
"nextStatement": null,
|
||||
"colour": "%{BKY_VARIABLES_DYNAMIC_HUE}",
|
||||
"tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}",
|
||||
"helpUrl": "%{BKY_VARIABLES_SET_HELPURL}",
|
||||
"extensions": ["contextMenu_variableDynamicSetterGetter"]
|
||||
}
|
||||
]); // END JSON EXTRACT (Do not delete this comment.)
|
||||
|
||||
/**
|
||||
* Mixin to add context menu items to create getter/setter blocks for this
|
||||
* setter/getter.
|
||||
* Used by blocks 'variables_set' and 'variables_get'.
|
||||
* @mixin
|
||||
* @augments Blockly.Block
|
||||
* @package
|
||||
* @readonly
|
||||
*/
|
||||
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
|
||||
/**
|
||||
* Add menu option to create getter/setter block for this setter/getter.
|
||||
* @param {!Array} options List of menu options to add to.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
customContextMenu: function(options) {
|
||||
// Getter blocks have the option to create a setter block, and vice versa.
|
||||
var opposite_type ;
|
||||
var contextMenuMsg ;
|
||||
if (this.type == 'variables_get_dynamic') {
|
||||
opposite_type = 'variables_set_dynamic';
|
||||
contextMenuMsg = Blockly.Msg.VARIABLES_GET_CREATE_SET;
|
||||
} else {
|
||||
opposite_type = 'variables_get_dynamic';
|
||||
contextMenuMsg = Blockly.Msg.VARIABLES_SET_CREATE_GET;
|
||||
}
|
||||
|
||||
var option = { enabled: this.workspace.remainingCapacity() > 0 };
|
||||
var name = this.getFieldValue('VAR');
|
||||
option.text = contextMenuMsg.replace('%1', name);
|
||||
var xmlField = goog.dom.createDom('field', null, name);
|
||||
xmlField.setAttribute('name', 'VAR');
|
||||
var variableModel = this.workspace.getVariable(name);
|
||||
xmlField.setAttribute('variabletype', variableModel.type);
|
||||
var xmlBlock = goog.dom.createDom('block', null, xmlField);
|
||||
xmlBlock.setAttribute('type', opposite_type);
|
||||
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
|
||||
options.push(option);
|
||||
},
|
||||
onchange: function() {
|
||||
var name = this.getFieldValue('VAR');
|
||||
var variableModel = this.workspace.getVariable(name);
|
||||
this.getField("VAR").variableTypes = [variableModel.type];
|
||||
if (this.type == 'variables_get_dynamic') {
|
||||
this.outputConnection.setCheck(variableModel.type);
|
||||
} else {
|
||||
this.getInput("VALUE").connection.setCheck(variableModel.type);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter',
|
||||
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
|
||||
@@ -241,6 +241,13 @@ Blockly.DELETE_AREA_TOOLBOX = 2;
|
||||
* @const {string}
|
||||
*/
|
||||
Blockly.VARIABLE_CATEGORY_NAME = 'VARIABLE';
|
||||
/**
|
||||
* String for use in the "custom" attribute of a category in toolbox xml.
|
||||
* This string indicates that the category should be dynamically populated with
|
||||
* variable blocks.
|
||||
* @const {string}
|
||||
*/
|
||||
Blockly.VARIABLE_DYNAMIC_CATEGORY_NAME = 'VARIABLES_DYNAMIC';
|
||||
|
||||
/**
|
||||
* String for use in the "custom" attribute of a category in toolbox xml.
|
||||
|
||||
127
core/variables_dynamic.js
Normal file
127
core/variables_dynamic.js
Normal file
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Utility functions for handling variables dynamic.
|
||||
*
|
||||
* @author duzc2dtw@gmail.com (Du Tian Wei)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.VariablesDynamic');
|
||||
|
||||
goog.require('Blockly.Variables');
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly.constants');
|
||||
goog.require('Blockly.VariableModel');
|
||||
goog.require('Blockly.Workspace');
|
||||
goog.require('goog.string');
|
||||
|
||||
|
||||
/**
|
||||
* Prompt the user for a new variable name.
|
||||
* @param {string} promptText The string of the prompt.
|
||||
* @param {string} defaultText The default value to show in the prompt's field.
|
||||
* @param {function(?string)} callback A callback. It will return the new
|
||||
* variable name, or null if the user picked something illegal.
|
||||
*/
|
||||
Blockly.VariablesDynamic.promptType = function(promptText, defaultText, callback) {
|
||||
Blockly.prompt(promptText, defaultText, function(newVarType) {
|
||||
// Merge runs of whitespace. Strip leading and trailing whitespace.
|
||||
// Beyond this, all types are legal.
|
||||
if (newVarType) {
|
||||
newVarType = newVarType.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
|
||||
}
|
||||
callback(newVarType);
|
||||
});
|
||||
};
|
||||
Blockly.VariablesDynamic.onCreateVariableButtonClick = function(button) {
|
||||
Blockly.VariablesDynamic.promptType(Blockly.Msg.NEW_VARIABLE_TYPE_TITLE, '', function(type) {
|
||||
if (type) {
|
||||
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, type);
|
||||
}
|
||||
});
|
||||
// workspace.createVariable("abc", "string");
|
||||
// workspace.createVariable("123", "number");
|
||||
// workspace.createVariable("abcd", "string");
|
||||
};
|
||||
/**
|
||||
* Construct the elements (blocks and button) required by the flyout for the
|
||||
* variable category.
|
||||
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
||||
* @return {!Array.<!Element>} Array of XML elements.
|
||||
*/
|
||||
Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
|
||||
var xmlList = [];
|
||||
var button = goog.dom.createDom('button');
|
||||
button.setAttribute('text', Blockly.Msg.NEW_VARIABLE);
|
||||
button.setAttribute('callbackKey', 'CREATE_VARIABLE');
|
||||
|
||||
workspace.registerButtonCallback('CREATE_VARIABLE', Blockly.VariablesDynamic.onCreateVariableButtonClick);
|
||||
|
||||
xmlList.push(button);
|
||||
|
||||
var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
|
||||
xmlList = xmlList.concat(blockList);
|
||||
return xmlList;
|
||||
};
|
||||
|
||||
/**
|
||||
* Construct the blocks required by the flyout for the variable category.
|
||||
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
||||
* @return {!Array.<!Element>} Array of XML block elements.
|
||||
*/
|
||||
Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) {
|
||||
var variableModelList = workspace.getAllVariables();
|
||||
variableModelList.sort(Blockly.VariableModel.compareByName);
|
||||
|
||||
var xmlList = [];
|
||||
if (variableModelList.length > 0) {
|
||||
|
||||
var varTypes = workspace.getVariableTypes();
|
||||
for (var i in varTypes) {
|
||||
var varType = varTypes[i];
|
||||
var variableModelListOfType = workspace.getVariablesOfType(varType);
|
||||
var firstVariable = variableModelListOfType[0];
|
||||
if (Blockly.Blocks['variables_set_dynamic']) {
|
||||
var gap = i == varTypes.length - 1 ? 24 : 8;
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_set_dynamic" gap="' + gap + '">' +
|
||||
Blockly.Variables.generateVariableFieldXml_(firstVariable) +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
for (var i = 0, variable; variable = variableModelList[i]; i++) {
|
||||
if (Blockly.Blocks['variables_get_dynamic']) {
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_get_dynamic" gap="8">' +
|
||||
Blockly.Variables.generateVariableFieldXml_(variable) +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
return xmlList;
|
||||
};
|
||||
@@ -104,6 +104,8 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface
|
||||
|
||||
this.registerToolboxCategoryCallback(Blockly.VARIABLE_CATEGORY_NAME,
|
||||
Blockly.Variables.flyoutCategory);
|
||||
this.registerToolboxCategoryCallback(Blockly.VARIABLE_DYNAMIC_CATEGORY_NAME,
|
||||
Blockly.VariablesDynamic.flyoutCategory);
|
||||
this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME,
|
||||
Blockly.Procedures.flyoutCategory);
|
||||
};
|
||||
|
||||
@@ -62,6 +62,8 @@ Blockly.Msg.LISTS_HUE = '260';
|
||||
Blockly.Msg.COLOUR_HUE = '20';
|
||||
/// {{Notranslate}} Hue value for all variable blocks.
|
||||
Blockly.Msg.VARIABLES_HUE = '330';
|
||||
/// {{Notranslate}} Hue value for all variable dynamic blocks.
|
||||
Blockly.Msg.VARIABLES_DYNAMIC_HUE = '310';
|
||||
/// {{Notranslate}} Hue value for all procedure blocks.
|
||||
Blockly.Msg.PROCEDURES_HUE = '290';
|
||||
|
||||
@@ -121,6 +123,8 @@ Blockly.Msg.RENAME_VARIABLE_TITLE = 'Rename all "%1" variables to:';
|
||||
// Variable creation
|
||||
/// button text - Text on the button used to launch the variable creation dialogue.
|
||||
Blockly.Msg.NEW_VARIABLE = 'Create variable...';
|
||||
/// prompt - Prompts the user to enter the type for a variable.
|
||||
Blockly.Msg.NEW_VARIABLE_TYPE_TITLE = 'New variable type:';
|
||||
/// prompt - Prompts the user to enter the name for a new variable. See [https://github.com/google/blockly/wiki/Variables#dropdown-menu https://github.com/google/blockly/wiki/Variables#dropdown-menu].
|
||||
Blockly.Msg.NEW_VARIABLE_TITLE = 'New variable name:';
|
||||
/// alert - Tells the user that the name they entered is already in use.
|
||||
|
||||
Reference in New Issue
Block a user