CCEvent.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. * Base class of all kinds of events.
  23. * @class
  24. * @extends cc.Class
  25. */
  26. cc.Event = cc.Class.extend(/** @lends cc.Event# */{
  27. _type: 0, // Event type
  28. _isStopped: false, //< whether the event has been stopped.
  29. _currentTarget: null, //< Current target
  30. _setCurrentTarget: function (target) {
  31. this._currentTarget = target;
  32. },
  33. ctor: function (type) {
  34. this._type = type;
  35. },
  36. /**
  37. * Gets the event type
  38. * @function
  39. * @returns {Number}
  40. */
  41. getType: function () {
  42. return this._type;
  43. },
  44. /**
  45. * Stops propagation for current event
  46. * @function
  47. */
  48. stopPropagation: function () {
  49. this._isStopped = true;
  50. },
  51. /**
  52. * Checks whether the event has been stopped
  53. * @function
  54. * @returns {boolean}
  55. */
  56. isStopped: function () {
  57. return this._isStopped;
  58. },
  59. /**
  60. * <p>
  61. * Gets current target of the event <br/>
  62. * note: It only be available when the event listener is associated with node. <br/>
  63. * It returns 0 when the listener is associated with fixed priority.
  64. * </p>
  65. * @function
  66. * @returns {cc.Node} The target with which the event associates.
  67. */
  68. getCurrentTarget: function () {
  69. return this._currentTarget;
  70. }
  71. });
  72. //event type
  73. /**
  74. * The type code of Touch event.
  75. * @constant
  76. * @type {number}
  77. */
  78. cc.Event.TOUCH = 0;
  79. /**
  80. * The type code of Keyboard event.
  81. * @constant
  82. * @type {number}
  83. */
  84. cc.Event.KEYBOARD = 1;
  85. /**
  86. * The type code of Acceleration event.
  87. * @constant
  88. * @type {number}
  89. */
  90. cc.Event.ACCELERATION = 2;
  91. /**
  92. * The type code of Mouse event.
  93. * @constant
  94. * @type {number}
  95. */
  96. cc.Event.MOUSE = 3;
  97. /**
  98. * The type code of Custom event.
  99. * @constant
  100. * @type {number}
  101. */
  102. cc.Event.CUSTOM = 4;
  103. /**
  104. * The Custom event
  105. * @class
  106. * @extends cc.Event
  107. */
  108. cc.EventCustom = cc.Event.extend(/** @lends cc.EventCustom# */{
  109. _eventName: null,
  110. _userData: null, // User data
  111. ctor: function (eventName) {
  112. cc.Event.prototype.ctor.call(this, cc.Event.CUSTOM);
  113. this._eventName = eventName;
  114. },
  115. /**
  116. * Sets user data
  117. * @param {*} data
  118. */
  119. setUserData: function (data) {
  120. this._userData = data;
  121. },
  122. /**
  123. * Gets user data
  124. * @returns {*}
  125. */
  126. getUserData: function () {
  127. return this._userData;
  128. },
  129. /**
  130. * Gets event name
  131. * @returns {String}
  132. */
  133. getEventName: function () {
  134. return this._eventName;
  135. }
  136. });
  137. /**
  138. * The mouse event
  139. * @class
  140. * @extends cc.Event
  141. */
  142. cc.EventMouse = cc.Event.extend(/** @lends cc.EventMouse# */{
  143. _eventType: 0,
  144. _button: 0,
  145. _x: 0,
  146. _y: 0,
  147. _prevX: 0,
  148. _prevY: 0,
  149. _scrollX: 0,
  150. _scrollY: 0,
  151. ctor: function (eventType) {
  152. cc.Event.prototype.ctor.call(this, cc.Event.MOUSE);
  153. this._eventType = eventType;
  154. },
  155. /**
  156. * Sets scroll data
  157. * @param {number} scrollX
  158. * @param {number} scrollY
  159. */
  160. setScrollData: function (scrollX, scrollY) {
  161. this._scrollX = scrollX;
  162. this._scrollY = scrollY;
  163. },
  164. /**
  165. * Returns the x axis scroll value
  166. * @returns {number}
  167. */
  168. getScrollX: function () {
  169. return this._scrollX;
  170. },
  171. /**
  172. * Returns the y axis scroll value
  173. * @returns {number}
  174. */
  175. getScrollY: function () {
  176. return this._scrollY;
  177. },
  178. /**
  179. * Sets cursor location
  180. * @param {number} x
  181. * @param {number} y
  182. */
  183. setLocation: function (x, y) {
  184. this._x = x;
  185. this._y = y;
  186. },
  187. /**
  188. * Returns cursor location
  189. * @return {cc.Point} location
  190. */
  191. getLocation: function () {
  192. return {x: this._x, y: this._y};
  193. },
  194. /**
  195. * Returns the current cursor location in screen coordinates
  196. * @return {cc.Point}
  197. */
  198. getLocationInView: function() {
  199. return {x: this._x, y: cc.view._designResolutionSize.height - this._y};
  200. },
  201. _setPrevCursor: function (x, y) {
  202. this._prevX = x;
  203. this._prevY = y;
  204. },
  205. /**
  206. * Returns the delta distance from the previous location to current location
  207. * @return {cc.Point}
  208. */
  209. getDelta: function () {
  210. return {x: this._x - this._prevX, y: this._y - this._prevY};
  211. },
  212. /**
  213. * Returns the X axis delta distance from the previous location to current location
  214. * @return {Number}
  215. */
  216. getDeltaX: function () {
  217. return this._x - this._prevX;
  218. },
  219. /**
  220. * Returns the Y axis delta distance from the previous location to current location
  221. * @return {Number}
  222. */
  223. getDeltaY: function () {
  224. return this._y - this._prevY;
  225. },
  226. /**
  227. * Sets mouse button
  228. * @param {number} button
  229. */
  230. setButton: function (button) {
  231. this._button = button;
  232. },
  233. /**
  234. * Returns mouse button
  235. * @returns {number}
  236. */
  237. getButton: function () {
  238. return this._button;
  239. },
  240. /**
  241. * Returns location X axis data
  242. * @returns {number}
  243. */
  244. getLocationX: function () {
  245. return this._x;
  246. },
  247. /**
  248. * Returns location Y axis data
  249. * @returns {number}
  250. */
  251. getLocationY: function () {
  252. return this._y;
  253. }
  254. });
  255. //Different types of MouseEvent
  256. /**
  257. * The none event code of mouse event.
  258. * @constant
  259. * @type {number}
  260. */
  261. cc.EventMouse.NONE = 0;
  262. /**
  263. * The event type code of mouse down event.
  264. * @constant
  265. * @type {number}
  266. */
  267. cc.EventMouse.DOWN = 1;
  268. /**
  269. * The event type code of mouse up event.
  270. * @constant
  271. * @type {number}
  272. */
  273. cc.EventMouse.UP = 2;
  274. /**
  275. * The event type code of mouse move event.
  276. * @constant
  277. * @type {number}
  278. */
  279. cc.EventMouse.MOVE = 3;
  280. /**
  281. * The event type code of mouse scroll event.
  282. * @constant
  283. * @type {number}
  284. */
  285. cc.EventMouse.SCROLL = 4;
  286. /**
  287. * The tag of Mouse left button
  288. * @constant
  289. * @type {Number}
  290. */
  291. cc.EventMouse.BUTTON_LEFT = 0;
  292. /**
  293. * The tag of Mouse right button (The right button number is 2 on browser)
  294. * @constant
  295. * @type {Number}
  296. */
  297. cc.EventMouse.BUTTON_RIGHT = 2;
  298. /**
  299. * The tag of Mouse middle button (The right button number is 1 on browser)
  300. * @constant
  301. * @type {Number}
  302. */
  303. cc.EventMouse.BUTTON_MIDDLE = 1;
  304. /**
  305. * The tag of Mouse button 4
  306. * @constant
  307. * @type {Number}
  308. */
  309. cc.EventMouse.BUTTON_4 = 3;
  310. /**
  311. * The tag of Mouse button 5
  312. * @constant
  313. * @type {Number}
  314. */
  315. cc.EventMouse.BUTTON_5 = 4;
  316. /**
  317. * The tag of Mouse button 6
  318. * @constant
  319. * @type {Number}
  320. */
  321. cc.EventMouse.BUTTON_6 = 5;
  322. /**
  323. * The tag of Mouse button 7
  324. * @constant
  325. * @type {Number}
  326. */
  327. cc.EventMouse.BUTTON_7 = 6;
  328. /**
  329. * The tag of Mouse button 8
  330. * @constant
  331. * @type {Number}
  332. */
  333. cc.EventMouse.BUTTON_8 = 7;
  334. /**
  335. * The touch event
  336. * @class
  337. * @extends cc.Event
  338. */
  339. cc.EventTouch = cc.Event.extend(/** @lends cc.EventTouch# */{
  340. _eventCode: 0,
  341. _touches: null,
  342. ctor: function (arr) {
  343. cc.Event.prototype.ctor.call(this, cc.Event.TOUCH);
  344. this._touches = arr || [];
  345. },
  346. /**
  347. * Returns event code
  348. * @returns {number}
  349. */
  350. getEventCode: function () {
  351. return this._eventCode;
  352. },
  353. /**
  354. * Returns touches of event
  355. * @returns {Array}
  356. */
  357. getTouches: function () {
  358. return this._touches;
  359. },
  360. _setEventCode: function (eventCode) {
  361. this._eventCode = eventCode;
  362. },
  363. _setTouches: function (touches) {
  364. this._touches = touches;
  365. }
  366. });
  367. /**
  368. * The maximum touch numbers
  369. * @constant
  370. * @type {Number}
  371. */
  372. cc.EventTouch.MAX_TOUCHES = 5;
  373. cc.EventTouch.EventCode = {BEGAN: 0, MOVED: 1, ENDED: 2, CANCELLED: 3};