vibrate.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebiten
  15. import (
  16. "time"
  17. "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
  18. "github.com/hajimehoshi/ebiten/v2/internal/vibrate"
  19. )
  20. // VibrateOptions represents the options for device vibration.
  21. type VibrateOptions struct {
  22. // Duration is the time duration of the effect.
  23. Duration time.Duration
  24. // Magnitude is the strength of the device vibration.
  25. // The value is in between 0 and 1.
  26. Magnitude float64
  27. }
  28. // Vibrate vibrates the device with the specified options.
  29. //
  30. // Vibrate works on mobiles and browsers.
  31. //
  32. // On browsers, Magnitude in the options is ignored.
  33. //
  34. // On Android, this line is required in the manifest setting to use Vibrate:
  35. //
  36. // <uses-permission android:name="android.permission.VIBRATE"/>
  37. //
  38. // On Android, Magnitude in the options is recognized only when the API Level is 26 or newer.
  39. // Otherwise, Magnitude is ignored.
  40. //
  41. // On iOS, CoreHaptics.framework is required to use Vibrate.
  42. //
  43. // On iOS, Vibrate works only when iOS version is 13.0 or newer.
  44. // Otherwise, Vibrate does nothing.
  45. //
  46. // Vibrate is concurrent-safe.
  47. func Vibrate(options *VibrateOptions) {
  48. vibrate.Vibrate(options.Duration, options.Magnitude)
  49. }
  50. // VibrateGamepadOptions represents the options for gamepad vibration.
  51. type VibrateGamepadOptions struct {
  52. // Duration is the time duration of the effect.
  53. Duration time.Duration
  54. // StrongMagnitude is the rumble intensity of a low-frequency rumble motor.
  55. // The value is in between 0 and 1.
  56. StrongMagnitude float64
  57. // WeakMagnitude is the rumble intensity of a high-frequency rumble motor.
  58. // The value is in between 0 and 1.
  59. WeakMagnitude float64
  60. }
  61. // VibrateGamepad vibrates the specified gamepad with the specified options.
  62. //
  63. // VibrateGamepad works only on browsers and Nintendo Switch so far.
  64. //
  65. // VibrateGamepad is concurrent-safe.
  66. func VibrateGamepad(gamepadID GamepadID, options *VibrateGamepadOptions) {
  67. g := gamepad.Get(gamepadID)
  68. if g == nil {
  69. return
  70. }
  71. g.Vibrate(options.Duration, options.StrongMagnitude, options.WeakMagnitude)
  72. }