diff options
Diffstat (limited to 'js/tools/drawing-tool-base.js')
-rw-r--r-- | js/tools/drawing-tool-base.js | 673 |
1 files changed, 673 insertions, 0 deletions
diff --git a/js/tools/drawing-tool-base.js b/js/tools/drawing-tool-base.js new file mode 100644 index 00000000..6fc34911 --- /dev/null +++ b/js/tools/drawing-tool-base.js | |||
@@ -0,0 +1,673 @@ | |||
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 | var Component = require("montage/ui/component").Component; | ||
9 | |||
10 | var snapManager = require("js/helper-classes/3D/snap-manager").SnapManager; | ||
11 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; | ||
12 | var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; | ||
13 | var Properties3D = require("js/helper-classes/Properties3D").Properties3D; | ||
14 | var drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils; | ||
15 | |||
16 | exports.DrawingToolBase = Montage.create(Montage, { | ||
17 | |||
18 | //!!!! HACK | ||
19 | // TODO: Need to find a better way to address this | ||
20 | stage: { | ||
21 | value: null | ||
22 | }, | ||
23 | |||
24 | stageComponent: { | ||
25 | value: null | ||
26 | }, | ||
27 | |||
28 | /** | ||
29 | * Used on the initial MouseDown for Drawing Tools | ||
30 | * | ||
31 | * Returns: An array containing: | ||
32 | * 0 - HitRec point | ||
33 | * 1 - X value converted to screen point | ||
34 | * 2 - Y value converted to screen point | ||
35 | */ | ||
36 | getInitialSnapPoint: { | ||
37 | value: function(x,y) { | ||
38 | // update the snap settings | ||
39 | snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() ); | ||
40 | snapManager.enableElementSnap( snapManager.elementSnapEnabledAppLevel() ); | ||
41 | snapManager.enableGridSnap( snapManager.gridSnapEnabledAppLevel() ); | ||
42 | |||
43 | // do the snap | ||
44 | var hitRec = snapManager.snap(x, y, true); | ||
45 | if (hitRec) { | ||
46 | // set up the working plane and convert the hit record to be working plane relative | ||
47 | var dragPlane = snapManager.setupDragPlanes( hitRec ); | ||
48 | var wpHitRec = hitRec.convertToWorkingPlane( dragPlane ); | ||
49 | |||
50 | var pt = hitRec.getScreenPoint(); | ||
51 | |||
52 | return( [wpHitRec, pt[0], pt[1]] ); | ||
53 | } | ||
54 | } | ||
55 | }, | ||
56 | |||
57 | /** | ||
58 | * Used on the Mouse Move to calculate new snap point. | ||
59 | */ | ||
60 | getUpdatedSnapPoint: { | ||
61 | value: function(x,y, snap3d, downHitRec) { | ||
62 | // update the snap settings | ||
63 | snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() ); | ||
64 | snapManager.enableElementSnap( snapManager.elementSnapEnabledAppLevel() ); | ||
65 | snapManager.enableGridSnap( snapManager.gridSnapEnabledAppLevel() ); | ||
66 | |||
67 | |||
68 | // do the first snap | ||
69 | var hitRec = snapManager.snap(x, y, snap3d ); | ||
70 | if (hitRec) { | ||
71 | if ((hitRec.getType() !== hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) { | ||
72 | hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() ); | ||
73 | } | ||
74 | |||
75 | if(downHitRec !== null) { | ||
76 | // if we are working off-plane, do a snap to the projected locations of the geometry | ||
77 | var thePlane = workingPlane; | ||
78 | if (snapManager.hasDragPlane()) | ||
79 | { | ||
80 | thePlane = snapManager.getDragPlane(); | ||
81 | } | ||
82 | |||
83 | // Return the up HitRec | ||
84 | return hitRec; | ||
85 | } else { | ||
86 | return null; | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | }, | ||
91 | |||
92 | getUpdatedSnapPointNoAppLevelEnabling: { | ||
93 | value: function(x,y, snap3d, downHitRec) { | ||
94 | |||
95 | |||
96 | // do the first snap | ||
97 | var hitRec = snapManager.snap(x, y, snap3d ); | ||
98 | if (hitRec) { | ||
99 | if ((hitRec.getType() !== hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) { | ||
100 | hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() ); | ||
101 | } | ||
102 | |||
103 | if(downHitRec !== null) { | ||
104 | // if we are working off-plane, do a snap to the projected locations of the geometry | ||
105 | var thePlane = workingPlane; | ||
106 | if (snapManager.hasDragPlane()) | ||
107 | { | ||
108 | thePlane = snapManager.getDragPlane(); | ||
109 | } | ||
110 | |||
111 | // Return the up HitRec | ||
112 | return hitRec; | ||
113 | } else { | ||
114 | return null; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | }, | ||
119 | |||
120 | |||
121 | setDownHitRec: { | ||
122 | value: function (x, y, do3DSnap) { | ||
123 | var hitRec = snapManager.snap(x, y, do3DSnap ); | ||
124 | if (hitRec) { | ||
125 | if ((hitRec.getType() != hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) { | ||
126 | //hitRec = hitRec.convertToWorkingPlane( workingPlane ); | ||
127 | snapManager.setupDragPlanes(hitRec); | ||
128 | hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() ); | ||
129 | } | ||
130 | |||
131 | return hitRec; | ||
132 | |||
133 | } | ||
134 | } | ||
135 | }, | ||
136 | |||
137 | drawSnapLastHit: { | ||
138 | value: function() { | ||
139 | snapManager.drawLastHit(); | ||
140 | } | ||
141 | }, | ||
142 | |||
143 | getHitRecPos: { | ||
144 | value: function (hitRec) { | ||
145 | if (!hitRec) | ||
146 | return null; | ||
147 | |||
148 | // get the hit rec. points in plane space | ||
149 | var psPos = hitRec.getLocalPoint(); | ||
150 | |||
151 | var stage = this.stage; | ||
152 | var stageOffset = viewUtils.getElementOffset(stage); | ||
153 | viewUtils.setViewportObj(stage); | ||
154 | |||
155 | // get the matrix taking the local hit point in plane space | ||
156 | // to world space of whatever element it is in. | ||
157 | var planeMat = hitRec.getPlaneMatrix(); | ||
158 | |||
159 | // get the center of the circle in stage world space | ||
160 | var swPos = viewUtils.postViewToStageWorld(MathUtils.transformPoint(psPos, hitRec.getPlaneMatrix()), hitRec.getElt()); | ||
161 | |||
162 | // the stage world space point is now relative to the center of the 3D space. To | ||
163 | // calculate the left and top offsets, this must be offset by the stage dimensions | ||
164 | swPos[0] += snapManager.getStageWidth() / 2.0; | ||
165 | swPos[1] += snapManager.getStageHeight() / 2.0; | ||
166 | |||
167 | return swPos; | ||
168 | } | ||
169 | }, | ||
170 | |||
171 | getCompletePoints: { | ||
172 | value: function(hitRec0, hitRec1) { | ||
173 | if (hitRec0 && hitRec1) { | ||
174 | |||
175 | // get the 2 snap points in plane space | ||
176 | var p0 = hitRec0.getLocalPoint(), | ||
177 | p1 = hitRec1.getLocalPoint(); | ||
178 | |||
179 | var stage = this.stage; | ||
180 | var stageOffset = viewUtils.getElementOffset(stage); | ||
181 | viewUtils.setViewportObj(stage); | ||
182 | |||
183 | // get the matrix taking the local hit point in plane space | ||
184 | // to world space of whatever element it is in. | ||
185 | var planeMat = hitRec0.getPlaneMatrix(); | ||
186 | |||
187 | // get the center of the circle in stage world space | ||
188 | var s0 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p0,hitRec0.getPlaneMatrix()), hitRec0.getElt() ), | ||
189 | s1 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p1,hitRec1.getPlaneMatrix()), hitRec1.getElt() ); | ||
190 | |||
191 | // apply the projected snap points | ||
192 | var s0Proj = false, s1Proj = false; | ||
193 | |||
194 | // find a "reasonable" plane | ||
195 | var thePlane = workingPlane.slice(0); | ||
196 | if (snapManager.hasDragPlane()) { | ||
197 | thePlane = snapManager.getDragPlane(); | ||
198 | } | ||
199 | |||
200 | var d0 = vecUtils.vecDot( 3, thePlane, s0 ) + thePlane[3], | ||
201 | d1 = vecUtils.vecDot( 3, thePlane, s1 ) + thePlane[3]; | ||
202 | var sign0 = MathUtils.fpSign( d0 ), sign1 = MathUtils.fpSign( d1 ); | ||
203 | if ((sign0 !== 0) || (sign1 !== 0)) { | ||
204 | // we need to pick a different plane | ||
205 | if ( MathUtils.fpCmp(d0,d1) === 0 ) { | ||
206 | thePlane[3] = -vecUtils.vecDot(3, thePlane, s0); | ||
207 | } else { | ||
208 | var vec = vecUtils.vecSubtract(3, s1, s0 ); | ||
209 | var yAxis = Vector.create([0,1,0]); | ||
210 | var tmp = vecUtils.vecCross( 3, vec, yAxis ); | ||
211 | var mag = vecUtils.vecMag(3, tmp); | ||
212 | if (MathUtils.fpSign(mag) === 0) { | ||
213 | thePlane = Vector.create( [0,0,1] ); | ||
214 | thePlane[3] = -vecUtils.vecDot(3, thePlane, s0); | ||
215 | } else { | ||
216 | var xAxis = vecUtils.vecCross( 3, yAxis, tmp ); | ||
217 | thePlane = vecUtils.vecCross( 3, xAxis, yAxis ); | ||
218 | vecUtils.vecNormalize(3, thePlane, 1.0 ); | ||
219 | thePlane[3] = -vecUtils.vecDot(3, thePlane, s0); | ||
220 | } | ||
221 | } | ||
222 | |||
223 | // recompute the plane matrix | ||
224 | planeMat = drawUtils.getPlaneToWorldMatrix(thePlane, MathUtils.getPointOnPlane(thePlane)); | ||
225 | } | ||
226 | |||
227 | // unproject the bounds | ||
228 | //var planeMatInv = planeMat.inverse(); | ||