mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Allow custom gaps to be specified between toolbox blocks.
This commit is contained in:
parent
d07d103f1d
commit
b10ef6da48
4 changed files with 98 additions and 79 deletions
|
@ -356,39 +356,39 @@ Blockly.Flyout.prototype.show = function(xmlList) {
|
|||
this.hide();
|
||||
// Delete any blocks from a previous showing.
|
||||
var blocks = this.workspace_.getTopBlocks(false);
|
||||
for (var x = 0, block; block = blocks[x]; x++) {
|
||||
for (var i = 0, block; block = blocks[i]; i++) {
|
||||
if (block.workspace == this.workspace_) {
|
||||
block.dispose(false, false);
|
||||
}
|
||||
}
|
||||
// Delete any background buttons from a previous showing.
|
||||
for (var x = 0, rect; rect = this.buttons_[x]; x++) {
|
||||
for (var i = 0, rect; rect = this.buttons_[i]; i++) {
|
||||
goog.dom.removeNode(rect);
|
||||
}
|
||||
this.buttons_.length = 0;
|
||||
|
||||
if (xmlList == Blockly.Variables.NAME_TYPE) {
|
||||
// Special category for variables.
|
||||
xmlList =
|
||||
Blockly.Variables.flyoutCategory(this.workspace_.targetWorkspace);
|
||||
} else if (xmlList == Blockly.Procedures.NAME_TYPE) {
|
||||
// Special category for procedures.
|
||||
xmlList =
|
||||
Blockly.Procedures.flyoutCategory(this.workspace_.targetWorkspace);
|
||||
}
|
||||
|
||||
var margin = this.CORNER_RADIUS;
|
||||
this.svgGroup_.style.display = 'block';
|
||||
|
||||
// Create the blocks to be shown in this flyout.
|
||||
var blocks = [];
|
||||
var gaps = [];
|
||||
if (xmlList == Blockly.Variables.NAME_TYPE) {
|
||||
// Special category for variables.
|
||||
Blockly.Variables.flyoutCategory(blocks, gaps, margin,
|
||||
/** @type {!Blockly.Workspace} */ (this.workspace_));
|
||||
} else if (xmlList == Blockly.Procedures.NAME_TYPE) {
|
||||
// Special category for procedures.
|
||||
Blockly.Procedures.flyoutCategory(blocks, gaps, margin,
|
||||
/** @type {!Blockly.Workspace} */ (this.workspace_));
|
||||
} else {
|
||||
for (var i = 0, xml; xml = xmlList[i]; i++) {
|
||||
if (xml.tagName && xml.tagName.toUpperCase() == 'BLOCK') {
|
||||
var block = Blockly.Xml.domToBlock(
|
||||
/** @type {!Blockly.Workspace} */ (this.workspace_), xml);
|
||||
blocks.push(block);
|
||||
gaps.push(margin * 3);
|
||||
}
|
||||
for (var i = 0, xml; xml = xmlList[i]; i++) {
|
||||
if (xml.tagName && xml.tagName.toUpperCase() == 'BLOCK') {
|
||||
var block = Blockly.Xml.domToBlock(
|
||||
/** @type {!Blockly.Workspace} */ (this.workspace_), xml);
|
||||
blocks.push(block);
|
||||
var gap = parseInt(xml.getAttribute('gap'), 10);
|
||||
gaps.push(gap || margin * 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -152,53 +152,65 @@ Blockly.Procedures.rename = function(text) {
|
|||
|
||||
/**
|
||||
* Construct the blocks required by the flyout for the procedure category.
|
||||
* @param {!Array.<!Blockly.Block>} blocks List of blocks to show.
|
||||
* @param {!Array.<number>} gaps List of widths between blocks.
|
||||
* @param {number} margin Standard margin width for calculating gaps.
|
||||
* @param {!Blockly.Workspace} workspace The flyout's workspace.
|
||||
* @param {!Blockly.Workspace} workspace The workspace contianing procedures.
|
||||
* @return {!Array.<!Element>} Array of XML block elements.
|
||||
*/
|
||||
Blockly.Procedures.flyoutCategory = function(blocks, gaps, margin, workspace) {
|
||||
Blockly.Procedures.flyoutCategory = function(workspace) {
|
||||
var xmlList = [];
|
||||
if (Blockly.Blocks['procedures_defnoreturn']) {
|
||||
var block = Blockly.Block.obtain(workspace, 'procedures_defnoreturn');
|
||||
block.initSvg();
|
||||
blocks.push(block);
|
||||
gaps.push(margin * 2);
|
||||
// <block type="procedures_defnoreturn" gap="16"></block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', 'procedures_defnoreturn');
|
||||
block.setAttribute('gap', 16);
|
||||
xmlList.push(block);
|
||||
}
|
||||
if (Blockly.Blocks['procedures_defreturn']) {
|
||||
var block = Blockly.Block.obtain(workspace, 'procedures_defreturn');
|
||||
block.initSvg();
|
||||
blocks.push(block);
|
||||
gaps.push(margin * 2);
|
||||
// <block type="procedures_defreturn" gap="16"></block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', 'procedures_defreturn');
|
||||
block.setAttribute('gap', 16);
|
||||
xmlList.push(block);
|
||||
}
|
||||
if (Blockly.Blocks['procedures_ifreturn']) {
|
||||
var block = Blockly.Block.obtain(workspace, 'procedures_ifreturn');
|
||||
block.initSvg();
|
||||
blocks.push(block);
|
||||
gaps.push(margin * 2);
|
||||
// <block type="procedures_ifreturn" gap="16"></block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', 'procedures_ifreturn');
|
||||
block.setAttribute('gap', 16);
|
||||
xmlList.push(block);
|
||||
}
|
||||
if (gaps.length) {
|
||||
if (xmlList.length) {
|
||||
// Add slightly larger gap between system blocks and user calls.
|
||||
gaps[gaps.length - 1] = margin * 3;
|
||||
xmlList[xmlList.length - 1].setAttribute('gap', 24);
|
||||
}
|
||||
|
||||
function populateProcedures(procedureList, templateName) {
|
||||
for (var x = 0; x < procedureList.length; x++) {
|
||||
var block = Blockly.Block.obtain(workspace, templateName);
|
||||
block.setFieldValue(procedureList[x][0], 'NAME');
|
||||
var tempIds = [];
|
||||
for (var t = 0; t < procedureList[x][1].length; t++) {
|
||||
tempIds[t] = 'ARG' + t;
|
||||
for (var i = 0; i < procedureList.length; i++) {
|
||||
var name = procedureList[i][0];
|
||||
var args = procedureList[i][1];
|
||||
// <block type="procedures_callnoreturn" gap="16">
|
||||
// <mutation name="do something">
|
||||
// <arg name="x"></arg>
|
||||
// </mutation>
|
||||
// </block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', templateName);
|
||||
block.setAttribute('gap', 16);
|
||||
var mutation = goog.dom.createDom('mutation');
|
||||
mutation.setAttribute('name', name);
|
||||
block.appendChild(mutation);
|
||||
for (var t = 0; t < args.length; t++) {
|
||||
var arg = goog.dom.createDom('arg');
|
||||
arg.setAttribute('name', args[t]);
|
||||
mutation.appendChild(arg);
|
||||
}
|
||||
block.setProcedureParameters(procedureList[x][1], tempIds);
|
||||
block.initSvg();
|
||||
blocks.push(block);
|
||||
gaps.push(margin * 2);
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
|
||||
var tuple = Blockly.Procedures.allProcedures(workspace.targetWorkspace);
|
||||
var tuple = Blockly.Procedures.allProcedures(workspace);
|
||||
populateProcedures(tuple[0], 'procedures_callnoreturn');
|
||||
populateProcedures(tuple[1], 'procedures_callreturn');
|
||||
return xmlList;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -93,43 +93,50 @@ Blockly.Variables.renameVariable = function(oldName, newName, workspace) {
|
|||
|
||||
/**
|
||||
* Construct the blocks required by the flyout for the variable category.
|
||||
* @param {!Array.<!Blockly.Block>} blocks List of blocks to show.
|
||||
* @param {!Array.<number>} gaps List of widths between blocks.
|
||||
* @param {number} margin Standard margin width for calculating gaps.
|
||||
* @param {!Blockly.Workspace} workspace The flyout's workspace.
|
||||
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
||||
* @return {!Array.<!Element>} Array of XML block elements.
|
||||
*/
|
||||
Blockly.Variables.flyoutCategory = function(blocks, gaps, margin, workspace) {
|
||||
var variableList = Blockly.Variables.allVariables(workspace.targetWorkspace);
|
||||
Blockly.Variables.flyoutCategory = function(workspace) {
|
||||
var variableList = Blockly.Variables.allVariables(workspace);
|
||||
variableList.sort(goog.string.caseInsensitiveCompare);
|
||||
// In addition to the user's variables, we also want to display the default
|
||||
// variable name at the top. We also don't want this duplicated if the
|
||||
// user has created a variable of the same name.
|
||||
variableList.unshift(null);
|
||||
var defaultVariable = undefined;
|
||||
goog.array.remove(variableList, Blockly.Msg.VARIABLES_DEFAULT_NAME);
|
||||
variableList.unshift(Blockly.Msg.VARIABLES_DEFAULT_NAME);
|
||||
|
||||
var xmlList = [];
|
||||
for (var i = 0; i < variableList.length; i++) {
|
||||
if (variableList[i] === defaultVariable) {
|
||||
continue;
|
||||
if (Blockly.Blocks['variables_set']) {
|
||||
// <block type="variables_set" gap="8">
|
||||
// <field name="VAR">item</field>
|
||||
// </block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', 'variables_set');
|
||||
if (Blockly.Blocks['variables_get']) {
|
||||
block.setAttribute('gap', 8);
|
||||
}
|
||||
var field = goog.dom.createDom('field', null, variableList[i]);
|
||||
field.setAttribute('name', 'VAR');
|
||||
block.appendChild(field);
|
||||
xmlList.push(block);
|
||||
}
|
||||
var getBlock = Blockly.Blocks['variables_get'] ?
|
||||
Blockly.Block.obtain(workspace, 'variables_get') : null;
|
||||
getBlock && getBlock.initSvg();
|
||||
var setBlock = Blockly.Blocks['variables_set'] ?
|
||||
Blockly.Block.obtain(workspace, 'variables_set') : null;
|
||||
setBlock && setBlock.initSvg();
|
||||
if (variableList[i] === null) {
|
||||
defaultVariable = (getBlock || setBlock).getVars()[0];
|
||||
} else {
|
||||
getBlock && getBlock.setFieldValue(variableList[i], 'VAR');
|
||||
setBlock && setBlock.setFieldValue(variableList[i], 'VAR');
|
||||
}
|
||||
setBlock && blocks.push(setBlock);
|
||||
getBlock && blocks.push(getBlock);
|
||||
if (getBlock && setBlock) {
|
||||
gaps.push(margin, margin * 3);
|
||||
} else {
|
||||
gaps.push(margin * 2);
|
||||
if (Blockly.Blocks['variables_get']) {
|
||||
// <block type="variables_get" gap="24">
|
||||
// <field name="VAR">item</field>
|
||||
// </block>
|
||||
var block = goog.dom.createDom('block');
|
||||
block.setAttribute('type', 'variables_get');
|
||||
if (Blockly.Blocks['variables_set']) {
|
||||
block.setAttribute('gap', 24);
|
||||
}
|
||||
var field = goog.dom.createDom('field', null, variableList[i]);
|
||||
field.setAttribute('name', 'VAR');
|
||||
block.appendChild(field);
|
||||
xmlList.push(block);
|
||||
}
|
||||
}
|
||||
return xmlList;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -228,7 +228,7 @@ h1 {
|
|||
<block type="logic_operation"></block>
|
||||
<block type="logic_negate"></block>
|
||||
<block type="logic_boolean"></block>
|
||||
<block type="logic_null"></block>
|
||||
<block type="logic_null" disabled="true"></block>
|
||||
<block type="logic_ternary"></block>
|
||||
</category>
|
||||
<category name="Loops" colour="120">
|
||||
|
@ -262,7 +262,7 @@ h1 {
|
|||
<block type="controls_flow_statements"></block>
|
||||
</category>
|
||||
<category name="Math" colour="230">
|
||||
<block type="math_number"></block>
|
||||
<block type="math_number" gap="32"></block>
|
||||
<block type="math_arithmetic">
|
||||
<value name="A">
|
||||
<shadow type="math_number">
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue