From 641c15f9060adc2a3e279d45ed2b73c48ac06d33 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 30 Jul 2022 00:29:14 +0200 Subject: [PATCH] Ported: [3.x] HTML5: Add support for Input.vibrate_handheld() - pattlebass https://github.com/godotengine/godot/commit/fb7ecc748ff7ef60e70078a8e52f355669066ddb --- core/os/os.cpp | 2 +- doc/classes/Input.xml | 4 +++- .../javascript/js/libs/library_pandemonium_input.js | 11 ++++++++++- platform/javascript/os_javascript.cpp | 13 +++++++++---- platform/javascript/os_javascript.h | 1 + platform/javascript/pandemonium_js.h | 1 + 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/os/os.cpp b/core/os/os.cpp index 771c9c5e9..c53b77b35 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -302,7 +302,7 @@ bool OS::is_process_running(const ProcessID &p_pid) const { } void OS::vibrate_handheld(int p_duration_ms) { - WARN_PRINT("vibrate_handheld() only works with Android and iOS"); + WARN_PRINT("vibrate_handheld() only works with Android, iOS and HTML5"); } bool OS::is_stdout_verbose() const { diff --git a/doc/classes/Input.xml b/doc/classes/Input.xml index 634d595a7..edba63e56 100644 --- a/doc/classes/Input.xml +++ b/doc/classes/Input.xml @@ -357,9 +357,11 @@ - Vibrate Android and iOS devices. + Vibrate handheld devices. + [b]Note:[/b] This method is implemented on Android, iOS, and HTML5. [b]Note:[/b] For Android, it requires enabling the [code]VIBRATE[/code] permission in the export preset. [b]Note:[/b] For iOS, specifying the duration is supported in iOS 13 and later. + [b]Note:[/b] Some web browsers such as Safari and Firefox for Android do not support this method. diff --git a/platform/javascript/js/libs/library_pandemonium_input.js b/platform/javascript/js/libs/library_pandemonium_input.js index 5648fd104..eb8a5b8d1 100644 --- a/platform/javascript/js/libs/library_pandemonium_input.js +++ b/platform/javascript/js/libs/library_pandemonium_input.js @@ -540,7 +540,16 @@ const PandemoniumInput = { PandemoniumRuntime.free(ptr); }, false); }, + + godot_js_input_vibrate_handheld__sig: 'vi', + godot_js_input_vibrate_handheld: function (p_duration_ms) { + if (typeof navigator.vibrate !== 'function') { + GodotRuntime.print('This browser does not support vibration.'); + } else { + navigator.vibrate(p_duration_ms); + } + }, }; autoAddDeps(PandemoniumInput, '$PandemoniumInput'); -mergeInto(LibraryManager.library, PandemoniumInput); \ No newline at end of file +mergeInto(LibraryManager.library, PandemoniumInput); diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 327593bb9..04d85c29a 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -1,4 +1,4 @@ -/*************************************************************************/ +/*************************************************************************/ os_java /* os_javascript.cpp */ /*************************************************************************/ /* This file is part of: */ @@ -56,8 +56,9 @@ #define DOM_BUTTON_XBUTTON1 3 #define DOM_BUTTON_XBUTTON2 4 -// Quit -void OS_JavaScript::request_quit_callback() { + // Quit + void + OS_JavaScript::request_quit_callback() { OS_JavaScript *os = get_singleton(); if (os && os->get_main_loop()) { os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST); @@ -972,9 +973,13 @@ bool OS_JavaScript::can_draw() const { return true; // Always? } +void OS_JavaScript::vibrate_handheld(int p_duration_ms) { + godot_js_input_vibrate_handheld(p_duration_ms); +} + String OS_JavaScript::get_user_data_dir() const { return "/userfs"; -}; +} String OS_JavaScript::get_cache_path() const { return "/home/web_user/.cache"; diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index c3f167c88..acdc76360 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -184,6 +184,7 @@ public: virtual String get_name() const; virtual void add_frame_delay(bool p_can_draw) {} virtual bool can_draw() const; + virtual void vibrate_handheld(int p_duration_ms); virtual String get_cache_path() const; virtual String get_config_path() const; diff --git a/platform/javascript/pandemonium_js.h b/platform/javascript/pandemonium_js.h index 1cba3d056..2c75efe3a 100644 --- a/platform/javascript/pandemonium_js.h +++ b/platform/javascript/pandemonium_js.h @@ -57,6 +57,7 @@ extern void pandemonium_js_input_mouse_move_cb(void (*p_callback)(double p_x, do extern void pandemonium_js_input_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y)); extern void pandemonium_js_input_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords); extern void pandemonium_js_input_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_input_vibrate_handheld(int p_duration_ms); // Input gamepad extern void pandemonium_js_input_gamepad_cb(void (*p_on_change)(int p_index, int p_connected, const char *p_id, const char *p_guid));