From 5d4002b7a920a53ea02b0e8caeaec80b804995e4 Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Wed, 8 Feb 2012 15:12:16 -0800 Subject: Merging fix for marquee selection that accounts for 3d values. Signed-off-by: Nivesh Rajbhandari --- js/helper-classes/3D/math-utils.js | 97 +++++++++++++++++++++++++++++++++++++- js/tools/SelectionTool.js | 40 +++++++++++++--- 2 files changed, 129 insertions(+), 8 deletions(-) (limited to 'js') diff --git a/js/helper-classes/3D/math-utils.js b/js/helper-classes/3D/math-utils.js index 71ed62a0..58f0680a 100644 --- a/js/helper-classes/3D/math-utils.js +++ b/js/helper-classes/3D/math-utils.js @@ -8,7 +8,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot // Class Utils // Math Utility functions /////////////////////////////////////////////////////////////////////// -var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; +var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils, + ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils, + Rectangle = require("js/helper-classes/3D/rectangle").Rectangle; var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { /////////////////////////////////////////////////////////////////////// @@ -536,6 +538,99 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { } }, + rectsOverlap: + { + value: function( pt, width, height, elt ) + { + // only consider rectangles with non-zero area + if ((width == 0) || (height == 0)) return false; + + // get the mins/maxs of the onput rectangle + var xMin, xMax, yMin, yMax; + if (width > 0) { xMin = pt[0]; xMax = pt[0] + width; } + else { xMax = pt[0]; xMin = pt[0] + width; } + if (height > 0) { yMin = pt[1]; yMax = pt[1] + height; } + else { yMax = pt[1]; yMin = pt[1] + height; } + + // get the bounds of the element in global screen space + var bounds = ViewUtils.getElementViewBounds3D( elt ); + var bounds3D = []; + for (var i=0; i<4; i++) + bounds3D[i] = ViewUtils.localToGlobal( bounds[i], elt ); + + // get the min/maxs for the element + var xMinElt = bounds3D[0][0], xMaxElt = bounds3D[0][0], + yMinElt = bounds3D[0][1], yMaxElt = bounds3D[0][1]; + for (var i=1; i<4; i++) + { + if (bounds3D[i][0] < xMinElt) xMinElt = bounds3D[i][0]; + else if (bounds3D[i][0] > xMaxElt) xMaxElt = bounds3D[i][0]; + if (bounds3D[i][1] < yMinElt) yMinElt = bounds3D[i][1]; + else if (bounds3D[i][1] > yMaxElt) yMaxElt = bounds3D[i][1]; + } + + // test 1. Overall bounding box test + if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax)) + return false; + + // test 2. See if any of the corners of the element are contained in the rectangle + var rect = Object.create(Rectangle, {}); + rect.set( pt[0], pt[1], width, height ); + for (var i=0; i<4; i++) + { + if (rect.contains( bounds3D[i][0], bounds3D[i][1] )) return true; + } + + // test 3. Bounding box tests on individual edges of the element + for (var i=0; i<4; i++) + { + var pt0 = bounds3D[i], + pt1 = bounds3D[(i+1)%4]; + + // get the extremes of the edge + if (pt0[0] < pt1[0]) { xMinElt = pt0[0]; xMaxElt = pt1[0]; } + else { xMaxElt = pt0[0]; xMinElt = pt1[0]; } + if (pt0[1] < pt1[1]) { yMinElt = pt0[1]; yMaxElt = pt1[1]; } + else { yMaxElt = pt0[1]; yMinElt = pt1[1]; } + + if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax)) + continue; + else + { + // intersect the element edge with the 4 sides of the rectangle + // vertical edges + var xRect = xMin; + for (var j=0; j<2; j++) + { + if ((xMinElt < xRect) && (xMaxElt > xRect)) + { + var t = (xRect - pt0[0])/(pt1[0] - pt0[0]); + var y = pt0[1] + t*(pt1[1] - pt0[1]); + if ((y >= yMin) && (y <= yMax)) return true; + } + xRect = xMax; + } + + // horizontal edges + var yRect = yMin; + for (var j=0; j<2; j++) + { + if ((yMinElt < yRect) && (yMaxElt > yRect)) + { + var t = (yRect - pt0[1])/(pt1[1] - pt0[1]); + var x = pt0[0] + t*(pt1[0] - pt0[0]); + if ((x >= xMin) && (x <= xMax)) return true; + } + yRect = yMax; + } + } + } + + // if we get here there is no overlap + return false; + } + }, + /////////////////////////////////////////////////////////////////////// // Bezier Methods /////////////////////////////////////////////////////////////////////// diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js index 862b2e88..48548271 100644 --- a/js/tools/SelectionTool.js +++ b/js/tools/SelectionTool.js @@ -135,18 +135,16 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { var box = []; selectedItems = []; - box[0] = this.downPoint.x - this.application.ninja.stage.documentOffsetLeft + this.application.ninja.stage.scrollLeft; - box[1] = this.downPoint.y - this.application.ninja.stage.documentOffsetTop + this.application.ninja.stage.scrollTop; - box[2] = box[0] + (point.x - this.downPoint.x); - box[3] = box[1] + (point.y - this.downPoint.y); - box = this.absoluteRectangle(box[0], box[1],box[2],box[3]); - + box[0] = this.downPoint.x; + box[1] = this.downPoint.y; + box[2] = point.x; + box[3] = point.y; //selectionManagerModule.selectionManager.marqueeSelection(box); var childNodes = this.application.ninja.currentDocument.documentRoot.childNodes; childNodes = Array.prototype.slice.call(childNodes, 0); childNodes.forEach(function(item) { - if(item.nodeType == 1 && SelectionTool._simpleCollisionDetection(item, box)) { + if(item.nodeType == 1 && SelectionTool._complicatedCollisionDetection(item, box)) { selectedItems.push(item); } }); @@ -802,6 +800,34 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { } }, + // TODO : Use the new element mediator to get element offsets + _complicatedCollisionDetection: + { + value: function(elt, box) + { + var left, top, width, height; + + left = box[0]; + width = box[2] - left; + if (width < 0) + { + left = box[2]; + width = -width; + } + top = box[1]; + height = box[3] - top; + if (height < 0) + { + top = box[3]; + height = -height; + } + + var rtnVal = MathUtils.rectsOverlap( [left,top], width, height, elt ); + + return rtnVal; + } + }, + // TODO : Use the new element mediator to get element offsets _simpleCollisionDetection: { value: function(ele, box){ -- cgit v1.2.3