123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- 'use strict';
- goog.provide('Blockly.ConnectionDB');
- goog.require('Blockly.Connection');
- Blockly.ConnectionDB = function() {
- };
- Blockly.ConnectionDB.prototype = new Array();
- Blockly.ConnectionDB.constructor = Blockly.ConnectionDB;
- Blockly.ConnectionDB.prototype.addConnection = function(connection) {
- if (connection.inDB_) {
- throw 'Connection already in database.';
- }
- if (connection.getSourceBlock().isInFlyout) {
-
- return;
- }
- var position = this.findPositionForConnection_(connection);
- this.splice(position, 0, connection);
- connection.inDB_ = true;
- };
- Blockly.ConnectionDB.prototype.findConnection = function(conn) {
- if (!this.length) {
- return -1;
- }
- var bestGuess = this.findPositionForConnection_(conn);
- if (bestGuess >= this.length) {
-
- return -1;
- }
- var yPos = conn.y_;
-
- var pointerMin = bestGuess;
- var pointerMax = bestGuess;
- while (pointerMin >= 0 && this[pointerMin].y_ == yPos) {
- if (this[pointerMin] == conn) {
- return pointerMin;
- }
- pointerMin--;
- }
- while (pointerMax < this.length && this[pointerMax].y_ == yPos) {
- if (this[pointerMax] == conn) {
- return pointerMax;
- }
- pointerMax++;
- }
- return -1;
- };
- Blockly.ConnectionDB.prototype.findPositionForConnection_ =
- function(connection) {
-
- if (!this.length) {
- return 0;
- }
- var pointerMin = 0;
- var pointerMax = this.length;
- while (pointerMin < pointerMax) {
- var pointerMid = Math.floor((pointerMin + pointerMax) / 2);
- if (this[pointerMid].y_ < connection.y_) {
- pointerMin = pointerMid + 1;
- } else if (this[pointerMid].y_ > connection.y_) {
- pointerMax = pointerMid;
- } else {
- pointerMin = pointerMid;
- break;
- }
- }
- return pointerMin;
- };
- Blockly.ConnectionDB.prototype.removeConnection_ = function(connection) {
- if (!connection.inDB_) {
- throw 'Connection not in database.';
- }
- var removalIndex = this.findConnection(connection);
- if (removalIndex == -1) {
- throw 'Unable to find connection in connectionDB.';
- }
- connection.inDB_ = false;
- this.splice(removalIndex, 1);
- };
- Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) {
- var db = this;
- var currentX = connection.x_;
- var currentY = connection.y_;
-
- var pointerMin = 0;
- var pointerMax = db.length - 2;
- var pointerMid = pointerMax;
- while (pointerMin < pointerMid) {
- if (db[pointerMid].y_ < currentY) {
- pointerMin = pointerMid;
- } else {
- pointerMax = pointerMid;
- }
- pointerMid = Math.floor((pointerMin + pointerMax) / 2);
- }
- var neighbours = [];
-
- function checkConnection_(yIndex) {
- var dx = currentX - db[yIndex].x_;
- var dy = currentY - db[yIndex].y_;
- var r = Math.sqrt(dx * dx + dy * dy);
- if (r <= maxRadius) {
- neighbours.push(db[yIndex]);
- }
- return dy < maxRadius;
- }
-
- pointerMin = pointerMid;
- pointerMax = pointerMid;
- if (db.length) {
- while (pointerMin >= 0 && checkConnection_(pointerMin)) {
- pointerMin--;
- }
- do {
- pointerMax++;
- } while (pointerMax < db.length && checkConnection_(pointerMax));
- }
- return neighbours;
- };
- Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) {
- return (Math.abs(this[index].y_ - baseY) <= maxRadius);
- };
- Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius,
- dxy) {
-
- if (!this.length) {
- return {connection: null, radius: maxRadius};
- }
-
- var baseY = conn.y_;
- var baseX = conn.x_;
- conn.x_ = baseX + dxy.x;
- conn.y_ = baseY + dxy.y;
-
-
-
- var closestIndex = this.findPositionForConnection_(conn);
- var bestConnection = null;
- var bestRadius = maxRadius;
- var temp;
-
- var pointerMin = closestIndex - 1;
- while (pointerMin >= 0 && this.isInYRange_(pointerMin, conn.y_, maxRadius)) {
- temp = this[pointerMin];
- if (conn.isConnectionAllowed(temp, bestRadius)) {
- bestConnection = temp;
- bestRadius = temp.distanceFrom(conn);
- }
- pointerMin--;
- }
- var pointerMax = closestIndex;
- while (pointerMax < this.length && this.isInYRange_(pointerMax, conn.y_,
- maxRadius)) {
- temp = this[pointerMax];
- if (conn.isConnectionAllowed(temp, bestRadius)) {
- bestConnection = temp;
- bestRadius = temp.distanceFrom(conn);
- }
- pointerMax++;
- }
-
- conn.x_ = baseX;
- conn.y_ = baseY;
-
- return {connection: bestConnection, radius: bestRadius};
- };
- Blockly.ConnectionDB.init = function(workspace) {
-
- var dbList = [];
- dbList[Blockly.INPUT_VALUE] = new Blockly.ConnectionDB();
- dbList[Blockly.OUTPUT_VALUE] = new Blockly.ConnectionDB();
- dbList[Blockly.NEXT_STATEMENT] = new Blockly.ConnectionDB();
- dbList[Blockly.PREVIOUS_STATEMENT] = new Blockly.ConnectionDB();
- workspace.connectionDBList = dbList;
- };
|