mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
Flaten bounding box data structure.
Old: a box object with two coordinate objects, each with two numbers. New: a box object with four numbers. The old system would make sense if there was a reason to group the top-left and bottom-right coordinates. But in our code we only pulled out top/bottom/left/right numbers. New code is simpler and faster.
This commit is contained in:
@@ -469,32 +469,30 @@ Blockly.BlockSvg.prototype.snapToGrid = function() {
|
||||
* Returns the coordinates of a bounding box describing the dimensions of this
|
||||
* block and any blocks stacked below it.
|
||||
* Coordinate system: workspace coordinates.
|
||||
* @return {!{topLeft: Blockly.utils.Coordinate, bottomRight: Blockly.utils.Coordinate}}
|
||||
* Object with top left and bottom right coordinates of the bounding box.
|
||||
* @return {!{top: number, bottom: number, left: number, right: number}}
|
||||
* Object with top, bottom, left, and right coordinates of the bounding box.
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.getBoundingRectangle = function() {
|
||||
var blockXY = this.getRelativeToSurfaceXY(this);
|
||||
var tab = this.outputConnection ? Blockly.BlockSvg.TAB_WIDTH : 0;
|
||||
var blockBounds = this.getHeightWidth();
|
||||
var topLeft;
|
||||
var bottomRight;
|
||||
var top = blockXY.y;
|
||||
var bottom = blockXY.y + blockBounds.height;
|
||||
var left, right;
|
||||
if (this.RTL) {
|
||||
// Width has the tab built into it already so subtract it here.
|
||||
topLeft = new Blockly.utils.Coordinate(
|
||||
blockXY.x - (blockBounds.width - tab), blockXY.y);
|
||||
left = blockXY.x - (blockBounds.width - tab);
|
||||
// Add the width of the tab/puzzle piece knob to the x coordinate
|
||||
// since X is the corner of the rectangle, not the whole puzzle piece.
|
||||
bottomRight = new Blockly.utils.Coordinate(blockXY.x + tab,
|
||||
blockXY.y + blockBounds.height);
|
||||
right = blockXY.x + tab;
|
||||
} else {
|
||||
// Subtract the width of the tab/puzzle piece knob to the x coordinate
|
||||
// since X is the corner of the rectangle, not the whole puzzle piece.
|
||||
topLeft = new Blockly.utils.Coordinate(blockXY.x - tab, blockXY.y);
|
||||
left = blockXY.x - tab;
|
||||
// Width has the tab built into it already so subtract it here.
|
||||
bottomRight = new Blockly.utils.Coordinate(
|
||||
blockXY.x + blockBounds.width - tab, blockXY.y + blockBounds.height);
|
||||
right = blockXY.x + blockBounds.width - tab;
|
||||
}
|
||||
return {topLeft: topLeft, bottomRight: bottomRight};
|
||||
return {top: top, bottom: bottom, left: left, right: right};
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -308,26 +308,26 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface,
|
||||
var objectMetrics = object.getBoundingRectangle();
|
||||
|
||||
// Bump any object that's above the top back inside.
|
||||
var overflowTop = metrics.viewTop - objectMetrics.topLeft.y;
|
||||
var overflowTop = metrics.viewTop - objectMetrics.top;
|
||||
if (overflowTop > 0) {
|
||||
object.moveBy(0, overflowTop);
|
||||
}
|
||||
|
||||
// Bump any object that's below the bottom back inside.
|
||||
var overflowBottom =
|
||||
metrics.viewBottom - objectMetrics.bottomRight.y;
|
||||
metrics.viewBottom - objectMetrics.bottom;
|
||||
if (overflowBottom < 0) {
|
||||
object.moveBy(0, overflowBottom);
|
||||
}
|
||||
|
||||
// Bump any object that's off the left back inside.
|
||||
var overflowLeft = metrics.viewLeft - objectMetrics.topLeft.x;
|
||||
var overflowLeft = metrics.viewLeft - objectMetrics.left;
|
||||
if (overflowLeft > 0) {
|
||||
object.moveBy(overflowLeft, 0);
|
||||
}
|
||||
|
||||
// Bump any object that's off the right back inside.
|
||||
var overflowRight = metrics.viewRight - objectMetrics.bottomRight.x;
|
||||
var overflowRight = metrics.viewRight - objectMetrics.right;
|
||||
if (overflowRight < 0) {
|
||||
object.moveBy(overflowRight, 0);
|
||||
}
|
||||
|
||||
@@ -419,30 +419,28 @@ Blockly.WorkspaceCommentSvg.prototype.clearTransformAttributes_ = function() {
|
||||
* Returns the coordinates of a bounding box describing the dimensions of this
|
||||
* comment.
|
||||
* Coordinate system: workspace coordinates.
|
||||
* @return {!{topLeft: Blockly.utils.Coordinate, bottomRight: Blockly.utils.Coordinate}}
|
||||
* Object with top left and bottom right coordinates of the bounding box.
|
||||
* @return {!{top: number, bottom: number, left: number, right: number}}
|
||||
* Object with top, bottom, left, and right coordinates of the bounding box.
|
||||
* @package
|
||||
*/
|
||||
Blockly.WorkspaceCommentSvg.prototype.getBoundingRectangle = function() {
|
||||
var blockXY = this.getRelativeToSurfaceXY();
|
||||
var commentBounds = this.getHeightWidth();
|
||||
var topLeft;
|
||||
var bottomRight;
|
||||
var top = blockXY.y;
|
||||
var bottom = blockXY.y + commentBounds.height;
|
||||
var left, right;
|
||||
if (this.RTL) {
|
||||
topLeft = new Blockly.utils.Coordinate(blockXY.x - (commentBounds.width),
|
||||
blockXY.y);
|
||||
left = blockXY.x - (commentBounds.width);
|
||||
// Add the width of the tab/puzzle piece knob to the x coordinate
|
||||
// since X is the corner of the rectangle, not the whole puzzle piece.
|
||||
bottomRight = new Blockly.utils.Coordinate(blockXY.x,
|
||||
blockXY.y + commentBounds.height);
|
||||
right = blockXY.x;
|
||||
} else {
|
||||
// Subtract the width of the tab/puzzle piece knob to the x coordinate
|
||||
// since X is the corner of the rectangle, not the whole puzzle piece.
|
||||
topLeft = new Blockly.utils.Coordinate(blockXY.x, blockXY.y);
|
||||
bottomRight = new Blockly.utils.Coordinate(blockXY.x + commentBounds.width,
|
||||
blockXY.y + commentBounds.height);
|
||||
left = blockXY.x;
|
||||
right = blockXY.x + commentBounds.width;
|
||||
}
|
||||
return {topLeft: topLeft, bottomRight: bottomRight};
|
||||
return {top: top, bottom: bottom, left: left, right: right};
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1370,24 +1370,24 @@ Blockly.WorkspaceSvg.prototype.getBlocksBoundingBox = function() {
|
||||
// Start at 1 since the 0th block was used for initialization
|
||||
for (var i = 1; i < topElements.length; i++) {
|
||||
var blockBoundary = topElements[i].getBoundingRectangle();
|
||||
if (blockBoundary.topLeft.x < boundary.topLeft.x) {
|
||||
boundary.topLeft.x = blockBoundary.topLeft.x;
|
||||
if (blockBoundary.left < boundary.left) {
|
||||
boundary.left = blockBoundary.left;
|
||||
}
|
||||
if (blockBoundary.bottomRight.x > boundary.bottomRight.x) {
|
||||
boundary.bottomRight.x = blockBoundary.bottomRight.x;
|
||||
if (blockBoundary.right > boundary.right) {
|
||||
boundary.right = blockBoundary.right;
|
||||
}
|
||||
if (blockBoundary.topLeft.y < boundary.topLeft.y) {
|
||||
boundary.topLeft.y = blockBoundary.topLeft.y;
|
||||
if (blockBoundary.top < boundary.top) {
|
||||
boundary.top = blockBoundary.top;
|
||||
}
|
||||
if (blockBoundary.bottomRight.y > boundary.bottomRight.y) {
|
||||
boundary.bottomRight.y = blockBoundary.bottomRight.y;
|
||||
if (blockBoundary.bottom > boundary.bottom) {
|
||||
boundary.bottom = blockBoundary.bottom;
|
||||
}
|
||||
}
|
||||
return {
|
||||
x: boundary.topLeft.x,
|
||||
y: boundary.topLeft.y,
|
||||
width: boundary.bottomRight.x - boundary.topLeft.x,
|
||||
height: boundary.bottomRight.y - boundary.topLeft.y
|
||||
x: boundary.left,
|
||||
y: boundary.top,
|
||||
width: boundary.right - boundary.left,
|
||||
height: boundary.bottom - boundary.top
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user