diff options
author | Valerio Virgillito | 2012-04-13 00:30:19 -0700 |
---|---|---|
committer | Valerio Virgillito | 2012-04-13 00:30:19 -0700 |
commit | f91e64235eb03c889ff4f5577c3e3480cd0d787f (patch) | |
tree | 946cfe53bd9281478f32c7a446c1c803383d8d17 /js/tools/SelectionTool.js | |
parent | 5b4826eced782fe7b4691d4bbbe0e3605d3164c1 (diff) | |
download | ninja-f91e64235eb03c889ff4f5577c3e3480cd0d787f.tar.gz |
removing _undo array and _targets array and use object instead of matching arrays to set properties
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'js/tools/SelectionTool.js')
-rwxr-xr-x | js/tools/SelectionTool.js | 254 |
1 files changed, 109 insertions, 145 deletions
diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js index 6dec781b..d9327f33 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 | }, |
@@ -296,160 +293,126 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { | |||
296 | }, | 293 | }, |
297 | 294 | ||
298 | _updateTargets: { | 295 | _updateTargets: { |
299 | value: function(addToUndoStack) { | 296 | value: function(addToUndo) { |
300 | var newLeft = [], | 297 | var modObject = [], mod3dObject = [], self = this; |
301 | newTop = [], | 298 | |
302 | newWidth = [], | 299 | this.application.ninja.selectedElements.forEach(function(element) { |
303 | newHeight = [], | 300 | |
304 | newStyles = [], | 301 | if(addToUndo) { |
305 | previousLeft = [], | 302 | if(!self._use3DMode) { |
306 | previousTop = [], | 303 | var prevX = element.elementModel.getProperty("x"); |
307 | previousWidth = [], | 304 | var prevY = element.elementModel.getProperty("y"); |
308 | previousHeight = [], | 305 | var prevW = element.elementModel.getProperty("w"); |
309 | previousStyles = []; | 306 | var prevH = element.elementModel.getProperty("h"); |
310 | var len = this.application.ninja.selectedElements.length; | 307 | var x = ElementsMediator.getProperty(element, "left"); |
311 | this._targets = []; | 308 | var y = ElementsMediator.getProperty(element, "top"); |
312 | for(var i = 0; i < len; i++) { | 309 | var w = ElementsMediator.getProperty(element, "width"); |
313 | var elt = this.application.ninja.selectedElements[i]; | 310 | var h = ElementsMediator.getProperty(element, "height"); |
314 | 311 | ||
315 | var curMat = viewUtils.getMatrixFromElement(elt); | 312 | // if we have a delta, that means the transform handles were used and |
316 | var curMatInv = glmat4.inverse(curMat, []); | 313 | // we should update the width and height too. Otherwise, just update left and top. |
317 | 314 | if(this.delta) { | |
318 | 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}}); |
319 | if(addToUndoStack) { | 316 | } else { |
320 | var prevX, | 317 | modObject.push({element:element, properties:{left: x, top:y}, previousProperties: {left: prevX, top:prevY}}); |
321 | prevY, | 318 | } |
322 | prevW, | 319 | |
323 | prevH, | 320 | } else { |
324 | _x, | 321 | // Not using the 3d mode |
325 | _y, | 322 | var previousMat = element.elementModel.getProperty("mat").slice(0); |
326 | _w, | 323 | var prevW = element.elementModel.getProperty("w"); |
327 | _h, | 324 | var prevH = element.elementModel.getProperty("h"); |
328 | previousMat, | 325 | var w = ElementsMediator.getProperty(element, "width"); |
329 | previousStyleStr, | 326 | var h = ElementsMediator.getProperty(element, "height"); |
330 | newStyleStr; | 327 | |
331 | 328 | var previousStyleStr = {dist:element.elementModel.getProperty("dist"), mat:MathUtils.scientificToDecimal(previousMat, 5)}; | |
332 | if(!this._use3DMode) { | 329 | var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(element), mat:MathUtils.scientificToDecimal(viewUtils.getMatrixFromElement(element), 5)}; |
333 | prevX = this._undoArray[i]._x; | 330 | |
334 | prevY = this._undoArray[i]._y; | 331 | modObject.push({element:element, properties:{width: w, height:h}, previousProperties: {width: prevW, height:prevH}}); |
335 | prevW = this._undoArray[i]._w; | 332 | mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr}); |
336 | prevH = this._undoArray[i]._h; | ||
337 | _x = parseInt(ElementsMediator.getProperty(elt, "left")); | ||
338 | _y = parseInt(ElementsMediator.getProperty(elt, "top")); | ||
339 | _w = parseInt(ElementsMediator.getProperty(elt, "width")); | ||
340 | _h = parseInt(ElementsMediator.getProperty(elt, "height")); | ||
341 | |||
342 | previousLeft.push(prevX + "px"); | ||
343 | previousTop.push(prevY + "px"); | ||
344 | previousWidth.push(prevW + "px"); | ||
345 | previousHeight.push(prevH + "px"); | ||
346 | newLeft.push(_x + "px"); | ||
347 | newTop.push(_y + "px"); | ||
348 | newWidth.push(_w + "px"); | ||
349 | newHeight.push(_h + "px"); | ||
350 | } | ||
351 | else | ||
352 | { | ||
353 | previousMat = this._undoArray[i].mat.slice(0); | ||
354 | prevW = this._undoArray[i]._w; | ||
355 | prevH = this._undoArray[i]._h; | ||
356 | _w = parseInt(ElementsMediator.getProperty(elt, "width")); | ||
357 | _h = parseInt(ElementsMediator.getProperty(elt, "height")); | ||
358 | previousWidth.push(prevW + "px"); | ||
359 | previousHeight.push(prevH + "px"); | ||
360 | newWidth.push(_w + "px"); | ||
361 | newHeight.push(_h + "px"); | ||
362 | |||
363 | previousStyleStr = {dist:this._undoArray[i].dist, | ||
364 | mat:MathUtils.scientificToDecimal(previousMat, 5)}; | ||
365 | newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(elt), | ||
366 | mat:MathUtils.scientificToDecimal(curMat, 5)}; | ||
367 | previousStyles.push(previousStyleStr); | ||
368 | newStyles.push(newStyleStr); | ||
369 | |||
370 | this._targets[i].mat = curMat; | ||
371 | this._targets[i].matInv = curMatInv; | ||
372 | } | 333 | } |
373 | } | 334 | } |
374 | } | 335 | }); |
375 | 336 | ||
376 | if(addToUndoStack) { | 337 | // Move them |
338 | if(addToUndo) { | ||
377 | if(!this._use3DMode) { | 339 | if(!this._use3DMode) { |
378 | // if we have a delta, that means the transform handles were used and | 340 | ElementsMediator.setProperties(modObject, "Change", "SelectionTool" ); |
379 | // we should update the width and height too. Otherwise, just update left and top. | ||
380 | if(this._delta) { | ||
381 | ElementsMediator.setProperties(this.application.ninja.selectedElements, {"left": newLeft, "top": newTop, "width": newWidth, "height": newHeight }, | ||
382 | { "left" : previousLeft, "top" : previousTop, "width": previousWidth, "height": previousHeight}, "Change", "selectionTool"); | ||
383 | } else { | ||
384 | ElementsMediator.setProperties(this.application.ninja.selectedElements, {"left": newLeft, "top": newTop }, {"left": previousLeft, "top": previousTop}, "Change", "selectionTool"); | ||
385 | } | ||
386 | } else { | 341 | } else { |
387 | // 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 |
388 | ElementsMediator.setProperties(this.application.ninja.selectedElements, {"width": newWidth, "height": newHeight }, {"width": previousWidth, "height": previousHeight}, "Change", "selectionTool"); | 343 | ElementsMediator.setProperties(modObject, "Change", "SelectionTool" ); |
389 | ElementsMediator.set3DProperties(this.application.ninja.selectedElements, newStyles, "Change", "translateTool", previousStyles); | 344 | ElementsMediator.set3DProperties(mod3dObject, "Change", "translateTool"); |
345 | |||
390 | } | 346 | } |
391 | } | 347 | } |
392 | // Save previous value for undo/redo | 348 | |
393 | this._undoArray = []; | 349 | this.application.ninja.selectedElements.forEach(function(element) { |
394 | for(i = 0, len = this._targets.length; i < len; i++) | 350 | element.elementModel.setProperty("x", ElementsMediator.getProperty(element, "left")); |
395 | { | 351 | element.elementModel.setProperty("y", ElementsMediator.getProperty(element, "top")); |
396 | var item = this._targets[i]; | 352 | element.elementModel.setProperty("w", ElementsMediator.getProperty(element, "width")); |
397 | _x = parseInt(ElementsMediator.getProperty(item.elt, "left")); | 353 | element.elementModel.setProperty("h", ElementsMediator.getProperty(element, "height")); |
398 | _y = parseInt(ElementsMediator.getProperty(item.elt, "top")); | 354 | element.elementModel.setProperty("mat", viewUtils.getMatrixFromElement(element)); |
399 | _w = parseInt(ElementsMediator.getProperty(item.elt, "width")); | 355 | element.elementModel.setProperty("matInv", glmat4.inverse(element.elementModel.getProperty("mat"), [])); |
400 | _h = parseInt(ElementsMediator.getProperty(item.elt, "height")); | 356 | element.elementModel.setProperty("dist", viewUtils.getPerspectiveDistFromElement(element)); |
401 | var _mat = viewUtils.getMatrixFromElement(item.elt); | 357 | }); |
402 | var _dist = viewUtils.getPerspectiveDistFromElement(item.elt); | ||
403 | this._undoArray.push({_x:_x, _y:_y, _w:_w, _h:_h, mat:_mat, dist:_dist}); | ||
404 | } | ||
405 | 358 | ||
406 | } | 359 | } |
407 | }, | 360 | }, |
408 | 361 | ||
409 | _moveElements: { | 362 | _moveElements: { |
410 | value: function (transMat) { | 363 | value: function (transMat) { |
411 | var len = this._targets.length, | 364 | var elt, curMat, targets = []; |
412 | i, | ||
413 | item, | ||
414 | elt, | ||
415 | curMat, | ||
416 | newLeft = [], | ||
417 | newTop = []; | ||
418 | 365 | ||
419 | var matInv = glmat4.inverse(this._startMat, []); | 366 | // var matInv = glmat4.inverse(this._startMat, []); |
420 | var nMat = glmat4.multiply(transMat, this._startMat, [] ); | 367 | // var qMat = glmat4.multiply(matInv, nMat, []); |
421 | var qMat = glmat4.multiply(matInv, nMat, []); | 368 | this._startMat = glmat4.multiply(transMat, this._startMat, [] ); |
422 | 369 | ||
423 | this._startMat = nMat; | 370 | var self = this; |
424 | 371 | ||
425 | for(i = 0; i < len; i++) | 372 | this.application.ninja.selectedElements.forEach(function(element) { |
426 | { | 373 | if(self._use3DMode) { |
427 | item = this._targets[i]; | 374 | curMat = element.elementModel.getProperty("mat"); |
428 | elt = item.elt; | 375 | |
429 | if(this._use3DMode) | 376 | curMat[12] += transMat[12]; |
430 | { | 377 | curMat[13] += transMat[13]; |
378 | curMat[14] += transMat[14]; | ||
379 | viewUtils.setMatrixForElement( element, curMat, true); | ||
380 | element.elementModel.setProperty("mat", curMat); | ||
381 | } else { | ||