CCInputManager.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. /**
  22. * ignore
  23. */
  24. /**
  25. * @constant
  26. * @type {number}
  27. */
  28. cc.UIInterfaceOrientationLandscapeLeft = -90;
  29. /**
  30. * @constant
  31. * @type {number}
  32. */
  33. cc.UIInterfaceOrientationLandscapeRight = 90;
  34. /**
  35. * @constant
  36. * @type {number}
  37. */
  38. cc.UIInterfaceOrientationPortraitUpsideDown = 180;
  39. /**
  40. * @constant
  41. * @type {number}
  42. */
  43. cc.UIInterfaceOrientationPortrait = 0;
  44. /**
  45. * <p>
  46. * This class manages all events of input. include: touch, mouse, accelerometer, keyboard <br/>
  47. * </p>
  48. * @class
  49. * @name cc.inputManager
  50. */
  51. cc.inputManager = /** @lends cc.inputManager# */{
  52. _mousePressed: false,
  53. _isRegisterEvent: false,
  54. _preTouchPoint: cc.p(0,0),
  55. _prevMousePoint: cc.p(0,0),
  56. _preTouchPool: [],
  57. _preTouchPoolPointer: 0,
  58. _touches: [],
  59. _touchesIntegerDict:{},
  60. _indexBitsUsed: 0,
  61. _maxTouches: 5,
  62. _accelEnabled: false,
  63. _accelInterval: 1/30,
  64. _accelMinus: 1,
  65. _accelCurTime: 0,
  66. _acceleration: null,
  67. _accelDeviceEvent: null,
  68. _getUnUsedIndex: function () {
  69. var temp = this._indexBitsUsed;
  70. for (var i = 0; i < this._maxTouches; i++) {
  71. if (!(temp & 0x00000001)) {
  72. this._indexBitsUsed |= (1 << i);
  73. return i;
  74. }
  75. temp >>= 1;
  76. }
  77. // all bits are used
  78. return -1;
  79. },
  80. _removeUsedIndexBit: function (index) {
  81. if (index < 0 || index >= this._maxTouches)
  82. return;
  83. var temp = 1 << index;
  84. temp = ~temp;
  85. this._indexBitsUsed &= temp;
  86. },
  87. _glView: null,
  88. /**
  89. * @function
  90. * @param {Array} touches
  91. */
  92. handleTouchesBegin: function (touches) {
  93. var selTouch, index, curTouch, touchID, handleTouches = [], locTouchIntDict = this._touchesIntegerDict;
  94. for(var i = 0, len = touches.length; i< len; i ++){
  95. selTouch = touches[i];
  96. touchID = selTouch.getID();
  97. index = locTouchIntDict[touchID];
  98. if(index == null){
  99. var unusedIndex = this._getUnUsedIndex();
  100. if (unusedIndex == -1) {
  101. cc.log(cc._LogInfos.inputManager_handleTouchesBegin, unusedIndex);
  102. continue;
  103. }
  104. //curTouch = this._touches[unusedIndex] = selTouch;
  105. curTouch = this._touches[unusedIndex] = new cc.Touch(selTouch._point.x, selTouch._point.y, selTouch.getID());
  106. curTouch._setPrevPoint(selTouch._prevPoint);
  107. locTouchIntDict[touchID] = unusedIndex;
  108. handleTouches.push(curTouch);
  109. }
  110. }
  111. if(handleTouches.length > 0){
  112. this._glView._convertTouchesWithScale(handleTouches);
  113. var touchEvent = new cc.EventTouch(handleTouches);
  114. touchEvent._eventCode = cc.EventTouch.EventCode.BEGAN;
  115. cc.eventManager.dispatchEvent(touchEvent);
  116. }
  117. },
  118. /**
  119. * @function
  120. * @param {Array} touches
  121. */
  122. handleTouchesMove: function(touches){
  123. var selTouch, index, touchID, handleTouches = [], locTouches = this._touches;
  124. for(var i = 0, len = touches.length; i< len; i ++){
  125. selTouch = touches[i];
  126. touchID = selTouch.getID();
  127. index = this._touchesIntegerDict[touchID];
  128. if(index == null){
  129. //cc.log("if the index doesn't exist, it is an error");
  130. continue;
  131. }
  132. if(locTouches[index]){
  133. locTouches[index]._setPoint(selTouch._point);
  134. locTouches[index]._setPrevPoint(selTouch._prevPoint);
  135. handleTouches.push(locTouches[index]);
  136. }
  137. }
  138. if(handleTouches.length > 0){
  139. this._glView._convertTouchesWithScale(handleTouches);
  140. var touchEvent = new cc.EventTouch(handleTouches);
  141. touchEvent._eventCode = cc.EventTouch.EventCode.MOVED;
  142. cc.eventManager.dispatchEvent(touchEvent);
  143. }
  144. },
  145. /**
  146. * @function
  147. * @param {Array} touches
  148. */
  149. handleTouchesEnd: function(touches){
  150. var handleTouches = this.getSetOfTouchesEndOrCancel(touches);
  151. if(handleTouches.length > 0) {
  152. this._glView._convertTouchesWithScale(handleTouches);
  153. var touchEvent = new cc.EventTouch(handleTouches);
  154. touchEvent._eventCode = cc.EventTouch.EventCode.ENDED;
  155. cc.eventManager.dispatchEvent(touchEvent);
  156. }
  157. },
  158. /**
  159. * @function
  160. * @param {Array} touches
  161. */
  162. handleTouchesCancel: function(touches){
  163. var handleTouches = this.getSetOfTouchesEndOrCancel(touches);
  164. if(handleTouches.length > 0) {
  165. this._glView._convertTouchesWithScale(handleTouches);
  166. var touchEvent = new cc.EventTouch(handleTouches);
  167. touchEvent._eventCode = cc.EventTouch.EventCode.CANCELLED;
  168. cc.eventManager.dispatchEvent(touchEvent);
  169. }
  170. },
  171. /**
  172. * @function
  173. * @param {Array} touches
  174. * @returns {Array}
  175. */
  176. getSetOfTouchesEndOrCancel: function(touches) {
  177. var selTouch, index, touchID, handleTouches = [], locTouches = this._touches, locTouchesIntDict = this._touchesIntegerDict;
  178. for(var i = 0, len = touches.length; i< len; i ++){
  179. selTouch = touches[i];
  180. touchID = selTouch.getID();
  181. index = locTouchesIntDict[touchID];
  182. if(index == null){
  183. continue; //cc.log("if the index doesn't exist, it is an error");
  184. }
  185. if(locTouches[index]){
  186. locTouches[index]._setPoint(selTouch._point);
  187. locTouches[index]._setPrevPoint(selTouch._prevPoint);
  188. handleTouches.push(locTouches[index]);
  189. this._removeUsedIndexBit(index);
  190. delete locTouchesIntDict[touchID];
  191. }
  192. }
  193. return handleTouches;
  194. },
  195. /**
  196. * @function
  197. * @param {HTMLElement} element
  198. * @return {Object}
  199. */
  200. getHTMLElementPosition: function (element) {
  201. var docElem = document.documentElement;
  202. var win = window;
  203. var box = null;
  204. if (typeof element.getBoundingClientRect === 'function') {
  205. box = element.getBoundingClientRect();
  206. } else {
  207. if (element instanceof HTMLCanvasElement) {
  208. box = {
  209. left: 0,
  210. top: 0,
  211. width: element.width,
  212. height: element.height
  213. };
  214. } else {
  215. box = {
  216. left: 0,
  217. top: 0,
  218. width: parseInt(element.style.width),
  219. height: parseInt(element.style.height)
  220. };
  221. }
  222. }
  223. return {
  224. left: box.left + win.pageXOffset - docElem.clientLeft,
  225. top: box.top + win.pageYOffset - docElem.clientTop,
  226. width: box.width,
  227. height: box.height
  228. };
  229. },
  230. /**
  231. * @function
  232. * @param {cc.Touch} touch
  233. * @return {cc.Touch}
  234. */
  235. getPreTouch: function(touch){
  236. var preTouch = null;
  237. var locPreTouchPool = this._preTouchPool;
  238. var id = touch.getID();
  239. for (var i = locPreTouchPool.length - 1; i >= 0; i--) {
  240. if (locPreTouchPool[i].getID() == id) {
  241. preTouch = locPreTouchPool[i];
  242. break;
  243. }
  244. }
  245. if (!preTouch)
  246. preTouch = touch;
  247. return preTouch;
  248. },
  249. /**
  250. * @function
  251. * @param {cc.Touch} touch
  252. */
  253. setPreTouch: function(touch){
  254. var find = false;
  255. var locPreTouchPool = this._preTouchPool;
  256. var id = touch.getID();
  257. for (var i = locPreTouchPool.length - 1; i >= 0; i--) {
  258. if (locPreTouchPool[i].getID() == id) {
  259. locPreTouchPool[i] = touch;
  260. find = true;
  261. break;
  262. }
  263. }
  264. if (!find) {
  265. if (locPreTouchPool.length <= 50) {
  266. locPreTouchPool.push(touch);
  267. } else {
  268. locPreTouchPool[this._preTouchPoolPointer] = touch;
  269. this._preTouchPoolPointer = (this._preTouchPoolPointer + 1) % 50;
  270. }
  271. }
  272. },
  273. /**
  274. * @function
  275. * @param {Number} tx
  276. * @param {Number} ty
  277. * @param {cc.Point} pos
  278. * @return {cc.Touch}
  279. */
  280. getTouchByXY: function(tx, ty, pos){
  281. var locPreTouch = this._preTouchPoint;
  282. var location = this._glView.convertToLocationInView(tx, ty, pos);
  283. var touch = new cc.Touch(location.x, location.y);
  284. touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
  285. locPreTouch.x = location.x;
  286. locPreTouch.y = location.y;
  287. return touch;
  288. },
  289. /**
  290. * @function
  291. * @param {cc.Point} location
  292. * @param {cc.Point} pos
  293. * @param {Number} eventType
  294. * @returns {cc.EventMouse}
  295. */
  296. getMouseEvent: function(location, pos, eventType){
  297. var locPreMouse = this._prevMousePoint;
  298. this._glView._convertMouseToLocationInView(location, pos);
  299. var mouseEvent = new cc.EventMouse(eventType);
  300. mouseEvent.setLocation(location.x, location.y);
  301. mouseEvent._setPrevCursor(locPreMouse.x, locPreMouse.y);
  302. locPreMouse.x = location.x;
  303. locPreMouse.y = location.y;
  304. return mouseEvent;
  305. },
  306. /**
  307. * @function
  308. * @param {Touch} event
  309. * @param {cc.Point} pos
  310. * @return {cc.Point}
  311. */
  312. getPointByEvent: function(event, pos){
  313. if (event.pageX != null) //not avalable in <= IE8
  314. return {x: event.pageX, y: event.pageY};
  315. pos.left -= document.body.scrollLeft;
  316. pos.top -= document.body.scrollTop;
  317. return {x: event.clientX, y: event.clientY};
  318. },
  319. /**
  320. * @function
  321. * @param {Touch} event
  322. * @param {cc.Point} pos
  323. * @returns {Array}
  324. */
  325. getTouchesByEvent: function(event, pos){
  326. var touchArr = [], locView = this._glView;
  327. var touch_event, touch, preLocation;
  328. var locPreTouch = this._preTouchPoint;
  329. var length = event.changedTouches.length;
  330. for (var i = 0; i < length; i++) {
  331. touch_event = event.changedTouches[i];
  332. if (touch_event) {
  333. var location;
  334. if (cc.sys.BROWSER_TYPE_FIREFOX === cc.sys.browserType)
  335. location = locView.convertToLocationInView(touch_event.pageX, touch_event.pageY, pos);
  336. else
  337. location = locView.convertToLocationInView(touch_event.clientX, touch_event.clientY, pos);
  338. if (touch_event.identifier != null) {
  339. touch = new cc.Touch(location.x, location.y, touch_event.identifier);
  340. //use Touch Pool
  341. preLocation = this.getPreTouch(touch).getLocation();
  342. touch._setPrevPoint(preLocation.x, preLocation.y);
  343. this.setPreTouch(touch);
  344. } else {
  345. touch = new cc.Touch(location.x, location.y);
  346. touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
  347. }
  348. locPreTouch.x = location.x;
  349. locPreTouch.y = location.y;
  350. touchArr.push(touch);
  351. }
  352. }
  353. return touchArr;
  354. },
  355. /**
  356. * @function
  357. * @param {HTMLElement} element
  358. */
  359. registerSystemEvent: function(element){
  360. if(this._isRegisterEvent) return;
  361. var locView = this._glView = cc.view;
  362. var selfPointer = this;
  363. var supportMouse = ('mouse' in cc.sys.capabilities), supportTouches = ('touches' in cc.sys.capabilities);
  364. //register touch event
  365. if (supportMouse) {
  366. cc._addEventListener(window, 'mousedown', function () {
  367. selfPointer._mousePressed = true;
  368. }, false);
  369. cc._addEventListener(window, 'mouseup', function (event) {
  370. var savePressed = selfPointer._mousePressed;
  371. selfPointer._mousePressed = false;
  372. if(!savePressed)
  373. return;
  374. var pos = selfPointer.getHTMLElementPosition(element);
  375. var location = selfPointer.getPointByEvent(event, pos);
  376. if (!cc.rectContainsPoint(new cc.Rect(pos.left, pos.top, pos.width, pos.height), location)){
  377. if(!supportTouches)
  378. selfPointer.handleTouchesEnd([selfPointer.getTouchByXY(location.x, location.y, pos)]);
  379. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.UP);
  380. mouseEvent.setButton(event.button);
  381. cc.eventManager.dispatchEvent(mouseEvent);
  382. }
  383. }, false);
  384. //register canvas mouse event
  385. cc._addEventListener(element,"mousedown", function (event) {
  386. selfPointer._mousePressed = true;
  387. var pos = selfPointer.getHTMLElementPosition(element);
  388. var location = selfPointer.getPointByEvent(event, pos);
  389. if(!supportTouches)
  390. selfPointer.handleTouchesBegin([selfPointer.getTouchByXY(location.x, location.y, pos)]);
  391. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.DOWN);
  392. mouseEvent.setButton(event.button);
  393. cc.eventManager.dispatchEvent(mouseEvent);
  394. event.stopPropagation();
  395. event.preventDefault();
  396. element.focus();
  397. }, false);
  398. cc._addEventListener(element, "mouseup", function (event) {
  399. selfPointer._mousePressed = false;
  400. var pos = selfPointer.getHTMLElementPosition(element);
  401. var location = selfPointer.getPointByEvent(event, pos);
  402. if(!supportTouches)
  403. selfPointer.handleTouchesEnd([selfPointer.getTouchByXY(location.x, location.y, pos)]);
  404. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.UP);
  405. mouseEvent.setButton(event.button);
  406. cc.eventManager.dispatchEvent(mouseEvent);
  407. event.stopPropagation();
  408. event.preventDefault();
  409. }, false);
  410. cc._addEventListener(element, "mousemove", function (event) {
  411. //if(!selfPointer._mousePressed)
  412. // return;
  413. var pos = selfPointer.getHTMLElementPosition(element);
  414. var location = selfPointer.getPointByEvent(event, pos);
  415. if(!supportTouches)
  416. selfPointer.handleTouchesMove([selfPointer.getTouchByXY(location.x, location.y, pos)]);
  417. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.MOVE);
  418. if(selfPointer._mousePressed)
  419. mouseEvent.setButton(event.button);
  420. else
  421. mouseEvent.setButton(null);
  422. cc.eventManager.dispatchEvent(mouseEvent);
  423. event.stopPropagation();
  424. event.preventDefault();
  425. }, false);
  426. cc._addEventListener(element, "mousewheel", function (event) {
  427. var pos = selfPointer.getHTMLElementPosition(element);
  428. var location = selfPointer.getPointByEvent(event, pos);
  429. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.SCROLL);
  430. mouseEvent.setButton(event.button);
  431. mouseEvent.setScrollData(0, event.wheelDelta);
  432. cc.eventManager.dispatchEvent(mouseEvent);
  433. event.stopPropagation();
  434. event.preventDefault();
  435. }, false);
  436. /* firefox fix */
  437. cc._addEventListener(element, "DOMMouseScroll", function(event) {
  438. var pos = selfPointer.getHTMLElementPosition(element);
  439. var location = selfPointer.getPointByEvent(event, pos);
  440. var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.SCROLL);
  441. mouseEvent.setButton(event.button);
  442. mouseEvent.setScrollData(0, event.detail * -120);
  443. cc.eventManager.dispatchEvent(mouseEvent);
  444. event.stopPropagation();
  445. event.preventDefault();
  446. }, false);
  447. }
  448. if(window.navigator.msPointerEnabled){
  449. var _pointerEventsMap = {
  450. "MSPointerDown" : selfPointer.handleTouchesBegin,
  451. "MSPointerMove" : selfPointer.handleTouchesMove,
  452. "MSPointerUp" : selfPointer.handleTouchesEnd,
  453. "MSPointerCancel" : selfPointer.handleTouchesCancel
  454. };
  455. for(var eventName in _pointerEventsMap){
  456. (function(_pointerEvent, _touchEvent){
  457. cc._addEventListener(element, _pointerEvent, function (event){
  458. var pos = selfPointer.getHTMLElementPosition(element);
  459. pos.left -= document.documentElement.scrollLeft;
  460. pos.top -= document.documentElement.scrollTop;
  461. _touchEvent.call(selfPointer, [selfPointer.getTouchByXY(event.clientX, event.clientY, pos)]);
  462. event.stopPropagation();
  463. }, false);
  464. })(eventName, _pointerEventsMap[eventName]);
  465. }
  466. }
  467. if(supportTouches) {
  468. //register canvas touch event
  469. cc._addEventListener(element,"touchstart", function (event) {
  470. if (!event.changedTouches) return;
  471. var pos = selfPointer.getHTMLElementPosition(element);
  472. pos.left -= document.body.scrollLeft;
  473. pos.top -= document.body.scrollTop;
  474. selfPointer.handleTouchesBegin(selfPointer.getTouchesByEvent(event, pos));
  475. event.stopPropagation();
  476. event.preventDefault();
  477. element.focus();
  478. }, false);
  479. cc._addEventListener(element, "touchmove", function (event) {
  480. if (!event.changedTouches) return;
  481. var pos = selfPointer.getHTMLElementPosition(element);
  482. pos.left -= document.body.scrollLeft;
  483. pos.top -= document.body.scrollTop;
  484. selfPointer.handleTouchesMove(selfPointer.getTouchesByEvent(event, pos));
  485. event.stopPropagation();
  486. event.preventDefault();
  487. }, false);
  488. cc._addEventListener(element, "touchend", function (event) {
  489. if (!event.changedTouches) return;
  490. var pos = selfPointer.getHTMLElementPosition(element);
  491. pos.left -= document.body.scrollLeft;
  492. pos.top -= document.body.scrollTop;
  493. selfPointer.handleTouchesEnd(selfPointer.getTouchesByEvent(event, pos));
  494. event.stopPropagation();
  495. event.preventDefault();
  496. }, false);
  497. cc._addEventListener(element, "touchcancel", function (event) {
  498. if (!event.changedTouches) return;
  499. var pos = selfPointer.getHTMLElementPosition(element);
  500. pos.left -= document.body.scrollLeft;
  501. pos.top -= document.body.scrollTop;
  502. locView.handleTouchesCancel(selfPointer.getTouchesByEvent(event, pos));
  503. event.stopPropagation();
  504. event.preventDefault();
  505. }, false);
  506. }
  507. //register keyboard event
  508. this._registerKeyboardEvent();
  509. //register Accelerometer event
  510. this._registerAccelerometerEvent();
  511. this._isRegisterEvent = true;
  512. },
  513. _registerKeyboardEvent: function(){},
  514. _registerAccelerometerEvent: function(){},
  515. /**
  516. * @function
  517. * @param {Number} dt
  518. */
  519. update:function(dt){
  520. if(this._accelCurTime > this._accelInterval){
  521. this._accelCurTime -= this._accelInterval;
  522. cc.eventManager.dispatchEvent(new cc.EventAcceleration(this._acceleration));
  523. }
  524. this._accelCurTime += dt;
  525. }
  526. };