diff options
Diffstat (limited to 'platform/javascript/js/libs/library_godot_display.js')
-rw-r--r-- | platform/javascript/js/libs/library_godot_display.js | 169 |
1 files changed, 85 insertions, 84 deletions
diff --git a/platform/javascript/js/libs/library_godot_display.js b/platform/javascript/js/libs/library_godot_display.js index 28f63ba557..9651b48952 100644 --- a/platform/javascript/js/libs/library_godot_display.js +++ b/platform/javascript/js/libs/library_godot_display.js @@ -38,13 +38,13 @@ const GodotDisplayListeners = { $GodotDisplayListeners: { handlers: [], - has: function(target, event, method, capture) { - return GodotDisplayListeners.handlers.findIndex(function(e) { + has: function (target, event, method, capture) { + return GodotDisplayListeners.handlers.findIndex(function (e) { return e.target === target && e.event === event && e.method === method && e.capture === capture; }) !== -1; }, - add: function(target, event, method, capture) { + add: function (target, event, method, capture) { if (GodotDisplayListeners.has(target, event, method, capture)) { return; } @@ -53,13 +53,13 @@ const GodotDisplayListeners = { this.event = p_event; this.method = p_method; this.capture = p_capture; - }; + } GodotDisplayListeners.handlers.push(new Handler(target, event, method, capture)); target.addEventListener(event, method, capture); }, - clear: function() { - GodotDisplayListeners.handlers.forEach(function(h) { + clear: function () { + GodotDisplayListeners.handlers.forEach(function (h) { h.target.removeEventListener(h.event, h.method, h.capture); }); GodotDisplayListeners.handlers.length = 0; @@ -84,20 +84,20 @@ const GodotDisplayDragDrop = { promises: [], pending_files: [], - add_entry: function(entry) { + 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); + GodotRuntime.error('Unrecognized entry...', entry); } }, - add_dir: function(entry) { - GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) { + add_dir: function (entry) { + GodotDisplayDragDrop.promises.push(new Promise(function (resolve, reject) { const reader = entry.createReader(); - reader.readEntries(function(entries) { + reader.readEntries(function (entries) { for (let i = 0; i < entries.length; i++) { GodotDisplayDragDrop.add_entry(entries[i]); } @@ -106,58 +106,58 @@ const GodotDisplayDragDrop = { })); }, - add_file: function(entry) { - GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) { - entry.file(function(file) { + add_file: function (entry) { + GodotDisplayDragDrop.promises.push(new Promise(function (resolve, reject) { + entry.file(function (file) { const reader = new FileReader(); - reader.onload = function() { + reader.onload = function () { const f = { - "path": file.relativePath || file.webkitRelativePath, - "name": file.name, - "type": file.type, - "size": file.size, - "data": reader.result + '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() + resolve(); }; - reader.onerror = function() { - GodotRuntime.print("Error reading file"); + reader.onerror = function () { + GodotRuntime.print('Error reading file'); reject(); - } + }; reader.readAsArrayBuffer(file); - }, function(err) { - GodotRuntime.print("Error!"); + }, function (err) { + GodotRuntime.print('Error!'); reject(); }); })); }, - process: function(resolve, reject) { + process: function (resolve, reject) { if (GodotDisplayDragDrop.promises.length === 0) { resolve(); return; } - GodotDisplayDragDrop.promises.pop().then(function() { - setTimeout(function() { + GodotDisplayDragDrop.promises.pop().then(function () { + setTimeout(function () { GodotDisplayDragDrop.process(resolve, reject); }, 0); }); }, - _process_event: function(ev, callback) { + _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) { + if ('getAsEntry' in item) { entry = item.getAsEntry(); - } else if ("webkitGetAsEntry" in item) { + } else if ('webkitGetAsEntry' in item) { entry = item.webkitGetAsEntry(); } if (entry) { @@ -165,24 +165,24 @@ const GodotDisplayDragDrop = { } } } else { - GodotRuntime.error("File upload not supported"); + GodotRuntime.error('File upload not supported'); } - new Promise(GodotDisplayDragDrop.process).then(function() { - const DROP = "/tmp/drop-" + parseInt(Math.random() * (1 << 30), 10) + "/"; + 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("/"); + let idx = path.indexOf('/'); if (idx === -1) { // Root file drops.push(DROP + path); } else { // Subdir const sub = path.substr(0, idx); - idx = sub.indexOf("/"); + idx = sub.indexOf('/'); if (idx < 0 && drops.indexOf(DROP + sub) === -1) { drops.push(DROP + sub); } @@ -192,37 +192,38 @@ const GodotDisplayDragDrop = { GodotDisplayDragDrop.promises = []; GodotDisplayDragDrop.pending_files = []; callback(drops); - const dirs = [DROP.substr(0, DROP.length -1)]; + const dirs = [DROP.substr(0, DROP.length - 1)]; // Remove temporary files files.forEach(function (file) { FS.unlink(file); - let dir = file.replace(DROP, ""); - let idx = dir.lastIndexOf("/"); + let dir = file.replace(DROP, ''); + let idx = dir.lastIndexOf('/'); while (idx > 0) { dir = dir.substr(0, idx); if (dirs.indexOf(DROP + dir) === -1) { dirs.push(DROP + dir); } - idx = dir.lastIndexOf("/"); + idx = dir.lastIndexOf('/'); } }); // Remove dirs. - dirs.sort(function(a, b) { + dirs.sort(function (a, b) { const al = (a.match(/\//g) || []).length; const bl = (b.match(/\//g) || []).length; - if (al > bl) + if (al > bl) { return -1; - else if (al < bl) + } else if (al < bl) { return 1; + } return 0; - }).forEach(function(dir) { + }).forEach(function (dir) { FS.rmdir(dir); }); }); }, - handler: function(callback) { - return function(ev) { + handler: function (callback) { + return function (ev) { GodotDisplayDragDrop._process_event(ev, callback); }; }, @@ -241,25 +242,25 @@ const GodotDisplayCursor = { shape: 'auto', visible: true, cursors: {}, - set_style: function(style) { + set_style: function (style) { GodotConfig.canvas.style.cursor = style; }, - set_shape: function(shape) { + set_shape: function (shape) { GodotDisplayCursor.shape = shape; let css = shape; if (shape in GodotDisplayCursor.cursors) { const c = GodotDisplayCursor.cursors[shape]; - css = 'url("' + c.url + '") ' + c.x + ' ' + c.y + ', auto'; + css = `url("${c.url}") ${c.x} ${c.y}, auto`; } if (GodotDisplayCursor.visible) { GodotDisplayCursor.set_style(css); } }, - clear: function() { + clear: function () { GodotDisplayCursor.set_style(''); GodotDisplayCursor.shape = 'auto'; GodotDisplayCursor.visible = true; - Object.keys(GodotDisplayCursor.cursors).forEach(function(key) { + Object.keys(GodotDisplayCursor.cursors).forEach(function (key) { URL.revokeObjectURL(GodotDisplayCursor.cursors[key]); delete GodotDisplayCursor.cursors[key]; }); @@ -279,35 +280,35 @@ const GodotDisplay = { window_icon: '', }, - godot_js_display_is_swap_ok_cancel: function() { + godot_js_display_is_swap_ok_cancel: function () { const win = (['Windows', 'Win64', 'Win32', 'WinCE']); - const plat = navigator.platform || ""; + const plat = navigator.platform || ''; if (win.indexOf(plat) !== -1) { return 1; } return 0; }, - godot_js_display_alert: function(p_text) { + godot_js_display_alert: function (p_text) { window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert }, - godot_js_display_pixel_ratio_get: function() { + godot_js_display_pixel_ratio_get: function () { return window.devicePixelRatio || 1; }, /* * Canvas */ - godot_js_display_canvas_focus: function() { + godot_js_display_canvas_focus: function () { GodotConfig.canvas.focus(); }, - godot_js_display_canvas_is_focused: function() { + godot_js_display_canvas_is_focused: function () { return document.activeElement === GodotConfig.canvas; }, - godot_js_display_canvas_bounding_rect_position_get: function(r_x, r_y) { + godot_js_display_canvas_bounding_rect_position_get: function (r_x, r_y) { const brect = GodotConfig.canvas.getBoundingClientRect(); GodotRuntime.setHeapValue(r_x, brect.x, 'i32'); GodotRuntime.setHeapValue(r_y, brect.y, 'i32'); @@ -316,26 +317,26 @@ const GodotDisplay = { /* * Touchscreen */ - godot_js_display_touchscreen_is_available: function() { + godot_js_display_touchscreen_is_available: function () { return 'ontouchstart' in window; }, /* * Clipboard */ - godot_js_display_clipboard_set: function(p_text) { + godot_js_display_clipboard_set: function (p_text) { const text = GodotRuntime.parseString(p_text); if (!navigator.clipboard || !navigator.clipboard.writeText) { return 1; } - navigator.clipboard.writeText(text).catch(function(e) { + navigator.clipboard.writeText(text).catch(function (e) { // Setting OS clipboard is only possible from an input callback. - GodotRuntime.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e); + GodotRuntime.error('Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:', e); }); return 0; }, - godot_js_display_clipboard_get: function(callback) { + godot_js_display_clipboard_get: function (callback) { const func = GodotRuntime.get_func(callback); try { navigator.clipboard.readText().then(function (result) { @@ -353,19 +354,19 @@ const GodotDisplay = { /* * Window */ - godot_js_display_window_request_fullscreen: function() { + godot_js_display_window_request_fullscreen: function () { const canvas = GodotConfig.canvas; - (canvas.requestFullscreen || canvas.msRequestFullscreen || - canvas.mozRequestFullScreen || canvas.mozRequestFullscreen || - canvas.webkitRequestFullscreen + (canvas.requestFullscreen || canvas.msRequestFullscreen + || canvas.mozRequestFullScreen || canvas.mozRequestFullscreen + || canvas.webkitRequestFullscreen ).call(canvas); }, - godot_js_display_window_title_set: function(p_data) { + godot_js_display_window_title_set: function (p_data) { document.title = GodotRuntime.parseString(p_data); }, - godot_js_display_window_icon_set: function(p_ptr, p_len) { + godot_js_display_window_icon_set: function (p_ptr, p_len) { let link = document.getElementById('-gd-engine-icon'); if (link === null) { link = document.createElement('link'); @@ -374,7 +375,7 @@ const GodotDisplay = { document.head.appendChild(link); } const old_icon = GodotDisplay.window_icon; - const png = new Blob([GodotRuntime.heapCopy(HEAPU8, p_ptr, p_len)], { type: "image/png" }); + const png = new Blob([GodotRuntime.heapCopy(HEAPU8, p_ptr, p_len)], { type: 'image/png' }); GodotDisplay.window_icon = URL.createObjectURL(png); link.href = GodotDisplay.window_icon; if (old_icon) { @@ -385,7 +386,7 @@ const GodotDisplay = { /* * Cursor */ - godot_js_display_cursor_set_visible: function(p_visible) { + godot_js_display_cursor_set_visible: function (p_visible) { const visible = p_visible !== 0; if (visible === GodotDisplayCursor.visible) { return; @@ -398,15 +399,15 @@ const GodotDisplay = { } }, - godot_js_display_cursor_is_hidden: function() { + godot_js_display_cursor_is_hidden: function () { return !GodotDisplayCursor.visible; }, - godot_js_display_cursor_set_shape: function(p_string) { + godot_js_display_cursor_set_shape: function (p_string) { GodotDisplayCursor.set_shape(GodotRuntime.parseString(p_string)); }, - godot_js_display_cursor_set_custom_shape: function(p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) { + godot_js_display_cursor_set_custom_shape: function (p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) { const shape = GodotRuntime.parseString(p_shape); const old_shape = GodotDisplayCursor.cursors[shape]; if (p_len > 0) { @@ -431,20 +432,20 @@ const GodotDisplay = { /* * Listeners */ - godot_js_display_notification_cb: function(callback, p_enter, p_exit, p_in, p_out) { + godot_js_display_notification_cb: function (callback, p_enter, p_exit, p_in, p_out) { const canvas = GodotConfig.canvas; const func = GodotRuntime.get_func(callback); const notif = [p_enter, p_exit, p_in, p_out]; - ['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function(evt_name, idx) { - GodotDisplayListeners.add(canvas, evt_name, function() { + ['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function (evt_name, idx) { + GodotDisplayListeners.add(canvas, evt_name, function () { func.bind(null, notif[idx]); }, true); }); }, - godot_js_display_paste_cb: function(callback) { + godot_js_display_paste_cb: function (callback) { const func = GodotRuntime.get_func(callback); - GodotDisplayListeners.add(window, 'paste', function(evt) { + GodotDisplayListeners.add(window, 'paste', function (evt) { const text = evt.clipboardData.getData('text'); const ptr = GodotRuntime.allocString(text); func(ptr); @@ -452,9 +453,9 @@ const GodotDisplay = { }, false); }, - godot_js_display_drop_files_cb: function(callback) { - const func = GodotRuntime.get_func(callback) - const dropFiles = function(files) { + 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; @@ -465,7 +466,7 @@ const GodotDisplay = { GodotRuntime.freeStringArray(argv, argc); }; const canvas = GodotConfig.canvas; - GodotDisplayListeners.add(canvas, 'dragover', function(ev) { + GodotDisplayListeners.add(canvas, 'dragover', function (ev) { // Prevent default behavior (which would try to open the file(s)) ev.preventDefault(); }, false); |