1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 var _p = cc.inputManager;
 27 
 28 /**
 29  * whether enable accelerometer event
 30  * @function
 31  * @param {Boolean} isEnable
 32  */
 33 _p.setAccelerometerEnabled = function(isEnable){
 34     var _t = this;
 35     if(_t._accelEnabled === isEnable)
 36         return;
 37 
 38     _t._accelEnabled = isEnable;
 39     var scheduler = cc.director.getScheduler();
 40     if(_t._accelEnabled){
 41         _t._accelCurTime = 0;
 42         scheduler.scheduleUpdate(_t);
 43     } else {
 44         _t._accelCurTime = 0;
 45         scheduler.scheduleUpdate(_t);
 46     }
 47 };
 48 
 49 /**
 50  * set accelerometer interval value
 51  * @function
 52  * @param {Number} interval
 53  */
 54 _p.setAccelerometerInterval = function(interval){
 55     if (this._accelInterval !== interval) {
 56         this._accelInterval = interval;
 57     }
 58 };
 59 
 60 _p._registerKeyboardEvent = function(){
 61     cc._addEventListener(cc._canvas, "keydown", function (e) {
 62         cc.eventManager.dispatchEvent(new cc.EventKeyboard(e.keyCode, true));
 63         e.stopPropagation();
 64         e.preventDefault();
 65     }, false);
 66     cc._addEventListener(cc._canvas, "keyup", function (e) {
 67         cc.eventManager.dispatchEvent(new cc.EventKeyboard(e.keyCode, false));
 68         e.stopPropagation();
 69         e.preventDefault();
 70     }, false);
 71 };
 72 
 73 _p._registerAccelerometerEvent = function(){
 74     var w = window, _t = this;
 75     _t._acceleration = new cc.Acceleration();
 76     _t._accelDeviceEvent = w.DeviceMotionEvent || w.DeviceOrientationEvent;
 77 
 78     //TODO fix DeviceMotionEvent bug on QQ Browser version 4.1 and below.
 79     if (cc.sys.browserType === cc.sys.BROWSER_TYPE_MOBILE_QQ)
 80         _t._accelDeviceEvent = window.DeviceOrientationEvent;
 81 
 82     var _deviceEventType = (_t._accelDeviceEvent === w.DeviceMotionEvent) ? "devicemotion" : "deviceorientation";
 83     var ua = navigator.userAgent;
 84     if (/Android/.test(ua) || (/Adr/.test(ua) && cc.sys.browserType === cc.BROWSER_TYPE_UC)) {
 85         _t._minus = -1;
 86     }
 87 
 88     cc._addEventListener(w, _deviceEventType, _t.didAccelerate.bind(_t), false);
 89 };
 90 
 91 _p.didAccelerate = function (eventData) {
 92     var _t = this, w = window;
 93     if (!_t._accelEnabled)
 94         return;
 95 
 96     var mAcceleration = _t._acceleration;
 97 
 98     var x, y, z;
 99 
100     if (_t._accelDeviceEvent === window.DeviceMotionEvent) {
101         var eventAcceleration = eventData["accelerationIncludingGravity"];
102         x = _t._accelMinus * eventAcceleration.x * 0.1;
103         y = _t._accelMinus * eventAcceleration.y * 0.1;
104         z = eventAcceleration.z * 0.1;
105     } else {
106         x = (eventData["gamma"] / 90) * 0.981;
107         y = -(eventData["beta"] / 90) * 0.981;
108         z = (eventData["alpha"] / 90) * 0.981;
109     }
110 
111     if(cc.sys.os === cc.sys.OS_ANDROID){
112         mAcceleration.x = -x;
113         mAcceleration.y = -y;
114     }else{
115         mAcceleration.x = x;
116         mAcceleration.y = y;
117     }
118     mAcceleration.z = z;
119 
120     mAcceleration.timestamp = eventData.timeStamp || Date.now();
121 
122     var tmpX = mAcceleration.x;
123     if(w.orientation === cc.UIInterfaceOrientationLandscapeRight){
124         mAcceleration.x = -mAcceleration.y;
125         mAcceleration.y = tmpX;
126     }else if(w.orientation === cc.UIInterfaceOrientationLandscapeLeft){
127         mAcceleration.x = mAcceleration.y;
128         mAcceleration.y = -tmpX;
129     }else if(w.orientation === cc.UIInterfaceOrientationPortraitUpsideDown){
130         mAcceleration.x = -mAcceleration.x;
131         mAcceleration.y = -mAcceleration.y;
132     }
133 };
134 
135 delete _p;