CCInputExtension.js 4.6 KB

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