Remove goog.math.Coordinate

This commit is contained in:
Neil Fraser
2019-06-06 14:51:01 -07:00
committed by Neil Fraser
parent f2c57dea1b
commit 5bf7069a2f
31 changed files with 314 additions and 195 deletions

View File

@@ -29,6 +29,7 @@ goog.provide('Blockly.Block');
goog.require('Blockly.Blocks');
goog.require('Blockly.Comment');
goog.require('Blockly.Connection');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockChange');
goog.require('Blockly.Events.BlockCreate');
@@ -41,8 +42,6 @@ goog.require('Blockly.utils');
goog.require('Blockly.Warning');
goog.require('Blockly.Workspace');
goog.require('goog.math.Coordinate');
/**
* Class for one block.
@@ -134,10 +133,10 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
/**
* The block's position in workspace units. (0, 0) is at the workspace's
* origin; scale does not change this value.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.xy_ = new goog.math.Coordinate(0, 0);
this.xy_ = new Blockly.utils.Coordinate(0, 0);
/** @type {!Blockly.Workspace} */
this.workspace = workspace;
@@ -1846,7 +1845,7 @@ Blockly.Block.prototype.setMutator = function(_mutator) {
/**
* Return the coordinates of the top-left corner of this block relative to the
* drawing surface's origin (0,0), in workspace units.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
*/
Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
return this.xy_;

View File

@@ -30,8 +30,8 @@
'use strict';
goog.provide('Blockly.BlockDragSurfaceSvg');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
@@ -83,7 +83,7 @@ Blockly.BlockDragSurfaceSvg.prototype.scale_ = 1;
* Cached value for the translation of the drag surface.
* This translation is in pixel units, because the scale is applied to the
* drag group rather than the top-level SVG.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
Blockly.BlockDragSurfaceSvg.prototype.surfaceXY_ = null;
@@ -118,7 +118,7 @@ Blockly.BlockDragSurfaceSvg.prototype.setBlocksAndShow = function(blocks) {
// appendChild removes the blocks from the previous parent
this.dragGroup_.appendChild(blocks);
this.SVG_.style.display = 'block';
this.surfaceXY_ = new goog.math.Coordinate(0, 0);
this.surfaceXY_ = new Blockly.utils.Coordinate(0, 0);
};
/**
@@ -165,18 +165,18 @@ Blockly.BlockDragSurfaceSvg.prototype.translateSurfaceInternal_ = function() {
* @param {number} y Y translation for the entire surface.
*/
Blockly.BlockDragSurfaceSvg.prototype.translateSurface = function(x, y) {
this.surfaceXY_ = new goog.math.Coordinate(x * this.scale_, y * this.scale_);
this.surfaceXY_ = new Blockly.utils.Coordinate(x * this.scale_, y * this.scale_);
this.translateSurfaceInternal_();
};
/**
* Reports the surface translation in scaled workspace coordinates.
* Use this when finishing a drag to return blocks to the correct position.
* @return {!goog.math.Coordinate} Current translation of the surface.
* @return {!Blockly.utils.Coordinate} Current translation of the surface.
*/
Blockly.BlockDragSurfaceSvg.prototype.getSurfaceTranslation = function() {
var xy = Blockly.utils.getRelativeXY(this.SVG_);
return new goog.math.Coordinate(xy.x / this.scale_, xy.y / this.scale_);
return new Blockly.utils.Coordinate(xy.x / this.scale_, xy.y / this.scale_);
};
/**

View File

@@ -27,12 +27,11 @@
goog.provide('Blockly.BlockDragger');
goog.require('Blockly.BlockAnimations');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.InsertionMarkerManager');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockMove');
goog.require('goog.math.Coordinate');
/**
* Class for a block dragger. It moves blocks around the workspace when they
@@ -83,7 +82,7 @@ Blockly.BlockDragger = function(block, workspace) {
/**
* The location of the top left corner of the dragging block at the beginning
* of the drag in workspace coordinates.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.startXY_ = this.draggingBlock_.getRelativeToSurfaceXY();
@@ -130,7 +129,7 @@ Blockly.BlockDragger.initIconData_ = function(block) {
var icons = descendant.getIcons();
for (var j = 0; j < icons.length; j++) {
var data = {
// goog.math.Coordinate with x and y properties (workspace coordinates).
// Blockly.utils.Coordinate with x and y properties (workspace coordinates).
location: icons[j].getIconLocation(),
// Blockly.Icon
icon: icons[j]
@@ -143,7 +142,7 @@ Blockly.BlockDragger.initIconData_ = function(block) {
/**
* Start dragging a block. This includes moving it to the drag surface.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at mouse down, in pixel units.
* @param {boolean} healStack Whether or not to heal the stack after
* disconnecting.
@@ -174,7 +173,7 @@ Blockly.BlockDragger.prototype.startBlockDrag = function(currentDragDeltaXY,
this.draggingBlock_.nextConnection.targetBlock())) {
this.draggingBlock_.unplug(healStack);
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = goog.math.Coordinate.sum(this.startXY_, delta);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
this.draggingBlock_.translate(newLoc.x, newLoc.y);
Blockly.BlockAnimations.disconnectUiEffect(this.draggingBlock_);
@@ -197,13 +196,13 @@ Blockly.BlockDragger.prototype.startBlockDrag = function(currentDragDeltaXY,
* Execute a step of block dragging, based on the given event. Update the
* display accordingly.
* @param {!Event} e The most recent move event.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel units.
* @package
*/
Blockly.BlockDragger.prototype.dragBlock = function(e, currentDragDeltaXY) {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = goog.math.Coordinate.sum(this.startXY_, delta);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
this.draggingBlock_.moveDuringDrag(newLoc);
this.dragIcons_(delta);
@@ -217,7 +216,7 @@ Blockly.BlockDragger.prototype.dragBlock = function(e, currentDragDeltaXY) {
/**
* Finish a block drag and put the block back on the workspace.
* @param {!Event} e The mouseup/touchend event.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel units.
* @package
*/
@@ -231,7 +230,7 @@ Blockly.BlockDragger.prototype.endBlockDrag = function(e, currentDragDeltaXY) {
Blockly.BlockAnimations.disconnectUiStop();
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = goog.math.Coordinate.sum(this.startXY_, delta);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
this.draggingBlock_.moveOffDragSurface_(newLoc);
var deleted = this.maybeDeleteBlock_();
@@ -319,14 +318,15 @@ Blockly.BlockDragger.prototype.updateCursorDuringBlockDrag_ = function() {
* correction for mutator workspaces.
* This function does not consider differing origins. It simply scales the
* input's x and y values.
* @param {!goog.math.Coordinate} pixelCoord A coordinate with x and y values
* @param {!Blockly.utils.Coordinate} pixelCoord A coordinate with x and y values
* in css pixel units.
* @return {!goog.math.Coordinate} The input coordinate divided by the workspace
* @return {!Blockly.utils.Coordinate} The input coordinate divided by the workspace
* scale.
* @private
*/
Blockly.BlockDragger.prototype.pixelsToWorkspaceUnits_ = function(pixelCoord) {
var result = new goog.math.Coordinate(pixelCoord.x / this.workspace_.scale,
var result = new Blockly.utils.Coordinate(
pixelCoord.x / this.workspace_.scale,
pixelCoord.y / this.workspace_.scale);
if (this.workspace_.isMutator) {
// If we're in a mutator, its scale is always 1, purely because of some
@@ -334,14 +334,14 @@ Blockly.BlockDragger.prototype.pixelsToWorkspaceUnits_ = function(pixelCoord) {
// the scale on the parent workspace.
// Fix that for dragging.
var mainScale = this.workspace_.options.parentWorkspace.scale;
result = result.scale(1 / mainScale);
result.scale(1 / mainScale);
}
return result;
};
/**
* Move all of the icons connected to this drag.
* @param {!goog.math.Coordinate} dxy How far to move the icons from their
* @param {!Blockly.utils.Coordinate} dxy How far to move the icons from their
* original positions, in workspace units.
* @private
*/
@@ -349,7 +349,7 @@ Blockly.BlockDragger.prototype.dragIcons_ = function(dxy) {
// Moving icons moves their associated bubbles.
for (var i = 0; i < this.dragIconData_.length; i++) {
var data = this.dragIconData_[i];
data.icon.setIconLocation(goog.math.Coordinate.sum(data.location, dxy));
data.icon.setIconLocation(Blockly.utils.Coordinate.sum(data.location, dxy));
}
};

View File

@@ -34,12 +34,11 @@ goog.provide('Blockly.Events.Create'); // Deprecated.
goog.provide('Blockly.Events.Delete'); // Deprecated.
goog.provide('Blockly.Events.Move'); // Deprecated.
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.Abstract');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
/**
* Abstract class for a block event.
@@ -431,7 +430,7 @@ Blockly.Events.Move.prototype.fromJson = function(json) {
if (json['newCoordinate']) {
var xy = json['newCoordinate'].split(',');
this.newCoordinate =
new goog.math.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
new Blockly.utils.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
}
};
@@ -475,7 +474,7 @@ Blockly.Events.Move.prototype.currentLocation_ = function() {
Blockly.Events.Move.prototype.isNull = function() {
return this.oldParentId == this.newParentId &&
this.oldInputName == this.newInputName &&
goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
Blockly.utils.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
};
/**

View File

@@ -29,6 +29,7 @@ goog.provide('Blockly.BlockSvg');
goog.require('Blockly.Block');
goog.require('Blockly.BlockAnimations');
goog.require('Blockly.ContextMenu');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.Ui');
goog.require('Blockly.Events.BlockMove');
@@ -39,8 +40,6 @@ goog.require('Blockly.Tooltip');
goog.require('Blockly.Touch');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* Class for a block's SVG representation.
@@ -121,7 +120,7 @@ Blockly.BlockSvg.prototype.width = 0;
/**
* Original location of block being dragged.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
Blockly.BlockSvg.prototype.dragStartXY_ = null;
@@ -301,7 +300,7 @@ Blockly.BlockSvg.prototype.setParent = function(newParent) {
* If the block is on the workspace, (0, 0) is the origin of the workspace
* coordinate system.
* This does not change with workspace scale.
* @return {!goog.math.Coordinate} Object with .x and .y properties in
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties in
* workspace coordinates.
*/
Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
@@ -331,7 +330,7 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
} while (element && element != this.workspace.getCanvas() &&
element != dragSurfaceGroup);
}
return new goog.math.Coordinate(x, y);
return new Blockly.utils.Coordinate(x, y);
};
/**
@@ -393,7 +392,7 @@ Blockly.BlockSvg.prototype.moveToDragSurface_ = function() {
* Move this block back to the workspace block canvas.
* Generally should be called at the same time as setDragging_(false).
* Does nothing if useDragSurface_ is false.
* @param {!goog.math.Coordinate} newXY The position the block should take on
* @param {!Blockly.utils.Coordinate} newXY The position the block should take on
* on the workspace canvas, in workspace coordinates.
* @private
*/
@@ -410,7 +409,7 @@ Blockly.BlockSvg.prototype.moveOffDragSurface_ = function(newXY) {
* Move this block during a drag, taking into account whether we are using a
* drag surface to translate blocks.
* This block must be a top-level block.
* @param {!goog.math.Coordinate} newLoc The location to translate to, in
* @param {!Blockly.utils.Coordinate} newLoc The location to translate to, in
* workspace coordinates.
* @package
*/
@@ -469,7 +468,7 @@ 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: goog.math.Coordinate, bottomRight: goog.math.Coordinate}}
* @return {!{topLeft: Blockly.utils.Coordinate, bottomRight: Blockly.utils.Coordinate}}
* Object with top left and bottom right coordinates of the bounding box.
*/
Blockly.BlockSvg.prototype.getBoundingRectangle = function() {
@@ -480,18 +479,18 @@ Blockly.BlockSvg.prototype.getBoundingRectangle = function() {
var bottomRight;
if (this.RTL) {
// Width has the tab built into it already so subtract it here.
topLeft = new goog.math.Coordinate(blockXY.x - (blockBounds.width - tab),
topLeft = new Blockly.utils.Coordinate(blockXY.x - (blockBounds.width - tab),
blockXY.y);
// 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 goog.math.Coordinate(blockXY.x + tab,
bottomRight = new Blockly.utils.Coordinate(blockXY.x + tab,
blockXY.y + blockBounds.height);
} 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 goog.math.Coordinate(blockXY.x - tab, blockXY.y);
topLeft = new Blockly.utils.Coordinate(blockXY.x - tab, blockXY.y);
// Width has the tab built into it already so subtract it here.
bottomRight = new goog.math.Coordinate(blockXY.x + blockBounds.width - tab,
bottomRight = new Blockly.utils.Coordinate(blockXY.x + blockBounds.width - tab,
blockXY.y + blockBounds.height);
}
return {topLeft: topLeft, bottomRight: bottomRight};

View File

@@ -26,13 +26,12 @@
goog.provide('Blockly.Bubble');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Touch');
goog.require('Blockly.userAgent');
goog.require('Blockly.utils');
goog.require('Blockly.Workspace');
goog.require('goog.math.Coordinate');
/**
* Class for UI bubble.
@@ -40,7 +39,7 @@ goog.require('goog.math.Coordinate');
* bubble.
* @param {!Element} content SVG content for the bubble.
* @param {Element} shape SVG element to avoid eclipsing.
* @param {!goog.math.Coordinate} anchorXY Absolute position of bubble's anchor
* @param {!Blockly.utils.Coordinate} anchorXY Absolute position of bubble's anchor
* point.
* @param {?number} bubbleWidth Width of bubble, or null if not resizable.
* @param {?number} bubbleHeight Height of bubble, or null if not resizable.
@@ -163,7 +162,7 @@ Blockly.Bubble.prototype.rendered_ = false;
/**
* Absolute coordinate of anchor point, in workspace coordinates.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
Blockly.Bubble.prototype.anchorXY_ = null;
@@ -336,7 +335,7 @@ Blockly.Bubble.prototype.resizeMouseDown_ = function(e) {
return;
}
// Left-click (or middle click)
this.workspace_.startDrag(e, new goog.math.Coordinate(
this.workspace_.startDrag(e, new Blockly.utils.Coordinate(
this.workspace_.RTL ? -this.width_ : this.width_, this.height_));
Blockly.Bubble.onMouseUpWrapper_ = Blockly.bindEventWithChecks_(document,
@@ -388,7 +387,7 @@ Blockly.Bubble.prototype.promote_ = function() {
/**
* Notification that the anchor has moved.
* Update the arrow and bubble accordingly.
* @param {!goog.math.Coordinate} xy Absolute location.
* @param {!Blockly.utils.Coordinate} xy Absolute location.
*/
Blockly.Bubble.prototype.setAnchorLocation = function(xy) {
this.anchorXY_ = xy;
@@ -774,7 +773,7 @@ Blockly.Bubble.prototype.dispose = function() {
* a drag surface.
* @param {Blockly.BlockDragSurfaceSvg} dragSurface The surface that carries
* rendered items during a drag, or null if no drag surface is in use.
* @param {!goog.math.Coordinate} newLoc The location to translate to, in
* @param {!Blockly.utils.Coordinate} newLoc The location to translate to, in
* workspace coordinates.
* @package
*/
@@ -796,10 +795,10 @@ Blockly.Bubble.prototype.moveDuringDrag = function(dragSurface, newLoc) {
/**
* Return the coordinates of the top-left corner of this bubble's body relative
* to the drawing surface's origin (0,0), in workspace units.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
*/
Blockly.Bubble.prototype.getRelativeToSurfaceXY = function() {
return new goog.math.Coordinate(
return new Blockly.utils.Coordinate(
this.anchorXY_.x + this.relativeLeft_,
this.anchorXY_.y + this.relativeTop_);
};

View File

@@ -27,13 +27,12 @@
goog.provide('Blockly.BubbleDragger');
goog.require('Blockly.Bubble');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.CommentMove');
goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceCommentSvg');
goog.require('goog.math.Coordinate');
/**
* Class for a bubble dragger. It moves things on the bubble canvas around the
@@ -78,7 +77,7 @@ Blockly.BubbleDragger = function(bubble, workspace) {
/**
* The location of the top left corner of the dragging bubble's body at the
* beginning of the drag, in workspace coordinates.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.startXY_ = this.draggingBubble_.getRelativeToSurfaceXY();
@@ -133,13 +132,13 @@ Blockly.BubbleDragger.prototype.startBubbleDrag = function() {
* Execute a step of bubble dragging, based on the given event. Update the
* display accordingly.
* @param {!Event} e The most recent move event.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel units.
* @package
*/
Blockly.BubbleDragger.prototype.dragBubble = function(e, currentDragDeltaXY) {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = goog.math.Coordinate.sum(this.startXY_, delta);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
this.draggingBubble_.moveDuringDrag(this.dragSurface_, newLoc);
@@ -196,7 +195,7 @@ Blockly.BubbleDragger.prototype.updateCursorDuringBubbleDrag_ = function() {
/**
* Finish a bubble drag and put the bubble back on the workspace.
* @param {!Event} e The mouseup/touchend event.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel units.
* @package
*/
@@ -206,7 +205,7 @@ Blockly.BubbleDragger.prototype.endBubbleDrag = function(
this.dragBubble(e, currentDragDeltaXY);
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = goog.math.Coordinate.sum(this.startXY_, delta);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
// Move the bubble to its final location.
this.draggingBubble_.moveTo(newLoc.x, newLoc.y);
@@ -251,14 +250,14 @@ Blockly.BubbleDragger.prototype.fireMoveEvent_ = function() {
* correction for mutator workspaces.
* This function does not consider differing origins. It simply scales the
* input's x and y values.
* @param {!goog.math.Coordinate} pixelCoord A coordinate with x and y values
* @param {!Blockly.utils.Coordinate} pixelCoord A coordinate with x and y values
* in css pixel units.
* @return {!goog.math.Coordinate} The input coordinate divided by the workspace
* @return {!Blockly.utils.Coordinate} The input coordinate divided by the workspace
* scale.
* @private
*/
Blockly.BubbleDragger.prototype.pixelsToWorkspaceUnits_ = function(pixelCoord) {
var result = new goog.math.Coordinate(pixelCoord.x / this.workspace_.scale,
var result = new Blockly.utils.Coordinate(pixelCoord.x / this.workspace_.scale,
pixelCoord.y / this.workspace_.scale);
if (this.workspace_.isMutator) {
// If we're in a mutator, its scale is always 1, purely because of some
@@ -266,7 +265,7 @@ Blockly.BubbleDragger.prototype.pixelsToWorkspaceUnits_ = function(pixelCoord) {
// the scale on the parent workspace.
// Fix that for dragging.
var mainScale = this.workspace_.options.parentWorkspace.scale;
result = result.scale(1 / mainScale);
result.scale(1 / mainScale);
}
return result;
};

View File

@@ -226,16 +226,17 @@ 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 {!goog.math.Coordinate} dxy Offset between this connection's location
* in the database and the current location (as a result of dragging).
* @param {!Blockly.utils.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,
dxy) {
// Don't bother.
if (!this.connections_.length) {
// Don't bother.
return {connection: null, radius: maxRadius};
}

View File

@@ -30,6 +30,7 @@
*/
goog.provide('Blockly.ContextMenu');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockCreate');
goog.require('Blockly.Msg');
@@ -39,7 +40,6 @@ goog.require('Blockly.utils.uiMenu');
goog.require('Blockly.Xml');
goog.require('goog.events');
goog.require('goog.math.Coordinate');
goog.require('goog.ui.Menu');
goog.require('goog.ui.MenuItem');
@@ -362,7 +362,7 @@ Blockly.ContextMenu.workspaceCommentOption = function(ws, e) {
var boundingRect = injectionDiv.getBoundingClientRect();
// The client coordinates offset by the injection div's upper left corner.
var clientOffsetPixels = new goog.math.Coordinate(
var clientOffsetPixels = new Blockly.utils.Coordinate(
e.clientX - boundingRect.left, e.clientY - boundingRect.top);
// The offset in pixels between the main workspace's origin and the upper
@@ -371,14 +371,13 @@ Blockly.ContextMenu.workspaceCommentOption = function(ws, e) {
// The position of the new comment in pixels relative to the origin of the
// main workspace.
var finalOffsetPixels = goog.math.Coordinate.difference(clientOffsetPixels,
var finalOffset = Blockly.utils.Coordinate.difference(clientOffsetPixels,
mainOffsetPixels);
// The position of the new comment in main workspace coordinates.
var finalOffsetMainWs = finalOffsetPixels.scale(1 / ws.scale);
finalOffset.scale(1 / ws.scale);
var commentX = finalOffsetMainWs.x;
var commentY = finalOffsetMainWs.y;
var commentX = finalOffset.x;
var commentY = finalOffset.y;
comment.moveBy(commentX, commentY);
if (ws.rendered) {
comment.initSvg();

View File

@@ -161,7 +161,7 @@ Blockly.DraggedConnectionManager.prototype.applyConnections = function() {
/**
* Update highlighted connections based on the most recent move location.
* @param {!goog.math.Coordinate} dxy Position relative to drag start,
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @param {?number} deleteArea One of {@link Blockly.DELETE_AREA_TRASH},
* {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
@@ -234,7 +234,7 @@ Blockly.DraggedConnectionManager.prototype.initAvailableConnections_ = function(
/**
* Find the new closest connection, and update internal state in response.
* @param {!goog.math.Coordinate} dxy Position relative to the drag start,
* @param {!Blockly.utils.Coordinate} dxy Position relative to the drag start,
* in workspace units.
* @return {boolean} Whether the closest connection has changed.
* @private

View File

@@ -869,7 +869,7 @@ Blockly.Field.prototype.getClickTarget_ = function() {
/**
* Return the absolute coordinates of the top-left corner of this field.
* The origin (0,0) is the top-left corner of the page body.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
* @private
*/
Blockly.Field.prototype.getAbsoluteXY_ = function() {

View File

@@ -26,6 +26,7 @@
goog.provide('Blockly.FieldTextInput');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.DropDownDiv');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockChange');
@@ -34,8 +35,6 @@ goog.require('Blockly.Msg');
goog.require('Blockly.userAgent');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* Class for an editable text field.
@@ -364,7 +363,7 @@ Blockly.FieldTextInput.prototype.resizeEditor_ = function() {
// In RTL mode block fields and LTR input fields the left edge moves,
// whereas the right edge is fixed. Reposition the editor.
var x = this.sourceBlock_.RTL ? bBox.right - div.offsetWidth : bBox.left;
var xy = new goog.math.Coordinate(x, bBox.top);
var xy = new Blockly.utils.Coordinate(x, bBox.top);
// Shift by a few pixels to line up exactly.
xy.y += 1;

View File

@@ -27,6 +27,7 @@
goog.provide('Blockly.Flyout');
goog.require('Blockly.Block');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockCreate');
goog.require('Blockly.Events.VarCreate');
@@ -38,8 +39,6 @@ goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceSvg');
goog.require('Blockly.Xml');
goog.require('goog.math.Coordinate');
/**
* Class for a flyout.
@@ -817,25 +816,23 @@ Blockly.Flyout.prototype.placeNewBlock_ = function(oldBlock) {
var flyoutOffsetPixels = this.workspace_.getOriginOffsetInPixels();
// The position of the old block in flyout workspace coordinates.
var oldBlockPosWs = oldBlock.getRelativeToSurfaceXY();
var oldBlockPos = oldBlock.getRelativeToSurfaceXY();
// The position of the old block in pixels relative to the flyout
// workspace's origin.
var oldBlockPosPixels = oldBlockPosWs.scale(this.workspace_.scale);
oldBlockPos.scale(this.workspace_.scale);
// The position of the old block in pixels relative to the upper left corner
// of the injection div.
var oldBlockOffsetPixels = goog.math.Coordinate.sum(flyoutOffsetPixels,
oldBlockPosPixels);
var oldBlockOffsetPixels = Blockly.utils.Coordinate.sum(flyoutOffsetPixels,
oldBlockPos);
// The position of the old block in pixels relative to the origin of the
// main workspace.
var finalOffsetPixels = goog.math.Coordinate.difference(oldBlockOffsetPixels,
var finalOffset = Blockly.utils.Coordinate.difference(oldBlockOffsetPixels,
mainOffsetPixels);
// The position of the old block in main workspace coordinates.
var finalOffsetMainWs = finalOffsetPixels.scale(1 / targetWorkspace.scale);
finalOffset.scale(1 / targetWorkspace.scale);
block.moveBy(finalOffsetMainWs.x, finalOffsetMainWs.y);
block.moveBy(finalOffset.x, finalOffset.y);
return block;
};

View File

@@ -26,10 +26,9 @@
goog.provide('Blockly.FlyoutButton');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* Class for a button in the flyout.
@@ -62,10 +61,10 @@ Blockly.FlyoutButton = function(workspace, targetWorkspace, xml, isLabel) {
this.text_ = xml.getAttribute('text');
/**
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.position_ = new goog.math.Coordinate(0, 0);
this.position_ = new Blockly.utils.Coordinate(0, 0);
/**
* Whether this button should be styled as a label.
@@ -205,7 +204,7 @@ Blockly.FlyoutButton.prototype.moveTo = function(x, y) {
/**
* Location of the button.
* @return {!goog.math.Coordinate} x, y coordinates.
* @return {!Blockly.utils.Coordinate} x, y coordinates.
* @package
*/
Blockly.FlyoutButton.prototype.getPosition = function() {

View File

@@ -312,7 +312,7 @@ Blockly.HorizontalFlyout.prototype.layout_ = function(contents, gaps) {
* Determine if a drag delta is toward the workspace, based on the position
* and orientation of the flyout. This is used in determineDragIntention_ to
* determine if a new block should be created or if the flyout should scroll.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at mouse down, in pixel units.
* @return {boolean} True if the drag is toward the workspace.
* @package

View File

@@ -290,7 +290,7 @@ Blockly.VerticalFlyout.prototype.layout_ = function(contents, gaps) {
* Determine if a drag delta is toward the workspace, based on the position
* and orientation of the flyout. This is used in determineDragIntention_ to
* determine if a new block should be created or if the flyout should scroll.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at mouse down, in pixel units.
* @return {boolean} True if the drag is toward the workspace.
* @package

View File

@@ -31,6 +31,7 @@ goog.require('Blockly.BlockAnimations');
goog.require('Blockly.BlockDragger');
goog.require('Blockly.BubbleDragger');
goog.require('Blockly.constants');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.Ui');
goog.require('Blockly.FlyoutDragger');
@@ -39,8 +40,6 @@ goog.require('Blockly.Touch');
goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceDragger');
goog.require('goog.math.Coordinate');
/*
* Note: In this file "start" refers to touchstart, mousedown, and pointerstart
@@ -60,14 +59,14 @@ Blockly.Gesture = function(e, creatorWorkspace) {
/**
* The position of the mouse when the gesture started. Units are css pixels,
* with (0, 0) at the top left of the browser window (mouseEvent clientX/Y).
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
*/
this.mouseDownXY_ = null;
/**
* How far the mouse has moved during this drag, in pixel units.
* (0, 0) is at this.mouseDownXY_.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
this.currentDragDeltaXY_ = null;
@@ -281,7 +280,7 @@ Blockly.Gesture.prototype.dispose = function() {
* @private
*/
Blockly.Gesture.prototype.updateFromEvent_ = function(e) {
var currentXY = new goog.math.Coordinate(e.clientX, e.clientY);
var currentXY = new Blockly.utils.Coordinate(e.clientX, e.clientY);
var changed = this.updateDragDelta_(currentXY);
// Exceeded the drag radius for the first time.
if (changed) {
@@ -293,18 +292,18 @@ Blockly.Gesture.prototype.updateFromEvent_ = function(e) {
/**
* DO MATH to set currentDragDeltaXY_ based on the most recent mouse position.
* @param {!goog.math.Coordinate} currentXY The most recent mouse/pointer
* @param {!Blockly.utils.Coordinate} currentXY The most recent mouse/pointer
* position, in pixel units, with (0, 0) at the window's top left corner.
* @return {boolean} True if the drag just exceeded the drag radius for the
* first time.
* @private
*/
Blockly.Gesture.prototype.updateDragDelta_ = function(currentXY) {
this.currentDragDeltaXY_ = goog.math.Coordinate.difference(currentXY,
this.currentDragDeltaXY_ = Blockly.utils.Coordinate.difference(currentXY,
this.mouseDownXY_);
if (!this.hasExceededDragRadius_) {
var currentDragDelta = goog.math.Coordinate.magnitude(
var currentDragDelta = Blockly.utils.Coordinate.magnitude(
this.currentDragDeltaXY_);
// The flyout has a different drag radius from the rest of Blockly.
@@ -513,7 +512,7 @@ Blockly.Gesture.prototype.doStart = function(e) {
Blockly.longStart_(e, this);
}
this.mouseDownXY_ = new goog.math.Coordinate(e.clientX, e.clientY);
this.mouseDownXY_ = new Blockly.utils.Coordinate(e.clientX, e.clientY);
this.healStack_ = e.altKey || e.ctrlKey || e.metaKey;
this.bindMouseEvents(e);

View File

@@ -26,10 +26,9 @@
goog.provide('Blockly.Icon');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* Class for an icon.
@@ -59,7 +58,7 @@ Blockly.Icon.prototype.bubble_ = null;
/**
* Absolute coordinate of icon's center.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @protected
*/
Blockly.Icon.prototype.iconXY_ = null;
@@ -172,7 +171,7 @@ Blockly.Icon.prototype.renderIcon = function(cursorX) {
/**
* Notification that the icon has moved. Update the arrow accordingly.
* @param {!goog.math.Coordinate} xy Absolute location in workspace coordinates.
* @param {!Blockly.utils.Coordinate} xy Absolute location in workspace coordinates.
*/
Blockly.Icon.prototype.setIconLocation = function(xy) {
this.iconXY_ = xy;
@@ -189,17 +188,17 @@ Blockly.Icon.prototype.computeIconLocation = function() {
// Find coordinates for the centre of the icon and update the arrow.
var blockXY = this.block_.getRelativeToSurfaceXY();
var iconXY = Blockly.utils.getRelativeXY(this.iconGroup_);
var newXY = new goog.math.Coordinate(
var newXY = new Blockly.utils.Coordinate(
blockXY.x + iconXY.x + this.SIZE / 2,
blockXY.y + iconXY.y + this.SIZE / 2);
if (!goog.math.Coordinate.equals(this.getIconLocation(), newXY)) {
if (!Blockly.utils.Coordinate.equals(this.getIconLocation(), newXY)) {
this.setIconLocation(newXY);
}
};
/**
* Returns the center of the block's icon relative to the surface.
* @return {!goog.math.Coordinate} Object with x and y properties in workspace
* @return {!Blockly.utils.Coordinate} Object with x and y properties in workspace
* coordinates.
*/
Blockly.Icon.prototype.getIconLocation = function() {

View File

@@ -223,7 +223,7 @@ Blockly.InsertionMarkerManager.prototype.applyConnections = function() {
/**
* Update highlighted connections based on the most recent move location.
* @param {!goog.math.Coordinate} dxy Position relative to drag start,
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @param {?number} deleteArea One of {@link Blockly.DELETE_AREA_TRASH},
* {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
@@ -317,7 +317,7 @@ Blockly.InsertionMarkerManager.prototype.initAvailableConnections_ = function()
* updated based on the closest candidate and the current drag distance.
* @param {!Object} candidate An object containing a local connection, a closest
* connection, and a radius. Returned by getCandidate_.
* @param {!goog.math.Coordinate} dxy Position relative to drag start,
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @return {boolean} Whether the preview should be updated.
* @private
@@ -362,7 +362,7 @@ Blockly.InsertionMarkerManager.prototype.shouldUpdatePreviews_ = function(
/**
* Find the nearest valid connection, which may be the same as the current
* closest connection.
* @param {!goog.math.Coordinate} dxy Position relative to drag start,
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @return {!Object} An object containing a local connection, a closest
* connection, and a radius.

View File

@@ -27,11 +27,10 @@
goog.provide('Blockly.RenderedConnection');
goog.require('Blockly.Connection');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* Class for a connection between blocks that may be rendered on screen.
@@ -45,10 +44,10 @@ Blockly.RenderedConnection = function(source, type) {
/**
* Workspace units, (0, 0) is top left of block.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.offsetInBlock_ = new goog.math.Coordinate(0, 0);
this.offsetInBlock_ = new Blockly.utils.Coordinate(0, 0);
};
goog.inherits(Blockly.RenderedConnection, Blockly.Connection);
@@ -144,7 +143,7 @@ Blockly.RenderedConnection.prototype.moveBy = function(dx, dy) {
/**
* Move this connection to the location given by its offset within the block and
* the location of the block's top left corner.
* @param {!goog.math.Coordinate} blockTL The location of the top left corner
* @param {!Blockly.utils.Coordinate} blockTL The location of the top left corner
* of the block, in workspace coordinates.
*/
Blockly.RenderedConnection.prototype.moveToOffset = function(blockTL) {
@@ -187,7 +186,7 @@ Blockly.RenderedConnection.prototype.tighten_ = function() {
* Find the closest compatible connection to this connection.
* All parameters are in workspace units.
* @param {number} maxLimit The maximum radius to another connection.
* @param {!goog.math.Coordinate} dxy Offset between this connection's location
* @param {!Blockly.utils.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,

View File

@@ -27,11 +27,10 @@
goog.provide('Blockly.Scrollbar');
goog.provide('Blockly.ScrollbarPair');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Touch');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/**
* A note on units: most of the numbers that are in CSS pixels are scaled if the
@@ -211,10 +210,10 @@ Blockly.Scrollbar = function(workspace, horizontal, opt_pair, opt_class) {
* The upper left corner of the scrollbar's SVG group in CSS pixels relative
* to the scrollbar's origin. This is usually relative to the injection div
* origin.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
this.position_ = new goog.math.Coordinate(0, 0);
this.position_ = new Blockly.utils.Coordinate(0, 0);
// Store the thickness in a temp variable for readability.
var scrollbarThickness = Blockly.Scrollbar.scrollbarThickness;
@@ -246,10 +245,10 @@ Blockly.Scrollbar = function(workspace, horizontal, opt_pair, opt_class) {
* The location of the origin of the workspace that the scrollbar is in,
* measured in CSS pixels relative to the injection div origin. This is usually
* (0, 0). When the scrollbar is in a flyout it may have a different origin.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
Blockly.Scrollbar.prototype.origin_ = new goog.math.Coordinate(0, 0);
Blockly.Scrollbar.prototype.origin_ = new Blockly.utils.Coordinate(0, 0);
/**
* The position of the mouse along this scrollbar's major axis at the start of
@@ -257,7 +256,7 @@ Blockly.Scrollbar.prototype.origin_ = new goog.math.Coordinate(0, 0);
* Units are CSS pixels, with (0, 0) at the top left of the browser window.
* For a horizontal scrollbar this is the x coordinate of the mouse down event;
* for a vertical scrollbar it's the y coordinate of the mouse down event.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
*/
Blockly.Scrollbar.prototype.startDragMouse_ = 0;
@@ -860,5 +859,5 @@ Blockly.Scrollbar.prototype.set = function(value) {
* @param {number} y The y coordinate of the scrollbar's origin, in CSS pixels.
*/
Blockly.Scrollbar.prototype.setOrigin = function(x, y) {
this.origin_ = new goog.math.Coordinate(x, y);
this.origin_ = new Blockly.utils.Coordinate(x, y);
};

View File

@@ -27,11 +27,10 @@
goog.provide('Blockly.TouchGesture');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Gesture');
goog.require('Blockly.utils');
goog.require('goog.math.Coordinate');
/*
* Note: In this file "start" refers to touchstart, mousedown, and pointerstart
@@ -58,7 +57,7 @@ Blockly.TouchGesture = function(e, creatorWorkspace) {
/**
* A map of cached points used for tracking multi-touch gestures.
* @type {Object<number|string, goog.math.Coordinate>}
* @type {Object<number|string, Blockly.utils.Coordinate>}
* @private
*/
this.cachedPoints_ = {};
@@ -240,7 +239,7 @@ Blockly.TouchGesture.prototype.handleTouchStart = function(e) {
if (pointers.length == 2) {
var point0 = this.cachedPoints_[pointers[0]];
var point1 = this.cachedPoints_[pointers[1]];
this.startDistance_ = goog.math.Coordinate.distance(point0, point1);
this.startDistance_ = Blockly.utils.Coordinate.distance(point0, point1);
this.isMultiTouch_ = true;
e.preventDefault();
}
@@ -263,7 +262,7 @@ Blockly.TouchGesture.prototype.handleTouchMove = function(e) {
// Calculate the distance between the two pointers
var point0 = this.cachedPoints_[pointers[0]];
var point1 = this.cachedPoints_[pointers[1]];
var moveDistance = goog.math.Coordinate.distance(point0, point1);
var moveDistance = Blockly.utils.Coordinate.distance(point0, point1);
var startDistance = this.startDistance_;
var scale = this.touchScale_ = moveDistance / startDistance;
@@ -301,14 +300,14 @@ Blockly.TouchGesture.prototype.handleTouchEnd = function(e) {
/**
* Helper function returning the current touch point coordinate.
* @param {!Event} e A touch or pointer event.
* @return {goog.math.Coordinate} The current touch point coordinate
* @return {Blockly.utils.Coordinate} The current touch point coordinate
* @package
*/
Blockly.TouchGesture.prototype.getTouchPoint = function(e) {
if (!this.startWorkspace_) {
return null;
}
return new goog.math.Coordinate(
return new Blockly.utils.Coordinate(
(e.pageX ? e.pageX : e.changedTouches[0].pageX),
(e.pageY ? e.pageY : e.changedTouches[0].pageY)
);

View File

@@ -32,10 +32,10 @@
*/
goog.provide('Blockly.utils');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Msg');
goog.require('Blockly.userAgent');
goog.require('goog.math.Coordinate');
goog.require('goog.style');
@@ -135,10 +135,10 @@ Blockly.utils.isTargetInput = function(e) {
* Return the coordinates of the top-left corner of this element relative to
* its parent. Only for SVG elements and children (e.g. rect, g, path).
* @param {!Element} element SVG element to find the coordinates of.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
*/
Blockly.utils.getRelativeXY = function(element) {
var xy = new goog.math.Coordinate(0, 0);
var xy = new Blockly.utils.Coordinate(0, 0);
// First, check for x and y attributes.
var x = element.getAttribute('x');
if (x) {
@@ -179,7 +179,7 @@ Blockly.utils.getRelativeXY = function(element) {
* @param {!Element} element SVG element to find the coordinates of. If this is
* not a child of the div Blockly was injected into, the behaviour is
* undefined.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
*/
Blockly.utils.getInjectionDivXY_ = function(element) {
var x = 0;
@@ -194,7 +194,7 @@ Blockly.utils.getInjectionDivXY_ = function(element) {
}
element = element.parentNode;
}
return new goog.math.Coordinate(x, y);
return new Blockly.utils.Coordinate(x, y);
};
/**
@@ -975,7 +975,7 @@ Blockly.utils.containsNode = function(parent, descendant) {
/**
* Gets the document scroll distance as a coordinate object.
* Copied from Closure's goog.dom.getDocumentScroll.
* @return {!goog.math.Coordinate} Object with values 'x' and 'y'.
* @return {!Blockly.utils.Coordinate} Object with values 'x' and 'y'.
*/
Blockly.utils.getDocumentScroll = function() {
var el = document.documentElement;
@@ -984,9 +984,9 @@ Blockly.utils.getDocumentScroll = function() {
// The keyboard on IE10 touch devices shifts the page using the pageYOffset
// without modifying scrollTop. For this case, we want the body scroll
// offsets.
return new goog.math.Coordinate(el.scrollLeft, el.scrollTop);
return new Blockly.utils.Coordinate(el.scrollLeft, el.scrollTop);
}
return new goog.math.Coordinate(
return new Blockly.utils.Coordinate(
win.pageXOffset || el.scrollLeft, win.pageYOffset || el.scrollTop);
};

139
core/utils_coordinate.js Normal file
View File

@@ -0,0 +1,139 @@
/**
* @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 coordinate 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.Coordinate
* @namespace
*/
goog.provide('Blockly.utils.Coordinate');
/**
* Class for representing coordinates and positions.
* @param {number=} opt_x Left, defaults to 0.
* @param {number=} opt_y Top, defaults to 0.
* @struct
* @constructor
*/
Blockly.utils.Coordinate = function(x, y) {
/**
* X-value
* @type {number}
*/
this.x = x;
/**
* Y-value
* @type {number}
*/
this.y = y;
};
/**
* Compares coordinates for equality.
* @param {Blockly.utils.Coordinate} a A Coordinate.
* @param {Blockly.utils.Coordinate} b A Coordinate.
* @return {boolean} True iff the coordinates are equal, or if both are null.
*/
Blockly.utils.Coordinate.equals = function(a, b) {
if (a == b) {
return true;
}
if (!a || !b) {
return false;
}
return a.x == b.x && a.y == b.y;
};
/**
* Returns the distance between two coordinates.
* @param {!Blockly.utils.Coordinate} a A Coordinate.
* @param {!Blockly.utils.Coordinate} b A Coordinate.
* @return {number} The distance between `a` and `b`.
*/
Blockly.utils.Coordinate.distance = function(a, b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
};
/**
* Returns the magnitude of a coordinate.
* @param {!Blockly.utils.Coordinate} a A Coordinate.
* @return {number} The distance between the origin and `a`.
*/
Blockly.utils.Coordinate.magnitude = function(a) {
return Math.sqrt(a.x * a.x + a.y * a.y);
};
/**
* Returns the difference between two coordinates as a new
* Blockly.utils.Coordinate.
* @param {!Blockly.utils.Coordinate} a A Coordinate.
* @param {!Blockly.utils.Coordinate} b A Coordinate.
* @return {!Blockly.utils.Coordinate} A Coordinate representing the difference
* between `a` and `b`.
*/
Blockly.utils.Coordinate.difference = function(a, b) {
return new Blockly.utils.Coordinate(a.x - b.x, a.y - b.y);
};
/**
* Returns the sum of two coordinates as a new Blockly.utils.Coordinate.
* @param {!Blockly.utils.Coordinate} a A Coordinate.
* @param {!Blockly.utils.Coordinate} b A Coordinate.
* @return {!Blockly.utils.Coordinate} A Coordinate representing the sum of the two
* coordinates.
*/
Blockly.utils.Coordinate.sum = function(a, b) {
return new Blockly.utils.Coordinate(a.x + b.x, a.y + b.y);
};
/**
* Scales this coordinate by the given scale factor.
* @param {number} s The scale factor to use for both x and y dimensions.
* @return {!Blockly.utils.Coordinate} This coordinate after scaling.
*/
Blockly.utils.Coordinate.prototype.scale = function(s) {
this.x *= s;
this.y *= s;
return this;
};
/**
* Translates this coordinate by the given offsets.
* respectively.
* @param {number} tx The value to translate x by.
* @param {number} ty The value to translate y by.
* @return {!Blockly.utils.Coordinate} This coordinate after translating.
*/
Blockly.utils.Coordinate.prototype.translate = function(tx, ty) {
this.x += tx;
this.y += ty;
return this;
};

View File

@@ -26,6 +26,7 @@
goog.provide('Blockly.WorkspaceComment');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.CommentChange');
goog.require('Blockly.Events.CommentCreate');
@@ -34,8 +35,6 @@ goog.require('Blockly.Events.CommentMove');
goog.require('Blockly.utils');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
/**
* Class for a workspace comment.
@@ -57,10 +56,10 @@ Blockly.WorkspaceComment = function(workspace, content, height, width, opt_id) {
/**
* The comment's position in workspace units. (0, 0) is at the workspace's
* origin; scale does not change this value.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @protected
*/
this.xy_ = new goog.math.Coordinate(0, 0);
this.xy_ = new Blockly.utils.Coordinate(0, 0);
/**
* The comment's height in workspace units. Scale does not change this value.
@@ -174,12 +173,12 @@ Blockly.WorkspaceComment.prototype.setWidth = function(width) {
/**
* Get stored location.
* @return {!goog.math.Coordinate} The comment's stored location. This is not
* valid if the comment is currently being dragged.
* @return {!Blockly.utils.Coordinate} The comment's stored location.
* This is not valid if the comment is currently being dragged.
* @package
*/
Blockly.WorkspaceComment.prototype.getXY = function() {
return this.xy_.clone();
return new Blockly.utils.Coordinate(this.xy_.x, this.xy_.y);
};
/**

View File

@@ -26,11 +26,10 @@
goog.provide('Blockly.WorkspaceCommentSvg.render');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceCommentSvg');
goog.require('goog.math.Coordinate');
/**
* Size of the resize icon.
@@ -265,7 +264,7 @@ Blockly.WorkspaceCommentSvg.prototype.resizeMouseDown_ = function(e) {
return;
}
// Left-click (or middle click)
this.workspace.startDrag(e, new goog.math.Coordinate(
this.workspace.startDrag(e, new Blockly.utils.Coordinate(
this.workspace.RTL ? -this.width_ : this.width_, this.height_));
this.onMouseUpWrapper_ = Blockly.bindEventWithChecks_(

View File

@@ -26,6 +26,7 @@
goog.provide('Blockly.WorkspaceCommentSvg');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.CommentCreate');
goog.require('Blockly.Events.CommentDelete');
@@ -34,8 +35,6 @@ goog.require('Blockly.Events.Ui');
goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceComment');
goog.require('goog.math.Coordinate');
/**
* Class for a workspace comment's SVG representation.
@@ -273,7 +272,7 @@ Blockly.WorkspaceCommentSvg.prototype.removeFocus = function() {
* If the comment is on the workspace, (0, 0) is the origin of the workspace
* coordinate system.
* This does not change with workspace scale.
* @return {!goog.math.Coordinate} Object with .x and .y properties in
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties in
* workspace coordinates.
* @package
*/
@@ -304,7 +303,7 @@ Blockly.WorkspaceCommentSvg.prototype.getRelativeToSurfaceXY = function() {
} while (element && element != this.workspace.getBubbleCanvas() &&
element != dragSurfaceGroup);
}
this.xy_ = new goog.math.Coordinate(x, y);
this.xy_ = new Blockly.utils.Coordinate(x, y);
return this.xy_;
};
@@ -319,7 +318,7 @@ Blockly.WorkspaceCommentSvg.prototype.moveBy = function(dx, dy) {
// TODO: Do I need to look up the relative to surface XY position here?
var xy = this.getRelativeToSurfaceXY();
this.translate(xy.x + dx, xy.y + dy);
this.xy_ = new goog.math.Coordinate(xy.x + dx, xy.y + dy);
this.xy_ = new Blockly.utils.Coordinate(xy.x + dx, xy.y + dy);
event.recordNew();
Blockly.Events.fire(event);
this.workspace.resizeContents();
@@ -333,7 +332,7 @@ Blockly.WorkspaceCommentSvg.prototype.moveBy = function(dx, dy) {
* @package
*/
Blockly.WorkspaceCommentSvg.prototype.translate = function(x, y) {
this.xy_ = new goog.math.Coordinate(x, y);
this.xy_ = new Blockly.utils.Coordinate(x, y);
this.getSvgRoot().setAttribute('transform',
'translate(' + x + ',' + y + ')');
};
@@ -363,7 +362,7 @@ Blockly.WorkspaceCommentSvg.prototype.moveToDragSurface_ = function() {
* Move this comment back to the workspace block canvas.
* Generally should be called at the same time as setDragging(false).
* Does nothing if useDragSurface_ is false.
* @param {!goog.math.Coordinate} newXY The position the comment should take on
* @param {!Blockly.utils.Coordinate} newXY The position the comment should take on
* on the workspace canvas, in workspace coordinates.
* @private
*/
@@ -381,7 +380,7 @@ Blockly.WorkspaceCommentSvg.prototype.moveOffDragSurface_ = function(newXY) {
* drag surface to translate blocks.
* @param {Blockly.BlockDragSurfaceSvg} dragSurface The surface that carries
* rendered items during a drag, or null if no drag surface is in use.
* @param {!goog.math.Coordinate} newLoc The location to translate to, in
* @param {!Blockly.utils.Coordinate} newLoc The location to translate to, in
* workspace coordinates.
* @package
*/
@@ -419,7 +418,7 @@ Blockly.WorkspaceCommentSvg.prototype.clearTransformAttributes_ = function() {
* Returns the coordinates of a bounding box describing the dimensions of this
* comment.
* Coordinate system: workspace coordinates.
* @return {!{topLeft: goog.math.Coordinate, bottomRight: goog.math.Coordinate}}
* @return {!{topLeft: Blockly.utils.Coordinate, bottomRight: Blockly.utils.Coordinate}}
* Object with top left and bottom right coordinates of the bounding box.
* @package
*/
@@ -429,17 +428,17 @@ Blockly.WorkspaceCommentSvg.prototype.getBoundingRectangle = function() {
var topLeft;
var bottomRight;
if (this.RTL) {
topLeft = new goog.math.Coordinate(blockXY.x - (commentBounds.width),
topLeft = new Blockly.utils.Coordinate(blockXY.x - (commentBounds.width),
blockXY.y);
// 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 goog.math.Coordinate(blockXY.x,
bottomRight = new Blockly.utils.Coordinate(blockXY.x,
blockXY.y + commentBounds.height);
} 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 goog.math.Coordinate(blockXY.x, blockXY.y);
bottomRight = new goog.math.Coordinate(blockXY.x + commentBounds.width,
topLeft = new Blockly.utils.Coordinate(blockXY.x, blockXY.y);
bottomRight = new Blockly.utils.Coordinate(blockXY.x + commentBounds.width,
blockXY.y + commentBounds.height);
}
return {topLeft: topLeft, bottomRight: bottomRight};

View File

@@ -119,7 +119,7 @@ Blockly.WorkspaceDragSurfaceSvg.prototype.translateSurface = function(x, y) {
/**
* Reports the surface translation in scaled workspace coordinates.
* Use this when finishing a drag to return blocks to the correct position.
* @return {!goog.math.Coordinate} Current translation of the surface
* @return {!Blockly.utils.Coordinate} Current translation of the surface
* @package
*/
Blockly.WorkspaceDragSurfaceSvg.prototype.getSurfaceTranslation = function() {

View File

@@ -26,7 +26,7 @@
goog.provide('Blockly.WorkspaceDragger');
goog.require('goog.math.Coordinate');
goog.require('Blockly.utils.Coordinate');
/**
@@ -48,10 +48,10 @@ Blockly.WorkspaceDragger = function(workspace) {
/**
* The scroll position of the workspace at the beginning of the drag.
* Coordinate system: pixel coordinates.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
this.startScrollXY_ = new goog.math.Coordinate(
this.startScrollXY_ = new Blockly.utils.Coordinate(
workspace.scrollX, workspace.scrollY);
};
@@ -76,7 +76,7 @@ Blockly.WorkspaceDragger.prototype.startDrag = function() {
/**
* Finish dragging the workspace and put everything back where it belongs.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel coordinates.
* @package
*/
@@ -88,11 +88,11 @@ Blockly.WorkspaceDragger.prototype.endDrag = function(currentDragDeltaXY) {
/**
* Move the workspace based on the most recent mouse movements.
* @param {!goog.math.Coordinate} currentDragDeltaXY How far the pointer has
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
* moved from the position at the start of the drag, in pixel coordinates.
* @package
*/
Blockly.WorkspaceDragger.prototype.drag = function(currentDragDeltaXY) {
var newXY = goog.math.Coordinate.sum(this.startScrollXY_, currentDragDeltaXY);
var newXY = Blockly.utils.Coordinate.sum(this.startScrollXY_, currentDragDeltaXY);
this.workspace_.scroll(newXY.x, newXY.y);
};

View File

@@ -30,6 +30,7 @@ goog.provide('Blockly.WorkspaceSvg');
//goog.require('Blockly.BlockSvg');
goog.require('Blockly.ConnectionDB');
goog.require('Blockly.constants');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockCreate');
goog.require('Blockly.Gesture');
@@ -51,8 +52,6 @@ goog.require('Blockly.WorkspaceDragSurfaceSvg');
goog.require('Blockly.Xml');
goog.require('Blockly.ZoomControls');
goog.require('goog.math.Coordinate');
/**
* Class for a workspace. This is an onscreen area with optional trashcan,
@@ -250,7 +249,7 @@ Blockly.WorkspaceSvg.prototype.startScrollY = 0;
/**
* Distance from mouse to object being dragged.
* @type {goog.math.Coordinate}
* @type {Blockly.utils.Coordinate}
* @private
*/
Blockly.WorkspaceSvg.prototype.dragDeltaXY_ = null;
@@ -339,7 +338,7 @@ Blockly.WorkspaceSvg.prototype.injectionDiv_ = null;
* Last known position of the page scroll.
* This is used to determine whether we have recalculated screen coordinate
* stuff since the page scrolled.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
* @private
*/
Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null;
@@ -429,7 +428,7 @@ Blockly.WorkspaceSvg.prototype.isVisible = function() {
* scales that after canvas SVG element, if it's a descendant.
* The origin (0,0) is the top-left corner of the Blockly SVG.
* @param {!Element} element Element to find the coordinates of.
* @return {!goog.math.Coordinate} Object with .x and .y properties.
* @return {!Blockly.utils.Coordinate} Object with .x and .y properties.
* @private
*/
Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) {
@@ -453,7 +452,7 @@ Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) {
y += xy.y * scale;
element = element.parentNode;
} while (element && element != this.getParentSvg());
return new goog.math.Coordinate(x, y);
return new Blockly.utils.Coordinate(x, y);
};
/**
@@ -461,7 +460,7 @@ Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) {
* origin in pixels.
* The workspace origin is where a block would render at position (0, 0).
* It is not the upper left corner of the workspace SVG.
* @return {!goog.math.Coordinate} Offset in pixels.
* @return {!Blockly.utils.Coordinate} Offset in pixels.
* @package
*/
Blockly.WorkspaceSvg.prototype.getOriginOffsetInPixels = function() {
@@ -774,7 +773,7 @@ Blockly.WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled =
function() {
/* eslint-disable indent */
var currScroll = Blockly.utils.getDocumentScroll();
if (!goog.math.Coordinate.equals(this.lastRecordedPageScroll_, currScroll)) {
if (!Blockly.utils.Coordinate.equals(this.lastRecordedPageScroll_, currScroll)) {
this.lastRecordedPageScroll_ = currScroll;
this.updateScreenCalculations_();
}
@@ -1063,7 +1062,7 @@ Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) {
var connections = block.getConnections_(false);
for (var i = 0, connection; connection = connections[i]; i++) {
var neighbour = connection.closest(Blockly.SNAP_RADIUS,
new goog.math.Coordinate(blockX, blockY));
new Blockly.utils.Coordinate(blockX, blockY));
if (neighbour.connection) {
collide = true;
break;
@@ -1227,7 +1226,7 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) {
/**
* Start tracking a drag of an object on this workspace.
* @param {!Event} e Mouse down event.
* @param {!goog.math.Coordinate} xy Starting location of object.
* @param {!Blockly.utils.Coordinate} xy Starting location of object.
*/
Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) {
// Record the starting offset between the bubble's location and the mouse.
@@ -1236,13 +1235,13 @@ Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) {
// Fix scale of mouse event.
point.x /= this.scale;
point.y /= this.scale;
this.dragDeltaXY_ = goog.math.Coordinate.difference(xy, point);
this.dragDeltaXY_ = Blockly.utils.Coordinate.difference(xy, point);
};
/**
* Track a drag of an object on this workspace.
* @param {!Event} e Mouse move event.
* @return {!goog.math.Coordinate} New location of object.
* @return {!Blockly.utils.Coordinate} New location of object.
*/
Blockly.WorkspaceSvg.prototype.moveDrag = function(e) {
var point = Blockly.utils.mouseToSvg(e, this.getParentSvg(),
@@ -1250,7 +1249,7 @@ Blockly.WorkspaceSvg.prototype.moveDrag = function(e) {
// Fix scale of mouse event.
point.x /= this.scale;
point.y /= this.scale;
return goog.math.Coordinate.sum(this.dragDeltaXY_, point);
return Blockly.utils.Coordinate.sum(this.dragDeltaXY_, point);
};
/**

View File

@@ -30,13 +30,12 @@ goog.provide('Blockly.Events.CommentCreate');
goog.provide('Blockly.Events.CommentDelete');
goog.provide('Blockly.Events.CommentMove');
goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.Events');
goog.require('Blockly.Events.Abstract');
goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
/**
* Abstract class for a comment event.
@@ -312,13 +311,13 @@ Blockly.Events.CommentMove = function(comment) {
/**
* The location before the move, in workspace coordinates.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
*/
this.oldCoordinate_ = comment.getXY();
/**
* The location after the move, in workspace coordinates.
* @type {!goog.math.Coordinate}
* @type {!Blockly.utils.Coordinate}
*/
this.newCoordinate_ = null;
};
@@ -346,7 +345,7 @@ Blockly.Events.CommentMove.prototype.type = Blockly.Events.COMMENT_MOVE;
/**
* Override the location before the move. Use this if you don't create the
* event until the end of the move, but you know the original location.
* @param {!goog.math.Coordinate} xy The location before the move, in workspace
* @param {!Blockly.utils.Coordinate} xy The location before the move, in workspace
* coordinates.
*/
Blockly.Events.CommentMove.prototype.setOldCoordinate = function(xy) {
@@ -377,7 +376,7 @@ Blockly.Events.CommentMove.prototype.fromJson = function(json) {
if (json['newCoordinate']) {
var xy = json['newCoordinate'].split(',');
this.newCoordinate_ =
new goog.math.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
new Blockly.utils.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
}
};
@@ -386,7 +385,7 @@ Blockly.Events.CommentMove.prototype.fromJson = function(json) {
* @return {boolean} False if something changed.
*/
Blockly.Events.CommentMove.prototype.isNull = function() {
return goog.math.Coordinate.equals(this.oldCoordinate_, this.newCoordinate_);
return Blockly.utils.Coordinate.equals(this.oldCoordinate_, this.newCoordinate_);
};
/**