diff options
Diffstat (limited to 'js/helper-classes/3D/snap-manager.js')
-rwxr-xr-x | js/helper-classes/3D/snap-manager.js | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/js/helper-classes/3D/snap-manager.js b/js/helper-classes/3D/snap-manager.js index 1caacd00..02e81a62 100755 --- a/js/helper-classes/3D/snap-manager.js +++ b/js/helper-classes/3D/snap-manager.js | |||
@@ -123,7 +123,8 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
123 | /////////////////////////////////////////////////////////////////////// | 123 | /////////////////////////////////////////////////////////////////////// |
124 | initialize: { | 124 | initialize: { |
125 | value: function() { | 125 | value: function() { |
126 | this.eventManager.addEventListener("elementDeleted", this, false); | 126 | this.eventManager.addEventListener("elementsRemoved", this, false); |
127 | this.eventManager.addEventListener("elementReplaced", this, false); | ||
127 | } | 128 | } |
128 | }, | 129 | }, |
129 | 130 | ||
@@ -163,12 +164,26 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
163 | }, | 164 | }, |
164 | 165 | ||
165 | 166 | ||
166 | handleElementDeleted: { | 167 | handleElementsRemoved: { |
167 | value: function(event) { | 168 | value: function(event) { |
168 | this.removeElementFrom2DCache(event.detail); | 169 | var self = this, elements = event.detail; |
170 | |||
171 | if(Array.isArray(elements)) { | ||
172 | elements = Array.prototype.slice.call(elements, 0); | ||
173 | elements.forEach(function(element) { | ||
174 | self.removeElementFrom2DCache(element); | ||
175 | }); | ||
176 | } else { | ||
177 | this.removeElementFrom2DCache(elements); | ||
178 | } | ||
169 | } | 179 | } |
170 | }, | 180 | }, |
171 | 181 | ||
182 | handleElementReplaced: { | ||
183 | value: function(event) { | ||
184 | this._isCacheInvalid = true; | ||
185 | } | ||
186 | }, | ||
172 | 187 | ||
173 | setCurrentStage: { | 188 | setCurrentStage: { |
174 | value: function(stage) { | 189 | value: function(stage) { |
@@ -214,7 +229,9 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
214 | parentPt = [quadPt[0], quadPt[1], 0.0]; | 229 | parentPt = [quadPt[0], quadPt[1], 0.0]; |
215 | else | 230 | else |
216 | parentPt = [xScreen, yScreen, 0.0]; | 231 | parentPt = [xScreen, yScreen, 0.0]; |
217 | var vec = viewUtils.parentToChildVec(parentPt, stage); | 232 | |
233 | var eyePt = []; | ||
234 | var vec = viewUtils.parentToChildVec(parentPt, stage, eyePt); | ||
218 | if (vec) | 235 | if (vec) |
219 | { | 236 | { |
220 | // activate the drag working plane | 237 | // activate the drag working plane |
@@ -226,7 +243,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
226 | var wp = currentWorkingPlane.slice(0); | 243 | var wp = currentWorkingPlane.slice(0); |
227 | var mat = viewUtils.getMatrixFromElement(stage); | 244 | var mat = viewUtils.getMatrixFromElement(stage); |
228 | wp = MathUtils.transformPlane(wp, mat); | 245 | wp = MathUtils.transformPlane(wp, mat); |
229 | var eyePt = viewUtils.getEyePoint(); | ||
230 | var projPt = MathUtils.vecIntersectPlane(eyePt, vec, wp); | 246 | var projPt = MathUtils.vecIntersectPlane(eyePt, vec, wp); |
231 | if (projPt) | 247 | if (projPt) |
232 | { | 248 | { |
@@ -471,7 +487,8 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
471 | value: function( plane ) { | 487 | value: function( plane ) { |
472 | this._elementCache = new Array; | 488 | this._elementCache = new Array; |
473 | 489 | ||
474 | var stage = this.getStage(); | 490 | // var stage = this.getStage(); |
491 | var stage = this.application.ninja.currentSelectedContainer || this.getStage(); | ||
475 | this.hLoadElementCache( stage, plane, 0 ); | 492 | this.hLoadElementCache( stage, plane, 0 ); |
476 | this._isCacheInvalid = false; | 493 | this._isCacheInvalid = false; |
477 | 494 | ||
@@ -488,6 +505,7 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
488 | var n = this._elementCache.length; | 505 | var n = this._elementCache.length; |
489 | this._elementCache[index] = this._elementCache[n-1]; | 506 | this._elementCache[index] = this._elementCache[n-1]; |
490 | this._elementCache.pop(); | 507 | this._elementCache.pop(); |
508 | target.elementModel.isIn2DSnapCache = false; | ||
491 | found = true; | 509 | found = true; |
492 | } | 510 | } |
493 | 511 | ||
@@ -570,6 +588,11 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
570 | 588 | ||
571 | hLoadElementCache : { | 589 | hLoadElementCache : { |
572 | value: function( elt, plane, depth ) { | 590 | value: function( elt, plane, depth ) { |
591 | if(depth > 1) | ||
592 | { | ||
593 | return; | ||
594 | } | ||
595 | |||
573 | if (depth > 0) | 596 | if (depth > 0) |
574 | { | 597 | { |
575 | // check if the element is on the specified plane | 598 | // check if the element is on the specified plane |
@@ -590,8 +613,7 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
590 | elt.elementModel.isIn2DSnapCache = false; | 613 | elt.elementModel.isIn2DSnapCache = false; |
591 | } | 614 | } |
592 | 615 | ||
593 | // TODO - Don't traverse components' children | 616 | // TODO - Don't traverse svg and components' children |
594 | // if(elt.elementModel && elt.elementModel.isComponent) | ||
595 | if(elt.nodeName.toLowerCase() === "svg" || (elt.elementModel && (elt.elementModel.isComponent || (elt.elementModel.selection === "SVG")))) | 617 | if(elt.nodeName.toLowerCase() === "svg" || (elt.elementModel && (elt.elementModel.isComponent || (elt.elementModel.selection === "SVG")))) |
596 | { | 618 | { |
597 | return; | 619 | return; |
@@ -688,8 +710,8 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
688 | var wp = currentWorkingPlane.slice(0); | 710 | var wp = currentWorkingPlane.slice(0); |
689 | var mat = viewUtils.getMatrixFromElement(stage); | 711 | var mat = viewUtils.getMatrixFromElement(stage); |
690 | wp = MathUtils.transformPlane(wp, mat); | 712 | wp = MathUtils.transformPlane(wp, mat); |
691 | var eyePt = viewUtils.getEyePoint(); | 713 | var eyePt = []; |
692 | var vec = viewUtils.parentToChildVec(gPt, stage); | 714 | var vec = viewUtils.parentToChildVec(gPt, stage, eyePt); |
693 | var projPt = MathUtils.vecIntersectPlane(eyePt, vec, wp); | 715 | var projPt = MathUtils.vecIntersectPlane(eyePt, vec, wp); |
694 | var wpMat = drawUtils.getPlaneToWorldMatrix(currentWorkingPlane, MathUtils.getPointOnPlane(currentWorkingPlane)); | 716 | var wpMat = drawUtils.getPlaneToWorldMatrix(currentWorkingPlane, MathUtils.getPointOnPlane(currentWorkingPlane)); |
695 | projPt[3] = 1.0; | 717 | projPt[3] = 1.0; |
@@ -964,7 +986,8 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
964 | snapToElements : { | 986 | snapToElements : { |
965 | value: function( screenPt, hitRecs ) { | 987 | value: function( screenPt, hitRecs ) { |
966 | // start at the stage. | 988 | // start at the stage. |
967 | var stage = this.getStage(); | 989 | // var stage = this.getStage(); |
990 | var stage = this.application.ninja.currentSelectedContainer || this.getStage(); | ||
968 | 991 | ||
969 | // the root should be the 'view' canvas, so the first matrix is the camera | 992 | // the root should be the 'view' canvas, so the first matrix is the camera |
970 | viewUtils.setViewportObj( stage ); | 993 | viewUtils.setViewportObj( stage ); |
@@ -980,6 +1003,10 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
980 | { | 1003 | { |
981 | value: function( elt, hitRecs, depth, globalScrPt ) | 1004 | value: function( elt, hitRecs, depth, globalScrPt ) |
982 | { | 1005 | { |
1006 | if(depth > 1) | ||
1007 | { | ||
1008 | return; | ||
1009 | } | ||
983 | // hit test the current object | 1010 | // hit test the current object |
984 | var hit; | 1011 | var hit; |
985 | if (depth > 0) // don't snap to the root | 1012 | if (depth > 0) // don't snap to the root |
@@ -1001,8 +1028,7 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
1001 | } | 1028 | } |
1002 | } | 1029 | } |
1003 | 1030 | ||
1004 | // TODO - Don't traverse components' children | 1031 | // TODO - Don't traverse svg and components' children |
1005 | // if(elt.elementModel && elt.elementModel.isComponent) | ||
1006 | if(elt.nodeName.toLowerCase() === "svg" || (elt.elementModel && (elt.elementModel.isComponent || (elt.elementModel.selection === "SVG")))) | 1032 | if(elt.nodeName.toLowerCase() === "svg" || (elt.elementModel && (elt.elementModel.isComponent || (elt.elementModel.selection === "SVG")))) |
1007 | { | 1033 | { |
1008 | return; | 1034 | return; |
@@ -1042,8 +1068,9 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
1042 | // Snapping is done in screen space, so convert the bounds from | 1068 | // Snapping is done in screen space, so convert the bounds from |
1043 | // local element space to global screen space | 1069 | // local element space to global screen space |
1044 | var bounds3D = new Array(); | 1070 | var bounds3D = new Array(); |
1071 | var eltMat = viewUtils.getLocalToGlobalMatrix( elt ); | ||
1045 | for (var i=0; i<4; i++) | 1072 | for (var i=0; i<4; i++) |
1046 | bounds3D[i] = viewUtils.localToGlobal( bounds[i], elt ); | 1073 | bounds3D[i] = viewUtils.localToGlobal2(bounds[i], eltMat); |
1047 | 1074 | ||
1048 | var hitRec = this.snapToScreenBounds( elt, globalScrPt, bounds, bounds3D ); | 1075 | var hitRec = this.snapToScreenBounds( elt, globalScrPt, bounds, bounds3D ); |
1049 | 1076 | ||
@@ -1327,7 +1354,7 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
1327 | var offset = viewUtils.getElementOffset( elt ); | 1354 | var offset = viewUtils.getElementOffset( elt ); |
1328 | MathUtils.makeDimension3( offset ); | 1355 | MathUtils.makeDimension3( offset ); |
1329 | var parentPt = vecUtils.vecAdd(3, scrPt, offset ); | 1356 | var parentPt = vecUtils.vecAdd(3, scrPt, offset ); |
1330 | var globalPt = viewUtils.localToGlobal( parentPt, elt.parentElement ); | 1357 | var globalPt = viewUtils.localToGlobal( parentPt, elt.offsetParent ); |
1331 | 1358 | ||
1332 | var dist = vecUtils.vecDist(2, globalPt, targetScrPt ); | 1359 | var dist = vecUtils.vecDist(2, globalPt, targetScrPt ); |
1333 | if (dist < this.ELEMENT_VERTEX_HIT_RAD) | 1360 | if (dist < this.ELEMENT_VERTEX_HIT_RAD) |
@@ -1365,7 +1392,7 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
1365 | var offset = viewUtils.getElementOffset( elt ); | 1392 | var offset = viewUtils.getElementOffset( elt ); |
1366 | MathUtils.makeDimension3( offset ); | 1393 | MathUtils.makeDimension3( offset ); |
1367 | var parentPt = vecUtils.vecAdd(3, scrPt, offset ); | 1394 | var parentPt = vecUtils.vecAdd(3, scrPt, offset ); |
1368 | var globalPt = viewUtils.localToGlobal( parentPt, elt.parentElement ); | 1395 | var globalPt = viewUtils.localToGlobal( parentPt, elt.offsetParent ); |
1369 | 1396 | ||
1370 | var dist = vecUtils.vecDist(2, globalPt, targetScrPt ); | 1397 | var dist = vecUtils.vecDist(2, globalPt, targetScrPt ); |
1371 | if (dist < this.ELEMENT_EDGE_HIT_RAD) | 1398 | if (dist < this.ELEMENT_EDGE_HIT_RAD) |
@@ -1402,6 +1429,8 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { | |||
1402 | case glObj.GEOM_TYPE_PATH: | 1429 | case glObj.GEOM_TYPE_PATH: |
1403 | // Snapping not implemented for these type, but don't throw an error... | 1430 | // Snapping |