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