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/helper-classes/3D/hit-record.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/helper-classes/3D/hit-record.js')
-rw-r--r-- | js/helper-classes/3D/hit-record.js | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/js/helper-classes/3D/hit-record.js b/js/helper-classes/3D/hit-record.js new file mode 100644 index 00000000..5546cc5a --- /dev/null +++ b/js/helper-classes/3D/hit-record.js | |||
@@ -0,0 +1,291 @@ | |||
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 | /////////////////////////////////////////////////////////////////////// | ||
8 | // Class HitRecord | ||
9 | // | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | |||
12 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; | ||
13 | var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; | ||
14 | var drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils; | ||
15 | var snapManagerModule = require("js/helper-classes/3D/snap-manager"); | ||
16 | |||
17 | var HitRecord = exports.HitRecord = Object.create(Object.prototype, | ||
18 | { | ||
19 | |||
20 | /////////////////////////////////////////////////////////////////////// | ||
21 | // Constant definitions | ||
22 | /////////////////////////////////////////////////////////////////////// | ||
23 | SNAP_TYPE_STAGE : { value: 1, writable: true }, | ||
24 | SNAP_TYPE_GRID_VERTEX : { value: 2, writable: true }, | ||
25 | SNAP_TYPE_GRID_HORIZONTAL : { value: 13, writable: true }, | ||
26 | SNAP_TYPE_GRID_VERTICAL: { value: 14, writable: true }, | ||
27 | SNAP_TYPE_ALIGN_MERGED: { value: 15, writable: true }, | ||
28 | SNAP_TYPE_HORIZONTAL_FROM_START: { value: 3, writable: true }, | ||
29 | SNAP_TYPE_VERTICAL_FROM_START: { value: 4, writable: true }, | ||
30 | SNAP_TYPE_ELEMENT: { value: 5, writable: true }, | ||
31 | SNAP_TYPE_ELEMENT_EDGE: { value: 6, writable: true }, | ||
32 | SNAP_TYPE_ELEMENT_VERTEX: { value: 7, writable: true }, | ||
33 | SNAP_TYPE_ALIGN_HORIZONTAL: { value: 8, writable: true }, | ||
34 | SNAP_TYPE_ALIGN_VERTICAL: { value: 9, writable: true }, | ||
35 | SNAP_TYPE_ALIGN_BOTH: { value: 10, writable: true }, | ||
36 | SNAP_TYPE_ELEMENT_CENTER: { value: 11, writable: true }, | ||
37 | SNAP_TYPE_CONTAINED_ELEMENT: { value: 12, writable: true }, | ||
38 | SNAP_TYPE_UNDEFINED: { value: null, writable: -1 }, | ||
39 | |||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | // Instance variables | ||
42 | /////////////////////////////////////////////////////////////////////// | ||
43 | _type: { value: this.SNAP_TYPE_UNDEFINED, writable: true }, | ||
44 | _elt: { value: null, writable: true }, // this can be null. example: snapping to the working plane | ||
45 | _screenPt: { value: null, writable: true }, // snap point in global screen space | ||
46 | _localPoint: { value: null, writable: true }, // snap point in the local space of the element | ||
47 | _plane: { value: null, writable: true }, // plane equation at the snap point in local object space | ||
48 | _planeMat: { value: Matrix.I(4) , writable: true }, // transform to take the point from plane space to the transformed view space of the element | ||
49 | _assocScrPt: { value: null, writable: true }, // associated screen point | ||
50 | _assocScrPt2: { value: null, writable: true }, // a second associated point for drawing multiple snap align hits | ||
51 | _planarHit: { value: false, writable: true }, | ||
52 | _snapBoundaryIndex : { value: -1, writable: true }, // this used for snap align to object boundaries | ||
53 | _isQuadPt :{ value: false, writable: true }, // used for snapping to an object's quadrant point | ||
54 | |||
55 | /////////////////////////////////////////////////////////////////////// | ||
56 | // Property accessors | ||
57 | /////////////////////////////////////////////////////////////////////// | ||
58 | getElt : { value: function() { return this._elt; }}, | ||
59 | setElt : { value: function(e) { this._elt = e; }}, | ||
60 | getElement : { value: function() { return this._elt; }}, | ||
61 | setElement : { value: function(e) { this._elt = e; }}, | ||
62 | |||
63 | getZIndex : { value: function() { return this._zIndex; }}, | ||
64 | setZIndex : { value: function(i) { this._zIndex = i; }}, | ||
65 | |||
66 | setScreenPoint : { value: function(s) { this._screenPt = s.slice(0); }}, | ||
67 | getScreenPoint : { value: function() { return this._screenPt.slice(0); }}, | ||
68 | |||
69 | setLocalPoint : { value: function(s) { this._localPt = s.slice(0); }}, | ||
70 | getLocalPoint : { value: function() { return this._localPt.slice(0); }}, | ||
71 | |||
72 | setAssociatedScreenPoint : { value: function(s) { this._assocScrPt = s.slice(0); }}, | ||
73 | getAssociatedScreenPoint : { value: function() { return this._assocScrPt.slice(0); }}, | ||
74 | hasAssociatedScreenPoint : { value: function() { return this._assocScrPt != null; }}, | ||
75 | |||
76 | setAssociatedScreenPoint2 : { value: function(s) { this._assocScrPt2 = s.slice(0); }}, | ||
77 | getAssociatedScreenPoint2 : { value: function() { return this._assocScrPt2.slice(0); }}, | ||
78 | hasAssociatedScreenPoint2 : { value: function() { return this._assocScrPt2 != null; }}, | ||
79 | |||
80 | setPlane : { value: function(p) { this._plane = p.slice(0); }}, | ||
81 | getPlane : { value: function() { return this._plane.slice(0); }}, | ||
82 | |||
83 | setPlaneMatrix : { value: function(pm) { this._planeMat = pm.slice(0); }}, | ||
84 | getPlaneMatrix : { value: function() { return this._planeMat.slice(0); }}, | ||
85 | |||
86 | setPlanarHit : { value: function(p) { this._planarHit = p; }}, | ||
87 | isPlanarHit : { value: function() { return this._planarHit; }}, | ||
88 | |||
89 | getSnapBoundaryIndex : { value: function() { return this._snapBoundaryIndex; }}, | ||
90 | setSnapBoundaryIndex : { value: function(i) { this._snapBoundaryIndex = i; }}, | ||
91 | |||
92 | getType : { value: function() { return this._type; }}, | ||
93 | setType : { value: function(t) {this._type = t; if (!this.checkType()) { throw new Error("invalid snap type"); return; } }}, | ||
94 | |||
95 | getUseQuadPoint : { value: function() { return this._isQuadPt; }}, | ||
96 | setUseQuadPoint : { value: function(q) {this._isQuadPt = q; }}, | ||
97 | |||
98 | /////////////////////////////////////////////////////////////////////// | ||
99 | // Methods | ||
100 | /////////////////////////////////////////////////////////////////////// | ||
101 | |||
102 | checkType :{ | ||
103 | value: function() { | ||
104 | var ok = false; | ||
105 | switch (this._type) | ||
106 | { | ||
107 | case this.SNAP_TYPE_STAGE: | ||
108 | //case this.SNAP_TYPE_GRID: | ||
109 | case this.SNAP_TYPE_GRID_VERTEX: | ||
110 | case this.SNAP_TYPE_GRID_HORIZONTAL: | ||
111 | case this.SNAP_TYPE_GRID_VERTICAL: | ||
112 | case this.SNAP_TYPE_HORIZONTAL_FROM_START: | ||
113 | case this.SNAP_TYPE_VERTICAL_FROM_START: | ||
114 | ok = true; | ||
115 | break; | ||
116 | |||
117 | case this.SNAP_TYPE_ALIGN_MERGED: | ||
118 | ok = true; | ||
119 | break; | ||
120 | |||
121 | case this.SNAP_TYPE_ELEMENT: | ||
122 | case this.SNAP_TYPE_ELEMENT_EDGE: | ||
123 | case this.SNAP_TYPE_ELEMENT_VERTEX: | ||
124 | case this.SNAP_TYPE_ELEMENT_CENTER: | ||
125 | case this.SNAP_TYPE_CONTAINED_ELEMENT: | ||
126 | ok = true; | ||
127 | break; | ||
128 | |||
129 | case this.SNAP_TYPE_ALIGN_HORIZONTAL: | ||
130 | case this.SNAP_TYPE_ALIGN_VERTICAL: | ||
131 | case this.SNAP_TYPE_ALIGN_BOTH: | ||
132 | ok = true; | ||
133 | break; | ||
134 | } | ||
135 | |||
136 | return ok; | ||
137 | } | ||
138 | }, | ||
139 | |||
140 | isSomeGridTypeSnap : { | ||
141 | value: function() { | ||
142 | return | ||
143 | ( | ||
144 | (this._type == this.SNAP_TYPE_GRID_VERTEX) || | ||
145 | (this._type == this.SNAP_TYPE_GRID_HORIZONTAL) || | ||
146 | (this._type == this.SNAP_TYPE_GRID_VERTICAL) | ||
147 | ) | ||
148 | } | ||
149 | }, | ||
150 | |||
151 | convertToWorkingPlane : { | ||
152 | value: function( wp ) { | ||
153 | var swp = this.calculateStageWorldPoint(); | ||
154 | var wpMat = drawUtils.getPlaneToWorldMatrix(wp, MathUtils.getPointOnPlane(wp)); | ||
155 | //var wpMatInv = wpMat.inverse(); | ||
156 | var wpMatInv = glmat4.inverse( wpMat, []); | ||
157 | var localPt = MathUtils.transformPoint( swp, wpMatInv ); | ||
158 | |||
159 | // create a new hit record | ||
160 | var hr = Object.create(HitRecord);//new HitRecord(); | ||
161 | hr.setType( this.SNAP_TYPE_STAGE ); | ||
162 | hr.setScreenPoint( this.getScreenPoint() ); | ||
163 | hr.setLocalPoint( localPt ); | ||
164 | hr.setPlane( wp ); | ||
165 | hr.setPlaneMatrix( wpMat ); | ||
166 | hr.setElt( snapManagerModule.SnapManager.getStage() ); | ||
167 | |||
168 | return hr; | ||
169 | } | ||
170 | }, | ||
171 | |||
172 | calculateStageWorldPoint : { | ||
173 | value: function() { | ||
174 | var wPt; | ||
175 | var elt = this.getElt(); | ||
176 | if (elt != null) | ||
177 | { | ||
178 | var localPt = this.getLocalPoint(); | ||
179 | var planeMat = this.getPlaneMatrix(); | ||
180 | wPt = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt ); | ||
181 | } | ||
182 | |||
183 | return wPt; | ||
184 | } | ||
185 | }, | ||
186 | |||
187 | |||
188 | calculateScreenPoint : { | ||
189 | value: function() { | ||
190 | var scrPt; | ||
191 | |||
192 | var stage = snapManagerModule.SnapManager.getStage(); | ||
193 | var stageMat = viewUtils.getMatrixFromElement( stage ); | ||
194 | var offset = viewUtils.getElementOffset( stage ); | ||
195 | offset[2] = 0; | ||
196 | viewUtils.pushViewportObj( stage ); | ||
197 | |||
198 | var elt = this.getElt(); | ||
199 | if (elt != null) | ||
200 | { | ||
201 | var localPt = this.getLocalPoint(); | ||
202 | var planeMat = this.getPlaneMatrix(); | ||
203 | scrPt = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt ); | ||
204 | scrPt = vecUtils.vecAdd( 3, viewUtils.viewToScreen( MathUtils.transformPoint(scrPt, stageMat) ), offset); | ||
205 | } | ||
206 | viewUtils.popViewportObj(); | ||
207 | |||
208 | return scrPt; | ||
209 | } | ||
210 | }, | ||
211 | |||
212 | calculateElementWorldPoint : { | ||
213 | value: function() { | ||
214 | var localPt = this.getLocalPoint(); | ||
215 | var worldPt = MathUtils.transformPoint( localPt, this.getPlaneMatrix() ); | ||
216 | |||