1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 cc.UIInterfaceOrientationLandscapeLeft = -90; 28 29 cc.UIInterfaceOrientationLandscapeRight = 90; 30 31 cc.UIInterfaceOrientationPortraitUpsideDown = 180; 32 33 cc.UIInterfaceOrientationPortrait = 0; 34 35 /** 36 * he device accelerometer reports values for each axis in units of g-force 37 */ 38 cc.Acceleration = function (x, y, z, timestamp) { 39 this.x = x || 0; 40 this.y = y || 0; 41 this.z = z || 0; 42 this.timestamp = timestamp || 0; 43 }; 44 45 /** 46 * @class 47 * @extends cc.Class 48 */ 49 cc.Accelerometer = cc.Class.extend(/** @lends cc.Accelerometer# */{ 50 setDelegate: function (delegate) { 51 cc.AccelerometerDispatcher.getInstance().addDelegate(delegate); 52 }, 53 setAccelerometerInterval: function (interval) { 54 cc.AccelerometerDispatcher.getInstance().setAccelerometerInterval(interval); 55 } 56 }); 57 58 /** 59 * 60 * The cc.AccelerometerDelegate defines a single method for 61 * receiving acceleration-related data from the system. 62 * @class 63 * @extends cc.Class 64 */ 65 cc.AccelerometerDispatcher = cc.Class.extend(/** @lends cc.AccelerometerDispatcher# */{ 66 _delegate: null, 67 _acceleration: null, 68 _deviceEvent: null, 69 _interval: 1/30, 70 _minus: 1, 71 _curTime:0, 72 init: function () { 73 this._acceleration = new cc.Acceleration(); 74 var w = window; 75 this._deviceEvent = w.DeviceMotionEvent || w.DeviceOrientationEvent; 76 77 //TODO fix DeviceMotionEvent bug on QQ Browser version 4.1 and below. 78 if (cc.Browser.type == "mqqbrowser") { 79 this._deviceEvent = window.DeviceOrientationEvent; 80 } 81 82 this._deviceEventType = (this._deviceEvent == w.DeviceMotionEvent) ? "devicemotion" : "deviceorientation"; 83 var ua = navigator.userAgent; 84 if (/Android/.test(ua) || (/Adr/.test(ua) && cc.Browser.type == "ucbrowser")) { 85 this._minus = -1; 86 } 87 }, 88 89 getDelegate: function () { 90 return this._delegate; 91 }, 92 93 addDelegate: function (delegate) { 94 this._delegate = delegate; 95 var acc = this.didAccelerate.bind(this), w = window, scheduler = cc.Director.getInstance().getScheduler(); 96 if (this._delegate) { 97 w.addEventListener(this._deviceEventType, acc, false); 98 scheduler.scheduleUpdateForTarget(this); 99 } else { 100 w.removeEventListener(this._deviceEventType, acc); 101 scheduler.unscheduleUpdateForTarget(this); 102 } 103 }, 104 105 setAccelerometerInterval: function (interval) { 106 if (this._interval !== interval) { 107 this._interval = interval; 108 } 109 }, 110 111 didAccelerate: function (eventData) { 112 if (!this._delegate) { 113 return; 114 } 115 116 var mAcceleration = this._acceleration; 117 if (this._deviceEvent == window.DeviceMotionEvent) { 118 var eventAcceleration = eventData.accelerationIncludingGravity; 119 mAcceleration.x = this._minus * eventAcceleration.x * 0.1; 120 mAcceleration.y = this._minus * eventAcceleration.y * 0.1; 121 mAcceleration.z = eventAcceleration.z * 0.1; 122 } 123 else { 124 mAcceleration.x = (eventData.gamma / 90) * 0.981; 125 mAcceleration.y = -(eventData.beta / 90) * 0.981; 126 mAcceleration.z = (eventData.alpha / 90) * 0.981; 127 } 128 mAcceleration.timestamp = eventData.timeStamp || Date.now(); 129 130 var tmpX = mAcceleration.x; 131 switch (window.orientation) { 132 case cc.UIInterfaceOrientationLandscapeRight://-90 133 mAcceleration.x = -mAcceleration.y; 134 mAcceleration.y = tmpX; 135 break; 136 137 case cc.UIInterfaceOrientationLandscapeLeft://90 138 mAcceleration.x = mAcceleration.y; 139 mAcceleration.y = -tmpX; 140 break; 141 142 case cc.UIInterfaceOrientationPortraitUpsideDown://180 143 mAcceleration.x = -mAcceleration.x; 144 mAcceleration.y = -mAcceleration.y; 145 break; 146 147 case cc.UIInterfaceOrientationPortrait://0 148 break; 149 } 150 }, 151 update:function(dt){ 152 if(this._curTime > this._interval){ 153 this._curTime -= this._interval; 154 this._delegate.onAccelerometer(this._acceleration); 155 } 156 this._curTime += dt; 157 } 158 }); 159 160 cc.AccelerometerDispatcher.getInstance = function () { 161 if (!this._instance) { 162 this._instance = new cc.AccelerometerDispatcher(); 163 this._instance.init(); 164 } 165 166 return this._instance; 167 };