Move input measurables into a separate file, and make them extend connection measurables.

This commit is contained in:
Rachel Fenichel
2019-08-20 14:07:34 -07:00
parent d790ad98ee
commit 611b9884ca
4 changed files with 171 additions and 126 deletions

View File

@@ -34,6 +34,10 @@ goog.require('Blockly.blockRendering.Row');
goog.require('Blockly.blockRendering.SpacerRow');
goog.require('Blockly.blockRendering.TopRow');
goog.require('Blockly.blockRendering.InlineInput');
goog.require('Blockly.blockRendering.ExternalValueInput');
goog.require('Blockly.blockRendering.StatementInput');
goog.require('Blockly.blockRendering.PreviousConnection');
goog.require('Blockly.blockRendering.NextConnection');
goog.require('Blockly.blockRendering.OutputConnection');

View File

@@ -1,44 +1,9 @@
goog.provide('Blockly.blockRendering.Input');
goog.provide('Blockly.blockRendering.InRowSpacer');
goog.require('Blockly.blockRendering.constants');
goog.require('Blockly.blockRendering.Measurable');
/**
* The base class to represent an input that takes up space on a block
* during rendering
* @param {!Blockly.Input} input The input to measure and store information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.Measurable}
*/
Blockly.blockRendering.Input = function(input) {
Blockly.blockRendering.Input.superClass_.constructor.call(this);
this.isInput = true;
this.input = input;
this.align = input.align;
this.connectedBlock = input.connection && input.connection.targetBlock() ?
input.connection.targetBlock() : null;
if (this.connectedBlock) {
var bBox = this.connectedBlock.getHeightWidth();
this.connectedBlockWidth = bBox.width;
this.connectedBlockHeight = bBox.height;
} else {
this.connectedBlockWidth = 0;
this.connectedBlockHeight = 0;
}
this.connection = input.connection;
this.connectionOffsetX = 0;
this.connectionOffsetY = 0;
};
goog.inherits(Blockly.blockRendering.Input, Blockly.blockRendering.Measurable);
/**
* An object containing information about the space an icon takes up during
* rendering
@@ -98,93 +63,6 @@ Blockly.blockRendering.Field = function(field, parentInput) {
};
goog.inherits(Blockly.blockRendering.Field, Blockly.blockRendering.Measurable);
/**
* An object containing information about the space an inline input takes up
* during rendering
* @param {!Blockly.Input} input The inline input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.Input}
*/
Blockly.blockRendering.InlineInput = function(input) {
Blockly.blockRendering.InlineInput.superClass_.constructor.call(this, input);
this.type = 'inline input';
if (!this.connectedBlock) {
this.height = Blockly.blockRendering.constants.EMPTY_INLINE_INPUT_HEIGHT;
this.width = this.connectionShape.width +
Blockly.blockRendering.constants.EMPTY_INLINE_INPUT_PADDING;
} else {
// We allow the dark path to show on the parent block so that the child
// block looks embossed. This takes up an extra pixel in both x and y.
this.width = this.connectedBlockWidth +
Blockly.blockRendering.constants.DARK_PATH_OFFSET;
this.height = this.connectedBlockHeight + Blockly.blockRendering.constants.DARK_PATH_OFFSET;
}
this.connectionOffsetY = Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
this.connectionHeight = this.connectionShape.height;
this.connectionWidth = this.connectionShape.width;
};
goog.inherits(Blockly.blockRendering.InlineInput, Blockly.blockRendering.Input);
/**
* An object containing information about the space a statement input takes up
* during rendering
* @param {!Blockly.Input} input The statement input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.Input}
*/
Blockly.blockRendering.StatementInput = function(input) {
Blockly.blockRendering.StatementInput.superClass_.constructor.call(this, input);
this.type = 'statement input';
if (!this.connectedBlock) {
this.height = Blockly.blockRendering.constants.EMPTY_STATEMENT_INPUT_HEIGHT;
} else {
this.height =
this.connectedBlockHeight + Blockly.blockRendering.constants.STATEMENT_BOTTOM_SPACER;
if (this.connectedBlock.nextConnection) {
this.height -= this.notchShape.height;
}
}
this.width = this.notchOffset + this.notchShape.width;
};
goog.inherits(Blockly.blockRendering.StatementInput,
Blockly.blockRendering.Input);
/**
* An object containing information about the space an external value input
* takes up during rendering
* @param {!Blockly.Input} input The external value input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.Input}
*/
Blockly.blockRendering.ExternalValueInput = function(input) {
Blockly.blockRendering.ExternalValueInput.superClass_.constructor.call(this, input);
this.type = 'external value input';
if (!this.connectedBlock) {
this.height = this.connectionShape.height;
} else {
this.height =
this.connectedBlockHeight - 2 * Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
}
this.width = this.connectionShape.width +
Blockly.blockRendering.constants.EXTERNAL_VALUE_INPUT_PADDING;
this.connectionOffsetY = Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
this.connectionHeight = this.connectionShape.height;
this.connectionWidth = this.connectionShape.width;
};
goog.inherits(Blockly.blockRendering.ExternalValueInput,
Blockly.blockRendering.Input);
/**
* An object containing information about the space a hat takes up during
* rendering.

View File

@@ -0,0 +1,162 @@
/**
* @license
* Visual Blocks Editor
*
* Copyright 2019 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 Objects representing inputs with connections on a rendered
* block.
* @author fenichel@google.com (Rachel Fenichel)
*/
goog.provide('Blockly.blockRendering.InputConnection');
goog.provide('Blockly.blockRendering.InlineInput');
goog.provide('Blockly.blockRendering.StatementInput');
goog.provide('Blockly.blockRendering.ExternalValueInput');
goog.require('Blockly.blockRendering.Connection');
goog.require('Blockly.blockRendering.Measurable');
goog.require('Blockly.blockRendering.constants');
/**
* The base class to represent an input that takes up space on a block
* during rendering
* @param {!Blockly.Input} input The input to measure and store information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.Connection}
*/
Blockly.blockRendering.InputConnection = function(input) {
Blockly.blockRendering.InputConnection.superClass_.constructor.call(this,
input.connection);
this.isInput = true;
this.input = input;
this.align = input.align;
this.connectedBlock = input.connection && input.connection.targetBlock() ?
input.connection.targetBlock() : null;
if (this.connectedBlock) {
var bBox = this.connectedBlock.getHeightWidth();
this.connectedBlockWidth = bBox.width;
this.connectedBlockHeight = bBox.height;
} else {
this.connectedBlockWidth = 0;
this.connectedBlockHeight = 0;
}
// TODO: change references to connectionModel, since that's on Connection.
this.connection = input.connection;
this.connectionOffsetX = 0;
this.connectionOffsetY = 0;
};
goog.inherits(Blockly.blockRendering.InputConnection,
Blockly.blockRendering.Connection);
/**
* An object containing information about the space an inline input takes up
* during rendering
* @param {!Blockly.Input} input The inline input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.InputConnection}
*/
Blockly.blockRendering.InlineInput = function(input) {
Blockly.blockRendering.InlineInput.superClass_.constructor.call(this,
input);
this.type = 'inline input';
if (!this.connectedBlock) {
this.height = Blockly.blockRendering.constants.EMPTY_INLINE_INPUT_HEIGHT;
this.width = this.connectionShape.width +
Blockly.blockRendering.constants.EMPTY_INLINE_INPUT_PADDING;
} else {
// We allow the dark path to show on the parent block so that the child
// block looks embossed. This takes up an extra pixel in both x and y.
this.width = this.connectedBlockWidth +
Blockly.blockRendering.constants.DARK_PATH_OFFSET;
this.height = this.connectedBlockHeight + Blockly.blockRendering.constants.DARK_PATH_OFFSET;
}
this.connectionOffsetY = Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
this.connectionHeight = this.connectionShape.height;
this.connectionWidth = this.connectionShape.width;
};
goog.inherits(Blockly.blockRendering.InlineInput,
Blockly.blockRendering.InputConnection);
/**
* An object containing information about the space a statement input takes up
* during rendering
* @param {!Blockly.Input} input The statement input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.InputConnection}
*/
Blockly.blockRendering.StatementInput = function(input) {
Blockly.blockRendering.StatementInput.superClass_.constructor.call(this,
input);
this.type = 'statement input';
if (!this.connectedBlock) {
this.height = Blockly.blockRendering.constants.EMPTY_STATEMENT_INPUT_HEIGHT;
} else {
this.height =
this.connectedBlockHeight + Blockly.blockRendering.constants.STATEMENT_BOTTOM_SPACER;
if (this.connectedBlock.nextConnection) {
this.height -= this.notchShape.height;
}
}
this.width = Blockly.blockRendering.constants.NOTCH_OFFSET_LEFT +
this.notchShape.width;
};
goog.inherits(Blockly.blockRendering.StatementInput,
Blockly.blockRendering.InputConnection);
/**
* An object containing information about the space an external value input
* takes up during rendering
* @param {!Blockly.Input} input The external value input to measure and store
* information for.
* @package
* @constructor
* @extends {Blockly.blockRendering.InputConnection}
*/
Blockly.blockRendering.ExternalValueInput = function(input) {
Blockly.blockRendering.ExternalValueInput.superClass_.constructor.call(this,
input);
this.type = 'external value input';
if (!this.connectedBlock) {
this.height = this.connectionShape.height;
} else {
this.height =
this.connectedBlockHeight - 2 * Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
}
this.width = this.connectionShape.width +
Blockly.blockRendering.constants.EXTERNAL_VALUE_INPUT_PADDING;
this.connectionOffsetY = Blockly.blockRendering.constants.TAB_OFFSET_FROM_TOP;
this.connectionHeight = this.connectionShape.height;
this.connectionWidth = this.connectionShape.width;
};
goog.inherits(Blockly.blockRendering.ExternalValueInput,
Blockly.blockRendering.InputConnection);

