Change Rect to use top/bottom/left/right

Instead of top/left/height/width.  Given our uses of Rect, it makes the math slightly simpler.

This is a setup for using Rect in other places.  Currently it is only used to describe delete areas.
This commit is contained in:
Neil Fraser
2019-06-07 02:39:04 -07:00
committed by Neil Fraser
parent 98a98bcce6
commit 6947010c49
5 changed files with 38 additions and 44 deletions

View File

@@ -347,17 +347,14 @@ Blockly.HorizontalFlyout.prototype.getClientRect = function() {
// area are still deleted. Must be larger than the largest screen size,
// but be smaller than half Number.MAX_SAFE_INTEGER (not available on IE).
var BIG_NUM = 1000000000;
var y = flyoutRect.top;
var height = flyoutRect.height;
var top = flyoutRect.top;
if (this.toolboxPosition_ == Blockly.TOOLBOX_AT_TOP) {
return new Blockly.utils.Rect(-BIG_NUM, y - BIG_NUM, BIG_NUM * 2,
BIG_NUM + height);
} else if (this.toolboxPosition_ == Blockly.TOOLBOX_AT_BOTTOM) {
return new Blockly.utils.Rect(-BIG_NUM, y, BIG_NUM * 2,
BIG_NUM + height);
var height = flyoutRect.height;
return new Blockly.utils.Rect(-BIG_NUM, top + height, -BIG_NUM, BIG_NUM);
} else { // Bottom.
return new Blockly.utils.Rect(top, -BIG_NUM, -BIG_NUM, BIG_NUM);
}
// TODO: Else throw error (should never happen).
};
/**

View File

@@ -325,12 +325,11 @@ Blockly.VerticalFlyout.prototype.getClientRect = function() {
// area are still deleted. Must be larger than the largest screen size,
// but be smaller than half Number.MAX_SAFE_INTEGER (not available on IE).
var BIG_NUM = 1000000000;
var x = flyoutRect.left;
var width = flyoutRect.width;
var left = flyoutRect.left;
if (this.toolboxPosition_ == Blockly.TOOLBOX_AT_LEFT) {
return new Blockly.utils.Rect(x - BIG_NUM, -BIG_NUM, BIG_NUM + width,
BIG_NUM * 2);
var width = flyoutRect.width;
return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, -BIG_NUM, left + width);
} else { // Right
// Firefox sometimes reports the wrong value for the client rect.
// See https://github.com/google/blockly/issues/1425 and
@@ -348,15 +347,15 @@ Blockly.VerticalFlyout.prototype.getClientRect = function() {
// visible area of the workspace should be more than ten pixels wide. If
// the browser reports that the flyout is within ten pixels of the left
// side of the workspace, ignore it and manually calculate the value.
if (Math.abs(targetWsLeftPixels - x) < 10) {
if (Math.abs(targetWsLeftPixels - left) < 10) {
// If we're in a mutator, its scale is always 1, purely because of some
// oddities in our rendering optimizations. The actual scale is the
// same as the scale on the parent workspace.
var scale = this.targetWorkspace_.options.parentWorkspace.scale;
x = x + this.leftEdge_ * scale;
left += this.leftEdge_ * scale;
}
}
return new Blockly.utils.Rect(x, -BIG_NUM, BIG_NUM + width, BIG_NUM * 2);
return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, left, BIG_NUM);
}
};

View File

@@ -545,23 +545,21 @@ Blockly.Toolbox.prototype.getClientRect = function() {
var BIG_NUM = 10000000;
var toolboxRect = this.HtmlDiv.getBoundingClientRect();
var x = toolboxRect.left;
var y = toolboxRect.top;
var width = toolboxRect.width;
var height = toolboxRect.height;
var top = toolboxRect.top;
var bottom = top + toolboxRect.height;
var left = toolboxRect.left;
var right = left + toolboxRect.width;
// Assumes that the toolbox is on the SVG edge. If this changes
// (e.g. toolboxes in mutators) then this code will need to be more complex.
if (this.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
return new Blockly.utils.Rect(-BIG_NUM, -BIG_NUM, BIG_NUM + x + width,
2 * BIG_NUM);
} else if (this.toolboxPosition == Blockly.TOOLBOX_AT_RIGHT) {
return new Blockly.utils.Rect(x, -BIG_NUM, BIG_NUM + width, 2 * BIG_NUM);
} else if (this.toolboxPosition == Blockly.TOOLBOX_AT_TOP) {
return new Blockly.utils.Rect(-BIG_NUM, -BIG_NUM, 2 * BIG_NUM,
BIG_NUM + y + height);
} else { // Bottom
return new Blockly.utils.Rect(0, y, 2 * BIG_NUM, BIG_NUM + width);
if (this.toolboxPosition == Blockly.TOOLBOX_AT_TOP) {
return new Blockly.utils.Rect(-BIG_NUM, bottom, -BIG_NUM, BIG_NUM);
} else if (this.toolboxPosition == Blockly.TOOLBOX_AT_BOTTOM) {
return new Blockly.utils.Rect(top, BIG_NUM, -BIG_NUM, BIG_NUM);
} else if (this.toolboxPosition == Blockly.TOOLBOX_AT_LEFT) {
return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, -BIG_NUM, right);
} else { // Right
return new Blockly.utils.Rect(-BIG_NUM, BIG_NUM, left, BIG_NUM);
}
};

View File

@@ -361,11 +361,12 @@ Blockly.Trashcan.prototype.getClientRect = function() {
}
var trashRect = this.svgGroup_.getBoundingClientRect();
var left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_;
var top = trashRect.top + this.SPRITE_TOP_ - this.MARGIN_HOTSPOT_;
var width = this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_;
var height = this.LID_HEIGHT_ + this.BODY_HEIGHT_ + 2 * this.MARGIN_HOTSPOT_;
return new Blockly.utils.Rect(left, top, width, height);
var bottom = top + this.LID_HEIGHT_ + this.BODY_HEIGHT_ +
2 * this.MARGIN_HOTSPOT_;
var left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_;
var right = left + this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_;
return new Blockly.utils.Rect(top, bottom, left, right);
};
/**

View File

@@ -35,25 +35,25 @@ goog.provide('Blockly.utils.Rect');
/**
* Class for representing rectangular regions.
* @param {number} x Left.
* @param {number} y Top.
* @param {number} w Width.
* @param {number} h Height.
* @param {number} top Top.
* @param {number} bottom Bottom.
* @param {number} left Left.
* @param {number} right Right.
* @struct
* @constructor
*/
Blockly.utils.Rect = function(x, y, w, h) {
Blockly.utils.Rect = function(top, bottom, left, right) {
/** @type {number} */
this.left = x;
this.top = top;
/** @type {number} */
this.top = y;
this.bottom = bottom;
/** @type {number} */
this.width = w;
this.left = left;
/** @type {number} */
this.height = h;
this.right = right;
};
/**
@@ -64,6 +64,5 @@ Blockly.utils.Rect = function(x, y, w, h) {
* @return {boolean} Whether this rectangle contains given coordinate.
*/
Blockly.utils.Rect.prototype.contains = function(x, y) {
return x >= this.left && x <= this.left + this.width &&
y >= this.top && y <= this.top + this.height;
return x >= this.left && x <= this.right && y >= this.top && y <= this.bottom;
};