Free positioning

This commit is contained in:
Gonzalo Bellver
2012-01-17 17:45:51 -03:00
parent ea89dff202
commit 61eca9f932
6 changed files with 104 additions and 98 deletions

View File

@@ -169,6 +169,7 @@ mindplot.layout.LayoutManager = new Class({
node.resetPositionState();
node.resetOrderState();
node.resetFreeState();
this._events.push(event);
}
this._collectChanges(this._treeSet.getChildren(node));

View File

@@ -40,7 +40,6 @@ mindplot.layout.Node = new Class({
},
isFree: function() {
var result = this._getProperty('free');
return this._getProperty('free');
},
@@ -48,6 +47,10 @@ mindplot.layout.Node = new Class({
return this._isPropertyChanged('free');
},
hasFreeDisplacementChanged: function() {
return this._isPropertyChanged('freeDisplacement');
},
setShrunken: function(value) {
this._setProperty('shrink', value);
},
@@ -75,6 +78,13 @@ mindplot.layout.Node = new Class({
}
},
resetFreeState : function() {
var prop = this._properties['freeDisplacement'];
if (prop) {
prop.hasChanged = false;
}
},
getOrder: function() {
return this._getProperty('order');
},

View File

@@ -75,7 +75,7 @@ mindplot.layout.OriginalLayout = new Class({
this._layoutChildren(node, heightById);
this._fixOverlapping(node, heightById);
this._fixOverlapping(node);
}, this);
},
@@ -97,13 +97,16 @@ mindplot.layout.OriginalLayout = new Class({
var heightChanged = node._branchHeight != newBranchHeight;
node._heightChanged = heightChanged || parentHeightChanged;
if (childrenOrderMoved || heightChanged || parentHeightChanged || freeChanged) {
if (childrenOrderMoved || heightChanged || parentHeightChanged) {
var sorter = node.getSorter();
var offsetById = sorter.computeOffsets(this._treeSet, node);
var parentPosition = node.getPosition();
children.forEach(function(child) {
var freeDisplacement = child.getFreeDisplacement();
var freeDisplacement = {x:0, y:0};
var offset = offsetById[child.getId()];
var parentX = parentPosition.x;
var parentY = parentPosition.y;
@@ -122,60 +125,24 @@ mindplot.layout.OriginalLayout = new Class({
}, this);
},
_fixOverlapping: function(node, heightById) {
// console.log("\t\tnode {id:" + node.getId() + "}"); //TODO(gb): Remove trace!!!
_fixOverlapping: function(node) {
var children = this._treeSet.getChildren(node);
var freeChildren = children.filter(function(child) {return child.isFree()});
if (node.hasFreeDisplacementChanged()) {
var yOffset = node.getFreeDisplacement().y;
var branchesToShift = this._treeSet.getBranchesInVerticalDirection(node, yOffset);
this._treeSet.shiftBranchPosition(node, node.getFreeDisplacement().x, node.getFreeDisplacement().y);
// Check for overlap only for free children
freeChildren.forEach(function(child) {
var childVertex = child.getVertex();
// console.log("\t\t\tchild {id:" + child.getId() + ", x0:(" + childVertex.a.x + "," + childVertex.a.y + "), x1:(" + childVertex.b.x + "," + childVertex.b.y + ")}"); //TODO(gb): Remove trace!!!
branchesToShift.forEach(function(branch) {
this._treeSet.shiftBranchPosition(branch, 0, yOffset);
},this);
// Overlap should only occur with siblings (?)
var colliders = this._getColliders(child);
colliders.forEach(function(collider) {
console.log("\t\t\tcollider {id:" + collider.getId() + ", with:" + child.getId() +"}"); //TODO(gb): Remove trace!!!
}, this);
if (colliders.length == 0) {
console.log("\t\t\tNo colliders"); //TODO(gb): Remove trace!!!
}
}, this);
}
children.forEach(function(child) {
this._fixOverlapping(child, heightById);
this._fixOverlapping(child);
}, this);
},
_getColliders: function(node) {
console.log("\t\tcheck with colliders for node " + node.getId() + ":`"); //TODO(gb): Remove trace!!!
var siblings = this._treeSet.getSiblings(node);
var colliders = [];
siblings.forEach(function(sibling) {
var collisions = this._checkCollision(node, sibling, colliders);
if (this._nodesCollide(node, sibling)) {
collisions.push(sibling);
}
}, this);
return colliders;
},
_checkCollision: function(checkNode, node, colliders) {
var children = this._treeSet.getChildren(node);
children.forEach(function(child) {
if (this._nodesCollide(checkNode, child)) {
colliders.push(child);
}
this._checkCollision(checkNode, child, colliders);
}, this);
return colliders;
},
_nodesCollide: function(nodeA, nodeB) {
var nodeAVertex = nodeA.getVertex();
var nodeBVertex = nodeB.getVertex();

View File

@@ -216,19 +216,45 @@ mindplot.layout.RootedTreeSet = new Class({
var children = this.getChildren(node);
children.forEach(function(child) {
this._shiftBranchPosition(child, xOffset, yOffset);
this.shiftBranchPosition(child, xOffset, yOffset);
}.bind(this));
},
_shiftBranchPosition : function(node, xOffset, yOffset) {
shiftBranchPosition: function(node, xOffset, yOffset) {
var position = node.getPosition();
node.setPosition({x:position.x + xOffset, y:position.y + yOffset});
var children = this.getChildren(node);
children.forEach(function(child) {
this._shiftBranchPosition(child, xOffset, yOffset);
this.shiftBranchPosition(child, xOffset, yOffset);
}.bind(this));
},
getBranchesInVerticalDirection: function(node, yOffset) {
// siblings with lower or higher order, depending on the direction of the offset
var siblings = this.getSiblings(node).filter(function(sibling) {
if (yOffset < 0)
return sibling.getOrder() < node.getOrder();
else
return sibling.getOrder() > node.getOrder();
});
// direct descendants of the root that do not contain the node and are on the same side
// and on the direction of the offset
var rootNode = this.getRootNode(node);
var branches = this.getChildren(rootNode).filter(function(child) {
return this._find(node.getId(), child);
}, this);
var branch = branches[0];
var rootDescendants = this.getSiblings(branch).filter(function(sibling) {
var sameSide = node.getPosition().x > rootNode.getPosition().x ? sibling.getPosition().x > rootNode.getPosition().x : sibling.getPosition().x < rootNode.getPosition().x;
var sameDirection = yOffset < 0 ? sibling.getOrder() < branch.getOrder() : sibling.getOrder() > branch.getOrder();
return sameSide && sameDirection;
}, this);
return siblings.combine(rootDescendants);
}
});