Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/js/libs/library_godot_input.js
10279 views
1
/**************************************************************************/
2
/* library_godot_input.js */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
/*
32
* IME API helper.
33
*/
34
35
const GodotIME = {
36
$GodotIME__deps: ['$GodotRuntime', '$GodotEventListeners'],
37
$GodotIME__postset: 'GodotOS.atexit(function(resolve, reject) { GodotIME.clear(); resolve(); });',
38
$GodotIME: {
39
ime: null,
40
active: false,
41
focusTimerIntervalId: -1,
42
43
getModifiers: function (evt) {
44
return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
45
},
46
47
ime_active: function (active) {
48
function clearFocusTimerInterval() {
49
clearInterval(GodotIME.focusTimerIntervalId);
50
GodotIME.focusTimerIntervalId = -1;
51
}
52
53
function focusTimer() {
54
if (GodotIME.ime == null) {
55
clearFocusTimerInterval();
56
return;
57
}
58
GodotIME.ime.focus();
59
}
60
61
if (GodotIME.focusTimerIntervalId > -1) {
62
clearFocusTimerInterval();
63
}
64
65
if (GodotIME.ime == null) {
66
return;
67
}
68
69
GodotIME.active = active;
70
if (active) {
71
GodotIME.ime.style.display = 'block';
72
GodotIME.focusTimerIntervalId = setInterval(focusTimer, 100);
73
} else {
74
GodotIME.ime.style.display = 'none';
75
GodotConfig.canvas.focus();
76
}
77
},
78
79
ime_position: function (x, y) {
80
if (GodotIME.ime == null) {
81
return;
82
}
83
const canvas = GodotConfig.canvas;
84
const rect = canvas.getBoundingClientRect();
85
const rw = canvas.width / rect.width;
86
const rh = canvas.height / rect.height;
87
const clx = (x / rw) + rect.x;
88
const cly = (y / rh) + rect.y;
89
90
GodotIME.ime.style.left = `${clx}px`;
91
GodotIME.ime.style.top = `${cly}px`;
92
},
93
94
init: function (ime_cb, key_cb, code, key) {
95
function key_event_cb(pressed, evt) {
96
const modifiers = GodotIME.getModifiers(evt);
97
GodotRuntime.stringToHeap(evt.code, code, 32);
98
GodotRuntime.stringToHeap(evt.key, key, 32);
99
key_cb(pressed, evt.repeat, modifiers);
100
evt.preventDefault();
101
}
102
function ime_event_cb(event) {
103
if (GodotIME.ime == null) {
104
return;
105
}
106
switch (event.type) {
107
case 'compositionstart':
108
ime_cb(0, null);
109
GodotIME.ime.innerHTML = '';
110
break;
111
case 'compositionupdate': {
112
const ptr = GodotRuntime.allocString(event.data);
113
ime_cb(1, ptr);
114
GodotRuntime.free(ptr);
115
} break;
116
case 'compositionend': {
117
const ptr = GodotRuntime.allocString(event.data);
118
ime_cb(2, ptr);
119
GodotRuntime.free(ptr);
120
GodotIME.ime.innerHTML = '';
121
} break;
122
default:
123
// Do nothing.
124
}
125
}
126
127
const ime = document.createElement('div');
128
ime.className = 'ime';
129
ime.style.background = 'none';
130
ime.style.opacity = 0.0;
131
ime.style.position = 'fixed';
132
ime.style.textAlign = 'left';
133
ime.style.fontSize = '1px';
134
ime.style.left = '0px';
135
ime.style.top = '0px';
136
ime.style.width = '100%';
137
ime.style.height = '40px';
138
ime.style.pointerEvents = 'none';
139
ime.style.display = 'none';
140
ime.contentEditable = 'true';
141
142
GodotEventListeners.add(ime, 'compositionstart', ime_event_cb, false);
143
GodotEventListeners.add(ime, 'compositionupdate', ime_event_cb, false);
144
GodotEventListeners.add(ime, 'compositionend', ime_event_cb, false);
145
GodotEventListeners.add(ime, 'keydown', key_event_cb.bind(null, 1), false);
146
GodotEventListeners.add(ime, 'keyup', key_event_cb.bind(null, 0), false);
147
148
ime.onblur = function () {
149
this.style.display = 'none';
150
GodotConfig.canvas.focus();
151
GodotIME.active = false;
152
};
153
154
GodotConfig.canvas.parentElement.appendChild(ime);
155
GodotIME.ime = ime;
156
},
157
158
clear: function () {
159
if (GodotIME.ime == null) {
160
return;
161
}
162
if (GodotIME.focusTimerIntervalId > -1) {
163
clearInterval(GodotIME.focusTimerIntervalId);
164
GodotIME.focusTimerIntervalId = -1;
165
}
166
GodotIME.ime.remove();
167
GodotIME.ime = null;
168
},
169
},
170
};
171
mergeInto(LibraryManager.library, GodotIME);
172
173
/*
174
* Gamepad API helper.
175
*/
176
const GodotInputGamepads = {
177
$GodotInputGamepads__deps: ['$GodotRuntime', '$GodotEventListeners'],
178
$GodotInputGamepads: {
179
samples: [],
180
181
get_pads: function () {
182
try {
183
// Will throw in iframe when permission is denied.
184
// Will throw/warn in the future for insecure contexts.
185
// See https://github.com/w3c/gamepad/pull/120
186
const pads = navigator.getGamepads();
187
if (pads) {
188
return pads;
189
}
190
return [];
191
} catch (e) {
192
return [];
193
}
194
},
195
196
get_samples: function () {
197
return GodotInputGamepads.samples;
198
},
199
200
get_sample: function (index) {
201
const samples = GodotInputGamepads.samples;
202
return index < samples.length ? samples[index] : null;
203
},
204
205
sample: function () {
206
const pads = GodotInputGamepads.get_pads();
207
const samples = [];
208
for (let i = 0; i < pads.length; i++) {
209
const pad = pads[i];
210
if (!pad) {
211
samples.push(null);
212
continue;
213
}
214
const s = {
215
standard: pad.mapping === 'standard',
216
buttons: [],
217
axes: [],
218
connected: pad.connected,
219
};
220
for (let b = 0; b < pad.buttons.length; b++) {
221
s.buttons.push(pad.buttons[b].value);
222
}
223
for (let a = 0; a < pad.axes.length; a++) {
224
s.axes.push(pad.axes[a]);
225
}
226
samples.push(s);
227
}
228
GodotInputGamepads.samples = samples;
229
},
230
231
init: function (onchange) {
232
GodotInputGamepads.samples = [];
233
function add(pad) {
234
const guid = GodotInputGamepads.get_guid(pad);
235
const c_id = GodotRuntime.allocString(pad.id);
236
const c_guid = GodotRuntime.allocString(guid);
237
onchange(pad.index, 1, c_id, c_guid);
238
GodotRuntime.free(c_id);
239
GodotRuntime.free(c_guid);
240
}
241
const pads = GodotInputGamepads.get_pads();
242
for (let i = 0; i < pads.length; i++) {
243
// Might be reserved space.
244
if (pads[i]) {
245
add(pads[i]);
246
}
247
}
248
GodotEventListeners.add(window, 'gamepadconnected', function (evt) {
249
if (evt.gamepad) {
250
add(evt.gamepad);
251
}
252
}, false);
253
GodotEventListeners.add(window, 'gamepaddisconnected', function (evt) {
254
if (evt.gamepad) {
255
onchange(evt.gamepad.index, 0);
256
}
257
}, false);
258
},
259
260
get_guid: function (pad) {
261
if (pad.mapping) {
262
return pad.mapping;
263
}
264
const ua = navigator.userAgent;
265
let os = 'Unknown';
266
if (ua.indexOf('Android') >= 0) {
267
os = 'Android';
268
} else if (ua.indexOf('Linux') >= 0) {
269
os = 'Linux';
270
} else if (ua.indexOf('iPhone') >= 0) {
271
os = 'iOS';
272
} else if (ua.indexOf('Macintosh') >= 0) {
273
// Updated iPads will fall into this category.
274
os = 'MacOSX';
275
} else if (ua.indexOf('Windows') >= 0) {
276
os = 'Windows';
277
}
278
279
const id = pad.id;
280
// Chrom* style: NAME (Vendor: xxxx Product: xxxx).
281
const exp1 = /vendor: ([0-9a-f]{4}) product: ([0-9a-f]{4})/i;
282
// Firefox/Safari style (Safari may remove leading zeroes).
283
const exp2 = /^([0-9a-f]+)-([0-9a-f]+)-/i;
284
let vendor = '';
285
let product = '';
286
if (exp1.test(id)) {
287
const match = exp1.exec(id);
288
vendor = match[1].padStart(4, '0');
289
product = match[2].padStart(4, '0');
290
} else if (exp2.test(id)) {
291
const match = exp2.exec(id);
292
vendor = match[1].padStart(4, '0');
293
product = match[2].padStart(4, '0');
294
}
295
if (!vendor || !product) {
296
return `${os}Unknown`;
297
}
298
return os + vendor + product;
299
},
300
},
301
};
302
mergeInto(LibraryManager.library, GodotInputGamepads);
303
304
/*
305
* Drag and drop helper.
306
* This is pretty big, but basically detect dropped files on GodotConfig.canvas,
307
* process them one by one (recursively for directories), and copies them to
308
* the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot
309
* event (that requires a string array of paths).
310
*
311
* NOTE: The temporary files are removed after the callback. This means that
312
* deferred callbacks won't be able to access the files.
313
*/
314
const GodotInputDragDrop = {
315
$GodotInputDragDrop__deps: ['$FS', '$GodotFS'],
316
$GodotInputDragDrop: {
317
promises: [],
318
pending_files: [],
319
320
add_entry: function (entry) {
321
if (entry.isDirectory) {
322
GodotInputDragDrop.add_dir(entry);
323
} else if (entry.isFile) {
324
GodotInputDragDrop.add_file(entry);
325
} else {
326
GodotRuntime.error('Unrecognized entry...', entry);
327
}
328
},
329
330
add_dir: function (entry) {
331
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
332
const reader = entry.createReader();
333
reader.readEntries(function (entries) {
334
for (let i = 0; i < entries.length; i++) {
335
GodotInputDragDrop.add_entry(entries[i]);
336
}
337
resolve();
338
});
339
}));
340
},
341
342
add_file: function (entry) {
343
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
344
entry.file(function (file) {
345
const reader = new FileReader();
346
reader.onload = function () {
347
const f = {
348
'path': file.relativePath || file.webkitRelativePath,
349
'name': file.name,
350
'type': file.type,
351
'size': file.size,
352
'data': reader.result,
353
};
354
if (!f['path']) {
355
f['path'] = f['name'];
356
}
357
GodotInputDragDrop.pending_files.push(f);
358
resolve();
359
};
360
reader.onerror = function () {
361
GodotRuntime.print('Error reading file');
362
reject();
363
};
364
reader.readAsArrayBuffer(file);
365
}, function (err) {
366
GodotRuntime.print('Error!');
367
reject();
368
});
369
}));
370
},
371
372
process: function (resolve, reject) {
373
if (GodotInputDragDrop.promises.length === 0) {
374
resolve();
375
return;
376
}
377
GodotInputDragDrop.promises.pop().then(function () {
378
setTimeout(function () {
379
GodotInputDragDrop.process(resolve, reject);
380
}, 0);
381
});
382
},
383
384
_process_event: function (ev, callback) {
385
ev.preventDefault();
386
if (ev.dataTransfer.items) {
387
// Use DataTransferItemList interface to access the file(s)
388
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
389
const item = ev.dataTransfer.items[i];
390
let entry = null;
391
if ('getAsEntry' in item) {
392
entry = item.getAsEntry();
393
} else if ('webkitGetAsEntry' in item) {
394
entry = item.webkitGetAsEntry();
395
}
396
if (entry) {
397
GodotInputDragDrop.add_entry(entry);
398
}
399
}
400
} else {
401
GodotRuntime.error('File upload not supported');
402
}
403
new Promise(GodotInputDragDrop.process).then(function () {
404
const DROP = `/tmp/drop-${parseInt(Math.random() * (1 << 30), 10)}/`;
405
const drops = [];
406
const files = [];
407
FS.mkdir(DROP.slice(0, -1)); // Without trailing slash
408
GodotInputDragDrop.pending_files.forEach((elem) => {
409
const path = elem['path'];
410
GodotFS.copy_to_fs(DROP + path, elem['data']);
411
let idx = path.indexOf('/');
412
if (idx === -1) {
413
// Root file
414
drops.push(DROP + path);
415
} else {
416
// Subdir
417
const sub = path.substr(0, idx);
418
idx = sub.indexOf('/');
419
if (idx < 0 && drops.indexOf(DROP + sub) === -1) {
420
drops.push(DROP + sub);
421
}
422
}
423
files.push(DROP + path);
424
});
425
GodotInputDragDrop.promises = [];
426
GodotInputDragDrop.pending_files = [];
427
callback(drops);
428
if (GodotConfig.persistent_drops) {
429
// Delay removal at exit.
430
GodotOS.atexit(function (resolve, reject) {
431
GodotInputDragDrop.remove_drop(files, DROP);
432
resolve();
433
});
434
} else {
435
GodotInputDragDrop.remove_drop(files, DROP);
436
}
437
});
438
},
439
440
remove_drop: function (files, drop_path) {
441
const dirs = [drop_path.substr(0, drop_path.length - 1)];
442
// Remove temporary files
443
files.forEach(function (file) {
444
FS.unlink(file);
445
let dir = file.replace(drop_path, '');
446
let idx = dir.lastIndexOf('/');
447
while (idx > 0) {
448
dir = dir.substr(0, idx);
449
if (dirs.indexOf(drop_path + dir) === -1) {
450
dirs.push(drop_path + dir);
451
}
452
idx = dir.lastIndexOf('/');
453
}
454
});
455
// Remove dirs.
456
dirs.sort(function (a, b) {
457
const al = (a.match(/\//g) || []).length;
458
const bl = (b.match(/\//g) || []).length;
459
if (al > bl) {
460
return -1;
461
} else if (al < bl) {
462
return 1;
463
}
464
return 0;
465
}).forEach(function (dir) {
466
FS.rmdir(dir);
467
});
468
},
469
470
handler: function (callback) {
471
return function (ev) {
472
GodotInputDragDrop._process_event(ev, callback);
473
};
474
},
475
},
476
};
477
mergeInto(LibraryManager.library, GodotInputDragDrop);
478
479
/*
480
* Godot exposed input functions.
481
*/
482
const GodotInput = {
483
$GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners', '$GodotInputGamepads', '$GodotInputDragDrop', '$GodotIME'],
484
$GodotInput: {
485
getModifiers: function (evt) {
486
return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
487
},
488
computePosition: function (evt, rect) {
489
const canvas = GodotConfig.canvas;
490
const rw = canvas.width / rect.width;
491
const rh = canvas.height / rect.height;
492
const x = (evt.clientX - rect.x) * rw;
493
const y = (evt.clientY - rect.y) * rh;
494
return [x, y];
495
},
496
},
497
498
/*
499
* Mouse API
500
*/
501
godot_js_input_mouse_move_cb__proxy: 'sync',
502
godot_js_input_mouse_move_cb__sig: 'vi',
503
godot_js_input_mouse_move_cb: function (callback) {
504
const func = GodotRuntime.get_func(callback);
505
const canvas = GodotConfig.canvas;
506
function move_cb(evt) {
507
const rect = canvas.getBoundingClientRect();
508
const pos = GodotInput.computePosition(evt, rect);
509
// Scale movement
510
const rw = canvas.width / rect.width;
511
const rh = canvas.height / rect.height;
512
const rel_pos_x = evt.movementX * rw;
513
const rel_pos_y = evt.movementY * rh;
514
const modifiers = GodotInput.getModifiers(evt);
515
func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers, evt.pressure);
516
}
517
GodotEventListeners.add(window, 'pointermove', move_cb, false);
518
},
519
520
godot_js_input_mouse_wheel_cb__proxy: 'sync',
521
godot_js_input_mouse_wheel_cb__sig: 'vi',
522
godot_js_input_mouse_wheel_cb: function (callback) {
523
const func = GodotRuntime.get_func(callback);
524
function wheel_cb(evt) {
525
if (func(evt.deltaMode, evt.deltaX ?? 0, evt.deltaY ?? 0)) {
526
evt.preventDefault();
527
}
528
}
529
GodotEventListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false);
530
},
531
532
godot_js_input_mouse_button_cb__proxy: 'sync',
533
godot_js_input_mouse_button_cb__sig: 'vi',
534
godot_js_input_mouse_button_cb: function (callback) {
535
const func = GodotRuntime.get_func(callback);
536
const canvas = GodotConfig.canvas;
537
function button_cb(p_pressed, evt) {
538
const rect = canvas.getBoundingClientRect();
539
const pos = GodotInput.computePosition(evt, rect);
540
const modifiers = GodotInput.getModifiers(evt);
541
// Since the event is consumed, focus manually.
542
// NOTE: The iframe container may not have focus yet, so focus even when already active.
543
if (p_pressed) {
544
GodotConfig.canvas.focus();
545
}
546
if (func(p_pressed, evt.button, pos[0], pos[1], modifiers)) {
547
evt.preventDefault();
548
}
549
}
550
GodotEventListeners.add(canvas, 'mousedown', button_cb.bind(null, 1), false);
551
GodotEventListeners.add(window, 'mouseup', button_cb.bind(null, 0), false);
552
},
553
554
/*
555
* Touch API
556
*/
557
godot_js_input_touch_cb__proxy: 'sync',
558
godot_js_input_touch_cb__sig: 'viii',
559
godot_js_input_touch_cb: function (callback, ids, coords) {
560
const func = GodotRuntime.get_func(callback);
561
const canvas = GodotConfig.canvas;
562
function touch_cb(type, evt) {
563
// Since the event is consumed, focus manually.
564
// NOTE: The iframe container may not have focus yet, so focus even when already active.
565
if (type === 0) {
566
GodotConfig.canvas.focus();
567
}
568
const rect = canvas.getBoundingClientRect();
569
const touches = evt.changedTouches;
570
for (let i = 0; i < touches.length; i++) {
571
const touch = touches[i];
572
const pos = GodotInput.computePosition(touch, rect);
573
GodotRuntime.setHeapValue(coords + (i * 2) * 8, pos[0], 'double');
574
GodotRuntime.setHeapValue(coords + (i * 2 + 1) * 8, pos[1], 'double');
575
GodotRuntime.setHeapValue(ids + i * 4, touch.identifier, 'i32');
576
}
577
func(type, touches.length);
578
if (evt.cancelable) {
579
evt.preventDefault();
580
}
581
}
582
GodotEventListeners.add(canvas, 'touchstart', touch_cb.bind(null, 0), false);
583
GodotEventListeners.add(canvas, 'touchend', touch_cb.bind(null, 1), false);
584
GodotEventListeners.add(canvas, 'touchcancel', touch_cb.bind(null, 1), false);
585
GodotEventListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false);
586
},
587
588
/*
589
* Key API
590
*/
591
godot_js_input_key_cb__proxy: 'sync',
592
godot_js_input_key_cb__sig: 'viii',
593
godot_js_input_key_cb: function (callback, code, key) {
594
const func = GodotRuntime.get_func(callback);
595
function key_cb(pressed, evt) {
596
const modifiers = GodotInput.getModifiers(evt);
597
GodotRuntime.stringToHeap(evt.code, code, 32);
598
GodotRuntime.stringToHeap(evt.key, key, 32);
599
func(pressed, evt.repeat, modifiers);
600
evt.preventDefault();
601
}
602
GodotEventListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false);
603
GodotEventListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false);
604
},
605
606
/*
607
* IME API
608
*/
609
godot_js_set_ime_active__proxy: 'sync',
610
godot_js_set_ime_active__sig: 'vi',
611
godot_js_set_ime_active: function (p_active) {
612
GodotIME.ime_active(p_active);
613
},
614
615
godot_js_set_ime_position__proxy: 'sync',
616
godot_js_set_ime_position__sig: 'vii',
617
godot_js_set_ime_position: function (p_x, p_y) {
618
GodotIME.ime_position(p_x, p_y);
619
},
620
621
godot_js_set_ime_cb__proxy: 'sync',
622
godot_js_set_ime_cb__sig: 'viiii',
623
godot_js_set_ime_cb: function (p_ime_cb, p_key_cb, code, key) {
624
const ime_cb = GodotRuntime.get_func(p_ime_cb);
625
const key_cb = GodotRuntime.get_func(p_key_cb);
626
GodotIME.init(ime_cb, key_cb, code, key);
627
},
628
629
godot_js_is_ime_focused__proxy: 'sync',
630
godot_js_is_ime_focused__sig: 'i',
631
godot_js_is_ime_focused: function () {
632
return GodotIME.active;
633
},
634
635
/*
636
* Gamepad API
637
*/
638
godot_js_input_gamepad_cb__proxy: 'sync',
639
godot_js_input_gamepad_cb__sig: 'vi',
640
godot_js_input_gamepad_cb: function (change_cb) {
641
const onchange = GodotRuntime.get_func(change_cb);
642
GodotInputGamepads.init(onchange);
643
},
644
645
godot_js_input_gamepad_sample_count__proxy: 'sync',
646
godot_js_input_gamepad_sample_count__sig: 'i',
647
godot_js_input_gamepad_sample_count: function () {
648
return GodotInputGamepads.get_samples().length;
649
},
650
651
godot_js_input_gamepad_sample__proxy: 'sync',
652
godot_js_input_gamepad_sample__sig: 'i',
653
godot_js_input_gamepad_sample: function () {
654
GodotInputGamepads.sample();
655
return 0;
656
},
657
658
godot_js_input_gamepad_sample_get__proxy: 'sync',
659
godot_js_input_gamepad_sample_get__sig: 'iiiiiii',
660
godot_js_input_gamepad_sample_get: function (p_index, r_btns, r_btns_num, r_axes, r_axes_num, r_standard) {
661
const sample = GodotInputGamepads.get_sample(p_index);
662
if (!sample || !sample.connected) {
663
return 1;
664
}
665
const btns = sample.buttons;
666
const btns_len = btns.length < 16 ? btns.length : 16;
667
for (let i = 0; i < btns_len; i++) {
668
GodotRuntime.setHeapValue(r_btns + (i << 2), btns[i], 'float');
669
}
670
GodotRuntime.setHeapValue(r_btns_num, btns_len, 'i32');
671
const axes = sample.axes;
672
const axes_len = axes.length < 10 ? axes.length : 10;
673
for (let i = 0; i < axes_len; i++) {
674
GodotRuntime.setHeapValue(r_axes + (i << 2), axes[i], 'float');
675
}
676
GodotRuntime.setHeapValue(r_axes_num, axes_len, 'i32');
677
const is_standard = sample.standard ? 1 : 0;
678
GodotRuntime.setHeapValue(r_standard, is_standard, 'i32');
679
return 0;
680
},
681
682
/*
683
* Drag/Drop API
684
*/
685
godot_js_input_drop_files_cb__proxy: 'sync',
686
godot_js_input_drop_files_cb__sig: 'vi',
687
godot_js_input_drop_files_cb: function (callback) {
688
const func = GodotRuntime.get_func(callback);
689
const dropFiles = function (files) {
690
const args = files || [];
691
if (!args.length) {
692
return;
693
}
694
const argc = args.length;
695
const argv = GodotRuntime.allocStringArray(args);
696
func(argv, argc);
697
GodotRuntime.freeStringArray(argv, argc);
698
};
699
const canvas = GodotConfig.canvas;
700
GodotEventListeners.add(canvas, 'dragover', function (ev) {
701
// Prevent default behavior (which would try to open the file(s))
702
ev.preventDefault();
703
}, false);
704
GodotEventListeners.add(canvas, 'drop', GodotInputDragDrop.handler(dropFiles));
705
},
706
707
/* Paste API */
708
godot_js_input_paste_cb__proxy: 'sync',
709
godot_js_input_paste_cb__sig: 'vi',
710
godot_js_input_paste_cb: function (callback) {
711
const func = GodotRuntime.get_func(callback);
712
GodotEventListeners.add(window, 'paste', function (evt) {
713
const text = evt.clipboardData.getData('text');
714
const ptr = GodotRuntime.allocString(text);
715
func(ptr);
716
GodotRuntime.free(ptr);
717
}, false);
718
},
719
720
godot_js_input_vibrate_handheld__proxy: 'sync',
721
godot_js_input_vibrate_handheld__sig: 'vi',
722
godot_js_input_vibrate_handheld: function (p_duration_ms) {
723
if (typeof navigator.vibrate !== 'function') {
724
GodotRuntime.print('This browser does not support vibration.');
725
} else {
726
navigator.vibrate(p_duration_ms);
727
}
728
},
729
};
730
731
autoAddDeps(GodotInput, '$GodotInput');
732
mergeInto(LibraryManager.library, GodotInput);
733
734