From 68616edcbc4aebd34ee98b78b1d165190fdcb9a6 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sun, 12 Sep 2021 13:40:07 +0200 Subject: [PATCH] [HTML5] Refactor JS library listeners to OS. --- platform/javascript/.eslintrc.libs.js | 2 +- .../js/libs/library_godot_display.js | 62 ++++--------------- .../javascript/js/libs/library_godot_input.js | 36 +++++------ .../javascript/js/libs/library_godot_os.js | 40 ++++++++++++ 4 files changed, 70 insertions(+), 70 deletions(-) diff --git a/platform/javascript/.eslintrc.libs.js b/platform/javascript/.eslintrc.libs.js index a46be42350c..8e579fd4625 100644 --- a/platform/javascript/.eslintrc.libs.js +++ b/platform/javascript/.eslintrc.libs.js @@ -15,7 +15,7 @@ module.exports = { "IDBFS": true, "GodotOS": true, "GodotConfig": true, - "GodotDisplayListeners": true, + "GodotEventListeners": true, "GodotRuntime": true, "GodotFS": true, "IDHandler": true, diff --git a/platform/javascript/js/libs/library_godot_display.js b/platform/javascript/js/libs/library_godot_display.js index 17d65b5906d..2689f1c22c6 100644 --- a/platform/javascript/js/libs/library_godot_display.js +++ b/platform/javascript/js/libs/library_godot_display.js @@ -28,49 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -/* - * Display Server listeners. - * Keeps track of registered event listeners so it can remove them on shutdown. - */ -const GodotDisplayListeners = { - $GodotDisplayListeners__deps: ['$GodotOS'], - $GodotDisplayListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayListeners.clear(); resolve(); });', - $GodotDisplayListeners: { - handlers: [], - - 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) { - if (GodotDisplayListeners.has(target, event, method, capture)) { - return; - } - function Handler(p_target, p_event, p_method, p_capture) { - this.target = p_target; - 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) { - h.target.removeEventListener(h.event, h.method, h.capture); - }); - GodotDisplayListeners.handlers.length = 0; - }, - }, -}; -mergeInto(LibraryManager.library, GodotDisplayListeners); - const GodotDisplayVK = { - $GodotDisplayVK__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners'], + $GodotDisplayVK__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners'], $GodotDisplayVK__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayVK.clear(); resolve(); });', $GodotDisplayVK: { textinput: null, @@ -96,12 +56,12 @@ const GodotDisplayVK = { elem.style.outline = 'none'; elem.readonly = true; elem.disabled = true; - GodotDisplayListeners.add(elem, 'input', function (evt) { + GodotEventListeners.add(elem, 'input', function (evt) { const c_str = GodotRuntime.allocString(elem.value); input_cb(c_str, elem.selectionEnd); GodotRuntime.free(c_str); }, false); - GodotDisplayListeners.add(elem, 'blur', function (evt) { + GodotEventListeners.add(elem, 'blur', function (evt) { elem.style.display = 'none'; elem.readonly = true; elem.disabled = true; @@ -334,7 +294,7 @@ mergeInto(LibraryManager.library, GodotDisplayScreen); * Exposes all the functions needed by DisplayServer implementation. */ const GodotDisplay = { - $GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayScreen', '$GodotDisplayVK'], + $GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'], $GodotDisplay: { window_icon: '', findDPI: function () { @@ -588,15 +548,15 @@ const GodotDisplay = { func(GodotDisplayScreen.isFullscreen()); } } - GodotDisplayListeners.add(document, 'fullscreenchange', change_cb, false); - GodotDisplayListeners.add(document, 'mozfullscreenchange', change_cb, false); - GodotDisplayListeners.add(document, 'webkitfullscreenchange', change_cb, false); + GodotEventListeners.add(document, 'fullscreenchange', change_cb, false); + GodotEventListeners.add(document, 'mozfullscreenchange', change_cb, false); + GodotEventListeners.add(document, 'webkitfullscreenchange', change_cb, false); }, godot_js_display_window_blur_cb__sig: 'vi', godot_js_display_window_blur_cb: function (callback) { const func = GodotRuntime.get_func(callback); - GodotDisplayListeners.add(window, 'blur', function () { + GodotEventListeners.add(window, 'blur', function () { func(); }, false); }, @@ -607,7 +567,7 @@ const GodotDisplay = { 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 () { + GodotEventListeners.add(canvas, evt_name, function () { func(notif[idx]); }, true); }); @@ -616,10 +576,10 @@ const GodotDisplay = { 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; - GodotDisplayListeners.add(canvas, 'contextmenu', function (ev) { + GodotEventListeners.add(canvas, 'contextmenu', function (ev) { ev.preventDefault(); }, false); - GodotDisplayListeners.add(canvas, 'webglcontextlost', function (ev) { + GodotEventListeners.add(canvas, 'webglcontextlost', function (ev) { alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert ev.preventDefault(); }, false); diff --git a/platform/javascript/js/libs/library_godot_input.js b/platform/javascript/js/libs/library_godot_input.js index 6cd81069171..587c320f355 100644 --- a/platform/javascript/js/libs/library_godot_input.js +++ b/platform/javascript/js/libs/library_godot_input.js @@ -32,7 +32,7 @@ * Gamepad API helper. */ const GodotInputGamepads = { - $GodotInputGamepads__deps: ['$GodotRuntime', '$GodotDisplayListeners'], + $GodotInputGamepads__deps: ['$GodotRuntime', '$GodotEventListeners'], $GodotInputGamepads: { samples: [], @@ -87,7 +87,7 @@ const GodotInputGamepads = { }, init: function (onchange) { - GodotDisplayListeners.samples = []; + GodotEventListeners.samples = []; function add(pad) { const guid = GodotInputGamepads.get_guid(pad); const c_id = GodotRuntime.allocString(pad.id); @@ -103,10 +103,10 @@ const GodotInputGamepads = { add(pads[i]); } } - GodotDisplayListeners.add(window, 'gamepadconnected', function (evt) { + GodotEventListeners.add(window, 'gamepadconnected', function (evt) { add(evt.gamepad); }, false); - GodotDisplayListeners.add(window, 'gamepaddisconnected', function (evt) { + GodotEventListeners.add(window, 'gamepaddisconnected', function (evt) { onchange(evt.gamepad.index, 0); }, false); }, @@ -334,7 +334,7 @@ mergeInto(LibraryManager.library, GodotInputDragDrop); * Godot exposed input functions. */ const GodotInput = { - $GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotDisplayListeners', '$GodotInputGamepads', '$GodotInputDragDrop'], + $GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners', '$GodotInputGamepads', '$GodotInputDragDrop'], $GodotInput: { getModifiers: function (evt) { return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3); @@ -367,7 +367,7 @@ const GodotInput = { const modifiers = GodotInput.getModifiers(evt); func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers); } - GodotDisplayListeners.add(window, 'mousemove', move_cb, false); + GodotEventListeners.add(window, 'mousemove', move_cb, false); }, godot_js_input_mouse_wheel_cb__sig: 'vi', @@ -378,7 +378,7 @@ const GodotInput = { evt.preventDefault(); } } - GodotDisplayListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false); + GodotEventListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false); }, godot_js_input_mouse_button_cb__sig: 'vi', @@ -393,8 +393,8 @@ const GodotInput = { evt.preventDefault(); } } - GodotDisplayListeners.add(canvas, 'mousedown', button_cb.bind(null, 1), false); - GodotDisplayListeners.add(window, 'mouseup', button_cb.bind(null, 0), false); + GodotEventListeners.add(canvas, 'mousedown', button_cb.bind(null, 1), false); + GodotEventListeners.add(window, 'mouseup', button_cb.bind(null, 0), false); }, /* @@ -419,10 +419,10 @@ const GodotInput = { evt.preventDefault(); } } - GodotDisplayListeners.add(canvas, 'touchstart', touch_cb.bind(null, 0), false); - GodotDisplayListeners.add(canvas, 'touchend', touch_cb.bind(null, 1), false); - GodotDisplayListeners.add(canvas, 'touchcancel', touch_cb.bind(null, 1), false); - GodotDisplayListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false); + GodotEventListeners.add(canvas, 'touchstart', touch_cb.bind(null, 0), false); + GodotEventListeners.add(canvas, 'touchend', touch_cb.bind(null, 1), false); + GodotEventListeners.add(canvas, 'touchcancel', touch_cb.bind(null, 1), false); + GodotEventListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false); }, /* @@ -438,8 +438,8 @@ const GodotInput = { func(pressed, evt.repeat, modifiers); evt.preventDefault(); } - GodotDisplayListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false); - GodotDisplayListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false); + GodotEventListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false); + GodotEventListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false); }, /* @@ -502,18 +502,18 @@ const GodotInput = { GodotRuntime.freeStringArray(argv, argc); }; const canvas = GodotConfig.canvas; - GodotDisplayListeners.add(canvas, 'dragover', function (ev) { + GodotEventListeners.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)); + GodotEventListeners.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) { + GodotEventListeners.add(window, 'paste', function (evt) { const text = evt.clipboardData.getData('text'); const ptr = GodotRuntime.allocString(text); func(ptr); diff --git a/platform/javascript/js/libs/library_godot_os.js b/platform/javascript/js/libs/library_godot_os.js index 99e7ee8b5f2..c552e99415d 100644 --- a/platform/javascript/js/libs/library_godot_os.js +++ b/platform/javascript/js/libs/library_godot_os.js @@ -328,3 +328,43 @@ const GodotOS = { autoAddDeps(GodotOS, '$GodotOS'); mergeInto(LibraryManager.library, GodotOS); + +/* + * Godot event listeners. + * Keeps track of registered event listeners so it can remove them on shutdown. + */ +const GodotEventListeners = { + $GodotEventListeners__deps: ['$GodotOS'], + $GodotEventListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotEventListeners.clear(); resolve(); });', + $GodotEventListeners: { + handlers: [], + + has: function (target, event, method, capture) { + return GodotEventListeners.handlers.findIndex(function (e) { + return e.target === target && e.event === event && e.method === method && e.capture === capture; + }) !== -1; + }, + + add: function (target, event, method, capture) { + if (GodotEventListeners.has(target, event, method, capture)) { + return; + } + function Handler(p_target, p_event, p_method, p_capture) { + this.target = p_target; + this.event = p_event; + this.method = p_method; + this.capture = p_capture; + } + GodotEventListeners.handlers.push(new Handler(target, event, method, capture)); + target.addEventListener(event, method, capture); + }, + + clear: function () { + GodotEventListeners.handlers.forEach(function (h) { + h.target.removeEventListener(h.event, h.method, h.capture); + }); + GodotEventListeners.handlers.length = 0; + }, + }, +}; +mergeInto(LibraryManager.library, GodotEventListeners);