[HTML5] Refactor display/input JS library code.
This commit is contained in:
parent
204822ed45
commit
c494e442bc
|
@ -543,14 +543,14 @@ void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, con
|
|||
|
||||
void DisplayServerJavaScript::process_joypads() {
|
||||
Input *input = Input::get_singleton();
|
||||
int32_t pads = godot_js_display_gamepad_sample_count();
|
||||
int32_t pads = godot_js_input_gamepad_sample_count();
|
||||
int32_t s_btns_num = 0;
|
||||
int32_t s_axes_num = 0;
|
||||
int32_t s_standard = 0;
|
||||
float s_btns[16];
|
||||
float s_axes[10];
|
||||
for (int idx = 0; idx < pads; idx++) {
|
||||
int err = godot_js_display_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
|
||||
int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
|
||||
if (err) {
|
||||
continue;
|
||||
}
|
||||
|
@ -713,11 +713,17 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive
|
|||
video_driver_index = p_video_driver;
|
||||
#endif
|
||||
|
||||
godot_js_display_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
|
||||
godot_js_display_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
|
||||
godot_js_display_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
|
||||
godot_js_display_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
|
||||
godot_js_display_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
|
||||
// JS Input interface (js/libs/library_godot_input.js)
|
||||
godot_js_input_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
|
||||
godot_js_input_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
|
||||
godot_js_input_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
|
||||
godot_js_input_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
|
||||
godot_js_input_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
|
||||
godot_js_input_paste_cb(update_clipboard_callback);
|
||||
godot_js_input_drop_files_cb(drop_files_js_callback);
|
||||
godot_js_input_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
|
||||
|
||||
// JS Display interface (js/libs/library_godot_display.js)
|
||||
godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
|
||||
godot_js_display_window_blur_cb(&window_blur_callback);
|
||||
godot_js_display_notification_cb(&send_window_event_callback,
|
||||
|
@ -725,9 +731,6 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive
|
|||
WINDOW_EVENT_MOUSE_EXIT,
|
||||
WINDOW_EVENT_FOCUS_IN,
|
||||
WINDOW_EVENT_FOCUS_OUT);
|
||||
godot_js_display_paste_cb(update_clipboard_callback);
|
||||
godot_js_display_drop_files_cb(drop_files_js_callback);
|
||||
godot_js_display_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
|
||||
godot_js_display_vk_cb(&vk_input_text_callback);
|
||||
|
||||
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
|
||||
|
@ -950,7 +953,7 @@ bool DisplayServerJavaScript::can_any_window_draw() const {
|
|||
|
||||
void DisplayServerJavaScript::process_events() {
|
||||
Input::get_singleton()->flush_buffered_events();
|
||||
if (godot_js_display_gamepad_sample() == OK) {
|
||||
if (godot_js_input_gamepad_sample() == OK) {
|
||||
process_joypads();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,12 +50,28 @@ extern int godot_js_os_execute(const char *p_json);
|
|||
extern void godot_js_os_shell_open(const char *p_uri);
|
||||
extern int godot_js_os_hw_concurrency_get();
|
||||
|
||||
// Input
|
||||
extern void godot_js_input_mouse_button_cb(int (*p_callback)(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers));
|
||||
extern void godot_js_input_mouse_move_cb(void (*p_callback)(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers));
|
||||
extern void godot_js_input_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y));
|
||||
extern void godot_js_input_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords);
|
||||
extern void godot_js_input_key_cb(void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);
|
||||
|
||||
// Input gamepad
|
||||
extern void godot_js_input_gamepad_cb(void (*p_on_change)(int p_index, int p_connected, const char *p_id, const char *p_guid));
|
||||
extern int godot_js_input_gamepad_sample();
|
||||
extern int godot_js_input_gamepad_sample_count();
|
||||
extern int godot_js_input_gamepad_sample_get(int p_idx, float r_btns[16], int32_t *r_btns_num, float r_axes[10], int32_t *r_axes_num, int32_t *r_standard);
|
||||
extern void godot_js_input_paste_cb(void (*p_callback)(const char *p_text));
|
||||
extern void godot_js_input_drop_files_cb(void (*p_callback)(char **p_filev, int p_filec));
|
||||
|
||||
// Display
|
||||
extern int godot_js_display_screen_dpi_get();
|
||||
extern double godot_js_display_pixel_ratio_get();
|
||||
extern void godot_js_display_alert(const char *p_text);
|
||||
extern int godot_js_display_touchscreen_is_available();
|
||||
extern int godot_js_display_is_swap_ok_cancel();
|
||||
extern void godot_js_display_setup_canvas(int p_width, int p_height, int p_fullscreen, int p_hidpi);
|
||||
|
||||
// Display canvas
|
||||
extern void godot_js_display_canvas_focus();
|
||||
|
@ -84,24 +100,10 @@ extern void godot_js_display_cursor_set_visible(int p_visible);
|
|||
extern void godot_js_display_cursor_lock_set(int p_lock);
|
||||
extern int godot_js_display_cursor_is_locked();
|
||||
|
||||
// Display gamepad
|
||||
extern void godot_js_display_gamepad_cb(void (*p_on_change)(int p_index, int p_connected, const char *p_id, const char *p_guid));
|
||||
extern int godot_js_display_gamepad_sample();
|
||||
extern int godot_js_display_gamepad_sample_count();
|
||||
extern int godot_js_display_gamepad_sample_get(int p_idx, float r_btns[16], int32_t *r_btns_num, float r_axes[10], int32_t *r_axes_num, int32_t *r_standard);
|
||||
|
||||
// Display listeners
|
||||
extern void godot_js_display_mouse_button_cb(int (*p_callback)(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers));
|
||||
extern void godot_js_display_mouse_move_cb(void (*p_callback)(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers));
|
||||
extern void godot_js_display_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y));
|
||||
extern void godot_js_display_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords);
|
||||
extern void godot_js_display_key_cb(void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);
|
||||
extern void godot_js_display_fullscreen_cb(void (*p_callback)(int p_fullscreen));
|
||||
extern void godot_js_display_window_blur_cb(void (*p_callback)());
|
||||
extern void godot_js_display_notification_cb(void (*p_callback)(int p_notification), int p_enter, int p_exit, int p_in, int p_out);
|
||||
extern void godot_js_display_paste_cb(void (*p_callback)(const char *p_text));
|
||||
extern void godot_js_display_drop_files_cb(void (*p_callback)(char **p_filev, int p_filec));
|
||||
extern void godot_js_display_setup_canvas(int p_width, int p_height, int p_fullscreen, int p_hidpi);
|
||||
|
||||
// Display Virtual Keyboard
|
||||
extern int godot_js_display_vk_available();
|
||||
|
|
|
@ -68,181 +68,6 @@ const GodotDisplayListeners = {
|
|||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayListeners);
|
||||
|
||||
/*
|
||||
* Drag and drop handler.
|
||||
* This is pretty big, but basically detect dropped files on GodotConfig.canvas,
|
||||
* process them one by one (recursively for directories), and copies them to
|
||||
* the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot
|
||||
* event (that requires a string array of paths).
|
||||
*
|
||||
* NOTE: The temporary files are removed after the callback. This means that
|
||||
* deferred callbacks won't be able to access the files.
|
||||
*/
|
||||
const GodotDisplayDragDrop = {
|
||||
$GodotDisplayDragDrop__deps: ['$FS', '$GodotFS'],
|
||||
$GodotDisplayDragDrop: {
|
||||
promises: [],
|
||||
pending_files: [],
|
||||
|
||||
add_entry: function (entry) {
|
||||
if (entry.isDirectory) {
|
||||
GodotDisplayDragDrop.add_dir(entry);
|
||||
} else if (entry.isFile) {
|
||||
GodotDisplayDragDrop.add_file(entry);
|
||||
} else {
|
||||
GodotRuntime.error('Unrecognized entry...', entry);
|
||||
}
|
||||
},
|
||||
|
||||
add_dir: function (entry) {
|
||||
GodotDisplayDragDrop.promises.push(new Promise(function (resolve, reject) {
|
||||
const reader = entry.createReader();
|
||||
reader.readEntries(function (entries) {
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
GodotDisplayDragDrop.add_entry(entries[i]);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
add_file: function (entry) {
|
||||
GodotDisplayDragDrop.promises.push(new Promise(function (resolve, reject) {
|
||||
entry.file(function (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
const f = {
|
||||
'path': file.relativePath || file.webkitRelativePath,
|
||||
'name': file.name,
|
||||
'type': file.type,
|
||||
'size': file.size,
|
||||
'data': reader.result,
|
||||
};
|
||||
if (!f['path']) {
|
||||
f['path'] = f['name'];
|
||||
}
|
||||
GodotDisplayDragDrop.pending_files.push(f);
|
||||
resolve();
|
||||
};
|
||||
reader.onerror = function () {
|
||||
GodotRuntime.print('Error reading file');
|
||||
reject();
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}, function (err) {
|
||||
GodotRuntime.print('Error!');
|
||||
reject();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
process: function (resolve, reject) {
|
||||
if (GodotDisplayDragDrop.promises.length === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
GodotDisplayDragDrop.promises.pop().then(function () {
|
||||
setTimeout(function () {
|
||||
GodotDisplayDragDrop.process(resolve, reject);
|
||||
}, 0);
|
||||
});
|
||||
},
|
||||
|
||||
_process_event: function (ev, callback) {
|
||||
ev.preventDefault();
|
||||
if (ev.dataTransfer.items) {
|
||||
// Use DataTransferItemList interface to access the file(s)
|
||||
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
|
||||
const item = ev.dataTransfer.items[i];
|
||||
let entry = null;
|
||||
if ('getAsEntry' in item) {
|
||||
entry = item.getAsEntry();
|
||||
} else if ('webkitGetAsEntry' in item) {
|
||||
entry = item.webkitGetAsEntry();
|
||||
}
|
||||
if (entry) {
|
||||
GodotDisplayDragDrop.add_entry(entry);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
GodotRuntime.error('File upload not supported');
|
||||
}
|
||||
new Promise(GodotDisplayDragDrop.process).then(function () {
|
||||
const DROP = `/tmp/drop-${parseInt(Math.random() * (1 << 30), 10)}/`;
|
||||
const drops = [];
|
||||
const files = [];
|
||||
FS.mkdir(DROP);
|
||||
GodotDisplayDragDrop.pending_files.forEach((elem) => {
|
||||
const path = elem['path'];
|
||||
GodotFS.copy_to_fs(DROP + path, elem['data']);
|
||||
let idx = path.indexOf('/');
|
||||
if (idx === -1) {
|
||||
// Root file
|
||||
drops.push(DROP + path);
|
||||
} else {
|
||||
// Subdir
|
||||
const sub = path.substr(0, idx);
|
||||
idx = sub.indexOf('/');
|
||||
if (idx < 0 && drops.indexOf(DROP + sub) === -1) {
|
||||
drops.push(DROP + sub);
|
||||
}
|
||||
}
|
||||
files.push(DROP + path);
|
||||
});
|
||||
GodotDisplayDragDrop.promises = [];
|
||||
GodotDisplayDragDrop.pending_files = [];
|
||||
callback(drops);
|
||||
if (GodotConfig.persistent_drops) {
|
||||
// Delay removal at exit.
|
||||
GodotOS.atexit(function (resolve, reject) {
|
||||
GodotDisplayDragDrop.remove_drop(files, DROP);
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
GodotDisplayDragDrop.remove_drop(files, DROP);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
remove_drop: function (files, drop_path) {
|
||||
const dirs = [drop_path.substr(0, drop_path.length - 1)];
|
||||
// Remove temporary files
|
||||
files.forEach(function (file) {
|
||||
FS.unlink(file);
|
||||
let dir = file.replace(drop_path, '');
|
||||
let idx = dir.lastIndexOf('/');
|
||||
while (idx > 0) {
|
||||
dir = dir.substr(0, idx);
|
||||
if (dirs.indexOf(drop_path + dir) === -1) {
|
||||
dirs.push(drop_path + dir);
|
||||
}
|
||||
idx = dir.lastIndexOf('/');
|
||||
}
|
||||
});
|
||||
// Remove dirs.
|
||||
dirs.sort(function (a, b) {
|
||||
const al = (a.match(/\//g) || []).length;
|
||||
const bl = (b.match(/\//g) || []).length;
|
||||
if (al > bl) {
|
||||
return -1;
|
||||
} else if (al < bl) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}).forEach(function (dir) {
|
||||
FS.rmdir(dir);
|
||||
});
|
||||
},
|
||||
|
||||
handler: function (callback) {
|
||||
return function (ev) {
|
||||
GodotDisplayDragDrop._process_event(ev, callback);
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayDragDrop);
|
||||
|
||||
const GodotDisplayVK = {
|
||||
|
||||
$GodotDisplayVK__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners'],
|
||||
|
@ -394,133 +219,6 @@ const GodotDisplayCursor = {
|
|||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayCursor);
|
||||
|
||||
/*
|
||||
* Display Gamepad API helper.
|
||||
*/
|
||||
const GodotDisplayGamepads = {
|
||||
$GodotDisplayGamepads__deps: ['$GodotRuntime', '$GodotDisplayListeners'],
|
||||
$GodotDisplayGamepads: {
|
||||
samples: [],
|
||||
|
||||
get_pads: function () {
|
||||
try {
|
||||
// Will throw in iframe when permission is denied.
|
||||
// Will throw/warn in the future for insecure contexts.
|
||||
// See https://github.com/w3c/gamepad/pull/120
|
||||
const pads = navigator.getGamepads();
|
||||
if (pads) {
|
||||
return pads;
|
||||
}
|
||||
return [];
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
get_samples: function () {
|
||||
return GodotDisplayGamepads.samples;
|
||||
},
|
||||
|
||||
get_sample: function (index) {
|
||||
const samples = GodotDisplayGamepads.samples;
|
||||
return index < samples.length ? samples[index] : null;
|
||||
},
|
||||
|
||||
sample: function () {
|
||||
const pads = GodotDisplayGamepads.get_pads();
|
||||
const samples = [];
|
||||
for (let i = 0; i < pads.length; i++) {
|
||||
const pad = pads[i];
|
||||
if (!pad) {
|
||||
samples.push(null);
|
||||
continue;
|
||||
}
|
||||
const s = {
|
||||
standard: pad.mapping === 'standard',
|
||||
buttons: [],
|
||||
axes: [],
|
||||
connected: pad.connected,
|
||||
};
|
||||
for (let b = 0; b < pad.buttons.length; b++) {
|
||||
s.buttons.push(pad.buttons[b].value);
|
||||
}
|
||||
for (let a = 0; a < pad.axes.length; a++) {
|
||||
s.axes.push(pad.axes[a]);
|
||||
}
|
||||
samples.push(s);
|
||||
}
|
||||
GodotDisplayGamepads.samples = samples;
|
||||
},
|
||||
|
||||
init: function (onchange) {
|
||||
GodotDisplayListeners.samples = [];
|
||||
function add(pad) {
|
||||
const guid = GodotDisplayGamepads.get_guid(pad);
|
||||
const c_id = GodotRuntime.allocString(pad.id);
|
||||
const c_guid = GodotRuntime.allocString(guid);
|
||||
onchange(pad.index, 1, c_id, c_guid);
|
||||
GodotRuntime.free(c_id);
|
||||
GodotRuntime.free(c_guid);
|
||||
}
|
||||
const pads = GodotDisplayGamepads.get_pads();
|
||||
for (let i = 0; i < pads.length; i++) {
|
||||
// Might be reserved space.
|
||||
if (pads[i]) {
|
||||
add(pads[i]);
|
||||
}
|
||||
}
|
||||
GodotDisplayListeners.add(window, 'gamepadconnected', function (evt) {
|
||||
add(evt.gamepad);
|
||||
}, false);
|
||||
GodotDisplayListeners.add(window, 'gamepaddisconnected', function (evt) {
|
||||
onchange(evt.gamepad.index, 0);
|
||||
}, false);
|
||||
},
|
||||
|
||||
get_guid: function (pad) {
|
||||
if (pad.mapping) {
|
||||
return pad.mapping;
|
||||
}
|
||||
const ua = navigator.userAgent;
|
||||
let os = 'Unknown';
|
||||
if (ua.indexOf('Android') >= 0) {
|
||||
os = 'Android';
|
||||
} else if (ua.indexOf('Linux') >= 0) {
|
||||
os = 'Linux';
|
||||
} else if (ua.indexOf('iPhone') >= 0) {
|
||||
os = 'iOS';
|
||||
} else if (ua.indexOf('Macintosh') >= 0) {
|
||||
// Updated iPads will fall into this category.
|
||||
os = 'MacOSX';
|
||||
} else if (ua.indexOf('Windows') >= 0) {
|
||||
os = 'Windows';
|
||||
}
|
||||
|
||||
const id = pad.id;
|
||||
// Chrom* style: NAME (Vendor: xxxx Product: xxxx)
|
||||
const exp1 = /vendor: ([0-9a-f]{4}) product: ([0-9a-f]{4})/i;
|
||||
// Firefox/Safari style (safari may remove leading zeores)
|
||||
const exp2 = /^([0-9a-f]+)-([0-9a-f]+)-/i;
|
||||
let vendor = '';
|
||||
let product = '';
|
||||
if (exp1.test(id)) {
|
||||
const match = exp1.exec(id);
|
||||
vendor = match[1].padStart(4, '0');
|
||||
product = match[2].padStart(4, '0');
|
||||
} else if (exp2.test(id)) {
|
||||
const match = exp2.exec(id);
|
||||
vendor = match[1].padStart(4, '0');
|
||||
product = match[2].padStart(4, '0');
|
||||
}
|
||||
if (!vendor || !product) {
|
||||
return `${os}Unknown`;
|
||||
}
|
||||
return os + vendor + product;
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotDisplayGamepads);
|
||||
|
||||
const GodotDisplayScreen = {
|
||||
$GodotDisplayScreen__deps: ['$GodotConfig', '$GodotOS', '$GL', 'emscripten_webgl_get_current_context'],
|
||||
$GodotDisplayScreen: {
|
||||
|
@ -636,7 +334,7 @@ mergeInto(LibraryManager.library, GodotDisplayScreen);
|
|||
* Exposes all the functions needed by DisplayServer implementation.
|
||||
*/
|
||||
const GodotDisplay = {
|
||||
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayDragDrop', '$GodotDisplayGamepads', '$GodotDisplayScreen', '$GodotDisplayVK'],
|
||||
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
|
||||
$GodotDisplay: {
|
||||
window_icon: '',
|
||||
findDPI: function () {
|
||||
|
@ -915,38 +613,6 @@ const GodotDisplay = {
|
|||
});
|
||||
},
|
||||
|
||||
godot_js_display_paste_cb__sig: 'vi',
|
||||
godot_js_display_paste_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
GodotDisplayListeners.add(window, 'paste', function (evt) {
|
||||
const text = evt.clipboardData.getData('text');
|
||||
const ptr = GodotRuntime.allocString(text);
|
||||
func(ptr);
|
||||
GodotRuntime.free(ptr);
|
||||
}, false);
|
||||
},
|
||||
|
||||
godot_js_display_drop_files_cb__sig: 'vi',
|
||||
godot_js_display_drop_files_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
const dropFiles = function (files) {
|
||||
const args = files || [];
|
||||
if (!args.length) {
|
||||
return;
|
||||
}
|
||||
const argc = args.length;
|
||||
const argv = GodotRuntime.allocStringArray(args);
|
||||
func(argv, argc);
|
||||
GodotRuntime.freeStringArray(argv, argc);
|
||||
};
|
||||
const canvas = GodotConfig.canvas;
|
||||
GodotDisplayListeners.add(canvas, 'dragover', function (ev) {
|
||||
// Prevent default behavior (which would try to open the file(s))
|
||||
ev.preventDefault();
|
||||
}, false);
|
||||
GodotDisplayListeners.add(canvas, 'drop', GodotDisplayDragDrop.handler(dropFiles));
|
||||
},
|
||||
|
||||
godot_js_display_setup_canvas__sig: 'viiii',
|
||||
godot_js_display_setup_canvas: function (p_width, p_height, p_fullscreen, p_hidpi) {
|
||||
const canvas = GodotConfig.canvas;
|
||||
|
@ -1006,49 +672,6 @@ const GodotDisplay = {
|
|||
GodotDisplayVK.init(input_cb);
|
||||
}
|
||||
},
|
||||
|
||||
/*
|
||||
* Gamepads
|
||||
*/
|
||||
godot_js_display_gamepad_cb__sig: 'vi',
|
||||
godot_js_display_gamepad_cb: function (change_cb) {
|
||||
const onchange = GodotRuntime.get_func(change_cb);
|
||||
GodotDisplayGamepads.init(onchange);
|
||||
},
|
||||
|
||||
godot_js_display_gamepad_sample_count__sig: 'i',
|
||||
godot_js_display_gamepad_sample_count: function () {
|
||||
return GodotDisplayGamepads.get_samples().length;
|
||||
},
|
||||
|
||||
godot_js_display_gamepad_sample__sig: 'i',
|
||||
godot_js_display_gamepad_sample: function () {
|
||||
GodotDisplayGamepads.sample();
|
||||
return 0;
|
||||
},
|
||||
|
||||
godot_js_display_gamepad_sample_get__sig: 'iiiiiii',
|
||||
godot_js_display_gamepad_sample_get: function (p_index, r_btns, r_btns_num, r_axes, r_axes_num, r_standard) {
|
||||
const sample = GodotDisplayGamepads.get_sample(p_index);
|
||||
if (!sample || !sample.connected) {
|
||||
return 1;
|
||||
}
|
||||
const btns = sample.buttons;
|
||||
const btns_len = btns.length < 16 ? btns.length : 16;
|
||||
for (let i = 0; i < btns_len; i++) {
|
||||
GodotRuntime.setHeapValue(r_btns + (i << 2), btns[i], 'float');
|
||||
}
|
||||
GodotRuntime.setHeapValue(r_btns_num, btns_len, 'i32');
|
||||
const axes = sample.axes;
|
||||
const axes_len = axes.length < 10 ? axes.length : 10;
|
||||
for (let i = 0; i < axes_len; i++) {
|
||||
GodotRuntime.setHeapValue(r_axes + (i << 2), axes[i], 'float');
|
||||
}
|
||||
GodotRuntime.setHeapValue(r_axes_num, axes_len, 'i32');
|
||||
const is_standard = sample.standard ? 1 : 0;
|
||||
GodotRuntime.setHeapValue(r_standard, is_standard, 'i32');
|
||||
return 0;
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(GodotDisplay, '$GodotDisplay');
|
||||
|
|
|
@ -28,8 +28,313 @@
|
|||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
/*
|
||||
* Gamepad API helper.
|
||||
*/
|
||||
const GodotInputGamepads = {
|
||||
$GodotInputGamepads__deps: ['$GodotRuntime', '$GodotDisplayListeners'],
|
||||
$GodotInputGamepads: {
|
||||
samples: [],
|
||||
|
||||
get_pads: function () {
|
||||
try {
|
||||
// Will throw in iframe when permission is denied.
|
||||
// Will throw/warn in the future for insecure contexts.
|
||||
// See https://github.com/w3c/gamepad/pull/120
|
||||
const pads = navigator.getGamepads();
|
||||
if (pads) {
|
||||
return pads;
|
||||
}
|
||||
return [];
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
get_samples: function () {
|
||||
return GodotInputGamepads.samples;
|
||||
},
|
||||
|
||||
get_sample: function (index) {
|
||||
const samples = GodotInputGamepads.samples;
|
||||
return index < samples.length ? samples[index] : null;
|
||||
},
|
||||
|
||||
sample: function () {
|
||||
const pads = GodotInputGamepads.get_pads();
|
||||
const samples = [];
|
||||
for (let i = 0; i < pads.length; i++) {
|
||||
const pad = pads[i];
|
||||
if (!pad) {
|
||||
samples.push(null);
|
||||
continue;
|
||||
}
|
||||
const s = {
|
||||
standard: pad.mapping === 'standard',
|
||||
buttons: [],
|
||||
axes: [],
|
||||
connected: pad.connected,
|
||||
};
|
||||
for (let b = 0; b < pad.buttons.length; b++) {
|
||||
s.buttons.push(pad.buttons[b].value);
|
||||
}
|
||||
for (let a = 0; a < pad.axes.length; a++) {
|
||||
s.axes.push(pad.axes[a]);
|
||||
}
|
||||
samples.push(s);
|
||||
}
|
||||
GodotInputGamepads.samples = samples;
|
||||
},
|
||||
|
||||
init: function (onchange) {
|
||||
GodotDisplayListeners.samples = [];
|
||||
function add(pad) {
|
||||
const guid = GodotInputGamepads.get_guid(pad);
|
||||
const c_id = GodotRuntime.allocString(pad.id);
|
||||
const c_guid = GodotRuntime.allocString(guid);
|
||||
onchange(pad.index, 1, c_id, c_guid);
|
||||
GodotRuntime.free(c_id);
|
||||
GodotRuntime.free(c_guid);
|
||||
}
|
||||
const pads = GodotInputGamepads.get_pads();
|
||||
for (let i = 0; i < pads.length; i++) {
|
||||
// Might be reserved space.
|
||||
if (pads[i]) {
|
||||
add(pads[i]);
|
||||
}
|
||||
}
|
||||
GodotDisplayListeners.add(window, 'gamepadconnected', function (evt) {
|
||||
add(evt.gamepad);
|
||||
}, false);
|
||||
GodotDisplayListeners.add(window, 'gamepaddisconnected', function (evt) {
|
||||
onchange(evt.gamepad.index, 0);
|
||||
}, false);
|
||||
},
|
||||
|
||||
get_guid: function (pad) {
|
||||
if (pad.mapping) {
|
||||
return pad.mapping;
|
||||
}
|
||||
const ua = navigator.userAgent;
|
||||
let os = 'Unknown';
|
||||
if (ua.indexOf('Android') >= 0) {
|
||||
os = 'Android';
|
||||
} else if (ua.indexOf('Linux') >= 0) {
|
||||
os = 'Linux';
|
||||
} else if (ua.indexOf('iPhone') >= 0) {
|
||||
os = 'iOS';
|
||||
} else if (ua.indexOf('Macintosh') >= 0) {
|
||||
// Updated iPads will fall into this category.
|
||||
os = 'MacOSX';
|
||||
} else if (ua.indexOf('Windows') >= 0) {
|
||||
os = 'Windows';
|
||||
}
|
||||
|
||||
const id = pad.id;
|
||||
// Chrom* style: NAME (Vendor: xxxx Product: xxxx)
|
||||
const exp1 = /vendor: ([0-9a-f]{4}) product: ([0-9a-f]{4})/i;
|
||||
// Firefox/Safari style (safari may remove leading zeores)
|
||||
const exp2 = /^([0-9a-f]+)-([0-9a-f]+)-/i;
|
||||
let vendor = '';
|
||||
let product = '';
|
||||
if (exp1.test(id)) {
|
||||
const match = exp1.exec(id);
|
||||
vendor = match[1].padStart(4, '0');
|
||||
product = match[2].padStart(4, '0');
|
||||
} else if (exp2.test(id)) {
|
||||
const match = exp2.exec(id);
|
||||
vendor = match[1].padStart(4, '0');
|
||||
product = match[2].padStart(4, '0');
|
||||
}
|
||||
if (!vendor || !product) {
|
||||
return `${os}Unknown`;
|
||||
}
|
||||
return os + vendor + product;
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotInputGamepads);
|
||||
|
||||
/*
|
||||
* Drag and drop helper.
|
||||
* This is pretty big, but basically detect dropped files on GodotConfig.canvas,
|
||||
* process them one by one (recursively for directories), and copies them to
|
||||
* the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot
|
||||
* event (that requires a string array of paths).
|
||||
*
|
||||
* NOTE: The temporary files are removed after the callback. This means that
|
||||
* deferred callbacks won't be able to access the files.
|
||||
*/
|
||||
const GodotInputDragDrop = {
|
||||
$GodotInputDragDrop__deps: ['$FS', '$GodotFS'],
|
||||
$GodotInputDragDrop: {
|
||||
promises: [],
|
||||
pending_files: [],
|
||||
|
||||
add_entry: function (entry) {
|
||||
if (entry.isDirectory) {
|
||||
GodotInputDragDrop.add_dir(entry);
|
||||
} else if (entry.isFile) {
|
||||
GodotInputDragDrop.add_file(entry);
|
||||
} else {
|
||||
GodotRuntime.error('Unrecognized entry...', entry);
|
||||
}
|
||||
},
|
||||
|
||||
add_dir: function (entry) {
|
||||
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
|
||||
const reader = entry.createReader();
|
||||
reader.readEntries(function (entries) {
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
GodotInputDragDrop.add_entry(entries[i]);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
add_file: function (entry) {
|
||||
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
|
||||
entry.file(function (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
const f = {
|
||||
'path': file.relativePath || file.webkitRelativePath,
|
||||
'name': file.name,
|
||||
'type': file.type,
|
||||
'size': file.size,
|
||||
'data': reader.result,
|
||||
};
|
||||
if (!f['path']) {
|
||||
f['path'] = f['name'];
|
||||
}
|
||||
GodotInputDragDrop.pending_files.push(f);
|
||||
resolve();
|
||||
};
|
||||
reader.onerror = function () {
|
||||
GodotRuntime.print('Error reading file');
|
||||
reject();
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}, function (err) {
|
||||
GodotRuntime.print('Error!');
|
||||
reject();
|
||||
});
|
||||
}));
|
||||
},
|
||||
|
||||
process: function (resolve, reject) {
|
||||
if (GodotInputDragDrop.promises.length === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
GodotInputDragDrop.promises.pop().then(function () {
|
||||
setTimeout(function () {
|
||||
GodotInputDragDrop.process(resolve, reject);
|
||||
}, 0);
|
||||
});
|
||||
},
|
||||
|
||||
_process_event: function (ev, callback) {
|
||||
ev.preventDefault();
|
||||
if (ev.dataTransfer.items) {
|
||||
// Use DataTransferItemList interface to access the file(s)
|
||||
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
|
||||
const item = ev.dataTransfer.items[i];
|
||||
let entry = null;
|
||||
if ('getAsEntry' in item) {
|
||||
entry = item.getAsEntry();
|
||||
} else if ('webkitGetAsEntry' in item) {
|
||||
entry = item.webkitGetAsEntry();
|
||||
}
|
||||
if (entry) {
|
||||
GodotInputDragDrop.add_entry(entry);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
GodotRuntime.error('File upload not supported');
|
||||
}
|
||||
new Promise(GodotInputDragDrop.process).then(function () {
|
||||
const DROP = `/tmp/drop-${parseInt(Math.random() * (1 << 30), 10)}/`;
|
||||
const drops = [];
|
||||
const files = [];
|
||||
FS.mkdir(DROP);
|
||||
GodotInputDragDrop.pending_files.forEach((elem) => {
|
||||
const path = elem['path'];
|
||||
GodotFS.copy_to_fs(DROP + path, elem['data']);
|
||||
let idx = path.indexOf('/');
|
||||
if (idx === -1) {
|
||||
// Root file
|
||||
drops.push(DROP + path);
|
||||
} else {
|
||||
// Subdir
|
||||
const sub = path.substr(0, idx);
|
||||
idx = sub.indexOf('/');
|
||||
if (idx < 0 && drops.indexOf(DROP + sub) === -1) {
|
||||
drops.push(DROP + sub);
|
||||
}
|
||||
}
|
||||
files.push(DROP + path);
|
||||
});
|
||||
GodotInputDragDrop.promises = [];
|
||||
GodotInputDragDrop.pending_files = [];
|
||||
callback(drops);
|
||||
if (GodotConfig.persistent_drops) {
|
||||
// Delay removal at exit.
|
||||
GodotOS.atexit(function (resolve, reject) {
|
||||
GodotInputDragDrop.remove_drop(files, DROP);
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
GodotInputDragDrop.remove_drop(files, DROP);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
remove_drop: function (files, drop_path) {
|
||||
const dirs = [drop_path.substr(0, drop_path.length - 1)];
|
||||
// Remove temporary files
|
||||
files.forEach(function (file) {
|
||||
FS.unlink(file);
|
||||
let dir = file.replace(drop_path, '');
|
||||
let idx = dir.lastIndexOf('/');
|
||||
while (idx > 0) {
|
||||
dir = dir.substr(0, idx);
|
||||
if (dirs.indexOf(drop_path + dir) === -1) {
|
||||
dirs.push(drop_path + dir);
|
||||
}
|
||||
idx = dir.lastIndexOf('/');
|
||||
}
|
||||
});
|
||||
// Remove dirs.
|
||||
dirs.sort(function (a, b) {
|
||||
const al = (a.match(/\//g) || []).length;
|
||||
const bl = (b.match(/\//g) || []).length;
|
||||
if (al > bl) {
|
||||
return -1;
|
||||
} else if (al < bl) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}).forEach(function (dir) {
|
||||
FS.rmdir(dir);
|
||||
});
|
||||
},
|
||||
|
||||
handler: function (callback) {
|
||||
return function (ev) {
|
||||
GodotInputDragDrop._process_event(ev, callback);
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
mergeInto(LibraryManager.library, GodotInputDragDrop);
|
||||
|
||||
/*
|
||||
* Godot exposed input functions.
|
||||
*/
|
||||
const GodotInput = {
|
||||
$GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners'],
|
||||
$GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners', '$GodotInputGamepads', '$GodotInputDragDrop'],
|
||||
$GodotInput: {
|
||||
getModifiers: function (evt) {
|
||||
return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
|
||||
|
@ -44,8 +349,11 @@ const GodotInput = {
|
|||
},
|
||||
},
|
||||
|
||||
godot_js_display_mouse_move_cb__sig: 'vi',
|
||||
godot_js_display_mouse_move_cb: function (callback) {
|
||||
/*
|
||||
* Mouse API
|
||||
*/
|
||||
godot_js_input_mouse_move_cb__sig: 'vi',
|
||||
godot_js_input_mouse_move_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
const canvas = GodotConfig.canvas;
|
||||
function move_cb(evt) {
|
||||
|
@ -62,8 +370,8 @@ const GodotInput = {
|
|||
GodotDisplayListeners.add(window, 'mousemove', move_cb, false);
|
||||
},
|
||||
|
||||
godot_js_display_mouse_wheel_cb__sig: 'vi',
|
||||
godot_js_display_mouse_wheel_cb: function (callback) {
|
||||
godot_js_input_mouse_wheel_cb__sig: 'vi',
|
||||
godot_js_input_mouse_wheel_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
function wheel_cb(evt) {
|
||||
if (func(evt['deltaX'] || 0, evt['deltaY'] || 0)) {
|
||||
|
@ -73,8 +381,8 @@ const GodotInput = {
|
|||
GodotDisplayListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false);
|
||||
},
|
||||
|
||||
godot_js_display_mouse_button_cb__sig: 'vi',
|
||||
godot_js_display_mouse_button_cb: function (callback) {
|
||||
godot_js_input_mouse_button_cb__sig: 'vi',
|
||||
godot_js_input_mouse_button_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
const canvas = GodotConfig.canvas;
|
||||
function button_cb(p_pressed, evt) {
|
||||
|
@ -89,8 +397,11 @@ const GodotInput = {
|
|||
GodotDisplayListeners.add(window, 'mouseup', button_cb.bind(null, 0), false);
|
||||
},
|
||||
|
||||
godot_js_display_touch_cb__sig: 'viii',
|
||||
godot_js_display_touch_cb: function (callback, ids, coords) {
|
||||
/*
|
||||
* Touch API
|
||||
*/
|
||||
godot_js_input_touch_cb__sig: 'viii',
|
||||
godot_js_input_touch_cb: function (callback, ids, coords) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
const canvas = GodotConfig.canvas;
|
||||
function touch_cb(type, evt) {
|
||||
|
@ -114,8 +425,11 @@ const GodotInput = {
|
|||
GodotDisplayListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false);
|
||||
},
|
||||
|
||||
godot_js_display_key_cb__sig: 'viii',
|
||||
godot_js_display_key_cb: function (callback, code, key) {
|
||||
/*
|
||||
* Key API
|
||||
*/
|
||||
godot_js_input_key_cb__sig: 'viii',
|
||||
godot_js_input_key_cb: function (callback, code, key) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
function key_cb(pressed, evt) {
|
||||
const modifiers = GodotInput.getModifiers(evt);
|
||||
|
@ -127,6 +441,85 @@ const GodotInput = {
|
|||
GodotDisplayListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false);
|
||||
GodotDisplayListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false);
|
||||
},
|
||||
|
||||
/*
|
||||
* Gamepad API
|
||||
*/
|
||||
godot_js_input_gamepad_cb__sig: 'vi',
|
||||
godot_js_input_gamepad_cb: function (change_cb) {
|
||||
const onchange = GodotRuntime.get_func(change_cb);
|
||||
GodotInputGamepads.init(onchange);
|
||||
},
|
||||
|
||||
godot_js_input_gamepad_sample_count__sig: 'i',
|
||||
godot_js_input_gamepad_sample_count: function () {
|
||||
return GodotInputGamepads.get_samples().length;
|
||||
},
|
||||
|
||||
godot_js_input_gamepad_sample__sig: 'i',
|
||||
godot_js_input_gamepad_sample: function () {
|
||||
GodotInputGamepads.sample();
|
||||
return 0;
|
||||
},
|
||||
|
||||
godot_js_input_gamepad_sample_get__sig: 'iiiiiii',
|
||||
godot_js_input_gamepad_sample_get: function (p_index, r_btns, r_btns_num, r_axes, r_axes_num, r_standard) {
|
||||
const sample = GodotInputGamepads.get_sample(p_index);
|
||||
if (!sample || !sample.connected) {
|
||||
return 1;
|
||||
}
|
||||
const btns = sample.buttons;
|
||||
const btns_len = btns.length < 16 ? btns.length : 16;
|
||||
for (let i = 0; i < btns_len; i++) {
|
||||
GodotRuntime.setHeapValue(r_btns + (i << 2), btns[i], 'float');
|
||||
}
|
||||
GodotRuntime.setHeapValue(r_btns_num, btns_len, 'i32');
|
||||
const axes = sample.axes;
|
||||
const axes_len = axes.length < 10 ? axes.length : 10;
|
||||
for (let i = 0; i < axes_len; i++) {
|
||||
GodotRuntime.setHeapValue(r_axes + (i << 2), axes[i], 'float');
|
||||
}
|
||||
GodotRuntime.setHeapValue(r_axes_num, axes_len, 'i32');
|
||||
const is_standard = sample.standard ? 1 : 0;
|
||||
GodotRuntime.setHeapValue(r_standard, is_standard, 'i32');
|
||||
return 0;
|
||||
},
|
||||
|
||||
/*
|
||||
* Drag/Drop API
|
||||
*/
|
||||
godot_js_input_drop_files_cb__sig: 'vi',
|
||||
godot_js_input_drop_files_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
const dropFiles = function (files) {
|
||||
const args = files || [];
|
||||
if (!args.length) {
|
||||
return;
|
||||
}
|
||||
const argc = args.length;
|
||||
const argv = GodotRuntime.allocStringArray(args);
|
||||
func(argv, argc);
|
||||
GodotRuntime.freeStringArray(argv, argc);
|
||||
};
|
||||
const canvas = GodotConfig.canvas;
|
||||
GodotDisplayListeners.add(canvas, 'dragover', function (ev) {
|
||||
// Prevent default behavior (which would try to open the file(s))
|
||||
ev.preventDefault();
|
||||
}, false);
|
||||
GodotDisplayListeners.add(canvas, 'drop', GodotInputDragDrop.handler(dropFiles));
|
||||
},
|
||||
|
||||
/* Paste API */
|
||||
godot_js_input_paste_cb__sig: 'vi',
|
||||
godot_js_input_paste_cb: function (callback) {
|
||||
const func = GodotRuntime.get_func(callback);
|
||||
GodotDisplayListeners.add(window, 'paste', function (evt) {
|
||||
const text = evt.clipboardData.getData('text');
|
||||
const ptr = GodotRuntime.allocString(text);
|
||||
func(ptr);
|
||||
GodotRuntime.free(ptr);
|
||||
}, false);
|
||||
},
|
||||
};
|
||||
|
||||
autoAddDeps(GodotInput, '$GodotInput');
|
||||
|
|
Loading…
Reference in New Issue