From f2c57dea1b01f9b3b039175b32a0c0f7fc3dfb07 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Thu, 6 Jun 2019 14:34:59 -0700 Subject: [PATCH] Remove goog.math.Rect dependency --- core/flyout_horizontal.js | 9 +++-- core/flyout_vertical.js | 9 +++-- core/toolbox.js | 12 +++---- core/trashcan.js | 8 ++--- core/utils_rect.js | 71 +++++++++++++++++++++++++++++++++++++++ core/workspace_svg.js | 7 ++-- 6 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 core/utils_rect.js diff --git a/core/flyout_horizontal.js b/core/flyout_horizontal.js index da62a8ffe..c5e092cb0 100644 --- a/core/flyout_horizontal.js +++ b/core/flyout_horizontal.js @@ -30,8 +30,7 @@ goog.require('Blockly.Block'); goog.require('Blockly.Flyout'); goog.require('Blockly.FlyoutButton'); goog.require('Blockly.utils'); - -goog.require('goog.math.Rect'); +goog.require('Blockly.utils.Rect'); /** @@ -336,7 +335,7 @@ Blockly.HorizontalFlyout.prototype.isDragTowardWorkspace = function( /** * Return the deletion rectangle for this flyout in viewport coordinates. - * @return {goog.math.Rect} Rectangle in which to delete. + * @return {Blockly.utils.Rect} Rectangle in which to delete. */ Blockly.HorizontalFlyout.prototype.getClientRect = function() { if (!this.svgGroup_) { @@ -352,10 +351,10 @@ Blockly.HorizontalFlyout.prototype.getClientRect = function() { var height = flyoutRect.height; if (this.toolboxPosition_ == Blockly.TOOLBOX_AT_TOP) { - return new goog.math.Rect(-BIG_NUM, y - BIG_NUM, BIG_NUM * 2, + 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 goog.math.Rect(-BIG_NUM, y, BIG_NUM * 2, + return new Blockly.utils.Rect(-BIG_NUM, y, BIG_NUM * 2, BIG_NUM + height); } // TODO: Else throw error (should never happen). diff --git a/core/flyout_vertical.js b/core/flyout_vertical.js index 96438050a..f14750e3a 100644 --- a/core/flyout_vertical.js +++ b/core/flyout_vertical.js @@ -31,8 +31,7 @@ goog.require('Blockly.Flyout'); goog.require('Blockly.FlyoutButton'); goog.require('Blockly.userAgent'); goog.require('Blockly.utils'); - -goog.require('goog.math.Rect'); +goog.require('Blockly.utils.Rect'); /** @@ -314,7 +313,7 @@ Blockly.VerticalFlyout.prototype.isDragTowardWorkspace = function( /** * Return the deletion rectangle for this flyout in viewport coordinates. - * @return {goog.math.Rect} Rectangle in which to delete. + * @return {Blockly.utils.Rect} Rectangle in which to delete. */ Blockly.VerticalFlyout.prototype.getClientRect = function() { if (!this.svgGroup_) { @@ -330,7 +329,7 @@ Blockly.VerticalFlyout.prototype.getClientRect = function() { var width = flyoutRect.width; if (this.toolboxPosition_ == Blockly.TOOLBOX_AT_LEFT) { - return new goog.math.Rect(x - BIG_NUM, -BIG_NUM, BIG_NUM + width, + return new Blockly.utils.Rect(x - BIG_NUM, -BIG_NUM, BIG_NUM + width, BIG_NUM * 2); } else { // Right // Firefox sometimes reports the wrong value for the client rect. @@ -357,7 +356,7 @@ Blockly.VerticalFlyout.prototype.getClientRect = function() { x = x + this.leftEdge_ * scale; } } - return new goog.math.Rect(x, -BIG_NUM, BIG_NUM + width, BIG_NUM * 2); + return new Blockly.utils.Rect(x, -BIG_NUM, BIG_NUM + width, BIG_NUM * 2); } }; diff --git a/core/toolbox.js b/core/toolbox.js index dbfdad801..758b184c8 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -32,12 +32,12 @@ goog.require('Blockly.Flyout'); goog.require('Blockly.HorizontalFlyout'); goog.require('Blockly.Touch'); goog.require('Blockly.utils'); +goog.require('Blockly.utils.Rect'); goog.require('Blockly.VerticalFlyout'); goog.require('goog.events'); goog.require('goog.events.EventType'); goog.require('goog.html.SafeHtml'); -goog.require('goog.math.Rect'); goog.require('goog.ui.tree.BaseNode'); goog.require('goog.ui.tree.TreeControl'); goog.require('goog.ui.tree.TreeNode'); @@ -530,7 +530,7 @@ Blockly.Toolbox.prototype.removeStyle = function(style) { /** * Return the deletion rectangle for this toolbox. - * @return {goog.math.Rect} Rectangle in which to delete. + * @return {Blockly.utils.Rect} Rectangle in which to delete. */ Blockly.Toolbox.prototype.getClientRect = function() { if (!this.HtmlDiv) { @@ -551,15 +551,15 @@ Blockly.Toolbox.prototype.getClientRect = function() { // 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 goog.math.Rect(-BIG_NUM, -BIG_NUM, BIG_NUM + x + width, + 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 goog.math.Rect(x, -BIG_NUM, BIG_NUM + width, 2 * BIG_NUM); + return new Blockly.utils.Rect(x, -BIG_NUM, BIG_NUM + width, 2 * BIG_NUM); } else if (this.toolboxPosition == Blockly.TOOLBOX_AT_TOP) { - return new goog.math.Rect(-BIG_NUM, -BIG_NUM, 2 * BIG_NUM, + return new Blockly.utils.Rect(-BIG_NUM, -BIG_NUM, 2 * BIG_NUM, BIG_NUM + y + height); } else { // Bottom - return new goog.math.Rect(0, y, 2 * BIG_NUM, BIG_NUM + width); + return new Blockly.utils.Rect(0, y, 2 * BIG_NUM, BIG_NUM + width); } }; diff --git a/core/trashcan.js b/core/trashcan.js index 2eb5cc8b6..2059ff42a 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -27,10 +27,9 @@ goog.provide('Blockly.Trashcan'); goog.require('Blockly.utils'); +goog.require('Blockly.utils.Rect'); goog.require('Blockly.Xml'); -goog.require('goog.math.Rect'); - /** * Class for a trash can. @@ -354,7 +353,7 @@ Blockly.Trashcan.prototype.position = function() { /** * Return the deletion rectangle for this trash can. - * @return {goog.math.Rect} Rectangle in which to delete. + * @return {Blockly.utils.Rect} Rectangle in which to delete. */ Blockly.Trashcan.prototype.getClientRect = function() { if (!this.svgGroup_) { @@ -366,8 +365,7 @@ Blockly.Trashcan.prototype.getClientRect = function() { 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 goog.math.Rect(left, top, width, height); - + return new Blockly.utils.Rect(left, top, width, height); }; /** diff --git a/core/utils_rect.js b/core/utils_rect.js new file mode 100644 index 000000000..c9cefbeec --- /dev/null +++ b/core/utils_rect.js @@ -0,0 +1,71 @@ +/** + * @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 Utility methods for rectangle manipulation. + * These methods are not specific to Blockly, and could be factored out into + * a JavaScript framework such as Closure. + * @author fraser@google.com (Neil Fraser) + */ +'use strict'; + +/** + * @name Blockly.utils.Rect + * @namespace + */ +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. + * @struct + * @constructor + * @implements {goog.math.IRect} + */ +Blockly.utils.Rect = function(x, y, w, h) { + /** @type {number} */ + this.left = x; + + /** @type {number} */ + this.top = y; + + /** @type {number} */ + this.width = w; + + /** @type {number} */ + this.height = h; +}; + +/** + * Tests whether this rectangle entirely contains another rectangle or + * coordinate. + * + * @param {number} x The x coordinate to test for containment. + * @param {number} y The y coordinate to test for containment. + * @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; +}; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index a4b28dfb5..bbcae5589 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -1201,11 +1201,12 @@ Blockly.WorkspaceSvg.prototype.recordDeleteAreas = function() { * which delete area the event is over. */ Blockly.WorkspaceSvg.prototype.isDeleteArea = function(e) { - var xy = new goog.math.Coordinate(e.clientX, e.clientY); - if (this.deleteAreaTrash_ && this.deleteAreaTrash_.contains(xy)) { + if (this.deleteAreaTrash_ && + this.deleteAreaTrash_.contains(e.clientX, e.clientY)) { return Blockly.DELETE_AREA_TRASH; } - if (this.deleteAreaToolbox_ && this.deleteAreaToolbox_.contains(xy)) { + if (this.deleteAreaToolbox_ && + this.deleteAreaToolbox_.contains(e.clientX, e.clientY)) { return Blockly.DELETE_AREA_TOOLBOX; } return Blockly.DELETE_AREA_NONE;