From 6947010c49444627c2e202ead889eef15dca1f2f Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 7 Jun 2019 02:39:04 -0700 Subject: [PATCH] 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. --- core/flyout_horizontal.js | 13 +++++-------- core/flyout_vertical.js | 13 ++++++------- core/toolbox.js | 26 ++++++++++++-------------- core/trashcan.js | 9 +++++---- core/utils/rect.js | 21 ++++++++++----------- 5 files changed, 38 insertions(+), 44 deletions(-) diff --git a/core/flyout_horizontal.js b/core/flyout_horizontal.js index 4b10c05f2..adb1f29b5 100644 --- a/core/flyout_horizontal.js +++ b/core/flyout_horizontal.js @@ -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). }; /** diff --git a/core/flyout_vertical.js b/core/flyout_vertical.js index c892e314e..fbf2720de 100644 --- a/core/flyout_vertical.js +++ b/core/flyout_vertical.js @@ -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); } }; diff --git a/core/toolbox.js b/core/toolbox.js index 92ad7a91c..edb37d3bc 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -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); } }; diff --git a/core/trashcan.js b/core/trashcan.js index d97c1ceae..956c21e43 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -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); }; /** diff --git a/core/utils/rect.js b/core/utils/rect.js index bc87b5abc..ff09cd44f 100644 --- a/core/utils/rect.js +++ b/core/utils/rect.js @@ -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; };