From c785cabe4057559cdb2abc5159fcd3a1691469d1 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Tue, 19 Apr 2016 23:24:42 -0700 Subject: [PATCH] Use more coordinates. --- core/block_svg.js | 9 ++++----- core/bubble.js | 9 +++++---- core/connection.js | 8 +++----- core/connection_db.js | 12 +++++------- core/events.js | 2 ++ core/workspace_svg.js | 29 +++++++++-------------------- 6 files changed, 28 insertions(+), 41 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index b2fab52cb..c58eade11 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -537,7 +537,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED); this.dragStartXY_ = this.getRelativeToSurfaceXY(); - this.workspace.startDrag(e, this.dragStartXY_.x, this.dragStartXY_.y); + this.workspace.startDrag(e, this.dragStartXY_); Blockly.dragMode_ = Blockly.DRAG_STICKY; Blockly.BlockSvg.onMouseUpWrapper_ = Blockly.bindEvent_(document, @@ -845,8 +845,7 @@ Blockly.BlockSvg.prototype.onMouseMove_ = function(e) { } if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Unrestricted dragging. - var dx = oldXY.x - this.dragStartXY_.x; - var dy = oldXY.y - this.dragStartXY_.y; + var dxy = goog.math.Coordinate.difference(oldXY, this.dragStartXY_); var group = this.getSvgRoot(); group.translate_ = 'translate(' + newXY.x + ',' + newXY.y + ')'; group.setAttribute('transform', group.translate_ + group.skew_); @@ -854,7 +853,7 @@ Blockly.BlockSvg.prototype.onMouseMove_ = function(e) { for (var i = 0; i < this.draggedBubbles_.length; i++) { var commentData = this.draggedBubbles_[i]; commentData.bubble.setIconLocation( - new goog.math.Coordinate(commentData.x + dx, commentData.y + dy)); + goog.math.Coordinate.sum(commentData, dxy)); } // Check to see if any of this block's connections are within range of @@ -865,7 +864,7 @@ Blockly.BlockSvg.prototype.onMouseMove_ = function(e) { var radiusConnection = Blockly.SNAP_RADIUS; for (var i = 0; i < myConnections.length; i++) { var myConnection = myConnections[i]; - var neighbour = myConnection.closest(radiusConnection, dx, dy); + var neighbour = myConnection.closest(radiusConnection, dxy); if (neighbour.connection) { closestConnection = neighbour.connection; localConnection = myConnection; diff --git a/core/bubble.js b/core/bubble.js index b5a99f40b..379b9b1b1 100644 --- a/core/bubble.js +++ b/core/bubble.js @@ -29,6 +29,7 @@ goog.provide('Blockly.Bubble'); goog.require('Blockly.Workspace'); goog.require('goog.dom'); goog.require('goog.math'); +goog.require('goog.math.Coordinate'); goog.require('goog.userAgent'); @@ -263,9 +264,9 @@ Blockly.Bubble.prototype.bubbleMouseDown_ = function(e) { // Left-click (or middle click) Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED); - this.workspace_.startDrag(e, + this.workspace_.startDrag(e, new goog.math.Coordinate( this.workspace_.RTL ? -this.relativeLeft_ : this.relativeLeft_, - this.relativeTop_); + this.relativeTop_)); Blockly.Bubble.onMouseUpWrapper_ = Blockly.bindEvent_(document, 'mouseup', this, Blockly.Bubble.unbindDragEvents_); @@ -306,8 +307,8 @@ Blockly.Bubble.prototype.resizeMouseDown_ = function(e) { // Left-click (or middle click) Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED); - this.workspace_.startDrag(e, - this.workspace_.RTL ? -this.width_ : this.width_, this.height_); + this.workspace_.startDrag(e, new goog.math.Coordinate( + this.workspace_.RTL ? -this.width_ : this.width_, this.height_)); Blockly.Bubble.onMouseUpWrapper_ = Blockly.bindEvent_(document, 'mouseup', this, Blockly.Bubble.unbindDragEvents_); diff --git a/core/connection.js b/core/connection.js index 8f32d746b..25adcf49d 100644 --- a/core/connection.js +++ b/core/connection.js @@ -664,16 +664,14 @@ Blockly.Connection.prototype.tighten_ = function() { /** * Find the closest compatible connection to this connection. * @param {number} maxLimit The maximum radius to another connection. - * @param {number} dx Horizontal offset between this connection's location - * in the database and the current location (as a result of dragging). - * @param {number} dy Vertical offset between this connection's location + * @param {!goog.math.Coordinate} dxy Offset between this connection's location * in the database and the current location (as a result of dragging). * @return {!{connection: ?Blockly.Connection, radius: number}} Contains two * properties:' connection' which is either another connection or null, * and 'radius' which is the distance. */ -Blockly.Connection.prototype.closest = function(maxLimit, dx, dy) { - return this.dbOpposite_.searchForClosest(this, maxLimit, dx, dy); +Blockly.Connection.prototype.closest = function(maxLimit, dxy) { + return this.dbOpposite_.searchForClosest(this, maxLimit, dxy); }; /** diff --git a/core/connection_db.js b/core/connection_db.js index 580ae353d..f0b69f7f5 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -226,16 +226,14 @@ Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { * @param {!Blockly.Connection} conn The connection searching for a compatible * mate. * @param {number} maxRadius The maximum radius to another connection. - * @param {number} dx Horizontal offset between this connection's location - * in the database and the current location (as a result of dragging). - * @param {number} dy Vertical offset between this connection's location + * @param {!goog.math.Coordinate} dxy Offset between this connection's location * in the database and the current location (as a result of dragging). * @return {!{connection: ?Blockly.Connection, radius: number}} Contains two * properties:' connection' which is either another connection or null, * and 'radius' which is the distance. */ -Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dx, - dy) { +Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, + dxy) { // Don't bother. if (!this.length) { return {connection: null, radius: maxRadius}; @@ -245,8 +243,8 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dx, var baseY = conn.y_; var baseX = conn.x_; - conn.x_ = baseX + dx; - conn.y_ = baseY + dy; + conn.x_ = baseX + dxy.x; + conn.y_ = baseY + dxy.y; // findPositionForConnection finds an index for insertion, which is always // after any block with the same y index. We want to search both forward diff --git a/core/events.js b/core/events.js index ee401c99e..3de8bc5c3 100644 --- a/core/events.js +++ b/core/events.js @@ -26,6 +26,8 @@ goog.provide('Blockly.Events'); +goog.require('goog.math.Coordinate'); + /** * Group ID for new events. Grouped events are indivisible. diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 3aeb66915..4b9570ce3 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -107,18 +107,11 @@ Blockly.WorkspaceSvg.prototype.startScrollX = 0; Blockly.WorkspaceSvg.prototype.startScrollY = 0; /** - * Horizontal distance from mouse to object being dragged. - * @type {number} + * Distance from mouse to object being dragged. + * @type {goog.math.Coordinate} * @private */ -Blockly.WorkspaceSvg.prototype.dragDeltaX_ = 0; - -/** - * Vertical distance from mouse to object being dragged. - * @type {number} - * @private - */ -Blockly.WorkspaceSvg.prototype.dragDeltaY_ = 0; +Blockly.WorkspaceSvg.prototype.dragDeltaXY_ = null; /** * Current scale. @@ -494,8 +487,8 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) { // Check for blocks in snap range to any of its connections. var connections = block.getConnections_(false); for (var i = 0, connection; connection = connections[i]; i++) { - var neighbour = - connection.closest(Blockly.SNAP_RADIUS, blockX, blockY); + var neighbour = connection.closest(Blockly.SNAP_RADIUS, + new goog.math.Coordinate(blockX, blockY)); if (neighbour.connection) { collide = true; break; @@ -620,17 +613,15 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { /** * Start tracking a drag of an object on this workspace. * @param {!Event} e Mouse down event. - * @param {number} x Starting horizontal location of object. - * @param {number} y Starting vertical location of object. + * @param {!goog.math.Coordinate} xy Starting location of object. */ -Blockly.WorkspaceSvg.prototype.startDrag = function(e, x, y) { +Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) { // Record the starting offset between the bubble's location and the mouse. var point = Blockly.mouseToSvg(e, this.getParentSvg()); // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; - this.dragDeltaX_ = x - point.x; - this.dragDeltaY_ = y - point.y; + this.dragDeltaXY_ = goog.math.Coordinate.difference(xy, point); }; /** @@ -643,9 +634,7 @@ Blockly.WorkspaceSvg.prototype.moveDrag = function(e) { // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; - var x = this.dragDeltaX_ + point.x; - var y = this.dragDeltaY_ + point.y; - return new goog.math.Coordinate(x, y); + return goog.math.Coordinate.sum(this.dragDeltaXY_, point); }; /**