/** * Construct the blocks required by the flyout for the structs category. * @param {!Blockly.Workspace} workspace The workspace this flyout is for. * @return {!Array.<!Element>} Array of XML block elements. */Blockly.Variables.flyoutStructBlocks = function(workspace) { var structs = workspace.getVariablesOfType('Struct'); var struct_fields = workspace.getVariablesOfType('Struct-Field'); var xmlList = []; if (structs.length > 0) { var mostRecentStruct = structs[structs.length - 1]; if (Blockly.Blocks['structs_is']) { var block = Blockly.utils.xml.createElement('block'); block.setAttribute('type', 'structs_is'); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( mostRecentStruct)); xmlList.push(block); } if (Blockly.Blocks['structs_make']) { for (var i = 0; i < structs.length; i++) { var block = Blockly.utils.xml.createElement('block'); var fields_arr = Blockly.Variables.getStructFields(workspace,toString(structs[i].name)); var gap = 24; var blockText = '<xml>' + '<block type="structs_make" gap="' + gap + '">' + '<data>' + fields_arr.length + '</data>' + '<field name="VAR" variabletype="Struct">' + structs[i].name + '</field>' + '</block>' + '</xml>'; var block = Blockly.Xml.textToDom(blockText).firstChild; xmlList.push(block); } } } if (struct_fields.length > 0) { if (Blockly.Blocks['structs_field_get']) { // struct_fields.sort(Blockly.VariableModel.compareByName); var block = Blockly.utils.xml.createElement('block'); block.setAttribute('type', 'structs_field_get'); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( struct_fields[struct_fields.length - 1])); xmlList.push(block); } } return xmlList;};Blockly.Blocks['structs_make'] = { init: function() { this.appendDummyInput() .appendField("(make-") .appendField(new Blockly.FieldVariable("", null, ['Struct'], 'Struct'), "VAR"); for (var i = 0; i < parseInt(this.data); i++) { this.appendValueInput("FIELD" + i).setCheck(null); } this.appendDummyInput().appendField(")"); this.setInputsInline(true); this.setOutput(true, "Struct"); this.setStyle("struct_blocks"); this.setTooltip(Blockly.Msg['STRUCTS_MAKE_TOOLTIP']); this.setHelpUrl("https://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._define-struct%29%29"); this.setEditable(false); }, };init: function(num) {...}block.appendChild(Blockly.Variables.generateVariableFieldDom( structs[i]));if (Blockly.Blocks['structs_make_0']) { for (var i = 0; i < structs.length; i++) { var block = Blockly.utils.xml.createElement('block'); var fields_num = Blockly.Variables.STRUCTS[structs[i].name].length; block.setAttribute('type', 'structs_make_' + fields_num); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( structs[i])); xmlList.push(block); } }
Blockly.Variables.flyoutStructBlocks = function(workspace) { var structs = workspace.getVariablesOfType('Struct'); console.log(structs); var struct_fields = workspace.getVariablesOfType('Struct-Field'); var xmlList = []; if (structs.length > 0) { // New variables are added to the end of the variableModelList. var mostRecentStruct = structs[structs.length - 1]; if (Blockly.Blocks['structs_is']) { var block = Blockly.utils.xml.createElement('block'); block.setAttribute('type', 'structs_is'); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( mostRecentStruct)); xmlList.push(block); } if (Blockly.Blocks['structs_make']) { for (var i = 0; i < structs.length; i++) { var block = Blockly.utils.xml.createElement('block'); var fields_num = Blockly.Variables.getFieldNum(workspace, structs[i].name); var gap = 24; var blockText = '<xml>' + '<block type="structs_make" gap="' + gap + '">' + '<field name="VAR" variabletype="Struct">' + structs[i].name + '</field>' + '<mutation items="' + fields_num + '"></mutation> ' + '</block>' + '</xml>'; var block = Blockly.Xml.textToDom(blockText).firstChild; xmlList.push(block); } } } if (struct_fields.length > 0) { if (Blockly.Blocks['structs_field_get']) { // struct_fields.sort(Blockly.VariableModel.compareByName); var block = Blockly.utils.xml.createElement('block'); block.setAttribute('type', 'structs_field_get'); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( struct_fields[struct_fields.length - 1])); xmlList.push(block); } } return xmlList;};
// block:
Blockly.Blocks['structs_make'] = { init: function() { this.appendDummyInput() .appendField("(make-") .appendField(new Blockly.FieldVariable("", null, ['Struct'], 'Struct'), "VAR"); this.updateShape_(); this.setInputsInline(true); this.setOutput(true, "Struct"); this.setStyle("struct_blocks"); this.setTooltip(Blockly.Msg['STRUCTS_MAKE_TOOLTIP']); this.setHelpUrl("https://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._define-struct%29%29"); this.setEditable(false); }, /** * Create XML to represent list inputs. * @return {!Element} XML storage element. * @this {Blockly.Block} */ mutationToDom: function() { var container = Blockly.utils.xml.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, /** * Parse XML to restore the list inputs. * @param {!Element} xmlElement XML storage element. * @this {Blockly.Block} */ domToMutation: function(xmlElement) { this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); this.updateShape_(); }, /** * Modify this block to have the correct number of inputs. * @private * @this {Blockly.Block} */ updateShape_: function() { this.removeInput('END', true); // Add inputs. for (var i = 0; i < this.itemCount_; i++) { if (!this.getInput('FIELD' + i)) { this.appendValueInput('FIELD' + i); } } this.appendDummyInput('END').appendField(')'); }};
--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/85c0f74d-5a2d-4b89-aa8b-787c37a8d82a%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/91ab8b72-546c-4a11-853c-a8061641bcc6%40googlegroups.com.
Blockly.Variables.createStructButtonHandler = function( workspace, opt_callback, opt_type) { var type = opt_type || 'Struct'; // This function needs to be named so it can be called recursively. var promptAndCheckWithAlert = function(defaultName) { Blockly.Variables.promptStructName(Blockly.Msg['NEW_STRUCT_TITLE'], defaultName, function(text) { if (text) { // prompt struct-properties: struct-name:field-name1,field-name2,... var input = text.replace(/\s+/g, ""); var struct_list = input.split(":"); var struct_name = struct_list[0]; var field_list; if (struct_list.length > 1) { field_list = struct_list[1].split(","); } var existing = Blockly.Variables.nameUsedWithAnyType_(struct_name, workspace); if (existing) { if (existing.type == type) { var msg = Blockly.Msg['STRUCT_ALREADY_EXISTS'].replace( '%1', existing.name); } else { var msg = Blockly.Msg['VARIABLE_ALREADY_EXISTS_FOR_ANOTHER_TYPE']; msg = msg.replace('%1', existing.name).replace('%2', existing.type); } Blockly.alert(msg, function() { promptAndCheckWithAlert(text); // Recurse }); } else if (struct_name == 'posn') { var msg = Blockly.Msg['STRUCT_ALREADY_EXISTS'].replace( '%1', 'posn'); Blockly.alert(msg, function() { promptAndCheckWithAlert(text); // Recurse }); } else { // No conflict // create struct // store structs with fields to change all components later // (-> rename, delete) Blockly.Variables.updateStructs(struct_name, field_list); workspace.createVariable(struct_name, type); if (struct_list.length > 1) { // struct mit fields for (var i = 0; i < field_list.length; i++) { var field_name = struct_name + "-" + field_list[i]; workspace.createVariable(field_name, 'Struct-Field'); } } if (opt_callback) { opt_callback(text); } } } else { // User canceled prompt. if (opt_callback) { opt_callback(null); } } }); }; promptAndCheckWithAlert('');};
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/91ab8b72-546c-4a11-853c-a8061641bcc6%40googlegroups.com.

function onVariableCreate(event) { if (event.type == Blockly.Events.VAR_CREATE && event.varType == 'Struct') { alert('new struct var') workspace.removeChangeListener(onVariableCreate); } } workspace.addChangeListener(onVariableCreate);
/** * @fileoverview Struct blocks for Blockly. */'use strict';
Blockly.Blocks['structs_is'] = { init: function() { this.appendValueInput("INPUT").setCheck(null).appendField("(") .appendField(new Blockly.FieldVariable("", null, ['Struct'], 'Struct'), "VAR") .appendField("?"); this.appendDummyInput().appendField(")"); this.setInputsInline(true); this.setOutput(true, "Boolean"); this.setStyle("struct_blocks"); this.setTooltip(Blockly.Msg['STRUCTS_IS_TOOLTIP']); this.setHelpUrl("https://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._define-struct%29%29"); }};
Blockly.Blocks['structs_field_get'] = { init: function() { this.appendValueInput("INPUT").setCheck(null).appendField("(") .appendField(new Blockly.FieldVariable("", null, ['Struct-Field'], 'Struct-Field'), "VAR"); this.appendDummyInput().appendField(")"); this.setInputsInline(true); this.setOutput(true, null); this.setStyle("struct_blocks"); this.setTooltip(Blockly.Msg['STRUCTS_FIELD_GET_TOOLTIP']); this.setHelpUrl("https://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._define-struct%29%29"); }};
Blockly.Blocks['structs_make'] = { init: function() { this.appendDummyInput() .appendField("(make-") .appendField(new Blockly.FieldVariable("", null, ['Struct'], 'Struct'), "VAR"); this.itemCount_ = 0; this.updateShape_(); this.setInputsInline(true); this.setOutput(true, "Struct"); this.setStyle("struct_blocks"); this.setTooltip(Blockly.Msg['STRUCTS_MAKE_TOOLTIP']); this.setHelpUrl("https://docs.racket-lang.org/htdp-langs/intermediate-lam.html#%28form._%28%28lib._lang%2Fhtdp-intermediate-lambda..rkt%29._define-struct%29%29"); this.setEditable(false); }, /** * Create XML to represent struct fields. * @return {!Element} XML storage element. * @this {Blockly.Block} */ mutationToDom: function() { var container = Blockly.utils.xml.createElement('mutation'); container.setAttribute('items', this.itemCount_); return container; }, /** * Parse XML to restore the struct fields. * @param {!Element} xmlElement XML storage element. * @this {Blockly.Block} */ domToMutation: function(xmlElement) { this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); this.updateShape_(); }, /** * Modify this block to have the correct number of fields. * @private * @this {Blockly.Block} */ updateShape_: function() { this.removeInput('END', true); // Add fields for (var i = 0; i < this.itemCount_; i++) { if (!this.getInput('FIELD' + i)) { this.appendValueInput('FIELD' + i); } } this.appendDummyInput('END').appendField(')'); }};
<script> // using a Fixed Size Workspace var workspace = Blockly.inject('blocklyDiv', {media: '../../media/', toolbox: document.getElementById('toolbox')}); workspace.registerToolboxCategoryCallback('STRUCT', Blockly.Variables.flyoutStructs);
function onVariableCreate(event) { if (event.type == Blockly.Events.VAR_CREATE && event.varType == 'Struct') { alert('new struct var') workspace.removeChangeListener(onVariableCreate); } } workspace.addChangeListener(onVariableCreate); // added in variables.js in core
Blockly.Variables.STRUCTS = [];
Blockly.Variables.updateStructs = function(name, val) { Blockly.Variables.STRUCTS.push([name, val]); console.log(Blockly.Variables.STRUCTS);};
/** * Handles "Create Struct" button * It will prompt the user for struct-name + field-names, including re-prompts if a name * is already in use among the workspace's variables. * * @param {!Blockly.Workspace} workspace The workspace on which to create the * struct. * @param {function(?string=)=} opt_callback A callback. It will be passed an * acceptable new variable name, or null if change is to be aborted (cancel * button), or undefined if an existing variable was chosen. * @param {string=} opt_type The type of the variable like 'int', 'string', or * ''. This will default to '', which is a specific type. */Blockly.Variables.createStructButtonHandler = function( workspace, opt_callback, opt_type) { var type = opt_type || 'Struct'; // This function needs to be named so it can be called recursively. var promptAndCheckWithAlert = function(defaultName) { Blockly.Variables.promptStructName(Blockly.Msg['NEW_STRUCT_TITLE'], defaultName, function(text) { if (text) { // prompt struct-properties: struct-name:field-name1,field-name2,... var input = text.replace(/\s+/g, ""); var struct_list = input.split(":"); var struct_name = struct_list[0]; var field_list = []; if (struct_list.length > 1) { // struct with fields for (var i = 0; i < field_list.length; i++) { var field_name = struct_name + "-" + field_list[i]; workspace.createVariable(field_name, 'Struct-Field'); } } if (opt_callback) { opt_callback(text); } } } else { // User canceled prompt. if (opt_callback) { opt_callback(null); } } }); }; promptAndCheckWithAlert('');};
/** * Prompt the user for a new struct 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.Variables.promptStructName = function(promptText, defaultText, callback) { Blockly.prompt(promptText, defaultText, function(newVar) { // Merge runs of whitespace. Strip leading and trailing whitespace. // Beyond this, all names are legal. if (newVar) { newVar = newVar.replace(/[\s+&\\#()$~%'"*<>{}=!§]/g,'').trim(); if (newVar == Blockly.Msg['RENAME_STRUCT'] || newVar == Blockly.Msg['NEW_STRUCT']) { // Ok, not ALL names are legal... newVar = null; } } callback(newVar); });};
/** * Construct the blocks required by the flyout for the structs category. * @param {!Blockly.Workspace} workspace The workspace this flyout is for. * @return {!Array.<!Element>} Array of XML block elements. */Blockly.Variables.flyoutStructBlocks = function(workspace) { var struct_fields = workspace.getVariablesOfType('Struct-Field'); var structs = workspace.getVariablesOfType('Struct'); console.log(structs); var STRUCTS = Blockly.Variables.STRUCTS; var struct_num = Blockly.Variables.STRUCTS.length; var xmlList = []; if (struct_num > 0) { if (Blockly.Blocks['structs_is']) { var block = Blockly.utils.xml.createElement('block'); var name = STRUCTS[struct_num - 1][0]; // most recent struct var gap = 24; var blockText = '<xml>' + '<block type="structs_is" gap="' + gap + '">' + '<field name="VAR" variabletype="Struct">' + name + '</field>' + '</block>' + '</xml>'; var block = Blockly.Xml.textToDom(blockText).firstChild; xmlList.push(block);
} if (Blockly.Blocks['structs_make']) { for (var i = 0; i < struct_num; i++) { var block = Blockly.utils.xml.createElement('block'); var name = STRUCTS[i][0]; var fields_num = STRUCTS[i][1].length; var gap = 24; var blockText = '<xml>' + '<block type="structs_make" gap="' + gap + '">' + '<field name="VAR" variabletype="Struct">' + name + '</field>' + '<mutation items="' + fields_num + '"></mutation> ' + '</block>' + '</xml>'; var block = Blockly.Xml.textToDom(blockText).firstChild; xmlList.push(block); } } } if (struct_fields.length > 0) { if (Blockly.Blocks['structs_field_get']) { var block = Blockly.utils.xml.createElement('block'); block.setAttribute('type', 'structs_field_get'); block.setAttribute('gap', 24); block.appendChild(Blockly.Variables.generateVariableFieldDom( struct_fields[struct_fields.length - 1])); xmlList.push(block); } } return xmlList;};
/** * Construct the elements (blocks and button) required by the flyout for the * structs category. * @param {!Blockly.Workspace} workspace The workspace containing variables. * @return {!Array.<!Element>} Array of XML elements. */Blockly.Variables.flyoutStructs = function(workspace) { var xmlList = []; var button = document.createElement('button'); button.setAttribute('text', '%{BKY_NEW_STRUCT}'); button.setAttribute('callbackKey', 'CREATE_STRUCT');
workspace.registerButtonCallback('CREATE_STRUCT', function(button) { Blockly.Variables.createStructButtonHandler(button.getTargetWorkspace(), null, 'Struct'); });
xmlList.push(button);
var blockList = Blockly.Variables.flyoutStructBlocks(workspace); xmlList = xmlList.concat(blockList); return xmlList;};
Blockly.Variables.createStructButtonHandler = function(
workspace, opt_callback, opt_type) {
var type = opt_type || 'Struct';
// This function needs to be named so it can be called recursively.
var promptAndCheckWithAlert = function(defaultName) {
Blockly.Variables.promptStructName(Blockly.Msg['NEW_STRUCT_TITLE'], defaultName,
function(text) {
if (!text) {
// User canceled prompt.
if (opt_callback) {
opt_callback(null);
}
}
// prompt struct-properties: struct-name:field-name1,field-name2,...
var input = text.replace(/\s+/g, "");
var struct_list = input.split(":");
var struct_name = struct_list[0];
var field_list = [];
if (struct_list.length > 1) { // struct with fields
field_list = struct_list[1].split(',');
}
Blockly.Variables.updateStructs(struct_name, field_list);
workspace.createVariable(struct_name, type);
for (var i = 0; i < field_list.length; i++) {
var field_name = struct_name + "-" + field_list[i];
workspace.createVariable(field_name, 'Struct-Field');
}
if (opt_callback) {
opt_callback(text);
}
});
};
promptAndCheckWithAlert('');
};
--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/a9b0888a-b782-45a9-9b95-4c419885caa0%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/10700a71-2744-42c5-8b32-33a5bc41d7bd%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/10700a71-2744-42c5-8b32-33a5bc41d7bd%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/f6b94a3c-c36f-4453-90b9-098eb9ccd3f1%40googlegroups.com.