CCTouchDispatcher.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * @constant
  24. * @type Number
  25. */
  26. cc.TouchSelectorBeganBit = 1 << 0;
  27. /**
  28. * @constant
  29. * @type Number
  30. */
  31. cc.TouchSelectorMovedBit = 1 << 1;
  32. /**
  33. * @constant
  34. * @type Number
  35. */
  36. cc.TouchSelectorEndedBit = 1 << 2;
  37. /**
  38. * @constant
  39. * @type Number
  40. */
  41. cc.TouchSelectorCancelledBit = 1 << 3;
  42. /**
  43. * @constant
  44. * @type Number
  45. */
  46. cc.TouchSelectorAllBits = (cc.TouchSelectorBeganBit | cc.TouchSelectorMovedBit | cc.TouchSelectorEndedBit | cc.TouchSelectorCancelledBit);
  47. /**
  48. * @constant
  49. * @type Number
  50. */
  51. cc.TOUCH_BEGAN = 0;
  52. /**
  53. * @constant
  54. * @type Number
  55. */
  56. cc.TOUCH_MOVED = 1;
  57. /**
  58. * @constant
  59. * @type Number
  60. */
  61. cc.TOUCH_ENDED = 2;
  62. /**
  63. * @constant
  64. * @type Number
  65. */
  66. cc.TOUCH_CANCELLED = 3;
  67. /**
  68. * @constant
  69. * @type Number
  70. */
  71. cc.TouchMax = 4;
  72. /**
  73. * @function
  74. * @param {cc.TouchHandler} p1
  75. * @param {cc.TouchHandler} p2
  76. * @return {Boolean}
  77. */
  78. cc.less = function (p1, p2) {
  79. return p1.getPriority() > p2.getPriority();
  80. };
  81. /**
  82. * @param {Number} type
  83. * Constructor
  84. */
  85. cc.TouchHandlerHelperData = function (type) {
  86. // we only use the type
  87. this.type = type;
  88. };
  89. /**
  90. * cc.TouchDispatcher.
  91. * Singleton that handles all the touch events.
  92. * The dispatcher dispatches events to the registered TouchHandlers.
  93. * There are 2 different type of touch handlers:
  94. * - Standard Touch Handlers
  95. * - Targeted Touch Handlers
  96. *
  97. * The Standard Touch Handlers work like the CocoaTouch touch handler: a set of touches is passed to the delegate.
  98. * On the other hand, the Targeted Touch Handlers only receive 1 touch at the time, and they can "swallow" touches (avoid the propagation of the event).
  99. *
  100. * Firstly, the dispatcher sends the received touches to the targeted touches.
  101. * These touches can be swallowed by the Targeted Touch Handlers. If there are still remaining touches, then the remaining touches will be sent
  102. * to the Standard Touch Handlers.
  103. * @class
  104. * @extends cc.Class
  105. */
  106. cc.TouchDispatcher = cc.Class.extend(/** @lends cc.TouchDispatcher# */ {
  107. _mousePressed:false,
  108. _targetedHandlers:null,
  109. _standardHandlers:null,
  110. _locked:false,
  111. _toAdd:false,
  112. _toRemove:false,
  113. _handlersToAdd:null,
  114. _handlersToRemove:null,
  115. _toQuit:false,
  116. _dispatchEvents:false,
  117. _handlerHelperData:[new cc.TouchHandlerHelperData(cc.TOUCH_BEGAN), new cc.TouchHandlerHelperData(cc.TOUCH_MOVED), new cc.TouchHandlerHelperData(cc.TOUCH_ENDED), new cc.TouchHandlerHelperData(cc.TOUCH_CANCELLED)],
  118. /**
  119. * @return {Boolean}
  120. */
  121. init:function () {
  122. this._dispatchEvents = true;
  123. this._targetedHandlers = [];
  124. this._standardHandlers = [];
  125. this._handlersToAdd = [];
  126. this._handlersToRemove = [];
  127. this._toRemove = false;
  128. this._toAdd = false;
  129. this._toQuit = false;
  130. this._locked = false;
  131. this._mousePressed = false;
  132. cc.TouchDispatcher.registerHtmlElementEvent(cc.canvas);
  133. return true;
  134. },
  135. _setMousePressed:function (pressed) {
  136. this._mousePressed = pressed;
  137. },
  138. _getMousePressed:function () {
  139. return this._mousePressed;
  140. },
  141. /**
  142. * Whether or not the events are going to be dispatched. Default: true
  143. * @return {Boolean}
  144. */
  145. isDispatchEvents:function () {
  146. return this._dispatchEvents;
  147. },
  148. /**
  149. * @param {Boolean} dispatchEvents
  150. */
  151. setDispatchEvents:function (dispatchEvents) {
  152. this._dispatchEvents = dispatchEvents;
  153. },
  154. /**
  155. * Adds a standard touch delegate to the dispatcher's list.
  156. * See StandardTouchDelegate description.
  157. * IMPORTANT: The delegate will be retained.
  158. * @param {Object} delegate
  159. * @param {Number} [priority=0]
  160. */
  161. _addStandardDelegate:function (delegate, priority) {
  162. priority = priority || 0;
  163. var handler = cc.StandardTouchHandler.create(delegate, priority);
  164. if (!this._locked) {
  165. this._standardHandlers = this.forceAddHandler(handler, this._standardHandlers);
  166. } else {
  167. /* If handler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and retrun.
  168. * Refer issue #752(cocos2d-x)
  169. */
  170. if (this._handlersToRemove.indexOf(delegate) != -1) {
  171. cc.ArrayRemoveObject(this._handlersToRemove, delegate);
  172. return;
  173. }
  174. this._handlersToAdd.push(handler);
  175. this._toAdd = true;
  176. }
  177. },
  178. /**
  179. * @param {Object} delegate
  180. * @param {Number} priority
  181. * @param {Boolean} swallowsTouches
  182. */
  183. _addTargetedDelegate:function (delegate, priority, swallowsTouches) {
  184. var handler = cc.TargetedTouchHandler.create(delegate, priority, swallowsTouches);
  185. if (!this._locked) {
  186. this._targetedHandlers = this.forceAddHandler(handler, this._targetedHandlers);
  187. } else {
  188. /* If handler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and retrun.
  189. * Refer issue #752(cocos2d-x)
  190. */
  191. if (this._handlersToRemove.indexOf(delegate) != -1) {
  192. cc.ArrayRemoveObject(this._handlersToRemove, delegate);
  193. return;
  194. }
  195. this._handlersToAdd.push(handler);
  196. this._toAdd = true;
  197. }
  198. },
  199. /**
  200. * Force add handler
  201. * @param {cc.TouchHandler} handler
  202. * @param {Array} array
  203. * @return {Array}
  204. */
  205. forceAddHandler:function (handler, array) {
  206. var u = 0, h;
  207. for (var i = 0; i < array.length; i++) {
  208. h = array[i];
  209. if (h) {
  210. if (h.getPriority() < handler.getPriority())
  211. ++u;
  212. if (h.getDelegate() == handler.getDelegate()) {
  213. cc.log("cc.TouchDispatcher.forceAddHandler(): The handler has been added.");
  214. return array;
  215. }
  216. }
  217. }
  218. return cc.ArrayAppendObjectToIndex(array, handler, u);
  219. },
  220. /**
  221. * Force remove all delegates
  222. */
  223. forceRemoveAllDelegates:function () {
  224. this._standardHandlers.length = 0;
  225. this._targetedHandlers.length = 0;
  226. },
  227. /**
  228. * Removes a touch delegate.
  229. * The delegate will be released
  230. * @param {cc.TouchDelegate} delegate
  231. */
  232. _removeDelegate:function (delegate) {
  233. if (delegate == null) {
  234. return;
  235. }
  236. if (!this._locked) {
  237. this.forceRemoveDelegate(delegate);
  238. } else {
  239. /*
  240. * If handler is contained in m_pHandlersToAdd, if so remove it from m_pHandlersToAdd and return.
  241. */
  242. var handler = this.findHandler(this._handlersToAdd, delegate);
  243. if (handler) {
  244. cc.ArrayRemoveObject(this._handlersToAdd, handler);
  245. return;
  246. }
  247. this._handlersToRemove.push(delegate);
  248. this._toRemove = true;
  249. }
  250. },
  251. /**
  252. * Removes all touch delegates, releasing all the delegates
  253. */
  254. removeAllDelegates:function () {
  255. if (!this._locked) {
  256. this.forceRemoveAllDelegates();
  257. } else {
  258. this._toQuit = true;
  259. }
  260. },
  261. /**
  262. * Changes the priority of a previously added delegate. The lower the number, the higher the priority
  263. * @param {Number} priority
  264. * @param {cc.TouchDelegate} delegate
  265. */
  266. setPriority:function (priority, delegate) {
  267. if(!delegate)
  268. throw "cc.TouchDispatcher.setPriority(): delegate should be non-null.";
  269. var handler = this.findHandler(delegate);
  270. if(!handler){
  271. cc.log("cc.TouchDispatcher.setPriority(): Can't find TouchHandler.");
  272. return;
  273. }
  274. if (handler.getPriority() != priority) {
  275. handler.setPriority(priority);
  276. this.rearrangeHandlers(this._targetedHandlers);
  277. this.rearrangeHandlers(this._standardHandlers);
  278. }
  279. },
  280. /**
  281. * @param {Array} touches
  282. * @param {event} event
  283. * @param {Number} index
  284. */
  285. touches:function (touches, event, index) {
  286. if(index< 0 || index >=4)
  287. throw "cc.TouchDispatcher.touches(): invalid index";
  288. this._locked = true;
  289. // optimization to prevent a mutable copy when it is not necessary
  290. var targetedHandlersCount = this._targetedHandlers.length;
  291. var standardHandlersCount = this._standardHandlers.length;
  292. var needsMutableSet = (targetedHandlersCount && standardHandlersCount);
  293. var mutableTouches = (needsMutableSet ? touches.slice() : touches);
  294. var helper = this._handlerHelperData[index];
  295. //
  296. // process the target handlers 1st
  297. //
  298. if (targetedHandlersCount > 0) {
  299. var touch, handler, claimed;
  300. for (var i = 0; i < touches.length; i++) {
  301. touch = touches[i];
  302. for (var j = 0; j < this._targetedHandlers.length; j++) {
  303. handler = this._targetedHandlers[j];
  304. if (!handler) {
  305. break;
  306. }
  307. claimed = false;
  308. if (index == cc.TOUCH_BEGAN) {
  309. if (handler.getDelegate().onTouchBegan) {
  310. claimed = handler.getDelegate().onTouchBegan(touch, event);
  311. if (claimed) {
  312. handler.getClaimedTouches().push(touch);
  313. }
  314. }
  315. //} else if (handler.getClaimedTouches().indexOf(touch)> -1){
  316. } else if (handler.getClaimedTouches().length > 0) {
  317. // moved ended cancelled
  318. claimed = true;
  319. switch (helper.type) {
  320. case cc.TOUCH_MOVED:
  321. if (cc.Browser.isMobile) {
  322. if (handler.getDelegate().onTouchMoved) handler.getDelegate().onTouchMoved(touch, event);
  323. } else {
  324. if (this._mousePressed && handler.getDelegate().onTouchMoved) handler.getDelegate().onTouchMoved(touch, event);
  325. }
  326. break;
  327. case cc.TOUCH_ENDED:
  328. if (handler.getDelegate().onTouchEnded) handler.getDelegate().onTouchEnded(touch, event);
  329. handler.getClaimedTouches().length = 0;
  330. //cc.ArrayRemoveObject(handler.getClaimedTouches(),touch);
  331. break;
  332. case cc.TOUCH_CANCELLED:
  333. if (handler.getDelegate().onTouchCancelled) handler.getDelegate().onTouchCancelled(touch, event);
  334. handler.getClaimedTouches().length = 0;
  335. //cc.ArrayRemoveObject(handler.getClaimedTouches(),touch);
  336. break;
  337. }
  338. }
  339. if (claimed && handler.isSwallowsTouches()) {
  340. if (needsMutableSet) {
  341. cc.ArrayRemoveObject(mutableTouches, touch);
  342. }
  343. break;
  344. }
  345. }
  346. }
  347. }
  348. //
  349. // process standard handlers 2nd
  350. //
  351. if (standardHandlersCount > 0) {
  352. for (i = 0; i < this._standardHandlers.length; i++) {
  353. handler = this._standardHandlers[i];
  354. if (!handler) {
  355. break;
  356. }
  357. switch (helper.type) {
  358. case cc.TOUCH_BEGAN:
  359. if (mutableTouches.length > 0) {
  360. if (handler.getDelegate().onTouchesBegan)
  361. handler.getDelegate().onTouchesBegan(mutableTouches, event);
  362. }
  363. break;
  364. case cc.TOUCH_MOVED:
  365. if (mutableTouches.length > 0) {
  366. if (cc.Browser.isMobile) {
  367. if (handler.getDelegate().onTouchesMoved)
  368. handler.getDelegate().onTouchesMoved(mutableTouches, event);
  369. } else {
  370. if (this._mousePressed && handler.getDelegate().onTouchesMoved)
  371. handler.getDelegate().onTouchesMoved(mutableTouches, event);
  372. }
  373. }
  374. break;
  375. case cc.TOUCH_ENDED:
  376. if (handler.getDelegate().onTouchesEnded && mutableTouches.length > 0)
  377. handler.getDelegate().onTouchesEnded(mutableTouches, event);
  378. break;
  379. case cc.TOUCH_CANCELLED:
  380. if (handler.getDelegate().onTouchesCancelled)
  381. handler.getDelegate().onTouchesCancelled(mutableTouches, event);
  382. break;
  383. }
  384. }
  385. }
  386. if (needsMutableSet) {
  387. mutableTouches = null;
  388. }
  389. //
  390. // Optimization. To prevent a [handlers copy] which is expensive
  391. // the add/removes/quit is done after the iterations
  392. //
  393. this._locked = false;
  394. if (this._toRemove) {
  395. this._toRemove = false;
  396. for (i = 0; i < this._handlersToRemove.length; i++) {
  397. this.forceRemoveDelegate(this._handlersToRemove[i]);
  398. }
  399. this._handlersToRemove.length = 0;
  400. }
  401. if (this._toAdd) {
  402. this._toAdd = false;
  403. for (i = 0; i < this._handlersToAdd.length; i++) {
  404. handler = this._handlersToAdd[i];
  405. if (!handler) {
  406. break;
  407. }
  408. if (handler instanceof cc.TargetedTouchHandler) {
  409. this._targetedHandlers = this.forceAddHandler(handler, this._targetedHandlers);
  410. } else {
  411. this._standardHandlers = this.forceAddHandler(handler, this._standardHandlers);
  412. }
  413. }
  414. this._handlersToAdd.length = 0;
  415. }
  416. if (this._toQuit) {
  417. this._toQuit = false;
  418. this.forceRemoveAllDelegates();
  419. }
  420. },
  421. /**
  422. * @param {Array} touches
  423. * @param {event} event
  424. */
  425. touchesBegan:function (touches, event) {
  426. if (this._dispatchEvents) {
  427. this.touches(touches, event, cc.TOUCH_BEGAN);
  428. }
  429. },
  430. /**
  431. * @param {Array} touches
  432. * @param {event} event
  433. */
  434. touchesMoved:function (touches, event) {
  435. if (this._dispatchEvents) {
  436. this.touches(touches, event, cc.TOUCH_MOVED);
  437. }
  438. },
  439. /**
  440. * @param {Array} touches
  441. * @param {event} event
  442. */
  443. touchesEnded:function (touches, event) {
  444. if (this._dispatchEvents) {
  445. this.touches(touches, event, cc.TOUCH_ENDED);
  446. }
  447. },
  448. /**
  449. * @param {Array} touches
  450. * @param {event} event
  451. */
  452. touchesCancelled:function (touches, event) {
  453. if (this._dispatchEvents) {
  454. this.touches(touches, event, cc.TOUCH_CANCELLED);
  455. }
  456. },
  457. /**
  458. * @param {Array||cc.TouchDelegate} array array or delegate
  459. * @param {cc.TouchDelegate} delegate
  460. * @return {cc.TargetedTouchHandler|cc.StandardTouchHandler|Null}
  461. */
  462. findHandler:function (array, delegate) {
  463. switch (arguments.length) {
  464. case 1:
  465. delegate = arguments[0];
  466. if(!delegate)
  467. throw "cc.TouchDispatcher.findHandler(): delegate should be non-null.";
  468. for (var i = 0; i < this._targetedHandlers.length; i++) {
  469. if (this._targetedHandlers[i].getDelegate() == delegate) {
  470. return this._targetedHandlers[i];
  471. }
  472. }
  473. for (i = 0; i < this._standardHandlers.length; i++) {
  474. if (this._standardHandlers[i].getDelegate() == delegate) {
  475. return this._standardHandlers[i];
  476. }
  477. }
  478. return null;
  479. break;
  480. case 2:
  481. if(!array)
  482. throw "cc.TouchDispatcher.findHandler(): array should be non-null.";
  483. if(!delegate)
  484. throw "cc.TouchDispatcher.findHandler(): delegate should be non-null.";
  485. for (i = 0; i < array.length; i++) {
  486. if (array[i].getDelegate() == delegate) {
  487. return array[i];
  488. }
  489. }
  490. return null;
  491. break;
  492. default:
  493. throw "Argument must be non-nil ";
  494. break;
  495. }
  496. },
  497. /**
  498. * @param {cc.TouchDelegate} delegate
  499. */
  500. forceRemoveDelegate:function (delegate) {
  501. var handler;
  502. // XXX: remove it from both handlers ???
  503. // remove handler from m_pStandardHandlers
  504. for (var i = 0; i < this._standardHandlers.length; i++) {
  505. handler = this._standardHandlers[i];
  506. if (handler && handler.getDelegate() == delegate) {
  507. cc.ArrayRemoveObject(this._standardHandlers, handler);
  508. break;
  509. }
  510. }
  511. for (i = 0; i < this._targetedHandlers.length; i++) {
  512. handler = this._targetedHandlers[i];
  513. if (handler && handler.getDelegate() == delegate) {
  514. cc.ArrayRemoveObject(this._targetedHandlers, handler);
  515. break;
  516. }
  517. }
  518. },
  519. /**
  520. * @param {Array} array
  521. */
  522. rearrangeHandlers:function (array) {
  523. array.sort(cc.less);
  524. }
  525. });
  526. /**
  527. * @type {cc.Point}
  528. */
  529. cc.TouchDispatcher.preTouchPoint = cc.p(0, 0);
  530. cc.TouchDispatcher.isRegisterEvent = false;
  531. cc.getHTMLElementPosition = function (element) {
  532. var docElem = document.documentElement;
  533. var win = window;
  534. var box = null;
  535. if (typeof element.getBoundingClientRect === 'function') {
  536. box = element.getBoundingClientRect();
  537. } else {
  538. if (element instanceof HTMLCanvasElement) {
  539. box = {
  540. left:0,
  541. top:0,
  542. width:element.width,
  543. height:element.height
  544. };
  545. } else {
  546. box = {
  547. left:0,
  548. top:0,
  549. width:parseInt(element.style.width),
  550. height:parseInt(element.style.height)
  551. };
  552. }
  553. }
  554. return {
  555. left:box.left + win.pageXOffset - docElem.clientLeft,
  556. top:box.top + win.pageYOffset - docElem.clientTop,
  557. width:box.width,
  558. height:box.height
  559. };
  560. };
  561. cc.ProcessMouseupEvent = function (element, event) {
  562. var pos = cc.getHTMLElementPosition(element);
  563. var tx, ty;
  564. if (event.pageX != null) { //not avalable in <= IE8
  565. tx = event.pageX;
  566. ty = event.pageY;
  567. } else {
  568. pos.left -= document.body.scrollLeft;
  569. pos.top -= document.body.scrollTop;
  570. tx = event.clientX;
  571. ty = event.clientY;
  572. }
  573. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  574. var touch = new cc.Touch(location.x, location.y);
  575. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  576. cc.TouchDispatcher.preTouchPoint.x = location.x;
  577. cc.TouchDispatcher.preTouchPoint.y = location.y;
  578. var posArr = [];
  579. posArr.push(touch);
  580. //csx cc.Director.getInstance().getTouchDispatcher().touchesEnded(posArr, null);
  581. cc.EGLView.getInstance().touchesEnded(posArr, null);
  582. };
  583. /**
  584. * @param {HTMLCanvasElement|HTMLDivElement} element
  585. */
  586. cc.TouchDispatcher.registerHtmlElementEvent = function (element) {
  587. if (cc.TouchDispatcher.isRegisterEvent) return;
  588. if (!cc.Browser.isMobile) {
  589. window.addEventListener('mousedown', function (event) {
  590. cc.Director.getInstance().getTouchDispatcher()._setMousePressed(true);
  591. });
  592. window.addEventListener('mouseup', function (event) {
  593. cc.Director.getInstance().getTouchDispatcher()._setMousePressed(false);
  594. var pos = cc.getHTMLElementPosition(element);
  595. var tx, ty;
  596. if (event.pageX != null) { //not avalable in <= IE8
  597. tx = event.pageX;
  598. ty = event.pageY;
  599. } else {
  600. pos.left -= document.body.scrollLeft;
  601. pos.top -= document.body.scrollTop;
  602. tx = event.clientX;
  603. ty = event.clientY;
  604. }
  605. if (!cc.rectContainsPoint(new cc.Rect(pos.left, pos.top, pos.width, pos.height), cc.p(tx, ty))) {
  606. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  607. var touch = new cc.Touch(location.x, location.y);
  608. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  609. cc.TouchDispatcher.preTouchPoint.x = location.x;
  610. cc.TouchDispatcher.preTouchPoint.y = location.y;
  611. var posArr = [];
  612. posArr.push(touch);
  613. //csx cc.Director.getInstance().getTouchDispatcher().touchesEnded(posArr, null);
  614. cc.EGLView.getInstance().touchesEnded(posArr, null);
  615. }
  616. });
  617. //register canvas mouse event
  618. element.addEventListener("mousedown", function (event) {
  619. var pos = cc.getHTMLElementPosition(element);
  620. var tx, ty;
  621. if (event.pageX != null) { //not avalable in <= IE8
  622. tx = event.pageX;
  623. ty = event.pageY;
  624. } else {
  625. pos.left -= document.body.scrollLeft;
  626. pos.top -= document.body.scrollTop;
  627. tx = event.clientX;
  628. ty = event.clientY;
  629. }
  630. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  631. var touch = new cc.Touch(location.x, location.y);
  632. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  633. cc.TouchDispatcher.preTouchPoint.x = location.x;
  634. cc.TouchDispatcher.preTouchPoint.y = location.y;
  635. var posArr = [];
  636. posArr.push(touch);
  637. //csx cc.Director.getInstance().getTouchDispatcher().touchesBegan(posArr, null);
  638. cc.EGLView.getInstance().touchesBegan(posArr, null);
  639. });
  640. element.addEventListener("mouseup", function (event) {
  641. cc.ProcessMouseupEvent(element, event);
  642. });
  643. element.addEventListener("mousemove", function (event) {
  644. var pos = cc.getHTMLElementPosition(element);
  645. var tx, ty;
  646. if (event.pageX != null) { //not avalable in <= IE8
  647. tx = event.pageX;
  648. ty = event.pageY;
  649. } else {
  650. pos.left -= document.body.scrollLeft;
  651. pos.top -= document.body.scrollTop;
  652. tx = event.clientX;
  653. ty = event.clientY;
  654. }
  655. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  656. var touch = new cc.Touch(location.x, location.y);
  657. //TODO this feature only chrome support
  658. //if((event.button == 0) && (event.which == 1))
  659. // touch._setPressed(true);
  660. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  661. cc.TouchDispatcher.preTouchPoint.x = location.x;
  662. cc.TouchDispatcher.preTouchPoint.y = location.y;
  663. var posArr = [];
  664. posArr.push(touch);
  665. //csx cc.Director.getInstance().getTouchDispatcher().touchesMoved(posArr, null);
  666. cc.EGLView.getInstance().touchesMoved(posArr, null);
  667. });
  668. }
  669. else if(window.navigator.msPointerEnabled){
  670. var _pointerEventsMap = {
  671. "MSPointerDown" : "touchesBegan",
  672. "MSPointerMove" : "touchesMoved",
  673. "MSPointerUp" : "touchesEnded",
  674. "MSPointerCancel" : "touchesCancelled"
  675. };
  676. for(var i in _pointerEventsMap){
  677. (function(_pointerEvent, _touchEvent){
  678. element.addEventListener(_pointerEvent, function (event){
  679. var pos = cc.getHTMLElementPosition(element);
  680. pos.left -= document.body.scrollLeft;
  681. pos.top -= document.body.scrollTop;
  682. var tx, ty, touch, preLocation;
  683. tx = event.clientX;
  684. ty = event.clientY;
  685. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  686. var touch = new cc.Touch(location.x, location.y);
  687. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  688. cc.TouchDispatcher.preTouchPoint.x = location.x;
  689. cc.TouchDispatcher.preTouchPoint.y = location.y;
  690. cc.Director.getInstance().getTouchDispatcher()[_touchEvent]([touch], null);
  691. event.stopPropagation();
  692. event.preventDefault();
  693. }, false);
  694. })(i, _pointerEventsMap[i]);
  695. }
  696. }
  697. else {
  698. //register canvas touch event
  699. element.addEventListener("touchstart", function (event) {
  700. if (!event.changedTouches) return;
  701. var posArr = [];
  702. var pos = cc.getHTMLElementPosition(element);
  703. pos.left -= document.body.scrollLeft;
  704. pos.top -= document.body.scrollTop;
  705. var touch_event, tx, ty, touch, preLocation;
  706. var length = event.changedTouches.length;
  707. for (var i = 0; i < length; i++) {
  708. touch_event = event.changedTouches[i];
  709. //tx = touch_event.pageX;
  710. //ty = touch_event.pageY;
  711. if (touch_event) {
  712. tx = touch_event.clientX;
  713. ty = touch_event.clientY;
  714. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  715. touch = null;
  716. if (touch_event.identifier) {
  717. touch = new cc.Touch(location.x, location.y, touch_event.identifier);
  718. //use Touch Pool
  719. preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation();
  720. touch._setPrevPoint(preLocation.x, preLocation.y);
  721. cc.TouchDispatcher._setPreTouch(touch);
  722. } else {
  723. touch = new cc.Touch(location.x, location.y);
  724. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  725. }
  726. cc.TouchDispatcher.preTouchPoint.x = location.x;
  727. cc.TouchDispatcher.preTouchPoint.y = location.y;
  728. posArr.push(touch);
  729. }
  730. }
  731. //csx cc.Director.getInstance().getTouchDispatcher().touchesBegan(posArr, null);
  732. cc.EGLView.getInstance().touchesBegan(posArr, null);
  733. event.stopPropagation();
  734. event.preventDefault();
  735. }, false);
  736. element.addEventListener("touchmove", function (event) {
  737. if (!event.changedTouches) return;
  738. var posArr = [];
  739. var pos = cc.getHTMLElementPosition(element);
  740. pos.left -= document.body.scrollLeft;
  741. pos.top -= document.body.scrollTop;
  742. var touch_event, tx, ty, touch, preLocation;
  743. var length = event.changedTouches.length;
  744. for (var i = 0; i < length; i++) {
  745. touch_event = event.changedTouches[i];
  746. //tx = touch_event.pageX;
  747. //ty = touch_event.pageY;
  748. if (touch_event) {
  749. tx = touch_event.clientX;
  750. ty = touch_event.clientY;
  751. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  752. touch = null;
  753. if (touch_event.identifier) {
  754. touch = new cc.Touch(location.x, location.y, touch_event.identifier);
  755. //use Touch Pool
  756. preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation();
  757. touch._setPrevPoint(preLocation.x, preLocation.y);
  758. cc.TouchDispatcher._setPreTouch(touch);
  759. } else {
  760. touch = new cc.Touch(location.x, location.y);
  761. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  762. }
  763. cc.TouchDispatcher.preTouchPoint.x = location.x;
  764. cc.TouchDispatcher.preTouchPoint.y = location.y;
  765. posArr.push(touch);
  766. }
  767. }
  768. //csx cc.Director.getInstance().getTouchDispatcher().touchesMoved(posArr, null);
  769. cc.EGLView.getInstance().touchesMoved(posArr, null);
  770. event.stopPropagation();
  771. event.preventDefault();
  772. }, false);
  773. element.addEventListener("touchend", function (event) {
  774. if (!event.changedTouches) return;
  775. var posArr = [];
  776. var pos = cc.getHTMLElementPosition(element);
  777. pos.left -= document.body.scrollLeft;
  778. pos.top -= document.body.scrollTop;
  779. var touch_event, tx, ty, touch, preLocation;
  780. var length = event.changedTouches.length;
  781. for (var i = 0; i < length; i++) {
  782. touch_event = event.changedTouches[i];
  783. //tx = touch_event.pageX;
  784. //ty = touch_event.pageY;
  785. if (touch_event) {
  786. tx = touch_event.clientX;
  787. ty = touch_event.clientY;
  788. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  789. touch = null;
  790. if (touch_event.identifier) {
  791. touch = new cc.Touch(location.x, location.y, touch_event.identifier);
  792. //use Touch Pool
  793. preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation();
  794. touch._setPrevPoint(preLocation.x, preLocation.y);
  795. cc.TouchDispatcher._deletePreTouchWithSameId(touch);
  796. } else {
  797. touch = new cc.Touch(location.x, location.y);
  798. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  799. }
  800. cc.TouchDispatcher.preTouchPoint.x = location.x;
  801. cc.TouchDispatcher.preTouchPoint.y = location.y;
  802. posArr.push(touch);
  803. }
  804. }
  805. //csx cc.Director.getInstance().getTouchDispatcher().touchesEnded(posArr, null);
  806. cc.EGLView.getInstance().touchesEnded(posArr, null);
  807. event.stopPropagation();
  808. event.preventDefault();
  809. }, false);
  810. element.addEventListener("touchcancel", function (event) {
  811. if (!event.changedTouches) return;
  812. var posArr = [];
  813. var pos = cc.getHTMLElementPosition(element);
  814. pos.left -= document.body.scrollLeft;
  815. pos.top -= document.body.scrollTop;
  816. var touch_event, tx, ty, touch, preLocation;
  817. var length = event.changedTouches.length;
  818. for (var i = 0; i < length; i++) {
  819. touch_event = event.changedTouches[i];
  820. //tx = touch_event.pageX;
  821. //ty = touch_event.pageY;
  822. if (touch_event) {
  823. tx = touch_event.clientX;
  824. ty = touch_event.clientY;
  825. var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos);
  826. touch = null;
  827. if (touch_event.identifier) {
  828. touch = new cc.Touch(location.x, location.y, touch_event.identifier);
  829. //use Touch Pool
  830. preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation();
  831. touch._setPrevPoint(preLocation.x, preLocation.y);
  832. cc.TouchDispatcher._deletePreTouchWithSameId(touch);
  833. } else {
  834. touch = new cc.Touch(location.x, location.y);
  835. touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y);
  836. }
  837. cc.TouchDispatcher.preTouchPoint.x = location.x;
  838. cc.TouchDispatcher.preTouchPoint.y = location.y;
  839. posArr.push(touch);
  840. }
  841. }
  842. //csx cc.Director.getInstance().getTouchDispatcher().touchesCancelled(posArr, null);
  843. cc.EGLView.getInstance().touchesCancelled(posArr, null);
  844. event.stopPropagation();
  845. event.preventDefault();
  846. }, false);
  847. }
  848. cc.TouchDispatcher.isRegisterEvent = true;
  849. };
  850. /**
  851. * @param {cc.Touch} touch
  852. * @return {cc.Touch} preTouch
  853. */
  854. cc.TouchDispatcher._getPreTouch = function (touch) {
  855. var preTouch = null;
  856. var preTouchPool = cc.TouchDispatcher._preTouchPool;
  857. var id = touch.getId();
  858. for (var i = preTouchPool.length - 1; i >= 0; i--) {
  859. if (preTouchPool[i].getId() == id) {
  860. preTouch = preTouchPool[i];
  861. break;
  862. }
  863. }
  864. if (!preTouch) {
  865. preTouch = touch;
  866. }
  867. return preTouch;
  868. };
  869. /**
  870. * @param {cc.Touch} touch
  871. */
  872. cc.TouchDispatcher._setPreTouch = function (touch) {
  873. var find = false;
  874. var preTouchPool = cc.TouchDispatcher._preTouchPool;
  875. var id = touch.getId();
  876. for (var i = preTouchPool.length - 1; i >= 0; i--) {
  877. if (preTouchPool[i].getId() == id) {
  878. preTouchPool[i] = touch;
  879. find = true;
  880. break;
  881. }
  882. }
  883. if (!find) {
  884. //debug touches{
  885. //cc.log("Pool.length: " + preTouchPool.length);
  886. //}
  887. if (preTouchPool.length <= 50) {
  888. preTouchPool.push(touch);
  889. } else {
  890. preTouchPool[cc.TouchDispatcher._preTouchPoolPointer] = touch;
  891. cc.TouchDispatcher._preTouchPoolPointer = (cc.TouchDispatcher._preTouchPoolPointer + 1) % 50;
  892. }
  893. }
  894. };
  895. /**
  896. * @param {cc.Touch} touch
  897. */
  898. cc.TouchDispatcher._deletePreTouchWithSameId = function (touch) {
  899. var changeTouch;
  900. var preTouchPool = cc.TouchDispatcher._preTouchPool;
  901. var id = touch.getId();
  902. for (var i = preTouchPool.length - 1; i >= 0; i--) {
  903. if (preTouchPool[i].getId() == id) {
  904. changeTouch = preTouchPool.pop();
  905. if (i != preTouchPool.length) {
  906. preTouchPool[i] = changeTouch;
  907. }
  908. break;
  909. }
  910. }
  911. };
  912. /**
  913. * @type {Array}
  914. */
  915. cc.TouchDispatcher._preTouchPool = [];
  916. /**
  917. * @type {Number}
  918. */
  919. cc.TouchDispatcher._preTouchPoolPointer = 0;
  920. /**
  921. * register a targeted touch delegate to the dispatcher's list.
  922. * @param {Number} priority
  923. * @param {Boolean} swallowsTouches
  924. * @param {Object} delegate
  925. */
  926. cc.registerTargetedDelegate = function(priority, swallowsTouches, delegate){
  927. cc.Director.getInstance().getTouchDispatcher()._addTargetedDelegate(delegate, priority, swallowsTouches);
  928. };
  929. /**
  930. * Adds a standard touch delegate to the dispatcher's list.
  931. * See StandardTouchDelegate description.
  932. * @param {Object} delegate
  933. * @param {Number} [priority=]
  934. */
  935. cc.registerStandardDelegate = function(delegate, priority){
  936. cc.Director.getInstance().getTouchDispatcher()._addStandardDelegate(delegate, priority);
  937. };
  938. /**
  939. * Removes a touch delegate. from TouchDispatcher
  940. * @param delegate
  941. */
  942. cc.unregisterTouchDelegate = function(delegate){
  943. cc.Director.getInstance().getTouchDispatcher()._removeDelegate(delegate);
  944. };