reverted to previous version of the trench checking algorithm

This commit is contained in:
Michael Yoshitaka Erlewine 2010-06-16 00:21:34 -04:00
parent 92ff278dcd
commit 02976e5207

View File

@ -105,7 +105,23 @@ Trench.prototype = {
var xRange = {min: rect.left, max: rect.left + rect.width}; var xRange = {min: rect.left, max: rect.left + rect.width};
var yRange = {min: rect.top, max: rect.top + rect.height}; var yRange = {min: rect.top, max: rect.top + rect.height};
switch (edge) { var edgeToCheck;
if (this.type == "border") {
if (this.edge == "left")
edgeToCheck = "right";
else if (this.edge == "right")
edgeToCheck = "left";
else if (this.edge == "top")
edgeToCheck = "bottom";
else if (this.edge == "bottom")
edgeToCheck = "top";
} else if (this.type == "guide") {
edgeToCheck = this.edge;
}
rect.adjustedEdge = edgeToCheck;
switch (edgeToCheck) {
case "left": case "left":
if (this.ruleOverlaps(rect.left, yRange)) { if (this.ruleOverlaps(rect.left, yRange)) {
rect.left = this.position; rect.left = this.position;
@ -178,44 +194,13 @@ var Trenches = {
delete self.trenches[id]; delete self.trenches[id];
}); });
}, },
comparePosition: function Trenches_comparePosition(a,b) {
return a.position - b.position;
},
activateOthersTrenches: function Trenches_activateOthersTrenches(element) { activateOthersTrenches: function Trenches_activateOthersTrenches(element) {
var aT = this.activeTrenches;
aT.left = [];
aT.right = [];
aT.top = [];
aT.bottom = [];
this.safeRegionCache = {left:{},right:{},top:{},bottom:{}};
this.trenches.forEach(function(t) { this.trenches.forEach(function(t) {
if (t.el === element) if (t.el === element)
return; return;
t.active = true; t.active = true;
t.show(); // debug t.show(); // debug
var relevantEdge;
if (t.type == "border") {
if (t.edge == "left")
relevantEdge = "right";
else if (t.edge == "right")
relevantEdge = "left";
else if (t.edge == "top")
relevantEdge = "bottom";
else if (t.edge == "bottom")
relevantEdge = "top";
} else if (t.type == "guide" || t.type == "barrier") {
relevantEdge = t.edge;
}
aT[relevantEdge].push({id: t.id, position: t.position, toString: function(){return '' + t.id + ':' + t.position}});
}); });
aT.left.sort(this.comparePosition);
aT.right.sort(this.comparePosition);
aT.top.sort(this.comparePosition);
aT.bottom.sort(this.comparePosition);
}, },
disactivate: function Trenches_disactivate() { disactivate: function Trenches_disactivate() {
this.trenches.forEach(function(t) { this.trenches.forEach(function(t) {
@ -223,74 +208,6 @@ var Trenches = {
t.show(); t.show();
}); });
}, },
snapEdge: function Trenches_snapEdge(rect,edge,assumeConstantSize,keepProportional) {
var aT = this.activeTrenches;
var position;
switch (edge) {
case 'left':
position = rect.left; break;
case 'right':
position = rect.left + rect.width; break;
case 'top':
position = rect.top; break;
case 'bottom':
position = rect.top + rect.height; break;
}
var safeRegionCache = this.safeRegionCache;
if ('min' in safeRegionCache[edge] && 'max' in safeRegionCache[edge]) {
if (safeRegionCache[edge].min < position && position < safeRegionCache[edge].max) {
return false;
}
}
// Find the relevant trenches to compare to:
// What we're doing here is planting a "rat" within the ordered list of edges that matter
// We then sort the resulting array, use indexOf on the rat, and figure out which trenches
// are closest to the edge in question.
var rat = {id:'rat',position:position};
var index = aT[edge].concat(rat).sort(this.comparePosition).indexOf(rat);
// Now we know to check aT[edge][index-1] and aT[edge][index]
var newRect;
var updated = false;
if (index-1 >= 0) {
var trench = this.getById(aT[edge][index-1].id);
newRect = trench.rectOverlaps(rect,edge,assumeConstantSize,keepProportional);
if (newRect) {
rect = newRect;
updated = true;
delete safeRegionCache[edge].min;
} else {
safeRegionCache[edge].min = trench.position + trench.radius;
}
} else {
safeRegionCache[edge].min = -10;
}
if (index < aT[edge].length) {
var trench = this.getById(aT[edge][index].id);
newRect = trench.rectOverlaps(rect,edge,assumeConstantSize,keepProportional);
if (newRect) {
rect = newRect;
updated = true;
delete safeRegionCache[edge].max;
} else {
safeRegionCache[edge].max = trench.position - trench.radius;
}
} else {
safeRegionCache[edge].max = Infinity;
}
if (updated)
return rect;
else
return false;
},
snap: function Trenches_snap(rect,assumeConstantSize,keepProportional) { snap: function Trenches_snap(rect,assumeConstantSize,keepProportional) {
var aT = this.activeTrenches; var aT = this.activeTrenches;
@ -298,39 +215,38 @@ var Trenches = {
var updatedX = false; var updatedX = false;
var updatedY = false; var updatedY = false;
var newRect; for (let i in this.trenches) {
newRect = this.snapEdge(rect,(this.preferLeft?'left':'right'),assumeConstantSize,keepProportional); var t = this.trenches[i];
if (newRect) { if (!t.active)
rect = newRect; continue;
updated = true; // newRect will be a new rect, or false
updatedX = true; var newRect = t.rectOverlaps(rect,assumeConstantSize,keepProportional);
}
if (!assumeConstantSize || !updatedX) {
newRect = this.snapEdge(rect,(this.preferLeft?'right':'left'),assumeConstantSize,keepProportional);
if (newRect) { if (newRect) {
rect = newRect; if (assumeConstantSize && updatedX && updatedY)
updated = true; break;
updatedX = true; if (assumeConstantSize && updatedX && (newRect.adjustedEdge == "left"||newRect.adjustedEdge == "right"))
continue;
if (assumeConstantSize && updatedY && (newRect.adjustedEdge == "top"||newRect.adjustedEdge == "bottom"))
continue;
rect = newRect;
updated = true;
// if updatedX, we don't need to update x any more.
if (newRect.adjustedEdge == "left" && this.preferLeft)
updatedX = true;
if (newRect.adjustedEdge == "right" && !this.preferLeft)
updatedX = true;
// if updatedY, we don't need to update x any more.
if (newRect.adjustedEdge == "top" && this.preferTop)
updatedY = true;
if (newRect.adjustedEdge == "bottom" && !this.preferTop)
updatedY = true;
} }
} }
newRect = this.snapEdge(rect,(this.preferTop?'top':'bottom'),assumeConstantSize,keepProportional);
if (newRect) {
rect = newRect;
updated = true;
updatedY = true;
}
if (!assumeConstantSize || !updatedY) {
newRect = this.snapEdge(rect,(this.preferTop?'bottom':'top'),assumeConstantSize,keepProportional);
if (newRect) {
rect = newRect;
updated = true;
updatedY = true;
}
}
if (updated) if (updated)
return rect; return rect;
else else