diff options
Diffstat (limited to 'js/tools/SelectionTool.js')
-rwxr-xr-x | js/tools/SelectionTool.js | 365 |
1 files changed, 149 insertions, 216 deletions
diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js index cff14226..7033ee3d 100755 --- a/js/tools/SelectionTool.js +++ b/js/tools/SelectionTool.js | |||
@@ -33,19 +33,16 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { | |||
33 | 33 | ||
34 | _areElementsOnSamePlane : { | 34 | _areElementsOnSamePlane : { |
35 | value: function () { | 35 | value: function () { |
36 | if(this._targets && this._targets.length) | 36 | if(this.application.ninja.selectedElements.length) { |
37 | { | 37 | var len = this.application.ninja.selectedElements.length; |
38 | var len = this._targets.length; | ||
39 | var plane = this.application.ninja.stage.stageDeps.snapManager.getDragPlane(); | 38 | var plane = this.application.ninja.stage.stageDeps.snapManager.getDragPlane(); |
40 | for(var i = 0; i < len; i++) | 39 | for(var i = 0; i < len; i++) { |
41 | { | 40 | if(!this.application.ninja.stage.stageDeps.snapManager.elementIsOnPlane(this.application.ninja.selectedElements[i], plane)) { |
42 | var elt = this._targets[i].elt; | ||
43 | if(!this.application.ninja.stage.stageDeps.snapManager.elementIsOnPlane(elt, plane)) | ||
44 | { | ||
45 | return false; | 41 | return false; |
46 | } | 42 | } |
47 | } | 43 | } |
48 | } | 44 | } |
45 | |||
49 | return true; | 46 | return true; |
50 | } | 47 | } |
51 | }, | 48 | }, |
@@ -60,6 +57,7 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { | |||
60 | else | 57 | else |
61 | { | 58 | { |
62 | this._canSnap = true; | 59 | this._canSnap = true; |
60 | this._updateTargets(); | ||
63 | } | 61 | } |
64 | 62 | ||
65 | this.isDrawing = true; | 63 | this.isDrawing = true; |
@@ -295,184 +293,103 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { | |||
295 | }, | 293 | }, |
296 | 294 | ||
297 | _updateTargets: { | 295 | _updateTargets: { |
298 | value: function(addToUndoStack) { | 296 | value: function(addToUndo) { |
299 | var newLeft = [], | 297 | var modObject = [], mod3dObject = [], self = this; |
300 | newTop = [], | 298 | |
301 | newWidth = [], | 299 | this.application.ninja.selectedElements.forEach(function(element) { |
302 | newHeight = [], | 300 | |
303 | newStyles = [], | 301 | if(addToUndo) { |
304 | previousLeft = [], | 302 | if(!self._use3DMode) { |
305 | previousTop = [], | 303 | var prevX = element.elementModel.getProperty("x"); |
306 | previousWidth = [], | 304 | var prevY = element.elementModel.getProperty("y"); |
307 | previousHeight = [], | 305 | var prevW = element.elementModel.getProperty("w"); |
308 | previousStyles = []; | 306 | var prevH = element.elementModel.getProperty("h"); |
309 | var len = this.application.ninja.selectedElements.length; | 307 | var x = ElementsMediator.getProperty(element, "left"); |
310 | this._targets = []; | 308 | var y = ElementsMediator.getProperty(element, "top"); |
311 | for(var i = 0; i < len; i++) { | 309 | var w = ElementsMediator.getProperty(element, "width"); |
312 | var elt = this.application.ninja.selectedElements[i]; | 310 | var h = ElementsMediator.getProperty(element, "height"); |
313 | 311 | ||
314 | var curMat = viewUtils.getMatrixFromElement(elt); | 312 | // if we have a delta, that means the transform handles were used and |
315 | var curMatInv = glmat4.inverse(curMat, []); | 313 | // we should update the width and height too. Otherwise, just update left and top. |
316 | 314 | if(this.delta) { | |
317 | this._targets.push({elt:elt, mat:curMat, matInv:curMatInv}); | 315 | modObject.push({element:element, properties:{left: x, top:y, width: w, height: h}, previousProperties: {left: prevX, top:prevY, width: prevW, height: prevH}}); |
318 | if(addToUndoStack) { | 316 | } else { |
319 | if(!this._use3DMode) { | 317 | modObject.push({element:element, properties:{left: x, top:y}, previousProperties: {left: prevX, top:prevY}}); |
320 | var previousMat = this._undoArray[i].mat.slice(0); | 318 | } |
321 | var prevX = this._undoArray[i]._x; | 319 | |
322 | var prevY = this._undoArray[i]._y; | ||
323 | var prevW = this._undoArray[i]._w; | ||
324 | var prevH = this._undoArray[i]._h; | ||
325 | var _x = parseInt(ElementsMediator.getProperty(elt, "left")) + curMat[12] - previousMat[12]; | ||
326 | var _y = parseInt(ElementsMediator.getProperty(elt, "top")) + curMat[13] - previousMat[13]; | ||
327 | var _w = parseInt(ElementsMediator.getProperty(elt, "width")); | ||
328 | var _h = parseInt(ElementsMediator.getProperty(elt, "height")); | ||
329 | |||
330 | previousLeft.push(prevX + "px"); | ||
331 | previousTop.push(prevY + "px"); | ||
332 | previousWidth.push(prevW + "px"); | ||
333 | previousHeight.push(prevH + "px"); | ||
334 | newLeft.push(_x + "px"); | ||
335 | newTop.push(_y + "px"); | ||
336 | newWidth.push(_w + "px"); | ||
337 | newHeight.push(_h + "px"); | ||
338 | |||
339 | viewUtils.setMatrixForElement(elt, previousMat); | ||
340 | |||
341 | this._targets[i].mat = previousMat; | ||
342 | this._targets[i].matInv = glmat4.inverse(previousMat, []); | ||
343 | } else { | 320 | } else { |
344 | var previousMat = this._undoArray[i].mat.slice(0); | 321 | // Not using the 3d mode |
345 | var prevW = this._undoArray[i]._w; | 322 | var previousMat = element.elementModel.getProperty("mat").slice(0); |
346 | var prevH = this._undoArray[i]._h; | 323 | var prevW = element.elementModel.getProperty("w"); |
347 | var _w = parseInt(ElementsMediator.getProperty(elt, "width")); | 324 | var prevH = element.elementModel.getProperty("h"); |
348 | var _h = parseInt(ElementsMediator.getProperty(elt, "height")); | 325 | var w = ElementsMediator.getProperty(element, "width"); |
349 | previousWidth.push(prevW + "px"); | 326 | var h = ElementsMediator.getProperty(element, "height"); |
350 | previousHeight.push(prevH + "px"); | 327 | |
351 | newWidth.push(_w + "px"); | 328 | var previousStyleStr = {dist:element.elementModel.getProperty("dist"), mat:MathUtils.scientificToDecimal(previousMat, 5)}; |
352 | newHeight.push(_h + "px"); | 329 | var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(element), mat:MathUtils.scientificToDecimal(viewUtils.getMatrixFromElement(element), 5)}; |
353 | 330 | ||
354 | var previousStyleStr = {dist:this._undoArray[i].dist, | 331 | modObject.push({element:element, properties:{width: w, height:h}, previousProperties: {width: prevW, height:prevH}}); |
355 | mat:MathUtils.scientificToDecimal(previousMat, 5)}; | 332 | mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr}); |
356 | var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(elt), | ||
357 | mat:MathUtils.scientificToDecimal(curMat, 5)}; | ||
358 | previousStyles.push(previousStyleStr); | ||
359 | newStyles.push(newStyleStr); | ||
360 | |||
361 | this._targets[i].mat = curMat; | ||
362 | this._targets[i].matInv = curMatInv; | ||
363 | } | ||
364 | } | ||
365 | } | ||
366 | if(addToUndoStack) | ||
367 | { | ||
368 | if(!this._use3DMode) | ||
369 | { | ||
370 | // if we have a delta, that means the transform handles were used and | ||
371 | // we should update the width and height too. Otherwise, just update left and top. | ||
372 | if(this._delta) | ||
373 | { | ||
374 | ElementsMediator.setProperties(this.application.ninja.selectedElements, | ||
375 | { "left": newLeft, "top": newTop, "width": newWidth, "height": newHeight }, | ||
376 | "Change", | ||
377 | "selectionTool", | ||
378 | { "left" : previousLeft, "top" : previousTop, "width": previousWidth, "height": previousHeight} | ||
379 | ); | ||
380 | } | ||
381 | else | ||
382 | { | ||
383 | ElementsMediator.setProperties(this.application.ninja.selectedElements, | ||
384 | { "left": newLeft, "top": newTop }, | ||
385 | "Change", | ||
386 | "selectionTool", | ||
387 | { "left" : previousLeft, "top" : previousTop } | ||
388 | ); | ||
389 | } | 333 | } |
390 | } | 334 | } |
391 | else | 335 | }); |
392 | { | 336 | |
337 | // Move them | ||
338 | if(addToUndo) { | ||
339 | if(!this._use3DMode) { | ||
340 | ElementsMediator.setProperties(modObject, "Change", "SelectionTool" ); | ||
341 | } else { | ||
393 | // TODO - We don't support transform handles in 3d space for now | 342 | // TODO - We don't support transform handles in 3d space for now |
394 | ElementsMediator.setProperties(this.application.ninja.selectedElements, | 343 | ElementsMediator.setProperties(modObject, "Change", "SelectionTool" ); |
395 | { "width": newWidth, "height": newHeight }, | 344 | ElementsMediator.set3DProperties(mod3dObject, "Change", "translateTool"); |
396 | "Change", | 345 | |
397 | "selectionTool", | ||
398 | { "width": previousWidth, "height": previousHeight} | ||
399 | ); | ||
400 | ElementsMediator.set3DProperties(this.application.ninja.selectedElements, | ||
401 | newStyles, | ||
402 | "Change", | ||
403 | "translateTool", | ||
404 | previousStyles | ||
405 | ); | ||
406 | } | 346 | } |
407 | } | 347 | } |
408 | // Save previous value for undo/redo | 348 | |
409 | this._undoArray = []; | 349 | this.application.ninja.selectedElements.forEach(function(element) { |
410 | for(i = 0, len = this._targets.length; i < len; i++) | 350 | element.elementModel.setProperty("x", ElementsMediator.getProperty(element, "left")); |
411 | { | 351 | element.elementModel.setProperty("y", ElementsMediator.getProperty(element, "top")); |
412 | var item = this._targets[i]; | 352 | element.elementModel.setProperty("w", ElementsMediator.getProperty(element, "width")); |
413 | _x = parseInt(ElementsMediator.getProperty(item.elt, "left")); | 353 | element.elementModel.setProperty("h", ElementsMediator.getProperty(element, "height")); |
414 | _y = parseInt(ElementsMediator.getProperty(item.elt, "top")); | 354 | element.elementModel.setProperty("mat", viewUtils.getMatrixFromElement(element)); |
415 | _w = parseInt(ElementsMediator.getProperty(item.elt, "width")); | 355 | element.elementModel.setProperty("matInv", glmat4.inverse(element.elementModel.getProperty("mat"), [])); |
416 | _h = parseInt(ElementsMediator.getProperty(item.elt, "height")); | 356 | element.elementModel.setProperty("dist", viewUtils.getPerspectiveDistFromElement(element)); |
417 | var _mat = viewUtils.getMatrixFromElement(item.elt); | 357 | }); |
418 | var _dist = viewUtils.getPerspectiveDistFromElement(item.elt); | ||
419 | this._undoArray.push({_x:_x, _y:_y, _w:_w, _h:_h, mat:_mat, dist:_dist}); | ||
420 | } | ||
421 | 358 | ||
422 | } | 359 | } |
423 | }, | 360 | }, |
424 | 361 | ||
425 | _moveElements: { | 362 | _moveElements: { |
426 | value: function (transMat) { | 363 | value: function (transMat) { |
427 | var len = this._targets.length, | 364 | var elt, curMat, targets = []; |
428 | i, | ||
429 | item, | ||
430 | elt, | ||
431 | curMat, | ||
432 | newLeft = [], | ||
433 | newTop = []; | ||
434 | 365 | ||
435 | var matInv = glmat4.inverse(this._startMat, []); |