CCCommon.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. * copy an new object
  24. * @function
  25. * @param {object|Array} obj source object
  26. * @return {Array|object}
  27. */
  28. cc.clone = function (obj) {
  29. // Cloning is better if the new object is having the same prototype chain
  30. // as the copied obj (or otherwise, the cloned object is certainly going to
  31. // have a different hidden class). Play with C1/C2 of the
  32. // PerformanceVirtualMachineTests suite to see how this makes an impact
  33. // under extreme conditions.
  34. //
  35. // Object.create(Object.getPrototypeOf(obj)) doesn't work well because the
  36. // prototype lacks a link to the constructor (Carakan, V8) so the new
  37. // object wouldn't have the hidden class that's associated with the
  38. // constructor (also, for whatever reasons, utilizing
  39. // Object.create(Object.getPrototypeOf(obj)) + Object.defineProperty is even
  40. // slower than the original in V8). Therefore, we call the constructor, but
  41. // there is a big caveat - it is possible that the this.init() in the
  42. // constructor would throw with no argument. It is also possible that a
  43. // derived class forgets to set "constructor" on the prototype. We ignore
  44. // these possibities for and the ultimate solution is a standardized
  45. // Object.clone(<object>).
  46. var newObj = (obj.constructor) ? new obj.constructor : {};
  47. // Assuming that the constuctor above initialized all properies on obj, the
  48. // following keyed assignments won't turn newObj into dictionary mode
  49. // becasue they're not *appending new properties* but *assigning existing
  50. // ones* (note that appending indexed properties is another story). See
  51. // CCClass.js for a link to the devils when the assumption fails.
  52. for (var key in obj) {
  53. var copy = obj[key];
  54. // Beware that typeof null == "object" !
  55. if (((typeof copy) == "object") && copy &&
  56. !(copy instanceof cc.Node) && !(copy instanceof HTMLElement)) {
  57. newObj[key] = cc.clone(copy);
  58. } else {
  59. newObj[key] = copy;
  60. }
  61. }
  62. return newObj;
  63. };
  64. /**
  65. * Function added for JS bindings compatibility. Not needed in cocos2d-html5.
  66. * @function
  67. * @param {object} jsObj subclass
  68. * @param {object} superclass
  69. */
  70. cc.associateWithNative = function (jsObj, superclass) {
  71. };
  72. /**
  73. * Is show bebug info on web page
  74. * @constant
  75. * @type {Boolean}
  76. */
  77. cc.IS_SHOW_DEBUG_ON_PAGE = cc.IS_SHOW_DEBUG_ON_PAGE || false;
  78. cc._logToWebPage = function (message) {
  79. var logList = document.getElementById("logInfoList");
  80. if (!logList) {
  81. var logDiv = document.createElement("Div");
  82. logDiv.setAttribute("id", "logInfoDiv");
  83. cc.canvas.parentNode.appendChild(logDiv);
  84. logDiv.setAttribute("width", "200");
  85. logDiv.setAttribute("height", cc.canvas.height);
  86. logDiv.style.zIndex = "99999";
  87. logDiv.style.position = "absolute";
  88. logDiv.style.top = "0";
  89. logDiv.style.left = "0";
  90. logList = document.createElement("ul");
  91. logDiv.appendChild(logList);
  92. logList.setAttribute("id", "logInfoList");
  93. logList.style.height = cc.canvas.height + "px";
  94. logList.style.color = "#fff";
  95. logList.style.textAlign = "left";
  96. logList.style.listStyle = "disc outside";
  97. logList.style.fontSize = "12px";
  98. logList.style.fontFamily = "arial";
  99. logList.style.padding = "0 0 0 20px";
  100. logList.style.margin = "0";
  101. logList.style.textShadow = "0 0 3px #000";
  102. logList.style.zIndex = "99998";
  103. logList.style.position = "absolute";
  104. logList.style.top = "0";
  105. logList.style.left = "0";
  106. logList.style.overflowY = "hidden";
  107. var tempDiv = document.createElement("Div");
  108. logDiv.appendChild(tempDiv);
  109. tempDiv.style.width = "200px";
  110. tempDiv.style.height = cc.canvas.height + "px";
  111. tempDiv.style.opacity = "0.1";
  112. tempDiv.style.background = "#fff";
  113. tempDiv.style.border = "1px solid #dfdfdf";
  114. tempDiv.style.borderRadius = "8px";
  115. }
  116. var addMessage = document.createElement("li");
  117. //var now = new Date();
  118. //addMessage.innerHTML = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " " + now.getMilliseconds() + " " + message;
  119. addMessage.innerHTML = message;
  120. if (logList.childNodes.length == 0) {
  121. logList.appendChild(addMessage);
  122. } else {
  123. logList.insertBefore(addMessage, logList.childNodes[0]);
  124. }
  125. };
  126. /**
  127. * Output Debug message.
  128. * @function
  129. * @param {String} message
  130. */
  131. cc.log = function (message) {
  132. if (!cc.IS_SHOW_DEBUG_ON_PAGE) {
  133. console.log.apply(console, arguments);
  134. } else {
  135. cc._logToWebPage(message);
  136. }
  137. };
  138. /**
  139. * Pop out a message box
  140. * @param {String} message
  141. * @function
  142. */
  143. cc.MessageBox = function (message) {
  144. console.log(message);
  145. };
  146. /**
  147. * Output Assert message.
  148. * @function
  149. * @param {Boolean} cond If cond is false, assert.
  150. * @param {String} message
  151. */
  152. cc.Assert = function (cond, message) {
  153. if (console.assert)
  154. console.assert(cond, message);
  155. else {
  156. if (!cond) {
  157. if (message)
  158. alert(message);
  159. }
  160. }
  161. };
  162. /**
  163. * Update Debug setting.
  164. * @function
  165. */
  166. cc.initDebugSetting = function () {
  167. // cocos2d debug
  168. if (cc.COCOS2D_DEBUG == 0) {
  169. cc.log = function () {
  170. };
  171. cc.logINFO = function () {
  172. };
  173. cc.logERROR = function () {
  174. };
  175. cc.Assert = function () {
  176. };
  177. } else if (cc.COCOS2D_DEBUG == 1) {
  178. cc.logINFO = cc.log;
  179. cc.logERROR = function () {
  180. };
  181. } else if (cc.COCOS2D_DEBUG > 1) {
  182. cc.logINFO = cc.log;
  183. cc.logERROR = cc.log;
  184. }// COCOS2D_DEBUG
  185. };
  186. // Enum the language type supportted now
  187. /**
  188. * English language code
  189. * @constant
  190. * @type Number
  191. */
  192. cc.LANGUAGE_ENGLISH = 0;
  193. /**
  194. * Chinese language code
  195. * @constant
  196. * @type Number
  197. */
  198. cc.LANGUAGE_CHINESE = 1;
  199. /**
  200. * French language code
  201. * @constant
  202. * @type Number
  203. */
  204. cc.LANGUAGE_FRENCH = 2;
  205. /**
  206. * Italian language code
  207. * @constant
  208. * @type Number
  209. */
  210. cc.LANGUAGE_ITALIAN = 3;
  211. /**
  212. * German language code
  213. * @constant
  214. * @type Number
  215. */
  216. cc.LANGUAGE_GERMAN = 4;
  217. /**
  218. * Spanish language code
  219. * @constant
  220. * @type Number
  221. */
  222. cc.LANGUAGE_SPANISH = 5;
  223. /**
  224. * Russian language code
  225. * @constant
  226. * @type Number
  227. */
  228. cc.LANGUAGE_RUSSIAN = 6;
  229. /**
  230. * Korean language code
  231. * @constant
  232. * @type Number
  233. */
  234. cc.LANGUAGE_KOREAN = 7;
  235. /**
  236. * Japanese language code
  237. * @constant
  238. * @type Number
  239. */
  240. cc.LANGUAGE_JAPANESE = 8;
  241. /**
  242. * Hungarian language code
  243. * @constant
  244. * @type Number
  245. */
  246. cc.LANGUAGE_HUNGARIAN = 9;
  247. /**
  248. * Portuguese language code
  249. * @constant
  250. * @type Number
  251. */
  252. cc.LANGUAGE_PORTUGUESE = 10;
  253. /**
  254. * Arabic language code
  255. * @constant
  256. * @type Number
  257. */
  258. cc.LANGUAGE_ARABIC = 11;
  259. /**
  260. * Norwegian language code
  261. * @constant
  262. * @type Number
  263. */
  264. cc.LANGUAGE_NORWEGIAN = 12;
  265. /**
  266. * Polish language code
  267. * @constant
  268. * @type Number
  269. */
  270. cc.LANGUAGE_POLISH = 13;
  271. /**
  272. * keymap
  273. * @example
  274. * //Example
  275. * //to mark a keydown
  276. * cc.keyDown[65] = true;
  277. * //or
  278. * cc.keyMap[cc.KEY.a]
  279. *
  280. * //to mark a keyup
  281. * do cc.keyDown[65] = false;
  282. *
  283. * //to find out if a key is down, check
  284. * if(cc.keyDown[65])
  285. * //or
  286. * if,(cc.keyDown[cc.KEY.space])
  287. * //if its undefined or false or null, its not pressed
  288. * @constant
  289. * @type object
  290. */
  291. cc.KEY = {
  292. backspace:8,
  293. tab:9,
  294. enter:13,
  295. shift:16, //should use shiftkey instead
  296. ctrl:17, //should use ctrlkey
  297. alt:18, //should use altkey
  298. pause:19,
  299. capslock:20,
  300. escape:27,
  301. pageup:33,
  302. pagedown:34,
  303. end:35,
  304. home:36,
  305. left:37,
  306. up:38,
  307. right:39,
  308. down:40,
  309. insert:45,
  310. Delete:46,
  311. 0:48,
  312. 1:49,
  313. 2:50,
  314. 3:51,
  315. 4:52,
  316. 5:53,
  317. 6:54,
  318. 7:55,
  319. 8:56,
  320. 9:57,
  321. a:65,
  322. b:66,
  323. c:67,
  324. d:68,
  325. e:69,
  326. f:70,
  327. g:71,
  328. h:72,
  329. i:73,
  330. j:74,
  331. k:75,
  332. l:76,
  333. m:77,
  334. n:78,
  335. o:79,
  336. p:80,
  337. q:81,
  338. r:82,
  339. s:83,
  340. t:84,
  341. u:85,
  342. v:86,
  343. w:87,
  344. x:88,
  345. y:89,
  346. z:90,
  347. num0:96,
  348. num1:97,
  349. num2:98,
  350. num3:99,
  351. num4:100,
  352. num5:101,
  353. num6:102,
  354. num7:103,
  355. num8:104,
  356. num9:105,
  357. '*':106,
  358. '+':107,
  359. '-':109,
  360. 'numdel':110,
  361. '/':111,
  362. f1:112, //f1-f12 dont work on ie
  363. f2:113,
  364. f3:114,
  365. f4:115,
  366. f5:116,
  367. f6:117,
  368. f7:118,
  369. f8:119,
  370. f9:120,
  371. f10:121,
  372. f11:122,
  373. f12:123,
  374. numlock:144,
  375. scrolllock:145,
  376. semicolon:186,
  377. ',':186,
  378. equal:187,
  379. '=':187,
  380. ';':188,
  381. comma:188,
  382. dash:189,
  383. '.':190,
  384. period:190,
  385. forwardslash:191,
  386. grave:192,
  387. '[':219,
  388. openbracket:219,
  389. ']':221,
  390. closebracket:221,
  391. backslash:220,
  392. quote:222,
  393. space:32
  394. };
  395. /**
  396. * Image Format:JPG
  397. * @constant
  398. * @type Number
  399. */
  400. cc.FMT_JPG = 0;
  401. /**
  402. * Image Format:PNG
  403. * @constant
  404. * @type Number
  405. */
  406. cc.FMT_PNG = 1;
  407. /**
  408. * Image Format:TIFF
  409. * @constant
  410. * @type Number
  411. */
  412. cc.FMT_TIFF = 2;
  413. /**
  414. * Image Format:RAWDATA
  415. * @constant
  416. * @type Number
  417. */
  418. cc.FMT_RAWDATA = 3;
  419. /**
  420. * Image Format:WEBP
  421. * @constant
  422. * @type Number
  423. */
  424. cc.FMT_WEBP = 4;
  425. /**
  426. * Image Format:UNKNOWN
  427. * @constant
  428. * @type Number
  429. */
  430. cc.FMT_UNKNOWN = 5;
  431. cc.getImageFormatByData = function (imgData) {
  432. // if it is a png file buffer.
  433. if (imgData.length > 8) {
  434. if (imgData[0] == 0x89
  435. && imgData[1] == 0x50
  436. && imgData[2] == 0x4E
  437. && imgData[3] == 0x47
  438. && imgData[4] == 0x0D
  439. && imgData[5] == 0x0A
  440. && imgData[6] == 0x1A
  441. && imgData[7] == 0x0A) {
  442. return cc.FMT_PNG;
  443. }
  444. }
  445. // if it is a tiff file buffer.
  446. if (imgData.length > 2) {
  447. if ((imgData[0] == 0x49 && imgData[1] == 0x49)
  448. || (imgData[0] == 0x4d && imgData[1] == 0x4d)
  449. || (imgData[0] == 0xff && imgData[1] == 0xd8)) {
  450. return cc.FMT_TIFF;
  451. }
  452. }
  453. return cc.FMT_UNKNOWN;
  454. };
  455. var CCNS_REG1 = /^\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*$/;
  456. var CCNS_REG2 = /^\s*\{\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*,\s*\{\s*([\-]?\d+[.]?\d*)\s*,\s*([\-]?\d+[.]?\d*)\s*\}\s*\}\s*$/
  457. /**
  458. * Returns a Core Graphics rectangle structure corresponding to the data in a given string. <br/>
  459. * The string is not localized, so items are always separated with a comma. <br/>
  460. * If the string is not well-formed, the function returns cc.RectZero.
  461. * @function
  462. * @param {String} content content A string object whose contents are of the form "{{x,y},{w, h}}",<br/>
  463. * where x is the x coordinate, y is the y coordinate, w is the width, and h is the height. <br/>
  464. * These components can represent integer or float values.
  465. * @return {cc.Rect} A Core Graphics structure that represents a rectangle.
  466. * Constructor
  467. * @example
  468. * // example
  469. * var rect = cc.RectFromString("{{3,2},{4,5}}");
  470. */
  471. cc.RectFromString = function (content) {
  472. var result = CCNS_REG2.exec(content);
  473. if(!result) return cc.RectZero();
  474. return cc.rect(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]), parseFloat(result[4]));
  475. };
  476. /**
  477. * Returns a Core Graphics point structure corresponding to the data in a given string.
  478. * @function
  479. * @param {String} content A string object whose contents are of the form "{x,y}",
  480. * where x is the x coordinate and y is the y coordinate.<br/>
  481. * The x and y values can represent integer or float values. <br/>
  482. * The string is not localized, so items are always separated with a comma.<br/>
  483. * @return {cc.Point} A Core Graphics structure that represents a point.<br/>
  484. * If the string is not well-formed, the function returns cc.PointZero.
  485. * Constructor
  486. * @example
  487. * //example
  488. * var point = cc.PointFromString("{3.0,2.5}");
  489. */
  490. cc.PointFromString = function (content) {
  491. var result = CCNS_REG1.exec(content);
  492. if(!result) return cc.PointZero();
  493. return cc.p(parseFloat(result[1]), parseFloat(result[2]));
  494. };
  495. /**
  496. * Returns a Core Graphics size structure corresponding to the data in a given string.
  497. * @function
  498. * @param {String} content A string object whose contents are of the form "{w, h}",<br/>
  499. * where w is the width and h is the height.<br/>
  500. * The w and h values can be integer or float values. <br/>
  501. * The string is not localized, so items are always separated with a comma.<br/>
  502. * @return {cc.Size} A Core Graphics structure that represents a size.<br/>
  503. * If the string is not well-formed, the function returns cc.SizeZero.
  504. * @example
  505. * // example
  506. * var size = cc.SizeFromString("{3.0,2.5}");
  507. */
  508. cc.SizeFromString = function (content) {
  509. var result = CCNS_REG1.exec(content);
  510. if(!result) return cc.SizeZero();
  511. return cc.size(parseFloat(result[1]), parseFloat(result[2]));
  512. };
  513. /**
  514. *
  515. * @param {Function} selector
  516. * @param {cc.Node} target
  517. * @param {Object|Number|String} data
  518. */
  519. cc.doCallback = function (selector, target, data) {
  520. if(!selector)
  521. return ;
  522. if (target && (typeof(selector) == "string")) {
  523. target[selector](data);
  524. } else if (target && (typeof(selector) == "function")) {
  525. selector.call(target, data);
  526. } else {
  527. selector(data);
  528. }
  529. };