-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBasicWorldWindowController.js
444 lines (391 loc) · 20.1 KB
/
BasicWorldWindowController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright 2003-2006, 2009, 2017, 2020 United States Government, as represented
* by the Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License
* at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
* software:
*
* ES6-Promise – under MIT License
* libtess.js – SGI Free Software License B
* Proj4 – under MIT License
* JSZip – under MIT License
*
* A complete listing of 3rd Party software notices and licenses included in
* WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
* PDF found in code directory.
*/
/**
* @exports BasicWorldWindowController
*/
define([
'./geom/Angle',
'./error/ArgumentError',
'./gesture/ClickRecognizer',
'./gesture/DragRecognizer',
'./gesture/GestureRecognizer',
'./util/Logger',
'./geom/Matrix',
'./gesture/PanRecognizer',
'./gesture/PinchRecognizer',
'./geom/Position',
'./gesture/RotationRecognizer',
'./gesture/TapRecognizer',
'./gesture/TiltRecognizer',
'./geom/Vec2',
'./geom/Vec3',
'./WorldWindowController',
'./util/WWMath'
],
function (Angle,
ArgumentError,
ClickRecognizer,
DragRecognizer,
GestureRecognizer,
Logger,
Matrix,
PanRecognizer,
PinchRecognizer,
Position,
RotationRecognizer,
TapRecognizer,
TiltRecognizer,
Vec2,
Vec3,
WorldWindowController,
WWMath) {
"use strict";
/**
* Constructs a window controller with basic capabilities.
* @alias BasicWorldWindowController
* @constructor
* @augments WorldWindowController
* @classDesc This class provides the default window controller for WorldWind for controlling the globe via user interaction.
* @param {WorldWindow} worldWindow The WorldWindow associated with this layer.
*/
var BasicWorldWindowController = function (worldWindow) {
WorldWindowController.call(this, worldWindow); // base class checks for a valid worldWindow
// Intentionally not documented.
this.primaryDragRecognizer = new DragRecognizer(this.wwd, null);
this.primaryDragRecognizer.addListener(this);
// Intentionally not documented.
this.secondaryDragRecognizer = new DragRecognizer(this.wwd, null);
this.secondaryDragRecognizer.addListener(this);
this.secondaryDragRecognizer.button = 2; // secondary mouse button
// Intentionally not documented.
this.panRecognizer = new PanRecognizer(this.wwd, null);
this.panRecognizer.addListener(this);
// Intentionally not documented.
this.pinchRecognizer = new PinchRecognizer(this.wwd, null);
this.pinchRecognizer.addListener(this);
// Intentionally not documented.
this.rotationRecognizer = new RotationRecognizer(this.wwd, null);
this.rotationRecognizer.addListener(this);
// Intentionally not documented.
this.tiltRecognizer = new TiltRecognizer(this.wwd, null);
this.tiltRecognizer.addListener(this);
// Establish the dependencies between gesture recognizers. The pan, pinch and rotate gesture may recognize
// simultaneously with each other.
this.panRecognizer.recognizeSimultaneouslyWith(this.pinchRecognizer);
this.panRecognizer.recognizeSimultaneouslyWith(this.rotationRecognizer);
this.pinchRecognizer.recognizeSimultaneouslyWith(this.rotationRecognizer);
// Since the tilt gesture is a subset of the pan gesture, pan will typically recognize before tilt,
// effectively suppressing tilt. Establish a dependency between the other touch gestures and tilt to provide
// tilt an opportunity to recognize.
this.panRecognizer.requireRecognizerToFail(this.tiltRecognizer);
this.pinchRecognizer.requireRecognizerToFail(this.tiltRecognizer);
this.rotationRecognizer.requireRecognizerToFail(this.tiltRecognizer);
// Intentionally not documented.
// this.tapRecognizer = new TapRecognizer(this.wwd, null);
// this.tapRecognizer.addListener(this);
// Intentionally not documented.
// this.clickRecognizer = new ClickRecognizer(this.wwd, null);
// this.clickRecognizer.addListener(this);
// Intentionally not documented.
this.beginPoint = new Vec2(0, 0);
this.lastPoint = new Vec2(0, 0);
this.beginHeading = 0;
this.beginTilt = 0;
this.beginRange = 0;
this.lastRotation = 0;
};
BasicWorldWindowController.prototype = Object.create(WorldWindowController.prototype);
// Intentionally not documented.
BasicWorldWindowController.prototype.onGestureEvent = function (e) {
var handled = WorldWindowController.prototype.onGestureEvent.call(this, e);
if (!handled) {
if (e.type === "wheel") {
handled = true;
this.handleWheelEvent(e);
}
else {
for (var i = 0, len = GestureRecognizer.allRecognizers.length; i < len; i++) {
var recognizer = GestureRecognizer.allRecognizers[i];
if (recognizer.target === this.wwd) {
handled |= recognizer.onGestureEvent(e); // use or-assignment to indicate if any recognizer handled the event
}
}
}
}
return handled;
};
// Intentionally not documented.
BasicWorldWindowController.prototype.gestureStateChanged = function (recognizer) {
if (recognizer === this.primaryDragRecognizer || recognizer === this.panRecognizer) {
this.handlePanOrDrag(recognizer);
}
else if (recognizer === this.secondaryDragRecognizer) {
this.handleSecondaryDrag(recognizer);
}
else if (recognizer === this.pinchRecognizer) {
this.handlePinch(recognizer);
}
else if (recognizer === this.rotationRecognizer) {
this.handleRotation(recognizer);
}
else if (recognizer === this.tiltRecognizer) {
this.handleTilt(recognizer);
}
// else if (recognizer === this.clickRecognizer || recognizer === this.tapRecognizer) {
// this.handleClickOrTap(recognizer);
// }
};
// Intentionally not documented.
// BasicWorldWindowController.prototype.handleClickOrTap = function (recognizer) {
// if (recognizer.state === WorldWind.RECOGNIZED) {
// var pickPoint = this.wwd.canvasCoordinates(recognizer.clientX, recognizer.clientY);
//
// // Identify if the top picked object contains a URL for hyperlinking
// var pickList = this.wwd.pick(pickPoint);
// var topObject = pickList.topPickedObject();
// // If the url object was appended, open the hyperlink
// if (topObject &&
// topObject.userObject &&
// topObject.userObject.userProperties &&
// topObject.userObject.userProperties.url) {
// window.open(topObject.userObject.userProperties.url, "_blank");
// }
// }
// };
// Intentionally not documented.
BasicWorldWindowController.prototype.handlePanOrDrag = function (recognizer) {
if (this.wwd.globe.is2D()) {
this.handlePanOrDrag2D(recognizer);
} else {
this.handlePanOrDrag3D(recognizer);
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handlePanOrDrag3D = function (recognizer) {
var state = recognizer.state,
tx = recognizer.translationX,
ty = recognizer.translationY;
var navigator = this.wwd.navigator;
if (state === WorldWind.BEGAN) {
this.lastPoint.set(0, 0);
} else if (state === WorldWind.CHANGED) {
// Convert the translation from screen coordinates to arc degrees. Use this navigator's range as a
// metric for converting screen pixels to meters, and use the globe's radius for converting from meters
// to arc degrees.
var canvas = this.wwd.canvas,
globe = this.wwd.globe,
globeRadius = WWMath.max(globe.equatorialRadius, globe.polarRadius),
distance = WWMath.max(1, navigator.range),
metersPerPixel = WWMath.perspectivePixelSize(canvas.clientWidth, canvas.clientHeight, distance),
forwardMeters = (ty - this.lastPoint[1]) * metersPerPixel,
sideMeters = -(tx - this.lastPoint[0]) * metersPerPixel,
forwardDegrees = (forwardMeters / globeRadius) * Angle.RADIANS_TO_DEGREES,
sideDegrees = (sideMeters / globeRadius) * Angle.RADIANS_TO_DEGREES;
// Apply the change in latitude and longitude to this navigator, relative to the current heading.
var sinHeading = Math.sin(navigator.heading * Angle.DEGREES_TO_RADIANS),
cosHeading = Math.cos(navigator.heading * Angle.DEGREES_TO_RADIANS);
navigator.lookAtLocation.latitude += forwardDegrees * cosHeading - sideDegrees * sinHeading;
navigator.lookAtLocation.longitude += forwardDegrees * sinHeading + sideDegrees * cosHeading;
this.lastPoint.set(tx, ty);
this.applyLimits();
this.wwd.redraw();
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handlePanOrDrag2D = function (recognizer) {
var state = recognizer.state,
x = recognizer.clientX,
y = recognizer.clientY,
tx = recognizer.translationX,
ty = recognizer.translationY;
var navigator = this.wwd.navigator;
if (state === WorldWind.BEGAN) {
this.beginPoint.set(x, y);
this.lastPoint.set(x, y);
} else if (state === WorldWind.CHANGED) {
var x1 = this.lastPoint[0],
y1 = this.lastPoint[1],
x2 = this.beginPoint[0] + tx,
y2 = this.beginPoint[1] + ty;
this.lastPoint.set(x2, y2);
var globe = this.wwd.globe,
ray = this.wwd.rayThroughScreenPoint(this.wwd.canvasCoordinates(x1, y1)),
point1 = new Vec3(0, 0, 0),
point2 = new Vec3(0, 0, 0),
origin = new Vec3(0, 0, 0);
if (!globe.intersectsLine(ray, point1)) {
return;
}
ray = this.wwd.rayThroughScreenPoint(this.wwd.canvasCoordinates(x2, y2));
if (!globe.intersectsLine(ray, point2)) {
return;
}
// Transform the original navigator state's modelview matrix to account for the gesture's change.
var modelview = Matrix.fromIdentity();
this.wwd.computeViewingTransform(null, modelview);
modelview.multiplyByTranslation(point2[0] - point1[0], point2[1] - point1[1], point2[2] - point1[2]);
// Compute the globe point at the screen center from the perspective of the transformed navigator state.
modelview.extractEyePoint(ray.origin);
modelview.extractForwardVector(ray.direction);
if (!globe.intersectsLine(ray, origin)) {
return;
}
// Convert the transformed modelview matrix to a set of navigator properties, then apply those
// properties to this navigator.
var params = modelview.extractViewingParameters(origin, navigator.roll, globe, {});
navigator.lookAtLocation.copy(params.origin);
navigator.range = params.range;
navigator.heading = params.heading;
navigator.tilt = params.tilt;
navigator.roll = params.roll;
this.applyLimits();
this.wwd.redraw();
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handleSecondaryDrag = function (recognizer) {
var state = recognizer.state,
tx = recognizer.translationX,
ty = recognizer.translationY;
var navigator = this.wwd.navigator;
if (state === WorldWind.BEGAN) {
this.beginHeading = navigator.heading;
this.beginTilt = navigator.tilt;
} else if (state === WorldWind.CHANGED) {
// Compute the current translation from screen coordinates to degrees. Use the canvas dimensions as a
// metric for converting the gesture translation to a fraction of an angle.
var headingDegrees = 180 * tx / this.wwd.canvas.clientWidth,
tiltDegrees = 90 * ty / this.wwd.canvas.clientHeight;
// Apply the change in heading and tilt to this navigator's corresponding properties.
navigator.heading = this.beginHeading + headingDegrees;
navigator.tilt = this.beginTilt + tiltDegrees;
this.applyLimits();
this.wwd.redraw();
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handlePinch = function (recognizer) {
var navigator = this.wwd.navigator;
var state = recognizer.state,
scale = recognizer.scale;
if (state === WorldWind.BEGAN) {
this.beginRange = navigator.range;
} else if (state === WorldWind.CHANGED) {
if (scale !== 0) {
// Apply the change in pinch scale to this navigator's range, relative to the range when the gesture
// began.
navigator.range = this.beginRange / scale;
this.applyLimits();
this.wwd.redraw();
}
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handleRotation = function (recognizer) {
var navigator = this.wwd.navigator;
var state = recognizer.state,
rotation = recognizer.rotation;
if (state === WorldWind.BEGAN) {
this.lastRotation = 0;
} else if (state === WorldWind.CHANGED) {
// Apply the change in gesture rotation to this navigator's current heading. We apply relative to the
// current heading rather than the heading when the gesture began in order to work simultaneously with
// pan operations that also modify the current heading.
navigator.heading -= rotation - this.lastRotation;
this.lastRotation = rotation;
this.applyLimits();
this.wwd.redraw();
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handleTilt = function (recognizer) {
var navigator = this.wwd.navigator;
var state = recognizer.state,
ty = recognizer.translationY;
if (state === WorldWind.BEGAN) {
this.beginTilt = navigator.tilt;
} else if (state === WorldWind.CHANGED) {
// Compute the gesture translation from screen coordinates to degrees. Use the canvas dimensions as a
// metric for converting the translation to a fraction of an angle.
var tiltDegrees = -90 * ty / this.wwd.canvas.clientHeight;
// Apply the change in heading and tilt to this navigator's corresponding properties.
navigator.tilt = this.beginTilt + tiltDegrees;
this.applyLimits();
this.wwd.redraw();
}
};
// Intentionally not documented.
BasicWorldWindowController.prototype.handleWheelEvent = function (event) {
var navigator = this.wwd.navigator;
// Normalize the wheel delta based on the wheel delta mode. This produces a roughly consistent delta across
// browsers and input devices.
var normalizedDelta;
if (event.deltaMode === WheelEvent.DOM_DELTA_PIXEL) {
normalizedDelta = event.deltaY;
} else if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) {
normalizedDelta = event.deltaY * 40;
} else if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
normalizedDelta = event.deltaY * 400;
}
// Compute a zoom scale factor by adding a fraction of the normalized delta to 1. When multiplied by the
// navigator's range, this has the effect of zooming out or zooming in depending on whether the delta is
// positive or negative, respectfully.
var scale = 1 + (normalizedDelta / 1000);
// Apply the scale to this navigator's properties.
navigator.range *= scale;
this.applyLimits();
this.wwd.redraw();
};
// Documented in super-class.
BasicWorldWindowController.prototype.applyLimits = function () {
var navigator = this.wwd.navigator;
// Clamp latitude to between -90 and +90, and normalize longitude to between -180 and +180.
navigator.lookAtLocation.latitude = WWMath.clamp(navigator.lookAtLocation.latitude, -90, 90);
navigator.lookAtLocation.longitude = Angle.normalizedDegreesLongitude(navigator.lookAtLocation.longitude);
// Clamp range to values greater than 1 in order to prevent degenerating to a first-person navigator when
// range is zero.
navigator.range = WWMath.clamp(navigator.range, 1, Number.MAX_VALUE);
// Normalize heading to between -180 and +180.
navigator.heading = Angle.normalizedDegrees(navigator.heading);
// Clamp tilt to between 0 and +90 to prevent the viewer from going upside down.
navigator.tilt = WWMath.clamp(navigator.tilt, 0, 90);
// Normalize heading to between -180 and +180.
navigator.roll = Angle.normalizedDegrees(navigator.roll);
// Apply 2D limits when the globe is 2D.
if (this.wwd.globe.is2D() && navigator.enable2DLimits) {
// Clamp range to prevent more than 360 degrees of visible longitude. Assumes a 45 degree horizontal
// field of view.
var maxRange = 2 * Math.PI * this.wwd.globe.equatorialRadius;
navigator.range = WWMath.clamp(navigator.range, 1, maxRange);
// Force tilt to 0 when in 2D mode to keep the viewer looking straight down.
navigator.tilt = 0;
}
};
return BasicWorldWindowController;
}
);