View File

@@ -31,7 +31,7 @@ goog.provide('Blockly.blockRendering.SpacerRow');
goog.provide('Blockly.blockRendering.TopRow');
goog.require('Blockly.blockRendering.constants');
goog.require('Blockly.blockRendering.Input');
goog.require('Blockly.blockRendering.InputConnection');
goog.require('Blockly.blockRendering.InRowSpacer');
goog.require('Blockly.blockRendering.Measurable');
goog.require('Blockly.blockRendering.NextConnection');
@@ -174,7 +174,8 @@ Blockly.blockRendering.Row.prototype.measure = function() {
/**
* Get the last input on this row, if it has one.
* TODO: Consider moving this to InputRow, if possible.
* @return {Blockly.blockRendering.Input} The last input on the row, or null.
* @return {Blockly.blockRendering.InputConnection} The last input on the row,
* or null.
* @package
*/
Blockly.blockRendering.Row.prototype.getLastInput = function() {
@@ -183,9 +184,9 @@ Blockly.blockRendering.Row.prototype.getLastInput = function() {
continue;
}
if (elem.isInput) {
return /** @type {Blockly.blockRendering.Input} */ (elem);
return /** @type {Blockly.blockRendering.InputConnection} */ (elem);
} else if (elem.isField()) {
return /** @type {Blockly.blockRendering.Input} */ (elem.parentInput);
return /** @type {Blockly.blockRendering.InputConnection} */ (elem.parentInput);
}
}
return null;