-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathgui.js
More file actions
238 lines (215 loc) · 8.42 KB
/
gui.js
File metadata and controls
238 lines (215 loc) · 8.42 KB
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
(function(){
var scene_key = 'Simple';
window.addEventListener('load', function () {
// Add GUI on scene load
map.scene.subscribe({
load: function (msg) {
addGUI();
}
});
});
var gui;
function addGUI () {
// Remove old GUI
if (gui != null) {
gui.destroy();
}
// Create GUI
gui = new dat.GUI({ autoPlace: true });
gui.domElement.parentNode.style.zIndex = 10000;
window.gui = gui;
scene = window.scene;
setLanguage(gui, scene);
setCamera(gui, scene);
setScene(gui);
setScreenshot(gui, scene);
setMediaRecorder(gui, scene);
setLayers(gui, scene);
}
function setScene(gui) {
// NOTE: using previous version of Mapzen/Nextzen basemaps, until some issues are
// resolved in current versions (syntax errors on shields, etc.)
var scenes = {
// Default style
'Simple': 'demos/scene.yaml',
// Nextzen (nee Mapzen) basemaps
'Bubble Wrap': {
import: [
'https://www.nextzen.org/carto/bubble-wrap-style/10/bubble-wrap-style.zip',
'https://www.nextzen.org/carto/bubble-wrap-style/10/themes/label-10.zip',
'https://www.nextzen.org/carto/bubble-wrap-style/10/themes/bubble-wrap-road-shields-usa.zip',
'https://www.nextzen.org/carto/bubble-wrap-style/10/themes/bubble-wrap-road-shields-international.zip'
]
},
'Walkabout': {
import: [
'https://www.nextzen.org/carto/walkabout-style/8/walkabout-style.zip',
'https://www.nextzen.org/carto/walkabout-style/8/themes/label-10.zip',
'https://www.nextzen.org/carto/walkabout-style/8/themes/walkabout-road-shields-usa.zip',
'https://www.nextzen.org/carto/walkabout-style/8/themes/walkabout-road-shields-international.zip'
]
},
'Refill': {
import: [
'https://www.nextzen.org/carto/refill-style/11/refill-style.zip',
'https://www.nextzen.org/carto/refill-style/11/themes/label-10.zip',
]
},
'Refill Blue Terrain': {
import: [
'https://www.nextzen.org/carto/refill-style/11/refill-style.zip',
'https://www.nextzen.org/carto/refill-style/11/themes/label-10.zip',
'https://www.nextzen.org/carto/refill-style/11/themes/color-blue.zip',
'https://www.nextzen.org/carto/refill-style/11/themes/no-texture.zip',
'https://www.nextzen.org/carto/refill-style/11/themes/terrain-pattern-dark.zip'
]
},
'Tron': {
import: [
'https://www.nextzen.org/carto/tron-style/6/tron-style.zip',
'https://www.nextzen.org/carto/tron-style/6/themes/label-10.zip'
]
},
// Crosshatch style (texture/shader demos)
'Crosshatch': 'demos/styles/crosshatch.zip',
// Fragment shader example
'Rainbow Buildings': {
import: [
'demos/scene.yaml',
'demos/styles/rainbow.yaml'
],
layers: {
buildings: {
polygons: {
draw: {
polygons: { style: 'rainbow' }
},
extruded: {
draw: {
polygons: { style: 'rainbow' }
}
}
}
}
}
},
// Vertex shader example
'Pop-up Buildings': {
import: [
'demos/scene.yaml',
'demos/styles/popup.yaml'
],
layers: {
buildings: {
polygons: {
extruded: {
draw: {
polygons: { style: 'popup' }
}
}
}
}
}
}
};
Object.keys(scenes).forEach(function(s){ scenes[s] = JSON.stringify(scenes[s]) }); // need to stringify JSON for dat.gui :(
gui.scene = scenes[scene_key];
gui.add(gui, 'scene', scenes).onChange(function(value) {
scene_key = Object.keys(scenes).filter(function(s){ return scenes[s] === value })[0]; // find scene from sample list
value = JSON.parse(value); // need to stringify JSON for dat.gui :(
scene.load(value);
});
}
function setLanguage(gui, scene){
var langs = {
'(default)': null,
'English': 'en',
'Russian': 'ru',
'Japanese': 'ja',
'German': 'de',
'French': 'fr',
'Arabic': 'ar',
'Hindi': 'hi',
'Spanish': 'es'
};
// only add if scene supports language
if (scene.config.global.language !== undefined || scene.config.global.ux_language !== undefined) {
gui.language = 'en';
scene.config.global.language = gui.language;
scene.config.global.ux_language = gui.language;
gui.add(gui, 'language', langs).onChange(function(value) {
scene.config.global.language = value; // for bundled demos
scene.config.global.ux_language = value; // for Nextzen basemaps
scene.updateConfig();
});
}
}
function setCamera(gui, scene){
// Only add if scene has all camera types
var cameras = scene.config.cameras;
if (cameras.perspective && cameras.isometric && cameras.flat) {
var camera_types = {
'Flat': 'flat',
'Perspective': 'perspective',
'Isometric': 'isometric'
};
gui.camera = scene.getActiveCamera();
gui.add(gui, 'camera', camera_types).onChange(function(value) {
scene.setActiveCamera(value);
});
}
}
function setScreenshot(gui, scene){
// Take a screenshot and save to file
gui.screenshot = function () {
return scene.screenshot().then(function(screenshot) {
// uses FileSaver.js: https://github.com/eligrey/FileSaver.js/
saveAs(screenshot.blob, 'tangram-' + (+new Date()) + '.png');
});
};
gui.add(gui, 'screenshot');
}
function setMediaRecorder(gui, scene){
// Take a video capture and save to file
if (typeof window.MediaRecorder == 'function') {
gui.video = function () {
if (!gui.video_capture) {
if (scene.startVideoCapture()) {
gui.video_capture = true;
gui.video_button.name('stop video');
}
}
else {
return scene.stopVideoCapture().then(function(video) {
gui.video_capture = false;
gui.video_button.name('capture video');
saveAs(video.blob, 'tangram-video-' + (+new Date()) + '.webm');
});
}
};
gui.video_button = gui.add(gui, 'video');
gui.video_button.name('capture video');
gui.video_capture = false;
}
}
function setLayers(gui, scene){
var layer_gui = gui.addFolder('Layers');
var layer_controls = {};
var layers = scene.config.layers;
for (var key in layers){
setOnChange(key);
}
function setOnChange(key) {
var layer = layers[key];
if (!layer) {
return;
}
layer_controls[key] = !(layer.enabled == false);
layer_gui.add(layer_controls, key)
.onChange(function(value) {
layer.enabled = value;
scene.updateConfig();
});
}
}
})();