internal/ui: separate 'vibrate' part to internal/vibrate

This commit is contained in:
Hajime Hoshi 2022-02-08 14:39:21 +09:00
parent d2d3673bd2
commit 9c8b4db81f
8 changed files with 60 additions and 21 deletions

View File

@ -124,9 +124,6 @@ func (*UserInterface) SetScreenTransparent(transparent bool) {
func (*UserInterface) SetInitFocused(focused bool) { func (*UserInterface) SetInitFocused(focused bool) {
} }
func (*UserInterface) Vibrate(duration time.Duration, magnitude float64) {
}
func (*UserInterface) Input() *Input { func (*UserInterface) Input() *Input {
return &theUserInterface.input return &theUserInterface.input
} }

View File

@ -1620,7 +1620,3 @@ func (u *UserInterface) setOrigPos(x, y int) {
u.origPosX = x u.origPosX = x
u.origPosY = y u.origPosY = y
} }
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
// Do nothing.
}

View File

@ -652,14 +652,6 @@ func (u *UserInterface) SetInitFocused(focused bool) {
u.initFocused = focused 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 { func (u *UserInterface) Input() *Input {
return &u.input return &u.input
} }

View File

@ -15,7 +15,7 @@
//go:build !ebitencbackend //go:build !ebitencbackend
// +build !ebitencbackend // +build !ebitencbackend
package ui package vibrate
import ( import (
"time" "time"
@ -107,7 +107,7 @@ static void vibrateOneShot(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx,
*/ */
import "C" import "C"
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { func Vibrate(duration time.Duration, magnitude float64) {
go func() { go func() {
_ = app.RunOnJVM(func(vm, env, ctx uintptr) error { _ = app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this? // TODO: This might be crash when this is called from init(). How can we detect this?

View File

@ -15,7 +15,7 @@
//go:build ios && !ebitencbackend //go:build ios && !ebitencbackend
// +build ios,!ebitencbackend // +build ios,!ebitencbackend
package ui package vibrate
// #cgo CFLAGS: -x objective-c // #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework AVFoundation -framework CoreHaptics -framework Foundation // #cgo LDFLAGS: -framework AVFoundation -framework CoreHaptics -framework Foundation
@ -114,7 +114,7 @@ import (
"time" "time"
) )
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { func Vibrate(duration time.Duration, magnitude float64) {
go func() { go func() {
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude)) C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
}() }()

View File

@ -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))
}
}

View File

@ -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.
}

View File

@ -18,7 +18,7 @@ import (
"time" "time"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "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. // VibrateOptions represents the options for device vibration.
@ -51,7 +51,7 @@ type VibrateOptions struct {
// //
// Vibrate is concurrent-safe. // Vibrate is concurrent-safe.
func Vibrate(options *VibrateOptions) { func Vibrate(options *VibrateOptions) {
ui.Get().Vibrate(options.Duration, options.Magnitude) vibrate.Vibrate(options.Duration, options.Magnitude)
} }
// VibrateGamepadOptions represents the options for gamepad vibration. // VibrateGamepadOptions represents the options for gamepad vibration.