From f91e64235eb03c889ff4f5577c3e3480cd0d787f Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Fri, 13 Apr 2012 00:30:19 -0700 Subject: removing _undo array and _targets array and use object instead of matching arrays to set properties Signed-off-by: Valerio Virgillito --- js/tools/Translate3DToolBase.js | 158 ++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 102 deletions(-) (limited to 'js/tools/Translate3DToolBase.js') diff --git a/js/tools/Translate3DToolBase.js b/js/tools/Translate3DToolBase.js index e4f1fb99..04359567 100755 --- a/js/tools/Translate3DToolBase.js +++ b/js/tools/Translate3DToolBase.js @@ -21,8 +21,7 @@ exports.Translate3DToolBase = Montage.create(ModifierToolBase, _clickedOnStage: { value: false }, HandleDoubleClick : { - value : function() - { + value : function() { } }, @@ -74,7 +73,7 @@ exports.Translate3DToolBase = Montage.create(ModifierToolBase, var transMat = Matrix.Translation( delta ); //console.log( "Translate: " + delta ); - if(this._inLocalMode && (this._targets.length === 1) ) + if(this._inLocalMode && (this.application.ninja.selectedElements.length === 1) ) { this._translateLocally(transMat); } @@ -86,45 +85,32 @@ exports.Translate3DToolBase = Montage.create(ModifierToolBase, }, - Reset : { - value : function() - { - var item, - mat, - dist, - newStyles = [], - previousStyles = [], - len = this.application.ninja.selectedElements.length; - for(var i = 0; i < len; i++) - { - // Reset to the identity matrix but retain the rotation values - item = this.application.ninja.selectedElements[i]; - mat = ElementsMediator.getMatrix(item); + Reset: { + value: function() { + var mat, iMat, dist, mod3dObject = [], self = this; + + this.application.ninja.selectedElements.forEach(function(element) { + // Reset to the identity matrix + //item = this.application.ninja.selectedElements[i]; + iMat = Matrix.I(4); + mat = ElementsMediator.getMatrix(element); mat[12] = 0; mat[13] = 0; mat[14] = 0; - dist = ElementsMediator.getPerspectiveDist(item); + dist = ElementsMediator.getPerspectiveDist(element); - var previousStyleStr = {dist:dist, mat:item.mat}; + var previousStyleStr = {dist:dist, mat:element.elementModel.getProperty("mat")}; + var newStyleStr = {dist:dist, mat:iMat}; - var newStyleStr = {dist:dist, mat:mat}; - - previousStyles.push(previousStyleStr); - newStyles.push(newStyleStr); - } + mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr}); + }); - ElementsMediator.set3DProperties(this.application.ninja.selectedElements, - newStyles, - "Change", - "translateTool", - previousStyles - ); + ElementsMediator.set3DProperties(mod3dObject, "Change", "rotateTool"); this.isDrawing = false; this.endDraw(event); - // Need to force stage to draw immediately so the new selection center is calculated this.application.ninja.stage.draw(); // And captureSelectionDrawn to draw the transform handles @@ -151,111 +137,79 @@ exports.Translate3DToolBase = Montage.create(ModifierToolBase, value: function (transMat) { //console.log( "_translateGlobally, startMat: " + this._startMat + ", transMat: " + transMat ); //console.log( "_translateGlobally, transMat: " + transMat ); - var len = this._targets.length, - i = 0, - item, - elt, + var self = this, curMat = viewUtils.getMatrixFromElement( this._target ), matInv = glmat4.inverse(this._startMat, []), - nMat = glmat4.multiply(transMat, this._startMat, [] ), - qMat = glmat4.multiply(matInv, nMat, []); + nMat = glmat4.multiply(transMat, this._startMat, [] ); +// qMat = glmat4.multiply(matInv, nMat, []); - if (this._mode === 1) - { + if(this._mode === 1) { var curInv = glmat4.inverse( curMat, [] ); transMat = glmat4.multiply( nMat, curInv, [] ); } var shouldUpdateStartMat = true; - if(this._clickedOnStage) - { + if(this._clickedOnStage) { shouldUpdateStartMat = false; - } - else if(this._mode !== 1) - { + } else if(this._mode !== 1) { this._startMat = nMat; } - for(i = 0; i < len; i++) - { - item = this._targets[i]; - elt = item.elt; - curMat = item.mat.slice(0); + this.application.ninja.selectedElements.forEach(function(element) { + curMat = element.elementModel.getProperty("mat").slice(0); -// glmat4.multiply(curMat, qMat, curMat); -// -// viewUtils.setMatrixForElement( elt, curMat, true); +// glmat4.multiply(curMat, qMat, curMat); +// viewUtils.setMatrixForElement( elt, curMat, true); curMat[12] += transMat[12]; curMat[13] += transMat[13]; curMat[14] += transMat[14]; - viewUtils.setMatrixForElement( elt, curMat, true); - if(shouldUpdateStartMat) - { + viewUtils.setMatrixForElement(element, curMat, true); + + if(shouldUpdateStartMat) { //console.log( "\t\tshouldUpdateStartMat" ); - this._targets[i].mat = curMat; - } - } + element.elementModel.setProperty("mat", curMat); + } + + }); } }, _updateTargets: { - value: function(addToUndoStack) { -// console.log( "_updateTargets" ); - var newStyles = [], - previousStyles = [], - len = this.application.ninja.selectedElements.length; - this._targets = []; - for(var i = 0; i < len; i++) - { - var elt = this.application.ninja.selectedElements[i]; + value: function(addToUndo) { + var mod3dObject = [], self = this; - var curMat = viewUtils.getMatrixFromElement(elt); - var curMatInv = glmat4.inverse(curMat, []); + this.application.ninja.selectedElements.forEach(function(element) { + if(addToUndo) { + var previousMat = element.elementModel.getProperty("mat").slice(0); + var previousStyleStr = {dist:element.elementModel.getProperty("dist"), mat:MathUtils.scientificToDecimal(previousMat, 5)}; + var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(element), mat:MathUtils.scientificToDecimal(viewUtils.getMatrixFromElement(element), 5)}; - this._targets.push({elt:elt, mat:curMat, matInv:curMatInv}); - if(addToUndoStack) - { - var previousStyleStr = {dist:this._undoArray[i].dist, mat:MathUtils.scientificToDecimal(this._undoArray[i].mat.slice(0), 5)}; - - var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(elt), mat:MathUtils.scientificToDecimal(curMat, 5)}; + mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr}); - previousStyles.push(previousStyleStr); - newStyles.push(newStyleStr); } - } - if(addToUndoStack) - { - ElementsMediator.set3DProperties(this.application.ninja.selectedElements, - newStyles, - "Change", - "translateTool", - previousStyles - ); - if(this._origin && this._delta) - { - if(this._handleMode !== null) - { + }); + + if(addToUndo) { + ElementsMediator.set3DProperties(mod3dObject, "Change", "translateTool"); + + if(this._origin && this._delta) { + if(this._handleMode !== null) { this._origin[this._handleMode] += this._delta; - } - else - { + } else { this._origin[0] += this._delta[0]; this._origin[1] += this._delta[1]; } } + this._delta = null; } - // Save previous value for undo/redo - this._undoArray = []; - for(i = 0, len = this._targets.length; i < len; i++) - { - var elt = this._targets[i].elt; - var _mat = viewUtils.getMatrixFromElement(elt); - var _dist = viewUtils.getPerspectiveDistFromElement(elt); - this._undoArray.push({mat:_mat, dist:_dist}); - } + + this.application.ninja.selectedElements.forEach(function(element) { + element.elementModel.setProperty("mat", viewUtils.getMatrixFromElement(element)); + element.elementModel.setProperty("dist", viewUtils.getPerspectiveDistFromElement(element)); + }); } }, -- cgit v1.2.3