mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 20:42:07 +01:00
internal/uidriver/mobile: make Vibrate async
This commit is contained in:
parent
8f1f9e3a5f
commit
eabd82cda0
@ -105,9 +105,11 @@ 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 (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
|
||||||
|
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?
|
||||||
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude))
|
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package mobile
|
|||||||
// #cgo LDFLAGS: -framework CoreHaptics
|
// #cgo LDFLAGS: -framework CoreHaptics
|
||||||
//
|
//
|
||||||
// #import <CoreHaptics/CoreHaptics.h>
|
// #import <CoreHaptics/CoreHaptics.h>
|
||||||
|
// #include <dispatch/dispatch.h>
|
||||||
//
|
//
|
||||||
// static id initializeHapticEngine(void) {
|
// static id initializeHapticEngine(void) {
|
||||||
// if (@available(iOS 13.0, *)) {
|
// if (@available(iOS 13.0, *)) {
|
||||||
@ -30,11 +31,13 @@ package mobile
|
|||||||
// NSError* error = nil;
|
// NSError* error = nil;
|
||||||
// CHHapticEngine* engine = [[CHHapticEngine alloc] initAndReturnError:&error];
|
// CHHapticEngine* engine = [[CHHapticEngine alloc] initAndReturnError:&error];
|
||||||
// if (error) {
|
// if (error) {
|
||||||
|
// NSLog(@"CHHapticEngine::initAndReturnError failed: %@", error);
|
||||||
// return nil;
|
// return nil;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// [engine startAndReturnError:&error];
|
// [engine startAndReturnError:&error];
|
||||||
// if (error) {
|
// if (error) {
|
||||||
|
// NSLog(@"CHHapticEngine::startAndReturnError failed %@", error);
|
||||||
// return nil;
|
// return nil;
|
||||||
// }
|
// }
|
||||||
// return engine;
|
// return engine;
|
||||||
@ -42,7 +45,7 @@ package mobile
|
|||||||
// return nil;
|
// return nil;
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// static void vibrate(double duration, double intensity) {
|
// static void vibrateOnMainThread(double duration, double intensity) {
|
||||||
// if (@available(iOS 13.0, *)) {
|
// if (@available(iOS 13.0, *)) {
|
||||||
// static BOOL initializeHapticEngineCalled = NO;
|
// static BOOL initializeHapticEngineCalled = NO;
|
||||||
// static CHHapticEngine* engine = nil;
|
// static CHHapticEngine* engine = nil;
|
||||||
@ -93,18 +96,20 @@ package mobile
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
//
|
||||||
|
// static void vibrate(double duration, double intensity) {
|
||||||
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
// vibrateOnMainThread(duration, intensity);
|
||||||
|
// });
|
||||||
|
// }
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var vibrationM sync.Mutex
|
|
||||||
|
|
||||||
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
|
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
|
||||||
vibrationM.Lock()
|
go func() {
|
||||||
defer vibrationM.Unlock()
|
|
||||||
|
|
||||||
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
|
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user