mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
add searchForClosest
This commit is contained in:
parent
1b1d777bc5
commit
c177fa5276
3 changed files with 3 additions and 105 deletions
|
@ -562,94 +562,6 @@ Blockly.Connection.prototype.closest = function(maxLimit, dx, dy) {
|
|||
return {connection: closestConnection, radius: this.distanceFrom(closestConnection)};
|
||||
}
|
||||
return {connection: null, radius: maxLimit};
|
||||
// if (this.targetConnection) {
|
||||
// // Don't offer to connect to a connection that's already connected.
|
||||
// return {connection: null, radius: maxLimit};
|
||||
// }
|
||||
// // Determine the opposite type of connection.
|
||||
// var db = this.dbOpposite_;
|
||||
|
||||
// // Since this connection is probably being dragged, add the delta.
|
||||
// var currentX = this.x_ + dx;
|
||||
// var currentY = this.y_ + dy;
|
||||
|
||||
// // Find the closest y location.
|
||||
// var candidatePosition = db.findPositionForConnection_(this);
|
||||
|
||||
// // Walk forward and back on the y axis looking for the closest x,y point.
|
||||
// var pointerMin = candidatePosition;
|
||||
// var pointerMax = candidatePosition;
|
||||
// var closestConnection = null;
|
||||
// var sourceBlock = this.sourceBlock_;
|
||||
// var thisConnection = this;
|
||||
// if (db.length) {
|
||||
// while (pointerMin >= 0 && checkConnection_(pointerMin)) {
|
||||
// pointerMin--;
|
||||
// }
|
||||
// do {
|
||||
// pointerMax++;
|
||||
// } while (pointerMax < db.length && checkConnection_(pointerMax));
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Computes if the current connection is within the allowed radius of another
|
||||
// * connection.
|
||||
// * This function is a closure and has access to outside variables.
|
||||
// * @param {number} yIndex The other connection's index in the database.
|
||||
// * @return {boolean} True if the search needs to continue: either the current
|
||||
// * connection's vertical distance from the other connection is less than
|
||||
// * the allowed radius, or if the connection is not compatible.
|
||||
// * @private
|
||||
// */
|
||||
// function checkConnection_(yIndex) {
|
||||
// var connection = db[yIndex];
|
||||
// if (connection.type == Blockly.OUTPUT_VALUE ||
|
||||
// connection.type == Blockly.PREVIOUS_STATEMENT) {
|
||||
// // Don't offer to connect an already connected left (male) value plug to
|
||||
// // an available right (female) value plug. Don't offer to connect the
|
||||
// // bottom of a statement block to one that's already connected.
|
||||
// if (connection.targetConnection) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// // Offering to connect the top of a statement block to an already connected
|
||||
// // connection is ok, we'll just insert it into the stack.
|
||||
|
||||
// // Offering to connect the left (male) of a value block to an already
|
||||
// // connected value pair is ok, we'll splice it in.
|
||||
// // However, don't offer to splice into an unmovable block.
|
||||
// if (connection.type == Blockly.INPUT_VALUE &&
|
||||
// connection.targetConnection &&
|
||||
// !connection.targetBlock().isMovable() &&
|
||||
// !connection.targetBlock().isShadow()) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// // Do type checking.
|
||||
// if (!thisConnection.checkType_(connection)) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// // Don't let blocks try to connect to themselves or ones they nest.
|
||||
// var targetSourceBlock = connection.sourceBlock_;
|
||||
// do {
|
||||
// if (sourceBlock == targetSourceBlock) {
|
||||
// return true;
|
||||
// }
|
||||
// targetSourceBlock = targetSourceBlock.getParent();
|
||||
// } while (targetSourceBlock);
|
||||
|
||||
// // Only connections within the maxLimit radius.
|
||||
// var dx = currentX - connection.x_;
|
||||
// var dy = currentY - connection.y_;
|
||||
// var r = Math.sqrt(dx * dx + dy * dy);
|
||||
// if (r <= maxLimit) {
|
||||
// closestConnection = connection;
|
||||
// maxLimit = r;
|
||||
// }
|
||||
// return Math.abs(dy) < maxLimit;
|
||||
// }
|
||||
// return {connection: closestConnection, radius: maxLimit};
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -141,6 +141,7 @@ Blockly.ConnectionDB.prototype.removeConnection_ = function(connection) {
|
|||
if (removalIndex == -1) {
|
||||
throw 'Unable to find connection in connectionDB.';
|
||||
}
|
||||
connection.inDB_ = false;
|
||||
this.splice(removalIndex, 1);
|
||||
};
|
||||
|
||||
|
@ -246,7 +247,7 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dx,
|
|||
var pointerMin = closestIndex - 1;
|
||||
while (pointerMin >= 0 && this.isInYRange_(pointerMin, conn.y_, maxRadius)) {
|
||||
temp = this[pointerMin];
|
||||
if (isConnectionAllowed(conn, temp, bestRadius)) {
|
||||
if (this.isConnectionAllowed(conn, temp, bestRadius)) {
|
||||
bestConnection = temp;
|
||||
bestRadius = temp.distanceFrom(conn);
|
||||
}
|
||||
|
@ -256,7 +257,7 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dx,
|
|||
var pointerMax = closestIndex;
|
||||
while (pointerMax < this.length && this.isInYRange_(pointerMax, conn.y_, maxRadius)) {
|
||||
temp = this[pointerMax];
|
||||
if (isConnectionAllowed(conn, temp, bestRadius)) {
|
||||
if (this.isConnectionAllowed(conn, temp, bestRadius)) {
|
||||
bestConnection = temp;
|
||||
bestRadius = temp.distanceFrom(conn);
|
||||
}
|
||||
|
|
|
@ -233,21 +233,6 @@ function test_DB_isConnectionAllowed() {
|
|||
assertFalse(db.isConnectionAllowed(one, two, 1000.0));
|
||||
}
|
||||
|
||||
// function test_DB_isConnectionAllowedNext() {
|
||||
// var db = new Blockly.ConnectionDB();
|
||||
// var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
|
||||
// one.setInput(new Input.InputValue("test input", "" /* align */, null /* checks */));
|
||||
|
||||
// var two = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
|
||||
// two.setInput(new Input.InputValue("test input", "" /* align */, null /* checks */));
|
||||
|
||||
// // Don't offer to connect the bottom of a statement block to one that's already connected.
|
||||
// varv three = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
|
||||
// assertTrue(db.isConnectionAllowed(one, three, 20.0));
|
||||
// three.connectReciprocally_(two);
|
||||
// assertFalse(db.isConnectionAllowed(one, three, 20.0));
|
||||
// }
|
||||
|
||||
function helper_getNeighbours(db, x, y, radius) {
|
||||
return db.getNeighbours(helper_createConnection(x, y, Blockly.NEXT_STATEMENT), radius);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue