CCCommon.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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. var cc = cc || {};
  23. cc._tmp = cc._tmp || {};
  24. /**
  25. * Function added for JS bindings compatibility. Not needed in cocos2d-html5.
  26. * @function
  27. * @param {object} jsObj subclass
  28. * @param {object} superclass
  29. */
  30. cc.associateWithNative = function (jsObj, superclass) {
  31. };
  32. /**
  33. * Key map for keyboard event
  34. *
  35. * @constant
  36. * @type {Object}
  37. * @example
  38. cc.eventManager.addListener({
  39. event: cc.EventListener.KEYBOARD,
  40. onKeyPressed: function(keyCode, event){
  41. if (cc.KEY["a"] == keyCode) {
  42. cc.log("A is pressed");
  43. }
  44. }
  45. }, this);
  46. */
  47. cc.KEY = {
  48. backspace:8,
  49. tab:9,
  50. enter:13,
  51. shift:16, //should use shiftkey instead
  52. ctrl:17, //should use ctrlkey
  53. alt:18, //should use altkey
  54. pause:19,
  55. capslock:20,
  56. escape:27,
  57. pageup:33,
  58. pagedown:34,
  59. end:35,
  60. home:36,
  61. left:37,
  62. up:38,
  63. right:39,
  64. down:40,
  65. insert:45,
  66. Delete:46,
  67. 0:48,
  68. 1:49,
  69. 2:50,
  70. 3:51,
  71. 4:52,
  72. 5:53,
  73. 6:54,
  74. 7:55,
  75. 8:56,
  76. 9:57,
  77. a:65,
  78. b:66,
  79. c:67,
  80. d:68,
  81. e:69,
  82. f:70,
  83. g:71,
  84. h:72,
  85. i:73,
  86. j:74,
  87. k:75,
  88. l:76,
  89. m:77,
  90. n:78,
  91. o:79,
  92. p:80,
  93. q:81,
  94. r:82,
  95. s:83,
  96. t:84,
  97. u:85,
  98. v:86,
  99. w:87,
  100. x:88,
  101. y:89,
  102. z:90,
  103. num0:96,
  104. num1:97,
  105. num2:98,
  106. num3:99,
  107. num4:100,
  108. num5:101,
  109. num6:102,
  110. num7:103,
  111. num8:104,
  112. num9:105,
  113. '*':106,
  114. '+':107,
  115. '-':109,
  116. 'numdel':110,
  117. '/':111,
  118. f1:112, //f1-f12 dont work on ie
  119. f2:113,
  120. f3:114,
  121. f4:115,
  122. f5:116,
  123. f6:117,
  124. f7:118,
  125. f8:119,
  126. f9:120,
  127. f10:121,
  128. f11:122,
  129. f12:123,
  130. numlock:144,
  131. scrolllock:145,
  132. semicolon:186,
  133. ',':186,
  134. equal:187,
  135. '=':187,
  136. ';':188,
  137. comma:188,
  138. dash:189,
  139. '.':190,
  140. period:190,
  141. forwardslash:191,
  142. grave:192,
  143. '[':219,
  144. openbracket:219,
  145. ']':221,
  146. closebracket:221,
  147. backslash:220,
  148. quote:222,
  149. space:32
  150. };
  151. /**
  152. * Image Format:JPG
  153. * @constant
  154. * @type {Number}
  155. */
  156. cc.FMT_JPG = 0;
  157. /**
  158. * Image Format:PNG
  159. * @constant
  160. * @type {Number}
  161. */
  162. cc.FMT_PNG = 1;
  163. /**
  164. * Image Format:TIFF
  165. * @constant
  166. * @type {Number}
  167. */
  168. cc.FMT_TIFF = 2;
  169. /**
  170. * Image Format:RAWDATA
  171. * @constant
  172. * @type {Number}
  173. */
  174. cc.FMT_RAWDATA = 3;
  175. /**
  176. * Image Format:WEBP
  177. * @constant
  178. * @type {Number}
  179. */
  180. cc.FMT_WEBP = 4;
  181. /**
  182. * Image Format:UNKNOWN
  183. * @constant
  184. * @type {Number}
  185. */
  186. cc.FMT_UNKNOWN = 5;
  187. /**
  188. * get image format by image data
  189. * @function
  190. * @param {Array} imgData
  191. * @returns {Number}
  192. */
  193. cc.getImageFormatByData = function (imgData) {
  194. // if it is a png file buffer.
  195. if (imgData.length > 8 && imgData[0] == 0x89
  196. && imgData[1] == 0x50
  197. && imgData[2] == 0x4E
  198. && imgData[3] == 0x47
  199. && imgData[4] == 0x0D
  200. && imgData[5] == 0x0A
  201. && imgData[6] == 0x1A
  202. && imgData[7] == 0x0A) {
  203. return cc.FMT_PNG;
  204. }
  205. // if it is a tiff file buffer.
  206. if (imgData.length > 2 && ((imgData[0] == 0x49 && imgData[1] == 0x49)
  207. || (imgData[0] == 0x4d && imgData[1] == 0x4d)
  208. || (imgData[0] == 0xff && imgData[1] == 0xd8))) {
  209. return cc.FMT_TIFF;
  210. }
  211. return cc.FMT_UNKNOWN;
  212. };
  213. /**
  214. * Another way to subclass: Using Google Closure.
  215. * The following code was copied + pasted from goog.base / goog.inherits
  216. * @function
  217. * @param {Function} childCtor
  218. * @param {Function} parentCtor
  219. */
  220. cc.inherits = function (childCtor, parentCtor) {
  221. function tempCtor() {}
  222. tempCtor.prototype = parentCtor.prototype;
  223. childCtor.superClass_ = parentCtor.prototype;
  224. childCtor.prototype = new tempCtor();
  225. childCtor.prototype.constructor = childCtor;
  226. // Copy "static" method, but doesn't generate subclasses.
  227. // for( var i in parentCtor ) {
  228. // childCtor[ i ] = parentCtor[ i ];
  229. // }
  230. };
  231. /**
  232. * @deprecated since v3.0, please use cc.Class.extend and _super
  233. * @cc.Class.extend
  234. */
  235. cc.base = function(me, opt_methodName, var_args) {
  236. var caller = arguments.callee.caller;
  237. if (caller.superClass_) {
  238. // This is a constructor. Call the superclass constructor.
  239. ret = caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1));
  240. return ret;
  241. }
  242. var args = Array.prototype.slice.call(arguments, 2);
  243. var foundCaller = false;
  244. for (var ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
  245. if (ctor.prototype[opt_methodName] === caller) {
  246. foundCaller = true;
  247. } else if (foundCaller) {
  248. return ctor.prototype[opt_methodName].apply(me, args);
  249. }
  250. }
  251. // If we did not find the caller in the prototype chain,
  252. // then one of two things happened:
  253. // 1) The caller is an instance method.
  254. // 2) This method was not called by the right caller.
  255. if (me[opt_methodName] === caller) {
  256. return me.constructor.prototype[opt_methodName].apply(me, args);
  257. } else {
  258. throw Error(
  259. 'cc.base called from a method of one name ' +
  260. 'to a method of a different name');
  261. }
  262. };