From 9c8b4db81f0fd47b86f1ef5d5339a63ddd15d2a9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 8 Feb 2022 14:39:21 +0900 Subject: [PATCH] internal/ui: separate 'vibrate' part to internal/vibrate --- internal/ui/ui_cbackend.go | 3 --- internal/ui/ui_glfw.go | 4 --- internal/ui/ui_js.go | 8 ------ internal/{ui => vibrate}/vibrate_android.go | 4 +-- internal/{ui => vibrate}/vibrate_ios.go | 4 +-- internal/vibrate/vibrate_js.go | 28 +++++++++++++++++++++ internal/vibrate/vibrate_null.go | 26 +++++++++++++++++++ vibrate.go | 4 +-- 8 files changed, 60 insertions(+), 21 deletions(-) rename internal/{ui => vibrate}/vibrate_android.go (97%) rename internal/{ui => vibrate}/vibrate_ios.go (97%) create mode 100644 internal/vibrate/vibrate_js.go create mode 100644 internal/vibrate/vibrate_null.go diff --git a/internal/ui/ui_cbackend.go b/internal/ui/ui_cbackend.go index 55e6f5af1..58ac63d0d 100644 --- a/internal/ui/ui_cbackend.go +++ b/internal/ui/ui_cbackend.go @@ -124,9 +124,6 @@ func (*UserInterface) SetScreenTransparent(transparent bool) { func (*UserInterface) SetInitFocused(focused bool) { } -func (*UserInterface) Vibrate(duration time.Duration, magnitude float64) { -} - func (*UserInterface) Input() *Input { return &theUserInterface.input } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index f7f38ae5d..bbc1f2c0d 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -1620,7 +1620,3 @@ func (u *UserInterface) setOrigPos(x, y int) { u.origPosX = x u.origPosY = y } - -func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { - // Do nothing. -} diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index ca03d9e36..7704d2804 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -652,14 +652,6 @@ func (u *UserInterface) SetInitFocused(focused bool) { u.initFocused = focused } -func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { - // magnitude is ignored. - - if js.Global().Get("navigator").Get("vibrate").Truthy() { - js.Global().Get("navigator").Call("vibrate", float64(duration/time.Millisecond)) - } -} - func (u *UserInterface) Input() *Input { return &u.input } diff --git a/internal/ui/vibrate_android.go b/internal/vibrate/vibrate_android.go similarity index 97% rename from internal/ui/vibrate_android.go rename to internal/vibrate/vibrate_android.go index 797b9cdc8..56023b43a 100644 --- a/internal/ui/vibrate_android.go +++ b/internal/vibrate/vibrate_android.go @@ -15,7 +15,7 @@ //go:build !ebitencbackend // +build !ebitencbackend -package ui +package vibrate import ( "time" @@ -107,7 +107,7 @@ static void vibrateOneShot(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx, */ import "C" -func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { +func Vibrate(duration time.Duration, magnitude float64) { go func() { _ = app.RunOnJVM(func(vm, env, ctx uintptr) error { // TODO: This might be crash when this is called from init(). How can we detect this? diff --git a/internal/ui/vibrate_ios.go b/internal/vibrate/vibrate_ios.go similarity index 97% rename from internal/ui/vibrate_ios.go rename to internal/vibrate/vibrate_ios.go index ff74768c5..b01f86fd9 100644 --- a/internal/ui/vibrate_ios.go +++ b/internal/vibrate/vibrate_ios.go @@ -15,7 +15,7 @@ //go:build ios && !ebitencbackend // +build ios,!ebitencbackend -package ui +package vibrate // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework AVFoundation -framework CoreHaptics -framework Foundation @@ -114,7 +114,7 @@ import ( "time" ) -func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { +func Vibrate(duration time.Duration, magnitude float64) { go func() { C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude)) }() diff --git a/internal/vibrate/vibrate_js.go b/internal/vibrate/vibrate_js.go new file mode 100644 index 000000000..083698b83 --- /dev/null +++ b/internal/vibrate/vibrate_js.go @@ -0,0 +1,28 @@ +// Copyright 2022 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vibrate + +import ( + "syscall/js" + "time" +) + +func Vibrate(duration time.Duration, magnitude float64) { + // magnitude is ignored. + + if js.Global().Get("navigator").Get("vibrate").Truthy() { + js.Global().Get("navigator").Call("vibrate", float64(duration/time.Millisecond)) + } +} diff --git a/internal/vibrate/vibrate_null.go b/internal/vibrate/vibrate_null.go new file mode 100644 index 000000000..bda302dfc --- /dev/null +++ b/internal/vibrate/vibrate_null.go @@ -0,0 +1,26 @@ +// Copyright 2022 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build (!android && !ios && !js) || ebitencbackend +// +build !android,!ios,!js ebitencbackend + +package vibrate + +import ( + "time" +) + +func Vibrate(duration time.Duration, magnitude float64) { + // Do nothing. +} diff --git a/vibrate.go b/vibrate.go index b5256d421..f6773adf4 100644 --- a/vibrate.go +++ b/vibrate.go @@ -18,7 +18,7 @@ import ( "time" "github.com/hajimehoshi/ebiten/v2/internal/gamepad" - "github.com/hajimehoshi/ebiten/v2/internal/ui" + "github.com/hajimehoshi/ebiten/v2/internal/vibrate" ) // VibrateOptions represents the options for device vibration. @@ -51,7 +51,7 @@ type VibrateOptions struct { // // Vibrate is concurrent-safe. func Vibrate(options *VibrateOptions) { - ui.Get().Vibrate(options.Duration, options.Magnitude) + vibrate.Vibrate(options.Duration, options.Magnitude) } // VibrateGamepadOptions represents the options for gamepad vibration.