-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
373 lines (331 loc) · 14.5 KB
/
index.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
/*
COPYRIGHT 2017 ESRI
-------------------
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:
https://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.
*/
/*
REFERENCE MATERIAL
------------------
ArcGIS API for JavaScript
https://developers.arcgis.com/javascript/latest/api-reference/index.html
The gamepad specification and compatiablity.
https://developer.mozilla.org/en-US/docs/Web/API/Gamepad
Sample application.
http://luser.github.io/gamepadtest/
Source code to sample application.
https://github.com/luser/gamepadtest
*/
/*
WEB SCENES
----------
Phillidephia
5c2293d8f06448f9a05fa508b4f28b9e
San Francisco
53d44be1fd7443a99cf0fbf7d95a2365
*/
require(
[
'esri/Camera',
'esri/WebScene',
'esri/views/SceneView',
'esri/widgets/Home',
'esri/widgets/Search',
'dojo/domReady!'
],
function (
Camera,
WebScene,
SceneView,
Home,
Search,
) {
$(document).ready(function () {
// Enforce strict mode
'use strict';
// Default web scene
var DEFAULT = '53d44be1fd7443a99cf0fbf7d95a2365';
// These constants dictate the speed of angular and linear motion. Adjust as necessary.
var ANGULAR_RATIO = 4;
var LINEAR_RATIO = 0.05;
// Use this variable to store the at-rest values of the joysticks. Older controllers may not be zero.
var origin = null;
var buttons = null;
var index = null;
var zooming = false;
// Extract parsed webscene or fall back on the hardcoded id.
var webscene = DEFAULT;
var vars = getUrlVars();
if (vars && vars.webscene) {
webscene = vars.webscene;
}
// Create a sceneview.
var view = new SceneView({
container: 'map',
map: new WebScene({
portalItem: {
id: webscene
}
}),
ui: {
components: ['attribution', 'compass']
}
});
view.then(function () {
// When the map loads immediately start the game loop.
window.requestAnimationFrame(gameloop);
});
// Add home button.
view.ui.add([
{
component: new Search({
view: view
}),
position: 'top-left',
index: 0
},
{
component: new Home({
view: view
}),
position: 'top-left',
index: 1
}
]);
// Show help popup.
var e = $(document.createElement('img'))
.attr('src', 'img/layout.png')
.css({
height: '150px'
});
$('#button-help').popover({
animation: true,
container: 'body',
content: e.get(0),
html: true,
placement: 'right',
title: 'Controller Configuration',
trigger: 'manual'
});
$('#button-help').popover('show');
$('#button-help').click(function () {
$('#button-help').popover('hide');
});
window.setTimeout(function () {
$('#button-help').popover('hide');
}, 15000);
// Extracts url query strings as a hashcode.
function getUrlVars() {
var vars = [];
var hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
// Parabolic function that will honor the original cardinality.
// This function is used to de- sensitize the xbox axes.
function parabolic(e) {
var p = e * e;
if (e < 0) {
p *= -1;
}
return p;
}
// Zooms to a webscene bookmark.
function openslide() {
if (index === null) {
return;
}
var slide = view.map.presentation.slides.getItemAt(index);
zooming = true;
slide.applyTo(view).then(function () {
zooming = false;
});
}
//var highlight = null;
var start = {
graphic: null,
highlight: null
};
// This function is called every frame.
function gameloop() {
// Skip code if current moving between locations.
if (!zooming) {
// Get all attached gamepads. Do not proceed if none exist.
var gamepads = navigator.getGamepads();
if (gamepads && gamepads.length > 0 && gamepads[0]) {
// For simplicity only use first connected gamepad. Assume xbox 360/one controller.
var xbox = gamepads[0];
// Get Esri camera.
var camera = view.camera;
// If this is the first loop then store the at-rest positions of the axis.
if (!origin) {
origin = xbox.axes.slice();
}
// Get the position of the two xbox axes, apply the origin correction and parabolic curve.
// The parabolic function will make movements near the origin less pronounced than the edges.
var lx = parabolic(xbox.axes[0] - origin[0]);
var ly = parabolic(xbox.axes[1] - origin[1]);
var rx = parabolic(xbox.axes[2] - origin[2]);
var ry = parabolic(xbox.axes[3] - origin[3]);
// Values for the left and right triggers.
var lt = parabolic(xbox.buttons[6].value);
var rt = parabolic(xbox.buttons[7].value);
// Calculate the new heading and tilt.
var heading = camera.heading + rx * ANGULAR_RATIO;
var tilt = camera.tilt - ry * ANGULAR_RATIO;
// Calculate the z-dependant linear ratio.
var speed = camera.position.z * LINEAR_RATIO;
// Calculate the x, y and z.
var x =
camera.position.x +
speed * lx * Math.cos(camera.heading * Math.PI / 180) +
speed * ly * Math.sin(-camera.heading * Math.PI / 180);
var y =
camera.position.y +
speed * lx * Math.sin(-camera.heading * Math.PI / 180) -
speed * ly * Math.cos(camera.heading * Math.PI / 180);
var z =
camera.position.z +
speed * -lt +
speed * rt;
// Assign an autocast camera using the heading, tilt and position calculated above.
view.camera = {
heading: heading,
position: {
x: x,
y: y,
z: z,
spatialReference: {
wkid: 102100
}
},
tilt: tilt
};
// Process button input.
var temp = xbox.buttons.map(function (button) {
return button.pressed;
});
if (buttons !== null) {
for (var i = 0; i < temp.length; i++) {
if (!buttons[i] && temp[i]) {
switch (i) {
// Green (A) button.
case 0:
if (!start || !start.graphic) {
view.popup.close();
return;
}
view.popup.set({
dockEnabled: true,
dockOptions: {
position: 'top-right'
}
});
view.popup.open({
features: [start.graphic]
});
break;
// Red (B) button.
case 1:
// Close popup window.
view.popup.close();
break;
// Menu button. Show/hide help popup.
case 8:
if ($('#modal-help').css('display') === 'none') {
$('#modal-help').modal('show');
}
else {
$('#modal-help').modal('hide');
}
break;
// Start button.
case 9:
origin = xbox.axes.slice();
break;
// Left bumper button
case 4:
if (index === null) {
index = view.map.presentation.slides.length - 1;
} else {
index--;
if (index < 0) {
index = view.map.presentation.slides.length - 1;
}
}
openslide();
break;
// Right bumper button.
case 5:
if (index === null) {
index = 0;
} else {
index++;
if (index > view.map.presentation.slides.length - 1) {
index = 0;
}
}
openslide();
break;
}
}
}
}
buttons = temp;
}
// Highlight graphic
view.hitTest({
x: view.width / 2,
y: view.height / 2
}).then(function (f) {
// Find graphic that intersects center of screen.
if (!f || !f.results || f.results.length === 0) {
if (start.highlight) {
start.highlight.remove();
}
return;
}
var graphic = f.results[0].graphic;
if (!graphic || !graphic.layer) {
if (start.highlight) {
start.highlight.remove();
}
return;
}
// Exit if graphic already selected.
if (start.graphic) {
if (start.graphic === graphic) {
return;
}
}
// Remove current highlight
if (start.highlight) {
start.highlight.remove();
}
// Highlight new graphic. Store reference.
view.whenLayerView(graphic.layer).then(function (v) {
start = {
graphic: graphic,
highlight: v.highlight(graphic)
};
});
});
}
// Lastly request that this function be re-run before the next re-paint.
window.requestAnimationFrame(gameloop);
}
});
}
);