Best way to use TouchEvent 'devicemotion'

Hello,

Im trying to work out some touch interfaces and seeing one ‘oh man’ moment in my tests and wondering if you might have a solution already.

I need to interact with device acceleration which uses the ‘devicemotion’ event. Its not supported in the app object which means I cant use the baked in bindings to do this.app.on(…). Adding a listener to the window works but then I dont get the scope on the object that I need. I could put the stuff I need in the window object or the prototype for the class and pull into the instance at update, but I both of those methods feel ‘hacky’ to me. I would prefer something cleaner if it exists.

Is there a way to create interfaces for events that will give me the same kind of interoperability as the rest of the supported events? I saw something called ‘TouchEvent’ out of the pc object that looked promising but the docs on it are a bit sparse.

Thanks

If you need something like that, I would probably just write a script/layer/code that forwarded the event on. Eg.

SomeScript.prototype.initialize = function () {
    var app = this.app;

    var handleMotionEvent = function handleMotionEvent(event) {
        app.fire('devicemotion', event);
    }

    window.addEventListener("devicemotion", handleMotionEvent, true);
}

The alternative is to create a globally accessible object that stores the values. E.g

SomeScript.prototype.initialize = function () {
    var app = this.app;
    app.deviceMotion = new pc.Vec3();

    var handleMotionEvent = function handleMotionEvent(event) {
        app.deviceMotion.x = event.accelerationIncludingGravity.x;
        app.deviceMotion.y = event.accelerationIncludingGravity.y;
        app.deviceMotion.z = event.accelerationIncludingGravity.z;
    }

    window.addEventListener("devicemotion", handleMotionEvent, true);
}

The tutorial section has quite a few projects on the touch input related events: http://developer.playcanvas.com/en/tutorials/?tags=input

I’ll give this a try. Thanks!