phoboslab / Ejecta

A Fast, Open Source JavaScript, Canvas & Audio Implementation for iOS
2.81k stars 322 forks source link

devicemotion can't support remote controller of AppleTV #598

Open finscn opened 8 years ago

finscn commented 8 years ago

is this feature in the roadmap?

ericjbasti commented 8 years ago

This is an interesting issue, because the only way to get the equivalent of device motion onto the atv requires the remote. The remote is a gamepad, and the current gamepad spec (which Dominic probably wants to stick to) doesn't support the required attributes.

It's easy to add in if you're looking to do so. In the EJBindingGamepad.m file you can add the gravity/acceleration attributes to the "microGamepad"

~ line 184 (EJBindingGamepad.m)

#if TARGET_OS_TV 
    // Provide the Remote's touch pad a axis in addition to the Up/Down/Left/Right buttons
    else if( controller.microGamepad ) {
        GCMicroGamepad *gamepad = controller.microGamepad;
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 0, JSValueMakeNumber(ctx, gamepad.dpad.xAxis.value), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 1, JSValueMakeNumber(ctx, -gamepad.dpad.yAxis.value), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 4, JSValueMakeNumber(ctx, controller.motion.gravity.x), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 5, JSValueMakeNumber(ctx, controller.motion.gravity.y), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 6, JSValueMakeNumber(ctx, controller.motion.gravity.z), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 7, JSValueMakeNumber(ctx, controller.motion.userAcceleration.x), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 8, JSValueMakeNumber(ctx, controller.motion.userAcceleration.y), NULL);
        JSObjectSetPropertyAtIndex(ctx, jsAxes, 9, JSValueMakeNumber(ctx, controller.motion.userAcceleration.z), NULL);
    }
#endif
return jsAxes;

You can then access this data the same way you would access the dpad info, just at the different array indexes. Not as nice as using the device motion api, but its not the same thing.

finscn commented 8 years ago

@phoboslab , I implement this feature in my fork https://github.com/finscn/Ejecta/commit/597e1e77ff25189a23ee1db7897bb579731561fb

I hope it could help you.

In my version , the devicemotion event isn't invoked by [deviceMotionDelegate triggerDeviceMotionEvents];.

It will be invoked when user call navigator.getGamepads(), I think 60FPS to hight .

finscn commented 8 years ago

I update my fork: https://github.com/finscn/Ejecta/commit/c6f19482d6f1b395b9295b9ccbba40164afdc4d6

finscn commented 8 years ago

Update again: https://github.com/finscn/Ejecta/commit/7bfc59cb170ba69ade12f079d9e91fef13345884