Keep simplifing connection algorithm.

This commit is contained in:
Paulo Gustavo Veiga 2012-08-18 23:20:23 -03:00
parent bff79159e1
commit 04fda82ba0
3 changed files with 47 additions and 81 deletions

View File

@ -30,52 +30,49 @@ mindplot.DragConnector = new Class({
var topics = this._designerModel.getTopics();
// Must be disconnected from their current connection ?.
var mainTopicToMainTopicConnection = this._lookUpForMainTopicToMainTopicConnection(dragTopic);
var candidates = this._searchConnectionCandidates(dragTopic);
var currentConnection = dragTopic.getConnectedToTopic();
if ($defined(currentConnection)) {
// MainTopic->MainTopicConnection.
if (currentConnection.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) {
if (mainTopicToMainTopicConnection != currentConnection) {
dragTopic.disconnect(this._workspace);
}
}
else if (currentConnection.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) {
// Distance if greater that the allowed.
var dragXPosition = dragTopic.getPosition().x;
var currentXPosition = currentConnection.getPosition().x;
if ($defined(mainTopicToMainTopicConnection)) {
// I have to change the current connection to a main topic.
dragTopic.disconnect(this._workspace);
} else if (Math.abs(dragXPosition - currentXPosition) > mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
dragTopic.disconnect(this._workspace);
}
}
if (currentConnection && (candidates.length == 0 || candidates[0] != currentConnection)) {
dragTopic.disconnect(this._workspace);
}
// Finally, connect nodes ...
if (!dragTopic.isConnected()) {
var centralTopic = topics[0];
if ($defined(mainTopicToMainTopicConnection)) {
dragTopic.connectTo(mainTopicToMainTopicConnection);
} else if (Math.abs(dragTopic.getPosition().x - centralTopic.getPosition().x) <= mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE) {
dragTopic.connectTo(centralTopic);
}
if (!dragTopic.isConnected() && candidates.length > 0) {
dragTopic.connectTo(candidates[0]);
}
},
_lookUpForMainTopicToMainTopicConnection:function (dragTopic) {
_searchConnectionCandidates:function (dragTopic) {
var topics = this._designerModel.getTopics();
// Filter all the nodes that are outside the boundary ...
// Perform a initial filter to discard topics:
// - Exclude dragged topic
// - Exclude dragTopic pivot
// - Nodes that are collapsed
var draggedNode = dragTopic.getDraggedTopic();
var sourcePosition = dragTopic.getPosition();
topics = topics.filter(function (topic) {
var pos = topic.getPosition();
return (sourcePosition.x - pos.x) * Math.sign(pos.x) > 0 && draggedNode != topic;
return draggedNode != topic && topic != dragTopic._draggedNode && !topic.areChildrenShrunken() && !topic.isCollapsed();
});
// Topics must be ordered based on the distance vertical distance to the drag topic ...
// Filter all the nodes that are outside the vertical boundary:
// * The node is to out of the x scope
// * The x distance greater the vertical tolerated distance
var sourcePosition = dragTopic.getPosition();
topics = topics.filter(function (topic) {
var tpos = topic.getPosition();
var distance = (sourcePosition.x - tpos.x) * Math.sign(sourcePosition.x);
// Center topic has different alignment than the rest of the nodes. That's why i need to divide it by two...
var width = topic.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE ? topic.getSize().width / 2 : topic.getSize().width;
return (distance > width) && ((distance - width) < mindplot.DragConnector.MAX_VERTICAL_CONNECTION_TOLERANCE);
});
// Assign a priority based on the distance:
// - Alignment with the targetNode
// - Vertical distance
//
topics = topics.sort(function (a, b) {
var aPos = a.getPosition();
var ad = (sourcePosition.x - aPos.x) * Math.sign(aPos.x);
@ -83,36 +80,29 @@ mindplot.DragConnector = new Class({
var bPos = b.getPosition();
var bd = (sourcePosition.x - bPos.x) * Math.sign(bPos.x);
return ad > bd;
var av = this._isVerticallyAligned(a.getSize(), a.getPosition(), sourcePosition);
var bv = this._isVerticallyAligned(b.getSize(), b.getPosition(), sourcePosition);
return ((bv ? 1000 : 1) + bd) - ((av ? 1000 : 1) + ad);
}.bind(this));
console.log("---- out ----");
topics.each(function (e) {
console.log(e.getText());
});
console.log("---- out ----");
// Check MainTopic->MainTopic connection...
var result = null;
var distance = null;
for (var i = 0; i < topics.length; i++) {
var position = dragTopic.getPosition();
var targetTopic = topics[i];
return topics;
},
var canBeConnected = dragTopic.canBeConnectedTo(targetTopic);
if (canBeConnected) {
var targetPosition = targetTopic.getPosition();
var fix = position.y > targetPosition.y;
var gap = 0;
if (targetTopic.getChildren().length > 0) {
gap = Math.abs(targetPosition.y - targetTopic.getChildren()[0].getPosition().y)
}
_isVerticallyAligned:function (targetSize, targetPosition, sourcePosition) {
return (sourcePosition.y - targetPosition.y) < targetSize.height;
var yDistance = Math.abs(position.y - fix * gap - targetPosition.y);
if (distance == null || yDistance < distance) {
result = targetTopic;
distance = yDistance;
break;
}
}
}
return result;
}
});
mindplot.DragConnector.CENTRAL_TO_MAINTOPIC_MAX_HORIZONTAL_DISTANCE = 400;
mindplot.DragConnector.MAX_VERTICAL_CONNECTION_TOLERANCE = 80;

View File

@ -95,29 +95,6 @@ mindplot.DragTopic = new Class({
dragPivot.disconnect(workspace);
},
canBeConnectedTo:function (targetTopic) {
$assert(targetTopic, 'parent can not be null');
var result = true;
if (!targetTopic.areChildrenShrunken() && !targetTopic.isCollapsed()) {
// Dragged node can not be connected to himself.
if (targetTopic == this._draggedNode) {
result = false;
} else {
var draggedNode = this.getDraggedTopic();
var topicPosition = this.getPosition();
var targetTopicModel = targetTopic.getModel();
var childTopicModel = draggedNode.getModel();
result = targetTopicModel.canBeConnected(childTopicModel, topicPosition, targetTopic.getSize());
}
} else {
result = false;
}
return result;
},
connectTo:function (parent) {
$assert(parent, 'Parent connection node can not be null.');

View File

@ -39,7 +39,6 @@ mindplot.widget.ColorPalettePanel = new Class({
method:'get',
async:false,
onRequest:function () {
console.log("loading ...");
},
onSuccess:function (responseText) {
result = responseText;