aboutsummaryrefslogtreecommitdiff
path: root/js/tools/Rotate3DToolBase.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/tools/Rotate3DToolBase.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/tools/Rotate3DToolBase.js')
-rw-r--r--js/tools/Rotate3DToolBase.js637
1 files changed, 637 insertions, 0 deletions
diff --git a/js/tools/Rotate3DToolBase.js b/js/tools/Rotate3DToolBase.js
new file mode 100644
index 00000000..6e17864b
--- /dev/null
+++ b/js/tools/Rotate3DToolBase.js
@@ -0,0 +1,637 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/* Base class for the 3D rotation tools
8Subclass RotateObject3DTool will rotate the object that was clicked.
9Subclass RotateStage3DTool will rotate the stage.
10 */
11var Montage = require("montage/core/core").Montage,
12 ModifierToolBase = require("js/tools/modifier-tool-base").ModifierToolBase,
13 snapManager = require("js/helper-classes/3D/snap-manager").SnapManager,
14 viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils,
15 vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
16 drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils,
17 ElementsMediator = require("js/mediators/element-mediator").ElementMediator;
18
19exports.Rotate3DToolBase = Montage.create(ModifierToolBase, {
20 _canSnap: { value: false },
21
22 _inLocalMode: { value: true, enumerable: true },
23
24 drawWithoutSnapping:
25 {
26 value: function(event)
27 {
28 if(this._handleMode !== null)
29 {
30 this._matL = this._handles[this._handleMode]._matL.slice(0);
31 this._planeEq = this._handles[this._handleMode]._planeEq.slice(0);
32 this._dirVecL = this._handles[this._handleMode]._dirVecL.slice(0);
33 this._startPoint = MathUtils.getLocalPoint(this.downPoint.x,
34 this.downPoint.y,
35 this._planeEq,
36 this._matL);
37 if(!this._startPoint)
38 {
39 this._startPoint = [this.downPoint.x, this.downPoint.y];
40 }
41 }
42 }
43 },
44
45 modifyElements: {
46 value: function(data, event) {
47 var mat,
48 angle,
49 pt0 = data.pt0,
50 pt1 = data.pt1;
51
52 if(this._handleMode !== null)
53 {
54 if(this._activateOriginHandle)
55 {
56 // move the transform origin handle
57 var dx = pt1.x - pt0.x;
58 var dy = pt1.y - pt0.y;
59 this._origin[0] += dx;
60 this._origin[1] += dy;
61
62 var len = this._targets.length;
63 if(len === 1)
64 {
65 this._startOriginArray[0][0] += dx;
66 this._startOriginArray[0][1] += dy;
67 }
68 this.downPoint.x = pt1.x;
69 this.downPoint.y = pt1.y;
70 this.DrawHandles();
71 return;
72 }
73
74 angle = this._getAngleToRotate(pt1.x, pt1.y);
75 if(event.shiftKey)
76 {
77 var f = Math.floor(angle/(Math.PI/4));
78 angle = f*Math.PI/4;
79 }
80 this._delta = angle;
81 switch(this._handleMode)
82 {
83 case 0:
84 // Rotate X;
85 mat = Matrix.RotationX(angle);
86 break;
87 case 1:
88 // Rotate Y
89 mat = Matrix.RotationY(angle);
90 break;
91 case 2:
92 // Rotate Z
93 mat = Matrix.RotationZ(angle);
94 break;
95 default:
96 break;
97 }
98 }
99 else
100 {
101 if (event.ctrlKey || event.metaKey)
102 {
103 var zAngle = this._mouseSpeed * (pt1.y - this.downPoint.y) * Math.PI / 180.0;
104 if (zAngle === 0)
105 {
106 zAngle = 0.01 * Math.PI / 180.0;
107 }
108 if(event.shiftKey)
109 {
110 var f = Math.floor(zAngle/(Math.PI/4));
111 zAngle = f*Math.PI/4;
112 }
113 mat = Matrix.RotationZ(zAngle);
114 }
115 else
116 {
117 var yAngle = this._mouseSpeed * (pt1.x - this.downPoint.x) * Math.PI / 180.0;
118 var xAngle = -this._mouseSpeed * (pt1.y - this.downPoint.y) * Math.PI / 180.0;
119 if(event.shiftKey)
120 {
121 var f = Math.floor(yAngle/(Math.PI/4));
122 yAngle = f*Math.PI/4;
123 f = Math.floor(xAngle/(Math.PI/4));
124 xAngle = f*Math.PI/4;
125 }
126
127 // check the orientation of the X axis
128 //if (drawUtils.drawYZ) xAngle = -xAngle;
129 var yMat = Matrix.RotationY( yAngle ),
130 xMat = Matrix.RotationX( xAngle );
131
132 mat = glmat4.multiply(yMat, xMat, []);
133 }
134 }
135
136 if(this._inLocalMode && (this._targets.length === 1) )
137 {
138 this._rotateLocally(mat);
139 }
140 else
141 {
142 this._rotateGlobally(mat);
143 }
144
145// this.UpdateSelection();
146 NJevent("elementChanging", {type : "Changing", redraw: false});
147 }
148 },
149
150 _rotateLocally: {
151 value: function (rotMat) {
152 var len = this._targets.length;
153 for(var i = 0; i < len; i++)
154 {
155 var item = this._targets[i];
156 var elt = item.elt;
157 var curMat = item.mat;
158
159 // pre-translate by the transformation center
160 var tMat = Matrix.I(4);
161
162 var transformCtr = this._startOriginArray[i];
163
164 tMat[12] = transformCtr[0];
165 tMat[13] = transformCtr[1];
166 tMat[14] = transformCtr[2];
167
168 var mat = glmat4.multiply(curMat, tMat, []);
169
170 // translate back
171 tMat[12] = -transformCtr[0];
172 tMat[13] = -transformCtr[1];
173 tMat[14] = -transformCtr[2];
174
175 glmat4.multiply(mat, rotMat, mat);
176
177
178 glmat4.multiply(mat, tMat, mat);
179
180 // while moving, set inline style to improve performance
181 viewUtils.setMatrixForElement( elt, mat, true );
182 }
183 }
184 },
185
186 _rotateGlobally: {
187 value: function (rotMat) {
188 var len = this._targets.length;
189 for(var i = 0; i < len; i++)
190 {
191 var item = this._targets[i];
192 var elt = item.elt;
193 var curMat = item.mat;
194
195 // pre-translate by the transformation center
196 var tMat = Matrix.I(4);
197
198 var transformCtr = this._startOriginArray[i].slice(0);
199 transformCtr = MathUtils.transformPoint(transformCtr, curMat);
200
201 tMat[12] = transformCtr[0];
202 tMat[13] = transformCtr[1];
203 tMat[14] = transformCtr[2];
204
205 var mat = glmat4.multiply(tMat, rotMat, []);
206
207 // translate back
208 tMat[12] = -transformCtr[0];
209 tMat[13] = -transformCtr[1];
210 tMat[14] = -transformCtr[2];
211
212 glmat4.multiply(mat, tMat, mat);
213
214 glmat4.multiply(mat, curMat, mat);
215
216 viewUtils.setMatrixForElement( elt, mat, true );
217 }
218 }
219 },
220
221 _getAngleToRotate: {
222 value: function (x, y) {
223 var angle;
224 var pt = MathUtils.getLocalPoint(x, y, this._planeEq, this._matL);
225 if(!pt)
226 {
227 //TODO - should this be _startPoint.x/y instead of downPoint.x/y?
228 var st = [this.downPoint.x, this.downPoint.y];
229 pt = [x, y];
230 var sub = vecUtils.vecSubtract(2, pt, st);
231 var dot = vecUtils.vecDot(2, sub, this._dirVecL);
232
233 angle = vecUtils.vecDist(2, pt, st) * 0.1;
234
235 if (dot < 0)
236 {
237 an