CCKeyboardDispatcher.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * android back button
  24. * @deprecated These were for android devices, but does not work in html5 environment
  25. * @constant
  26. * @type Number
  27. */
  28. cc.TYPE_BACK_CLICKED = 1;
  29. /**
  30. * android menu button
  31. * @deprecated for android devices, does not work in html5 environment
  32. * @constant
  33. * @type Number
  34. */
  35. cc.TYPE_MENU_CLICKED = 2;
  36. /**
  37. * Dispatch the keyboard message
  38. * @class
  39. * @extends cc.Class
  40. */
  41. cc.KeyboardDispatcher = cc.Class.extend(/** @lends cc.KeyboardDispatcher# */{
  42. /**
  43. * add delegate to concern keyboard msg
  44. * @param {cc.KeyboardDelegate} delegate keyboard delegate object
  45. */
  46. addDelegate:function (delegate) {
  47. if (!delegate)
  48. return;
  49. if (!this._locked)
  50. this.forceAddDelegate(delegate);
  51. else {
  52. this._handlersToAdd.push(delegate);
  53. this._toAdd = true;
  54. }
  55. },
  56. /**
  57. * remove the delegate from the delegates who concern keyboard msg
  58. * @param {cc.KeyboardDelegate} delegate
  59. */
  60. removeDelegate:function (delegate) {
  61. if (!delegate) {
  62. return;
  63. }
  64. if (!this._locked) {
  65. this.forceRemoveDelegate(delegate);
  66. }
  67. else {
  68. this._handlersToRemove.push(delegate);
  69. this._toRemove = true;
  70. }
  71. },
  72. /**
  73. * force add the delegate
  74. * @param {cc.KeyboardDelegate} delegate
  75. */
  76. forceAddDelegate:function (delegate) {
  77. var handler = cc.KeyboardHandler.create(delegate);
  78. if (handler) {
  79. //if handler already exist
  80. var locDelegates = this._delegates;
  81. for (var i = 0, len = locDelegates.length; i < len; i++) {
  82. if (locDelegates[i].getDelegate() == handler.getDelegate()) {
  83. cc.log("cc.KeyboardDispatcher.forceAddDelegate(): the delegate has been added.");
  84. return;
  85. }
  86. }
  87. this._delegates.push(handler);
  88. }
  89. },
  90. /**
  91. * force remove the delegate
  92. * @param {cc.KeyboardDelegate} delegate
  93. */
  94. forceRemoveDelegate:function (delegate) {
  95. var locDelegates = this._delegates;
  96. for (var i = 0, len = locDelegates.length; i < len; i++) {
  97. if (locDelegates[i].getDelegate() == delegate) {
  98. locDelegates.splice(i, 1);
  99. return;
  100. }
  101. }
  102. },
  103. /**
  104. * dispatch the keyboard message to the delegates
  105. * @param {event} e
  106. * @param {Boolean} keydown whether this is a keydown or keyup
  107. * @return {Boolean}
  108. */
  109. dispatchKeyboardMSG:function (e, keydown) {
  110. this._locked = true;
  111. e.stopPropagation();
  112. e.preventDefault();
  113. var i = 0;
  114. //update keymap
  115. if (keydown && e) { //if keydown and our keymap doesnt have it
  116. //execute all deletegate that registered a keyboard event
  117. for (i = 0; i < this._delegates.length; i++) {
  118. if(this._delegates[i].getDelegate() && this._delegates[i].getDelegate().onKeyDown)
  119. this._delegates[i].getDelegate().onKeyDown(e.keyCode);
  120. }
  121. } else if (!keydown && e) {//if keyup and our keymap have that key in it
  122. for (i = 0; i < this._delegates.length; i++) {
  123. if(this._delegates[i].getDelegate() && this._delegates[i].getDelegate().onKeyUp)
  124. this._delegates[i].getDelegate().onKeyUp(e.keyCode);
  125. }
  126. }
  127. this._locked = false;
  128. if (this._toRemove) {
  129. this._toRemove = false;
  130. var locHandlersToRemove = this._handlersToRemove;
  131. for (i = 0; i < locHandlersToRemove.length; ++i) {
  132. this.forceRemoveDelegate(locHandlersToRemove[i]);
  133. }
  134. locHandlersToRemove.length = 0;
  135. }
  136. if (this._toAdd) {
  137. this._toAdd = false;
  138. var locHandlersToAdd = this._handlersToAdd;
  139. for (i = 0; i < locHandlersToAdd.length; ++i) {
  140. this.forceAddDelegate(locHandlersToAdd[i]);
  141. }
  142. locHandlersToAdd.length = 0;
  143. }
  144. return true;
  145. },
  146. //private
  147. _delegates:[],
  148. _locked:false,
  149. _toAdd:false,
  150. _toRemove:false,
  151. _handlersToAdd:[],
  152. _handlersToRemove:[]
  153. });
  154. /**
  155. * Returns the shared cc.KeyboardDispatcher object for the system.
  156. * @return {cc.keyboardDispatcher}
  157. */
  158. cc.KeyboardDispatcher.getInstance = function () {
  159. if (!cc.keyboardDispatcher) {
  160. cc.keyboardDispatcher = new cc.KeyboardDispatcher();
  161. //make canvas focusable
  162. cc.canvas.setAttribute('tabindex', 1);
  163. cc.canvas.style.outline = 'none';
  164. cc.canvas.style.cursor = 'default';
  165. cc.canvas.addEventListener("keydown", function (e) {
  166. cc.keyboardDispatcher.dispatchKeyboardMSG(e, true);
  167. });
  168. cc.canvas.addEventListener("keyup", function (e) {
  169. cc.keyboardDispatcher.dispatchKeyboardMSG(e, false);
  170. });
  171. }
  172. return cc.keyboardDispatcher;
  173. };
  174. /**
  175. * Release the shared cc.KeyboardDispatcher object from the system.
  176. */
  177. cc.KeyboardDispatcher.purgeSharedDispatcher = function () {
  178. if (cc.keyboardDispatcher) {
  179. delete cc.keyboardDispatcher;
  180. cc.keyboardDispatcher = null;
  181. }
  182. };