-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
320 lines (265 loc) · 11 KB
/
index.html
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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<title>Pixelc</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="icon180.png">
<link rel="shortcut icon" href="icon196.png" sizes="196x196">
</head>
<style>
/* The emscripten opengl canvas */
#canvas {
position: absolute;
top: 0px;
left: 0px;
margin: 0px;
width: 100%;
height: 100%;
overflow: hidden;
display: block;
border: 0px none;
background-color: black;
}
/* The startup texts... (like downloading) font size according to viewport size */
.startup_text {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
font-size: 4vmin;
color: lightgray;
overflow: hidden;
}
/* input is wrapped in modal_button label */
input[type="file"] { display: none; }
/* The background of modal windows */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: hidden;
background-color: rgb(0, 0, 0); /* Fallback color */
background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
}
/* Modal content/box */
.modal_content {
background-color: #fefefe;
margin-top: 15%;
margin-left: auto;
margin-right: auto;
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}
/* Set the used font size according to the viewport size */
.modal_font {
font-size: 4vmin;
}
/* Set the used font size according to the viewport size */
.modal_font_small {
font-size: 3vmin;
}
/* wraps input file */
.modal_button {
font-size: 4vmin;
border: 2vmin solid #ccc;
display: inline-block;
padding: 3vmin 6vmin;
cursor: pointer;
}
/* The modal window close button (also font size according to the viewport size) */
.modal_content_close {
color: #aaa;
float: right;
font-size: 6vmin;
font-weight: bold;
}
.modal_content_close:hover,
.modal_content_close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
<div id="modal_window_file" class="modal">
<div class="modal_content">
<span class="modal_content_close">×</span>
<p class="modal_font">File Upload</p>
<label id="modal_upload_button" class="modal_button">
<input id="file_uploader" type="file"/>
Upload file
</label>
<label id="modal_upload_png_button" class="modal_button">
<input id="file_uploader_png" type="file" accept="image/png"/>
Upload .png file
</label>
<label id="modal_upload_txt_button" class="modal_button">
<input id="file_uploader_txt" type="file" accept="text/plain"/>
Upload .txt file
</label>
<label id="modal_upload_json_button" class="modal_button">
<input id="file_uploader_json" type="file" accept="application/json"/>
Upload .json file
</label>
<p class="modal_font_small">(Please don't zoom in)</p>
</div>
</div>
<p class="startup_text"></p>
<script type='text/javascript'>
// called from the engine on error
// startup checks
function set_exit_failure_error_msg() {
var newContent = '<!DOCTYPE html><html><body style="background-color:black;"><h1 style="color:white;">Potato Browsers are not supported!</h1><p style="color:silver;">Full WebGL2.0 is needed!</p></body></html>';
document.open();
document.write(newContent);
document.close();
}
// called from s_assume
function set_assume(text) {
var newContent = '<!DOCTYPE html><html><body style="background-color:black;"><h1 style="color:white;">' + text + '</h1></body></html>';
document.open();
document.write(newContent);
document.close();
}
// called from the engine to offer a download file
function offer_file_as_download(filename) {
try {
var mime = "application/octet-stream";
filename_lower = filename.toLowerCase();
if (filename_lower.endsWith(".png"))
mime = "image/png";
else if (filename_lower.endsWith(".txt"))
mime = "text/plain";
else if (filename_lower.endsWith(".json"))
mime = "application/json";
let content = Module.FS.readFile(filename);
console.log(`Offering download of "${filename}", with ${content.length} bytes...`);
var a = document.createElement('a');
a.download = filename;
a.href = URL.createObjectURL(new Blob([content], {type: mime}));
a.style.display = 'none';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}, 2000);
} catch (e) {
console.log('offering file download failed for file: ', filename);
}
}
//
// local file upload stuff, see ask_for_file_upload below
//
var file_upload_filename = 'none.txt'
var file_upload_is_ascii = false;
function handle_file_upload(evt) {
console.log('got a file event upload for: ' + file_upload_filename + ' is_ascii: ' + file_upload_is_ascii);
try {
var files = evt.target.files; // FileList object
console.log('file upload name: ' + files[0].name);
var fr = new FileReader();
fr.onload = function () {
var data = fr.result;
var stream = FS.open(file_upload_filename, 'w');
var data_bytes = new Uint8Array(data);
FS.write(stream, data_bytes, 0, data_bytes.length, 0);
FS.close(stream);
// close the modal window
modal_file = document.getElementById("modal_window_file");
modal_file.style.display = "none";
// call done
ccall('e_io_file_upload_done', 'v', ['string', 'boolean', 'string'],
[file_upload_filename, file_upload_is_ascii, files[0].name],
{async: true});
};
fr.readAsArrayBuffer(files[0]);
} catch (e) {
console.log('file event failed for upload for: ' + file_upload_filename + ' is_ascii: ' + file_upload_is_ascii);
}
}
file_uploader = document.getElementById('file_uploader');
file_uploader_png = document.getElementById('file_uploader_png');
file_uploader_txt = document.getElementById('file_uploader_txt');
file_uploader_json = document.getElementById('file_uploader_json');
file_uploader.addEventListener('change', handle_file_upload, false);
file_uploader_png.addEventListener('change', handle_file_upload, false);
file_uploader_txt.addEventListener('change', handle_file_upload, false);
file_uploader_json.addEventListener('change', handle_file_upload, false);
// called from the engine to ask for a file upload
function ask_for_file_upload(filename, as_ascii) {
// Check for the various File API support.
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
alert('The File APIs are not fully supported in this browser.');
return;
}
console.log(`Ask for file upload for "${filename}"`);
file_upload_filename = filename;
file_upload_is_ascii = as_ascii;
uploader_btn = document.getElementById('modal_upload_button');
uploader_png_btn = document.getElementById('modal_upload_png_button');
uploader_txt_btn = document.getElementById('modal_upload_txt_button');
uploader_json_btn = document.getElementById('modal_upload_json_button');
uploader_btn.style.display = "none";
uploader_png_btn.style.display = "none";
uploader_txt_btn.style.display = "none";
uploader_json_btn.style.display = "none";
filename_lower = filename.toLowerCase();
if (filename_lower.endsWith(".png"))
uploader_png_btn.style.display = "block";
else if (filename_lower.endsWith(".txt"))
uploader_txt_btn.style.display = "block";
else if (filename_lower.endsWith(".json"))
uploader_json_btn.style.display = "block";
else
uploader_btn.style.display = "block";
// open modal box
modal_file = document.getElementById("modal_window_file");
modal_file.style.display = "block";
}
// Get the all file modal windows and its close buttons
var modal_file = document.getElementById("modal_window_file");
var modal_file_close = document.getElementsByClassName("modal_content_close")[0];
// When the user clicks on <span> (x), close the modal
modal_file_close.onclick = function () {
modal_file.style.display = "none";
}
// emscripten module
var Module = {
canvas: (function () {
var canvas = document.getElementById('canvas');
canvas.addEventListener("webglcontextlost", function (e) {
alert('WebGL context lost. You will need to reload the page.');
e.preventDefault();
}, false);
return canvas;
})(),
setStatus: function(text) {
console.log("Module::setStatus: "+ text);
var startup_text = document.getElementsByClassName("startup_text")[0];
if(!text) {
startup_text.style.display = "none";
} else {
startup_text.style.display = "block";
startup_text.innerHTML = text;
}
},
};
window.onerror = function (event) {
set_exit_failure_error_msg();
};
</script>
<script async type="text/javascript" src="index.js"></script>
</body>
</html>