diff options
| author | Rémi Verschelde <rverschelde@gmail.com> | 2024-05-02 19:06:03 +0200 |
|---|---|---|
| committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-05-02 19:06:03 +0200 |
| commit | 5ea3f0bd75d7f6320b79ab791afb967c72a303a0 (patch) | |
| tree | 9a3e429a54fee6bed2cd405f3ed4ad5d4bdfd8ca /platform | |
| parent | 4e9543d8494f175bc0e772541a15c059bf6d6835 (diff) | |
| parent | 789c6ebdfd72ec9141e04ef162471983e7fdee94 (diff) | |
| download | redot-engine-5ea3f0bd75d7f6320b79ab791afb967c72a303a0.tar.gz | |
Merge pull request #91143 from RadiantUwU/add-input-amplitude
Add `amplitude` argument to `Input.vibrate_handheld`
Diffstat (limited to 'platform')
| -rw-r--r-- | platform/android/java/lib/src/org/godotengine/godot/Godot.kt | 21 | ||||
| -rw-r--r-- | platform/android/java_godot_wrapper.cpp | 13 | ||||
| -rw-r--r-- | platform/android/java_godot_wrapper.h | 2 | ||||
| -rw-r--r-- | platform/android/os_android.cpp | 4 | ||||
| -rw-r--r-- | platform/android/os_android.h | 2 | ||||
| -rw-r--r-- | platform/ios/ios.h | 2 | ||||
| -rw-r--r-- | platform/ios/ios.mm | 42 | ||||
| -rw-r--r-- | platform/ios/os_ios.h | 2 | ||||
| -rw-r--r-- | platform/ios/os_ios.mm | 8 | ||||
| -rw-r--r-- | platform/web/os_web.cpp | 2 | ||||
| -rw-r--r-- | platform/web/os_web.h | 2 |
11 files changed, 70 insertions, 30 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt index ce53aeebcb..fbdf07e6c2 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt @@ -894,16 +894,25 @@ class Godot(private val context: Context) : SensorEventListener { */ @SuppressLint("MissingPermission") @Keep - private fun vibrate(durationMs: Int) { + private fun vibrate(durationMs: Int, amplitude: Int) { if (durationMs > 0 && requestPermission("VIBRATE")) { val vibratorService = getActivity()?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? ?: return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - vibratorService.vibrate( - VibrationEffect.createOneShot( - durationMs.toLong(), - VibrationEffect.DEFAULT_AMPLITUDE + if (amplitude <= -1) { + vibratorService.vibrate( + VibrationEffect.createOneShot( + durationMs.toLong(), + VibrationEffect.DEFAULT_AMPLITUDE + ) ) - ) + } else { + vibratorService.vibrate( + VibrationEffect.createOneShot( + durationMs.toLong(), + amplitude + ) + ) + } } else { // deprecated in API 26 vibratorService.vibrate(durationMs.toLong()) diff --git a/platform/android/java_godot_wrapper.cpp b/platform/android/java_godot_wrapper.cpp index 61be6fc5db..0e766e7d56 100644 --- a/platform/android/java_godot_wrapper.cpp +++ b/platform/android/java_godot_wrapper.cpp @@ -72,7 +72,7 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_activity, jobject p_ _get_granted_permissions = p_env->GetMethodID(godot_class, "getGrantedPermissions", "()[Ljava/lang/String;"); _get_ca_certificates = p_env->GetMethodID(godot_class, "getCACertificates", "()Ljava/lang/String;"); _init_input_devices = p_env->GetMethodID(godot_class, "initInputDevices", "()V"); - _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(I)V"); + _vibrate = p_env->GetMethodID(godot_class, "vibrate", "(II)V"); _get_input_fallback_mapping = p_env->GetMethodID(godot_class, "getInputFallbackMapping", "()Ljava/lang/String;"); _on_godot_setup_completed = p_env->GetMethodID(godot_class, "onGodotSetupCompleted", "()V"); _on_godot_main_loop_started = p_env->GetMethodID(godot_class, "onGodotMainLoopStarted", "()V"); @@ -331,11 +331,18 @@ void GodotJavaWrapper::init_input_devices() { } } -void GodotJavaWrapper::vibrate(int p_duration_ms) { +void GodotJavaWrapper::vibrate(int p_duration_ms, float p_amplitude) { if (_vibrate) { JNIEnv *env = get_jni_env(); ERR_FAIL_NULL(env); - env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms); + + int j_amplitude = -1.0; + + if (p_amplitude != -1.0) { + j_amplitude = CLAMP(int(p_amplitude * 255), 1, 255); + } + + env->CallVoidMethod(godot_instance, _vibrate, p_duration_ms, j_amplitude); } } diff --git a/platform/android/java_godot_wrapper.h b/platform/android/java_godot_wrapper.h index 93998021a9..e86391d4e3 100644 --- a/platform/android/java_godot_wrapper.h +++ b/platform/android/java_godot_wrapper.h @@ -102,7 +102,7 @@ public: Vector<String> get_granted_permissions() const; String get_ca_certificates() const; void init_input_devices(); - void vibrate(int p_duration_ms); + void vibrate(int p_duration_ms, float p_amplitude = -1.0); String get_input_fallback_mapping(); int create_new_godot_instance(const List<String> &args); void begin_benchmark_measure(const String &p_context, const String &p_label); diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 463a307854..c60125c34e 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -746,8 +746,8 @@ ANativeWindow *OS_Android::get_native_window() const { #endif } -void OS_Android::vibrate_handheld(int p_duration_ms) { - godot_java->vibrate(p_duration_ms); +void OS_Android::vibrate_handheld(int p_duration_ms, float p_amplitude) { + godot_java->vibrate(p_duration_ms, p_amplitude); } String OS_Android::get_config_path() const { diff --git a/platform/android/os_android.h b/platform/android/os_android.h index 7bdbeef77a..b150ef4f61 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -153,7 +153,7 @@ public: virtual Error move_to_trash(const String &p_path) override; - void vibrate_handheld(int p_duration_ms) override; + void vibrate_handheld(int p_duration_ms, float p_amplitude = -1.0) override; virtual String get_config_path() const override; diff --git a/platform/ios/ios.h b/platform/ios/ios.h index d488cde257..cb5be64cee 100644 --- a/platform/ios/ios.h +++ b/platform/ios/ios.h @@ -51,7 +51,7 @@ public: static void alert(const char *p_alert, const char *p_title); bool supports_haptic_engine(); - void vibrate_haptic_engine(float p_duration_seconds); + void vibrate_haptic_engine(float p_duration_seconds, float p_amplitude); String get_model() const; String get_rate_url(int p_app_id) const; diff --git a/platform/ios/ios.mm b/platform/ios/ios.mm index 0a2e1fd5cd..6943de5ac8 100644 --- a/platform/ios/ios.mm +++ b/platform/ios/ios.mm @@ -69,21 +69,41 @@ CHHapticEngine *iOS::get_haptic_engine_instance() API_AVAILABLE(ios(13)) { return haptic_engine; } -void iOS::vibrate_haptic_engine(float p_duration_seconds) API_AVAILABLE(ios(13)) { +void iOS::vibrate_haptic_engine(float p_duration_seconds, float p_amplitude) API_AVAILABLE(ios(13)) { if (@available(iOS 13, *)) { // We need the @available check every time to make the compiler happy... if (supports_haptic_engine()) { CHHapticEngine *cur_haptic_engine = get_haptic_engine_instance(); if (cur_haptic_engine) { - NSDictionary *hapticDict = @{ - CHHapticPatternKeyPattern : @[ - @{CHHapticPatternKeyEvent : @{ - CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous, - CHHapticPatternKeyTime : @(CHHapticTimeImmediate), - CHHapticPatternKeyEventDuration : @(p_duration_seconds) - }, - }, - ], - }; + NSDictionary *hapticDict; + if (p_amplitude < 0) { + hapticDict = @{ + CHHapticPatternKeyPattern : @[ + @{CHHapticPatternKeyEvent : @{ + CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous, + CHHapticPatternKeyTime : @(CHHapticTimeImmediate), + CHHapticPatternKeyEventDuration : @(p_duration_seconds), + }, + }, + ], + }; + } else { + hapticDict = @{ + CHHapticPatternKeyPattern : @[ + @{CHHapticPatternKeyEvent : @{ + CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous, + CHHapticPatternKeyTime : @(CHHapticTimeImmediate), + CHHapticPatternKeyEventDuration : @(p_duration_seconds), + CHHapticPatternKeyEventParameters : @[ + @{ + CHHapticPatternKeyParameterID : @("HapticIntensity"), + CHHapticPatternKeyParameterValue : @(p_amplitude) + }, + ], + }, + }, + ], + }; + } NSError *error; CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error]; diff --git a/platform/ios/os_ios.h b/platform/ios/os_ios.h index c4782a4768..b7c5a73065 100644 --- a/platform/ios/os_ios.h +++ b/platform/ios/os_ios.h @@ -123,7 +123,7 @@ public: virtual String get_unique_id() const override; virtual String get_processor_name() const override; - virtual void vibrate_handheld(int p_duration_ms = 500) override; + virtual void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0) override; virtual bool _check_internal_feature_support(const String &p_feature) override; diff --git a/platform/ios/os_ios.mm b/platform/ios/os_ios.mm index 52d496d641..35b87ea647 100644 --- a/platform/ios/os_ios.mm +++ b/platform/ios/os_ios.mm @@ -571,9 +571,13 @@ String OS_IOS::get_system_font_path(const String &p_font_name, int p_weight, int return ret; } -void OS_IOS::vibrate_handheld(int p_duration_ms) { +void OS_IOS::vibrate_handheld(int p_duration_ms, float p_amplitude) { if (ios->supports_haptic_engine()) { - ios->vibrate_haptic_engine((float)p_duration_ms / 1000.f); + if (p_amplitude > 0.0) { + p_amplitude = CLAMP(p_amplitude, 0.0, 1.0); + } + + ios->vibrate_haptic_engine((float)p_duration_ms / 1000.f, p_amplitude); } else { // iOS <13 does not support duration for vibration AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); diff --git a/platform/web/os_web.cpp b/platform/web/os_web.cpp index ab4e7f8470..6b6c9ddd63 100644 --- a/platform/web/os_web.cpp +++ b/platform/web/os_web.cpp @@ -174,7 +174,7 @@ void OS_Web::add_frame_delay(bool p_can_draw) { #endif } -void OS_Web::vibrate_handheld(int p_duration_ms) { +void OS_Web::vibrate_handheld(int p_duration_ms, float p_amplitude) { godot_js_input_vibrate_handheld(p_duration_ms); } diff --git a/platform/web/os_web.h b/platform/web/os_web.h index a825938e96..55a5fcc6c6 100644 --- a/platform/web/os_web.h +++ b/platform/web/os_web.h @@ -98,7 +98,7 @@ public: // Implemented in web_main.cpp loop callback instead. void add_frame_delay(bool p_can_draw) override; - void vibrate_handheld(int p_duration_ms) override; + void vibrate_handheld(int p_duration_ms, float p_amplitude) override; String get_cache_path() const override; String get_config_path() const override; |
