diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/tools/PanTool.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/tools/PanTool.js')
-rw-r--r-- | js/tools/PanTool.js | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/js/tools/PanTool.js b/js/tools/PanTool.js new file mode 100644 index 00000000..ba40b020 --- /dev/null +++ b/js/tools/PanTool.js | |||
@@ -0,0 +1,378 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | var Montage = require("montage/core/core").Montage, | ||
8 | drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils, | ||
9 | vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils, | ||
10 | viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils, | ||
11 | snapManager = require("js/helper-classes/3D/snap-manager").SnapManager, | ||
12 | Keyboard = require("js/mediators/keyboard-mediator").Keyboard; | ||
13 | toolBase = require("js/tools/ToolBase").toolBase; | ||
14 | |||
15 | exports.PanTool = Montage.create(toolBase, | ||
16 | { | ||
17 | _localPt :{value: [0,0] , writable:true}, | ||
18 | _worldPt :{value: [0,0] , writable:true}, | ||
19 | _globalPt :{value: [0,0] , writable:true}, | ||
20 | _globalToUCWorld :{value: [] , writable:true}, | ||
21 | _lastGPt :{value: [0,0], writable:true}, | ||
22 | |||
23 | Configure: { | ||
24 | value: function ( doActivate ) | ||
25 | { | ||
26 | if (doActivate) | ||
27 | { | ||
28 | NJevent("enableStageMove"); | ||
29 | this.eventManager.addEventListener( "toolDoubleClick", this, false); | ||
30 | this.application.ninja.stage.canvas.addEventListener("mousewheel", this, false); | ||
31 | this.activate(); | ||
32 | } | ||
33 | else | ||
34 | { | ||
35 | NJevent("disableStageMove"); | ||
36 | this.eventManager.removeEventListener( "toolDoubleClick", this, false); | ||
37 | this.application.ninja.stage.canvas.removeEventListener("mousewheel", this, false); | ||
38 | this.deactivate(); | ||
39 | } | ||
40 | } | ||
41 | }, | ||
42 | |||
43 | HandleLeftButtonDown: { | ||
44 | value : function ( event ) { | ||
45 | this._isDrawing = true; | ||
46 | |||
47 | this.mouseDown( event ); | ||
48 | } | ||
49 | }, | ||
50 | |||
51 | HandleMouseMove: | ||
52 | { | ||
53 | value : function (event) | ||
54 | { | ||
55 | this.mouseMove( event ); | ||
56 | } | ||
57 | }, | ||
58 | |||
59 | HandleLeftButtonUp: | ||
60 | { | ||
61 | value : function ( event ) | ||
62 | { | ||
63 | //if(this._isDrawing) | ||
64 | { | ||
65 | // do one final mouse move to update the scrollbars | ||
66 | this.mouseUp( event ); | ||
67 | |||
68 | this.application.ninja.stage.clearDrawingCanvas(); | ||
69 | this._hasDraw = false; | ||
70 | this._isDrawing = false; | ||
71 | } | ||
72 | } | ||
73 | }, | ||
74 | |||
75 | HandleKeyPress: { | ||
76 | value: function(event) { | ||
77 | if(event.altKey) | ||
78 | { | ||
79 | this._altKeyDown = true; | ||
80 | //console.log( "altKeyDown" ); | ||
81 | } | ||
82 | } | ||
83 | }, | ||
84 | |||
85 | HandleKeyUp: { | ||
86 | value: function(event) { | ||
87 | if(event.keyCode === Keyboard.ALT) | ||
88 | { | ||
89 | this._altKeyDown = false; | ||
90 | console.log( "altKeyUp" ); | ||
91 | } | ||
92 | } | ||
93 | }, | ||
94 | |||
95 | handleToolDoubleClick: | ||
96 | { | ||
97 | value: function () | ||
98 | { | ||
99 | var uc = this.application.ninja.currentDocument.documentRoot; | ||
100 | //var uc = documentManagerModule.DocumentManager.activeDocument | ||
101 | var ucMat = viewUtils.getMatrixFromElement(uc); | ||
102 | |||
103 | var noTrans = ucMat.slice(); | ||
104 | noTrans[12] = 0; noTrans[13] = 0; noTrans[14] = 0; | ||
105 | var ucMatInv = glmat4.inverse( ucMat, [] ); | ||
106 | var deltaMat = glmat4.multiply( noTrans, ucMatInv, [] ); | ||
107 | |||
108 | this.application.ninja.stage.centerStage(); | ||
109 | |||
110 | this.applyDeltaMat( deltaMat ); | ||
111 | } | ||
112 | }, | ||
113 | |||
114 | handleMousewheel : | ||
115 | { | ||
116 | value:function(event) | ||
117 | { | ||
118 | var zoom = this.application.ninja.documentBar.zoomFactor/100.0; | ||
119 | if (!zoom) zoom = 1.0; | ||
120 | |||
121 | var delta = 0; | ||
122 | if (event.wheelDelta) | ||
123 | delta = 10*event.wheelDelta/120; | ||
124 | //console.log( "delta: " + delta ); | ||
125 | |||
126 | this.application.ninja.stage._iframeContainer.scrollLeft += delta; | ||
127 | this.application.ninja.stage._scrollLeft += delta; | ||
128 | |||
129 | delta *= zoom; | ||
130 | |||
131 | var uc = this.application.ninja.currentDocument.documentRoot; | ||
132 | var ucMat = viewUtils.getMatrixFromElement(uc); | ||
133 | var offset = viewUtils.getElementOffset( uc ); | ||
134 | //console.log( "uc offset: " + offset[0] ); | ||
135 | |||
136 | var localToGlobalMat = viewUtils.getLocalToGlobalMatrix( uc ); | ||
137 | var globalToLocalMat = glmat4.inverse( localToGlobalMat, []); | ||
138 | |||
139 | var w = uc.offsetWidth, | ||
140 | h = uc.offsetHeight; | ||
141 | if(uc.width) | ||
142 | w = uc.width; | ||
143 | if(uc.height) | ||
144 | h = uc.height; | ||
145 | var localPt = [ w/2, h/2, 0]; | ||
146 | var globalPt = MathUtils.transformAndDivideHomogeneousPoint( localPt, localToGlobalMat ); | ||
147 | this.doMouseDown( { x:globalPt[0], y:globalPt[1] } ); | ||
148 | |||
149 | globalPt[0] += delta; | ||
150 | this._isDrawing = true; | ||
151 | this.doMouseMove( { x:globalPt[0], y:globalPt[1] } ); | ||
152 | this._isDrawing = false; | ||
153 | } | ||
154 | }, | ||
155 | |||
156 | |||
157 | ///////////////////////////////////////////////////////////////////// | ||
158 | // Simple tool API | ||
159 | activate: | ||
160 | { | ||
161 | value: function() | ||
162 | { | ||
163 | //console.log( "PanTool.activate" ); | ||
164 | } | ||
165 | }, | ||
166 | |||
167 | deactivate: | ||
168 | { | ||
169 | value: function() | ||
170 | { | ||
171 | //console.log( "PanTool.deactivate" ); | ||
172 | } | ||
173 | }, | ||
174 | |||
175 | mouseDown: | ||
176 | { | ||
177 | value: function( event ) | ||
178 | { | ||
179 | //console.log( "PanTool.mouseDown" ); | ||
180 | if (!this.application.ninja.currentDocument) return; | ||
181 | |||
182 | var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, | ||
183 | new WebKitPoint(event.pageX, event.pageY)); | ||
184 | this.doMouseDown( point ); | ||
185 | } | ||
186 | }, | ||
187 | |||
188 | |||
189 | doMouseDown: | ||
190 | { | ||
191 | value: function( point ) | ||
192 | { | ||
193 | //var tmpPt, tmpPt2, tmpPt3, tmpPt4, tmpMat, tmpMat2; // DEBUG. (see use of these points below) | ||
194 | var hitRec = snapManager.snap( point.x, point.y, true ); | ||
195 | if (hitRec) | ||
196 | { | ||
197 | //console.log( "hit: " + hitRec.getElement().id ); | ||
198 | var globalPt = [point.x, point.y]; | ||
199 | var elt = hitRec.getElement(); | ||
200 | if (elt) | ||
201 | { | ||
202 | // get the userContent object (stage) and its matrix | ||
203 | var userContent = this.application.ninja.currentDocument.documentRoot; | ||
204 | var ucMat = viewUtils.getMatrixFromElement(userContent); | ||
205 | |||
206 | var localToGlobalMat = viewUtils.getLocalToGlobalMatrix( elt ); | ||
207 | var globalToLocalMat = glmat4.inverse( localToGlobalMat, []); | ||
208 | |||
209 | if (elt != userContent) | ||
210 | this._localPt = hitRec.calculateElementPreTransformScreenPoint(); | ||
211 | else | ||
212 | { | ||
213 | var localPt = hitRec.calculateElementWorldPoint(); | ||
214 | viewUtils.pushViewportObj( userContent ); | ||
215 | var cop = viewUtils.getCenterOfProjection(); | ||
216 | this._localPt = [cop[0] + localPt[0], cop[1] + localPt[1], localPt[2]]; | ||
217 | viewUtils.popViewportObj(); | ||
218 | } | ||
219 | this._globalPt = MathUtils.transformAndDivideHomogeneousPoint( this._localPt, localToGlobalMat ); | ||
220 | var tmpLocal = MathUtils.transformAndDivideHomogeneousPoint( this._globalPt, globalToLocalMat ); | ||
221 | |||
222 | this._lastGPt = this._globalPt.slice(); | ||
223 | |||
224 | // set up the matrices we will be needing | ||
225 | var eltToStageWorldMat = glmat4.multiply( ucMat, viewUtils.getObjToStageWorldMatrix(elt, true), []); | ||
226 | this._worldPt = MathUtils.transformAndDivideHomogeneousPoint( this._localPt, eltToStageWorldMat ); | ||
227 | // console.log( "screenPt: " + globalPt ); | ||
228 | // console.log( "_worldPt: " + this._worldPt ); | ||
229 | // console.log( "_localPt: " + this._localPt ); | ||
230 | // console.log( "_globalPt: " + this._globalPt ); | ||
231 | // console.log( "hit localPt: " + hitRec.calculateElementPreTransformScreenPoint() ); | ||
232 | |||
233 | // get a matrix from user content world space to the screen | ||
234 | viewUtils.pushViewportObj( userContent ); | ||
235 | var cop = viewUtils.getCenterOfProjection(); | ||
236 | var pDist = viewUtils.getPerspectiveDistFromElement(userContent); | ||
237 | var projMat = glmat4.scale(Matrix.I(4), [pDist,pDist,pDist], []); | ||