CCTouchHandler.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * cc.TouchHandler
  24. * Object than contains the delegate and priority of the event handler.
  25. * @class
  26. * @extends cc.Class
  27. */
  28. cc.TouchHandler = cc.Class.extend(/** @lends cc.TouchHandler# */{
  29. _delegate:null,
  30. _priority:0,
  31. _enabledSelectors:0,
  32. /**
  33. * @return {cc.TouchDelegate}
  34. */
  35. getDelegate:function () {
  36. return this._delegate;
  37. },
  38. /**
  39. * @param {cc.TouchDelegate} delegate
  40. */
  41. setDelegate:function (delegate) {
  42. this._delegate = delegate;
  43. },
  44. /**
  45. * @return {Number}
  46. */
  47. getPriority:function () {
  48. return this._priority;
  49. },
  50. /**
  51. * @param {Number} priority
  52. */
  53. setPriority:function (priority) {
  54. this._priority = priority;
  55. },
  56. /**
  57. * Enabled selectors
  58. * @return {Number}
  59. */
  60. getEnabledSelectors:function () {
  61. return this._enabledSelectors;
  62. },
  63. /**
  64. * @param {Number} value
  65. */
  66. setEnalbedSelectors:function (value) {
  67. this._enabledSelectors = value;
  68. },
  69. /**
  70. * initializes a TouchHandler with a delegate and a priority
  71. * @param {cc.TouchDelegate} delegate
  72. * @param {Number} priority
  73. * @return {Boolean}
  74. */
  75. initWithDelegate:function (delegate, priority) {
  76. if(!delegate)
  77. throw "cc.TouchHandler.initWithDelegate(): touch delegate should not be null";
  78. this._delegate = delegate;
  79. this._priority = priority;
  80. this._enabledSelectors = 0;
  81. return true;
  82. }
  83. });
  84. /**
  85. * Create a TouchHandler with a delegate and a priority
  86. * @param {cc.TouchDelegate} delegate
  87. * @param {Number} priority
  88. * @return {cc.TouchHandler}
  89. */
  90. cc.TouchHandler.create = function (delegate, priority) {
  91. var handler = new cc.TouchHandler();
  92. if (handler) {
  93. handler.initWithDelegate(delegate, priority);
  94. }
  95. return handler;
  96. };
  97. /**
  98. * cc.StandardTouchHandler
  99. * It forwardes each event to the delegate.
  100. * @class
  101. * @extends cc.TouchHandler
  102. */
  103. cc.StandardTouchHandler = cc.TouchHandler.extend(/** @lends cc.StandardTouchHandler# */{
  104. /**
  105. * Initializes a TouchHandler with a delegate and a priority
  106. * @param {cc.TouchDelegate} delegate
  107. * @param {Number} priority
  108. * @return {Boolean}
  109. */
  110. initWithDelegate:function (delegate, priority) {
  111. return cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority);
  112. }
  113. });
  114. /**
  115. * Create a TouchHandler with a delegate and a priority
  116. * @param {Object} delegate
  117. * @param {Number} priority
  118. * @return {cc.StandardTouchHandler}
  119. */
  120. cc.StandardTouchHandler.create = function (delegate, priority) {
  121. var handler = new cc.StandardTouchHandler();
  122. if (handler) {
  123. handler.initWithDelegate(delegate, priority);
  124. }
  125. return handler;
  126. };
  127. /**
  128. * @class
  129. * @extends cc.TouchHandler
  130. */
  131. cc.TargetedTouchHandler = cc.TouchHandler.extend(/** @lends cc.TargetedTouchHandler# */{
  132. _swallowsTouches:false,
  133. _claimedTouches:null,
  134. /**
  135. * Whether or not the touches are swallowed
  136. * @return {Boolean}
  137. */
  138. isSwallowsTouches:function () {
  139. return this._swallowsTouches;
  140. },
  141. /**
  142. * @param {Boolean} swallowsTouches
  143. */
  144. setSwallowsTouches:function (swallowsTouches) {
  145. this._swallowsTouches = swallowsTouches;
  146. },
  147. /**
  148. * MutableSet that contains the claimed touches
  149. * @return {Array}
  150. */
  151. getClaimedTouches:function () {
  152. return this._claimedTouches;
  153. },
  154. /**
  155. * Initializes a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not
  156. * @param {cc.TouchDelegate} delegate
  157. * @param {Number} priority
  158. * @param {Boolean} swallow
  159. * @return {Boolean}
  160. */
  161. initWithDelegate:function (delegate, priority, swallow) {
  162. if (cc.TouchHandler.prototype.initWithDelegate.call(this, delegate, priority)) {
  163. this._claimedTouches = [];
  164. this._swallowsTouches = swallow;
  165. return true;
  166. }
  167. return false;
  168. }
  169. });
  170. /**
  171. * Create a TargetedTouchHandler with a delegate, a priority and whether or not it swallows touches or not
  172. * @param {Object} delegate
  173. * @param {Number} priority
  174. * @param {Boolean} swallow
  175. * @return {cc.TargetedTouchHandler}
  176. */
  177. cc.TargetedTouchHandler.create = function (delegate, priority, swallow) {
  178. var handler = new cc.TargetedTouchHandler();
  179. if (handler) {
  180. handler.initWithDelegate(delegate, priority, swallow);
  181. }
  182. return handler;
  183. };