Add automatic mode for internal/external input layout.

This commit is contained in:
Neil Fraser
2015-06-10 18:45:15 -07:00
parent 838a6a954b
commit 059c2f1778
126 changed files with 454 additions and 1475 deletions

View File

@@ -94,7 +94,7 @@ Blockly.Block.prototype.fill = function(workspace, prototypeName) {
this.nextConnection = null;
this.previousConnection = null;
this.inputList = [];
this.inputsInline = false;
this.inputsInline = undefined;
this.rendered = false;
this.disabled = false;
this.tooltip = '';
@@ -127,6 +127,8 @@ Blockly.Block.prototype.fill = function(workspace, prototypeName) {
if (goog.isFunction(this.init)) {
this.init();
}
// Record initial inline state.
this.inputsInlineDefault = this.inputsInline;
};
/**
@@ -762,6 +764,33 @@ Blockly.Block.prototype.setInputsInline = function(newBoolean) {
}
};
/**
* Get whether value inputs are arranged horizontally or vertically.
* @return {boolean} True if inputs are horizontal.
*/
Blockly.Block.prototype.getInputsInline = function() {
if (this.inputsInline != undefined) {
// Set explicitly.
return this.inputsInline;
}
// Not defined explicitly. Figure out what would look best.
for (var i = 1; i < this.inputList.length; i++) {
if (this.inputList[i - 1].type == Blockly.DUMMY_INPUT &&
this.inputList[i].type == Blockly.DUMMY_INPUT) {
// Two dummy inputs in a row. Don't inline them.
return false;
}
}
for (var i = 1; i < this.inputList.length; i++) {
if (this.inputList[i - 1].type == Blockly.INPUT_VALUE &&
this.inputList[i].type == Blockly.DUMMY_INPUT) {
// Dummy input after a value input. Inline them.
return true;
}
}
return false;
};
/**
* Set whether the block is disabled or not.
* @param {boolean} disabled True if disabled.

View File

@@ -526,14 +526,17 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
// Option to make block inline.
if (!this.collapsed_) {
for (var i = 0; i < this.inputList.length; i++) {
if (this.inputList[i].type == Blockly.INPUT_VALUE) {
// Only display this option if there is a value input on the block.
for (var i = 1; i < this.inputList.length; i++) {
if (this.inputList[i - 1].type != Blockly.NEXT_STATEMENT &&
this.inputList[i].type != Blockly.NEXT_STATEMENT) {
// Only display this option if there are two value or dummy inputs
// next to each other.
var inlineOption = {enabled: true};
inlineOption.text = this.inputsInline ? Blockly.Msg.EXTERNAL_INPUTS :
Blockly.Msg.INLINE_INPUTS;
var isInline = this.getInputsInline();
inlineOption.text = isInline ?
Blockly.Msg.EXTERNAL_INPUTS : Blockly.Msg.INLINE_INPUTS;
inlineOption.callback = function() {
block.setInputsInline(!block.inputsInline);
block.setInputsInline(!isInline);
};
options.push(inlineOption);
break;
@@ -1405,7 +1408,7 @@ Blockly.BlockSvg.prototype.renderCompute_ = function(iconWidth) {
var hasStatement = false;
var hasDummy = false;
var lastType = undefined;
var isInline = this.inputsInline && !this.isCollapsed();
var isInline = this.getInputsInline() && !this.isCollapsed();
for (var i = 0, input; input = inputList[i]; i++) {
if (!input.isVisible()) {
continue;

View File

@@ -46,8 +46,8 @@ Blockly.Xml.workspaceToDom = function(workspace) {
for (var i = 0, block; block = blocks[i]; i++) {
var element = Blockly.Xml.blockToDom_(block);
var xy = block.getRelativeToSurfaceXY();
element.setAttribute('x', workspace.RTL ? width - xy.x : xy.x);
element.setAttribute('y', xy.y);
element.setAttribute('x', Math.round(workspace.RTL ? width - xy.x : xy.x));
element.setAttribute('y', Math.round(xy.y));
xml.appendChild(element);
}
return xml;
@@ -102,7 +102,6 @@ Blockly.Xml.blockToDom_ = function(block) {
element.appendChild(dataElement);
}
var hasValues = false;
for (var i = 0, input; input = block.inputList[i]; i++) {
var container;
var empty = true;
@@ -112,7 +111,6 @@ Blockly.Xml.blockToDom_ = function(block) {
var childBlock = input.connection.targetBlock();
if (input.type == Blockly.INPUT_VALUE) {
container = goog.dom.createDom('value');
hasValues = true;
} else if (input.type == Blockly.NEXT_STATEMENT) {
container = goog.dom.createDom('statement');
}
@@ -126,7 +124,7 @@ Blockly.Xml.blockToDom_ = function(block) {
element.appendChild(container);
}
}
if (hasValues) {
if (block.inputsInlineDefault != block.inputsInline) {
element.setAttribute('inline', block.inputsInline);
}
if (block.isCollapsed()) {