CCTouchDelegateProtocol.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * The data of touch event
  24. * @class
  25. * @extends cc.Class
  26. */
  27. cc.Touch = cc.Class.extend(/** @lends cc.Touch# */{
  28. _point:null,
  29. _prevPoint:cc.PointZero(),
  30. _id:0,
  31. /**
  32. * Constructor
  33. */
  34. ctor:function (x, y, id) {
  35. this._point = cc.p(x || 0, y || 0);
  36. this._id = id || 0;
  37. },
  38. /**
  39. * get point of touch
  40. * @return {cc.Point}
  41. */
  42. getLocation:function () {
  43. return this._point;
  44. },
  45. /**
  46. * @return {cc.Point}
  47. */
  48. getPreviousLocation:function () {
  49. return this._prevPoint;
  50. },
  51. /**
  52. * @return {cc.Point}
  53. */
  54. getDelta:function () {
  55. return cc.pSub(this._point, this._prevPoint);
  56. },
  57. /**
  58. * @return {Number}
  59. */
  60. getID:function () {
  61. return this._id;
  62. },
  63. /**
  64. * @return {Number}
  65. */
  66. getId:function () {
  67. return this._id;
  68. },
  69. /**
  70. * set information to touch
  71. * @param {Number} id
  72. * @param {Number} x
  73. * @param {Number} y
  74. */
  75. setTouchInfo:function (id, x, y) {
  76. this._prevPoint = this._point;
  77. this._point = cc.p(x || 0, y || 0);
  78. this._id = id;
  79. },
  80. _setPrevPoint:function (x, y) {
  81. this._prevPoint = cc.p(x || 0, y || 0);
  82. }
  83. });
  84. /**
  85. * @class
  86. * @extends cc.Class
  87. */
  88. cc.TouchDelegate = cc.Class.extend(/** @lends cc.TouchDelegate# */{
  89. _eventTypeFuncMap:null,
  90. /**
  91. * Virtual function
  92. * @param {cc.Touch} touch
  93. * @param {event} event
  94. * @return {Boolean}
  95. */
  96. onTouchBegan:function (touch, event) {
  97. return false;
  98. },
  99. /**
  100. * Virtual function
  101. * @param {cc.Touch} touch
  102. * @param {event} event
  103. */
  104. onTouchMoved:function (touch, event) {
  105. },
  106. /**
  107. * Virtual function
  108. * @param {cc.Touch} touch
  109. * @param {event} event
  110. */
  111. onTouchEnded:function (touch, event) {
  112. },
  113. /**
  114. * Virtual function
  115. * @param {cc.Touch} touch
  116. * @param {event} event
  117. */
  118. onTouchCancelled:function (touch, event) {
  119. },
  120. /**
  121. * Virtual function
  122. * @param {Array} touches
  123. * @param {event} event
  124. */
  125. onTouchesBegan:function (touches, event) {
  126. },
  127. /**
  128. * Virtual function
  129. * @param {Array} touches
  130. * @param {event} event
  131. */
  132. onTouchesMoved:function (touches, event) {
  133. },
  134. /**
  135. * Virtual function
  136. * @param {Array} touches
  137. * @param {event} event
  138. */
  139. onTouchesEnded:function (touches, event) {
  140. },
  141. /**
  142. * Virtual function
  143. * @param {Array} touches
  144. * @param {event} event
  145. */
  146. onTouchesCancelled:function (touches, event) {
  147. },
  148. /*
  149. * In TouchesTest, class Padle inherits from cc.Sprite and cc.TargetedTouchDelegate.
  150. * When it invoke cc.registerTargetedDelegate(0, true, this),
  151. * it will crash in cc.TouchHandler.initWithDelegate() because of dynamic_cast() on android.
  152. * I don't know why, so add these functions for the subclass to invoke it's own retain() and release
  153. *Virtual function
  154. */
  155. touchDelegateRetain:function () {
  156. },
  157. /**
  158. * Virtual function
  159. */
  160. touchDelegateRelease:function () {
  161. }
  162. });
  163. /**
  164. * Using this type of delegate results in two benefits:
  165. * - 1. You don't need to deal with cc.Sets, the dispatcher does the job of splitting
  166. * them. You get exactly one UITouch per call.
  167. * - 2. You can *claim* a UITouch by returning YES in onTouchBegan. Updates of claimed
  168. * touches are sent only to the delegate(s) that claimed them. So if you get a move/
  169. * ended/cancelled update you're sure it's your touch. This frees you from doing a
  170. * lot of checks when doing multi-touch.
  171. *
  172. * (The name TargetedTouchDelegate relates to updates "targeting" their specific
  173. * handler, without bothering the other handlers.)
  174. * @class
  175. * @extends cc.Class
  176. */
  177. cc.TargetedTouchDelegate = cc.TouchDelegate.extend(/** @lends cc.TargetedTouchDelegate# */{
  178. /**
  179. * Return YES to claim the touch.
  180. * @param {cc.Touch} touch
  181. * @param {event} event
  182. * @return {Boolean}
  183. */
  184. onTouchBegan:function (touch, event) {
  185. return false;
  186. },
  187. /**
  188. * Virtual function
  189. * @param {cc.Touch} touch
  190. * @param {event} event
  191. */
  192. onTouchMoved:function (touch, event) {
  193. },
  194. /**
  195. * Virtual function
  196. * @param {cc.Touch} touch
  197. * @param {event} event
  198. */
  199. onTouchEnded:function (touch, event) {
  200. },
  201. /**
  202. * Virtual function
  203. * @param {cc.Touch} touch
  204. * @param {event} event
  205. */
  206. onTouchCancelled:function (touch, event) {
  207. }
  208. });
  209. /**
  210. * This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled).
  211. * @class
  212. * @extends cc.Class
  213. */
  214. cc.StandardTouchDelegate = cc.TouchDelegate.extend(/** @lends cc.StandardTouchDelegate# */{
  215. /**
  216. * Virtual function
  217. * @param {Array} touches
  218. * @param {event} event
  219. */
  220. onTouchesBegan:function (touches, event) {
  221. },
  222. /**
  223. * Virtual function
  224. * @param {Array} touches
  225. * @param {event} event
  226. */
  227. onTouchesMoved:function (touches, event) {
  228. },
  229. /**
  230. * Virtual function
  231. * @param {Array} touches
  232. * @param {event} event
  233. */
  234. onTouchesEnded:function (touches, event) {
  235. },
  236. /**
  237. * Virtual function
  238. * @param {Array} touches
  239. * @param {event} event
  240. */
  241. onTouchesCancelled:function (touches, event) {
  242. }
  243. });