aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhwc4872012-02-06 15:50:07 -0800
committerhwc4872012-02-06 15:50:07 -0800
commit9d122c5f1632b1225a543049913c36b9a030110c (patch)
treee13ca71c7129a32c4aaed072c2edf4a8de4dd52d
parent1a8c017e37c5c0dec445ebafad634ef997fcd1bb (diff)
downloadninja-9d122c5f1632b1225a543049913c36b9a030110c.tar.gz
Enhanced the overlap test for the selection tool marquee select.
-rw-r--r--js/helper-classes/3D/math-utils.js97
-rw-r--r--js/helper-classes/3D/snap-manager.js6
-rw-r--r--js/tools/SelectionTool.js40
3 files changed, 129 insertions, 14 deletions
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
8// Class Utils 8// Class Utils
9// Math Utility functions 9// Math Utility functions
10/////////////////////////////////////////////////////////////////////// 10///////////////////////////////////////////////////////////////////////
11var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; 11var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
12 ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils,
13 Rectangle = require("js/helper-classes/3D/rectangle").Rectangle;
12 14
13var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { 15var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
14 /////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////
@@ -536,6 +538,99 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, {
536 } 538 }
537 }, 539 },
538 540
541 rectsOverlap:
542 {
543 value: function( pt, width, height, elt )
544 {
545 // only consider rectangles with non-zero area
546 if ((width == 0) || (height == 0)) return false;
547
548 // get the mins/maxs of the onput rectangle
549 var xMin, xMax, yMin, yMax;
550 if (width > 0) { xMin = pt[0]; xMax = pt[0] + width; }
551 else { xMax = pt[0]; xMin = pt[0] + width; }
552 if (height > 0) { yMin = pt[1]; yMax = pt[1] + height; }
553 else { yMax = pt[1]; yMin = pt[1] + height; }
554
555 // get the bounds of the element in global screen space
556 var bounds = ViewUtils.getElementViewBounds3D( elt );
557 var bounds3D = [];
558 for (var i=0; i<4; i++)
559 bounds3D[i] = ViewUtils.localToGlobal( bounds[i], elt );
560
561 // get the min/maxs for the element
562 var xMinElt = bounds3D[0][0], xMaxElt = bounds3D[0][0],
563 yMinElt = bounds3D[0][1], yMaxElt = bounds3D[0][1];
564 for (var i=1; i<4; i++)
565 {
566 if (bounds3D[i][0] < xMinElt) xMinElt = bounds3D[i][0];
567 else if (bounds3D[i][0] > xMaxElt) xMaxElt = bounds3D[i][0];
568 if (bounds3D[i][1] < yMinElt) yMinElt = bounds3D[i][1];
569 else if (bounds3D[i][1] > yMaxElt) yMaxElt = bounds3D[i][1];
570 }
571
572 // test 1. Overall bounding box test
573 if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax))
574 return false;
575
576 // test 2. See if any of the corners of the element are contained in the rectangle
577 var rect = Object.create(Rectangle, {});
578 rect.set( pt[0], pt[1], width, height );
579 for (var i=0; i<4; i++)
580 {
581 if (rect.contains( bounds3D[i][0], bounds3D[i][1] )) return true;
582 }
583
584 // test 3. Bounding box tests on individual edges of the element
585 for (var i=0; i<4; i++)
586 {
587 var pt0 = bounds3D[i],
588 pt1 = bounds3D[(i+1)%4];
589
590 // get the extremes of the edge
591 if (pt0[0] < pt1[0]) { xMinElt = pt0[0]; xMaxElt = pt1[0]; }
592 else { xMaxElt = pt0[0]; xMinElt = pt1[0]; }
593 if (pt0[1] < pt1[1]) { yMinElt = pt0[1]; yMaxElt = pt1[1]; }
594 else { yMaxElt = pt0[1]; yMinElt = pt1[1]; }
595
596 if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax))
597 continue;
598 else
599 {
600 // intersect the element edge with the 4 sides of the rectangle
601 // vertical edges
602 var xRect = xMin;
603 for (var j=0; j<2; j++)
604 {
605 if ((xMinElt < xRect) && (xMaxElt > xRect))
606 {
607 var t = (xRect - pt0[0])/(pt1[0] - pt0[0]);
608 var y = pt0[1] + t*(pt1[1] - pt0[1]);
609 if ((y >= yMin) && (y <= yMax)) return true;
610 }
611 xRect = xMax;
612 }
613
614 // horizontal edges
615 var yRect = yMin;
616 for (var j=0; j<2; j++)
617 {
618 if ((yMinElt < yRect) && (yMaxElt > yRect))
619 {
620 var t = (yRect - pt0[1])/(pt1[1] - pt0[1]);
621 var x = pt0[0] + t*(pt1[0] - pt0[0]);
622 if ((x >= xMin) && (x <= xMax)) return true;
623 }
624 yRect = yMax;
625 }
626 }
627 }
628
629 // if we get here there is no overlap
630 return false;
631 }
632 },
633
539 /////////////////////////////////////////////////////////////////////// 634 ///////////////////////////////////////////////////////////////////////
540 // Bezier Methods 635 // Bezier Methods
541 /////////////////////////////////////////////////////////////////////// 636 ///////////////////////////////////////////////////////////////////////
diff --git a/js/helper-classes/3D/snap-manager.js b/js/helper-classes/3D/snap-manager.js
index 7e1260bf..f3e8b823 100644
--- a/js/helper-classes/3D/snap-manager.js
+++ b/js/helper-classes/3D/snap-manager.js
@@ -971,7 +971,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, {
971 value: function( screenPt, hitRecs ) { 971 value: function( screenPt, hitRecs ) {
972 // start at the stage. 972 // start at the stage.
973 var stage = this.getStage(); 973 var stage = this.getStage();
974 //var stagePt = viewUtils.parentToChild( screenPt, stage );
975 974
976 // the root should be the 'view' canvas, so the first matrix is the camera 975 // the root should be the 'view' canvas, so the first matrix is the camera
977 viewUtils.setViewportObj( stage ); 976 viewUtils.setViewportObj( stage );
@@ -997,7 +996,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, {
997 hit = this.snapToElement( elt, globalScrPt ); 996 hit = this.snapToElement( elt, globalScrPt );
998 if (hit) 997 if (hit)
999 { 998 {
1000 //hitRecs.push( hit );
1001 if (!hit.checkType()) 999 if (!hit.checkType())
1002 { 1000 {
1003 console.log( "invalid hit record: " + hit.getTypeString() ); 1001 console.log( "invalid hit record: " + hit.getTypeString() );
@@ -1016,17 +1014,14 @@ var SnapManager = exports.SnapManager = Montage.create(Component, {
1016 } 1014 }
1017 // test the rest of the tree 1015 // test the rest of the tree
1018 var n = elt.childElementCount; 1016 var n = elt.childElementCount;
1019 //var eltPt = viewUtils.parentToChild( parentPt, elt, true );
1020 if (n > 0) 1017 if (n > 0)
1021 { 1018 {
1022 for (var i=0; i<n; i++) 1019 for (var i=0; i<n; i++)
1023 { 1020 {
1024 var child = elt.children[i]; 1021 var child = elt.children[i];
1025 //var childPt = viewUtils.parentToChild( scrPt, child );
1026 hit = this.hSnapToElements( child, hitRecs, (depth+1), globalScrPt ); 1022 hit = this.hSnapToElements( child, hitRecs, (depth+1), globalScrPt );
1027 if (hit) 1023 if (hit)
1028 { 1024 {
1029 //hitRecs.push( hit );
1030 if (!hit.checkType()) 1025 if (!hit.checkType())
1031 { 1026 {
1032 console.log( "invalid hit record: " + hit.getTypeString() ); 1027 console.log( "invalid hit record: " + hit.getTypeString() );
@@ -1034,7 +1029,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, {
1034 } 1029 }
1035 else 1030 else
1036 hitRecs.push( hit ); 1031 hitRecs.push( hit );
1037
1038 } 1032 }
1039 } 1033 }
1040 } 1034 }
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, {
135 var box = []; 135 var box = [];
136 selectedItems = []; 136 selectedItems = [];
137 137
138 box[0] = this.downPoint.x - this.application.ninja.stage.documentOffsetLeft + this.application.ninja.stage.scrollLeft; 138 box[0] = this.downPoint.x;
139 box[1] = this.downPoint.y - this.application.ninja.stage.documentOffsetTop + this.application.ninja.stage.scrollTop; 139 box[1] = this.downPoint.y;
140 box[2] = box[0] + (point.x - this.downPoint.x); 140 box[2] = point.x;
141 box[3] = box[1] + (point.y - this.downPoint.y); 141 box[3] = point.y;
142 box = this.absoluteRectangle(box[0], box[1],box[2],box[3]);
143
144 142
145 //selectionManagerModule.selectionManager.marqueeSelection(box); 143 //selectionManagerModule.selectionManager.marqueeSelection(box);
146 var childNodes = this.application.ninja.currentDocument.documentRoot.childNodes; 144 var childNodes = this.application.ninja.currentDocument.documentRoot.childNodes;
147 childNodes = Array.prototype.slice.call(childNodes, 0); 145 childNodes = Array.prototype.slice.call(childNodes, 0);
148 childNodes.forEach(function(item) { 146 childNodes.forEach(function(item) {
149 if(item.nodeType == 1 && SelectionTool._simpleCollisionDetection(item, box)) { 147 if(item.nodeType == 1 && SelectionTool._complicatedCollisionDetection(item, box)) {
150 selectedItems.push(item); 148 selectedItems.push(item);
151 } 149 }
152 }); 150 });
@@ -803,6 +801,34 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, {
803 }, 801 },
804 802
805 // TODO : Use the new element mediator to get element offsets