CCMacro.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. /**
  23. * @constant
  24. * @type Number
  25. */
  26. cc.INVALID_INDEX = -1;
  27. /**
  28. * PI is the ratio of a circle's circumference to its diameter.
  29. * @constant
  30. * @type Number
  31. */
  32. cc.PI = Math.PI;
  33. /**
  34. * @constant
  35. * @type Number
  36. */
  37. cc.FLT_MAX = parseFloat('3.402823466e+38F');
  38. /**
  39. * @constant
  40. * @type Number
  41. */
  42. cc.FLT_MIN = parseFloat("1.175494351e-38F");
  43. /**
  44. * @constant
  45. * @type Number
  46. */
  47. cc.RAD = cc.PI / 180;
  48. /**
  49. * @constant
  50. * @type Number
  51. */
  52. cc.DEG = 180 / cc.PI;
  53. /**
  54. * maximum unsigned int value
  55. * @constant
  56. * @type Number
  57. */
  58. cc.UINT_MAX = 0xffffffff;
  59. /**
  60. * <p>
  61. * simple macro that swaps 2 variables<br/>
  62. * modified from c++ macro, you need to pass in the x and y variables names in string, <br/>
  63. * and then a reference to the whole object as third variable
  64. * </p>
  65. * @param {String} x
  66. * @param {String} y
  67. * @param {Object} ref
  68. * @function
  69. * @deprecated since v3.0
  70. */
  71. cc.swap = function (x, y, ref) {
  72. if ((typeof ref) == 'object' && (typeof ref.x) != 'undefined' && (typeof ref.y) != 'undefined') {
  73. var tmp = ref[x];
  74. ref[x] = ref[y];
  75. ref[y] = tmp;
  76. } else
  77. cc.log(cc._LogInfos.swap);
  78. };
  79. /**
  80. * <p>
  81. * Linear interpolation between 2 numbers, the ratio sets how much it is biased to each end
  82. * </p>
  83. * @param {Number} a number A
  84. * @param {Number} b number B
  85. * @param {Number} r ratio between 0 and 1
  86. * @function
  87. * @example
  88. * cc.lerp(2,10,0.5)//returns 6<br/>
  89. * cc.lerp(2,10,0.2)//returns 3.6
  90. */
  91. cc.lerp = function (a, b, r) {
  92. return a + (b - a) * r;
  93. };
  94. /**
  95. * get a random number from 0 to 0xffffff
  96. * @function
  97. * @returns {number}
  98. */
  99. cc.rand = function () {
  100. return Math.random() * 0xffffff;
  101. };
  102. /**
  103. * returns a random float between -1 and 1
  104. * @return {Number}
  105. * @function
  106. */
  107. cc.randomMinus1To1 = function () {
  108. return (Math.random() - 0.5) * 2;
  109. };
  110. /**
  111. * returns a random float between 0 and 1
  112. * @return {Number}
  113. * @function
  114. */
  115. cc.random0To1 = Math.random;
  116. /**
  117. * converts degrees to radians
  118. * @param {Number} angle
  119. * @return {Number}
  120. * @function
  121. */
  122. cc.degreesToRadians = function (angle) {
  123. return angle * cc.RAD;
  124. };
  125. /**
  126. * converts radians to degrees
  127. * @param {Number} angle
  128. * @return {Number}
  129. * @function
  130. */
  131. cc.radiansToDegrees = function (angle) {
  132. return angle * cc.DEG;
  133. };
  134. /**
  135. * converts radians to degrees
  136. * @param {Number} angle
  137. * @return {Number}
  138. * @function
  139. */
  140. cc.radiansToDegress = function (angle) {
  141. cc.log(cc._LogInfos.radiansToDegress);
  142. return angle * cc.DEG;
  143. };
  144. /**
  145. * @constant
  146. * @type Number
  147. */
  148. cc.REPEAT_FOREVER = Number.MAX_VALUE - 1;
  149. /**
  150. * default gl blend src function. Compatible with premultiplied alpha images.
  151. * @constant
  152. * @type Number
  153. */
  154. cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302;
  155. /**
  156. * default gl blend dst function. Compatible with premultiplied alpha images.
  157. * @constant
  158. * @type Number
  159. */
  160. cc.BLEND_DST = 0x0303;
  161. /**
  162. * Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
  163. * @param {cc.Node} node setup node
  164. * @function
  165. */
  166. cc.nodeDrawSetup = function (node) {
  167. //cc.glEnable(node._glServerState);
  168. if (node._shaderProgram) {
  169. //cc._renderContext.useProgram(node._shaderProgram._programObj);
  170. node._shaderProgram.use();
  171. node._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
  172. }
  173. };
  174. /**
  175. * <p>
  176. * GL states that are enabled:<br/>
  177. * - GL_TEXTURE_2D<br/>
  178. * - GL_VERTEX_ARRAY<br/>
  179. * - GL_TEXTURE_COORD_ARRAY<br/>
  180. * - GL_COLOR_ARRAY<br/>
  181. * </p>
  182. * @function
  183. */
  184. cc.enableDefaultGLStates = function () {
  185. //TODO OPENGL STUFF
  186. /*
  187. glEnableClientState(GL_VERTEX_ARRAY);
  188. glEnableClientState(GL_COLOR_ARRAY);
  189. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  190. glEnable(GL_TEXTURE_2D);*/
  191. };
  192. /**
  193. * <p>
  194. * Disable default GL states:<br/>
  195. * - GL_TEXTURE_2D<br/>
  196. * - GL_TEXTURE_COORD_ARRAY<br/>
  197. * - GL_COLOR_ARRAY<br/>
  198. * </p>
  199. * @function
  200. */
  201. cc.disableDefaultGLStates = function () {
  202. //TODO OPENGL
  203. /*
  204. glDisable(GL_TEXTURE_2D);
  205. glDisableClientState(GL_COLOR_ARRAY);
  206. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  207. glDisableClientState(GL_VERTEX_ARRAY);
  208. */
  209. };
  210. /**
  211. * <p>
  212. * Increments the GL Draws counts by one.<br/>
  213. * The number of calls per frame are displayed on the screen when the CCDirector's stats are enabled.<br/>
  214. * </p>
  215. * @param {Number} addNumber
  216. * @function
  217. */
  218. cc.incrementGLDraws = function (addNumber) {
  219. cc.g_NumberOfDraws += addNumber;
  220. };
  221. /**
  222. * @constant
  223. * @type Number
  224. */
  225. cc.FLT_EPSILON = 0.0000001192092896;
  226. /**
  227. * <p>
  228. * On Mac it returns 1;<br/>
  229. * On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
  230. * </p>
  231. * @return {Number}
  232. * @function
  233. */
  234. cc.contentScaleFactor = cc.IS_RETINA_DISPLAY_SUPPORTED ? function () {
  235. return cc.director.getContentScaleFactor();
  236. } : function () {
  237. return 1;
  238. };
  239. /**
  240. * Converts a Point in points to pixels
  241. * @param {cc.Point} points
  242. * @return {cc.Point}
  243. * @function
  244. */
  245. cc.pointPointsToPixels = function (points) {
  246. var scale = cc.contentScaleFactor();
  247. return cc.p(points.x * scale, points.y * scale);
  248. };
  249. /**
  250. * Converts a Point in pixels to points
  251. * @param {cc.Rect} pixels
  252. * @return {cc.Point}
  253. * @function
  254. */
  255. cc.pointPixelsToPoints = function (pixels) {
  256. var scale = cc.contentScaleFactor();
  257. return cc.p(pixels.x / scale, pixels.y / scale);
  258. };
  259. cc._pointPixelsToPointsOut = function(pixels, outPoint){
  260. var scale = cc.contentScaleFactor();
  261. outPoint.x = pixels.x / scale;
  262. outPoint.y = pixels.y / scale;
  263. };
  264. /**
  265. * Converts a Size in points to pixels
  266. * @param {cc.Size} sizeInPoints
  267. * @return {cc.Size}
  268. * @function
  269. */
  270. cc.sizePointsToPixels = function (sizeInPoints) {
  271. var scale = cc.contentScaleFactor();
  272. return cc.size(sizeInPoints.width * scale, sizeInPoints.height * scale);
  273. };
  274. /**
  275. * Converts a size in pixels to points
  276. * @param {cc.Size} sizeInPixels
  277. * @return {cc.Size}
  278. * @function
  279. */
  280. cc.sizePixelsToPoints = function (sizeInPixels) {
  281. var scale = cc.contentScaleFactor();
  282. return cc.size(sizeInPixels.width / scale, sizeInPixels.height / scale);
  283. };
  284. cc._sizePixelsToPointsOut = function (sizeInPixels, outSize) {
  285. var scale = cc.contentScaleFactor();
  286. outSize.width = sizeInPixels.width / scale;
  287. outSize.height = sizeInPixels.height / scale;
  288. };
  289. /**
  290. * Converts a rect in pixels to points
  291. * @param {cc.Rect} pixel
  292. * @return {cc.Rect}
  293. * @function
  294. */
  295. cc.rectPixelsToPoints = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) {
  296. var scale = cc.contentScaleFactor();
  297. return cc.rect(pixel.x / scale, pixel.y / scale,
  298. pixel.width / scale, pixel.height / scale);
  299. } : function (p) {
  300. return p;
  301. };
  302. /**
  303. * Converts a rect in points to pixels
  304. * @param {cc.Rect} point
  305. * @return {cc.Rect}
  306. * @function
  307. */
  308. cc.rectPointsToPixels = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
  309. var scale = cc.contentScaleFactor();
  310. return cc.rect(point.x * scale, point.y * scale,
  311. point.width * scale, point.height * scale);
  312. } : function (p) {
  313. return p;
  314. };
  315. /**
  316. * @constant
  317. * @type Number
  318. */
  319. cc.ONE = 1;
  320. /**
  321. * @constant
  322. * @type Number
  323. */
  324. cc.ZERO = 0;
  325. /**
  326. * @constant
  327. * @type Number
  328. */
  329. cc.SRC_ALPHA = 0x0302;
  330. /**
  331. * @constant
  332. * @type Number
  333. */
  334. cc.SRC_ALPHA_SATURATE = 0x308;
  335. /**
  336. * @constant
  337. * @type Number
  338. */
  339. cc.SRC_COLOR = 0x300;
  340. /**
  341. * @constant
  342. * @type Number
  343. */
  344. cc.DST_ALPHA = 0x304;
  345. /**
  346. * @constant
  347. * @type Number
  348. */
  349. cc.DST_COLOR = 0x306;
  350. /**
  351. * @constant
  352. * @type Number
  353. */
  354. cc.ONE_MINUS_SRC_ALPHA = 0x0303;
  355. /**
  356. * @constant
  357. * @type Number
  358. */
  359. cc.ONE_MINUS_SRC_COLOR = 0x301;
  360. /**
  361. * @constant
  362. * @type Number
  363. */
  364. cc.ONE_MINUS_DST_ALPHA = 0x305;
  365. /**
  366. * @constant
  367. * @type Number
  368. */
  369. cc.ONE_MINUS_DST_COLOR = 0x0307;
  370. /**
  371. * @constant
  372. * @type Number
  373. */
  374. cc.ONE_MINUS_CONSTANT_ALPHA = 0x8004;
  375. /**
  376. * @constant
  377. * @type Number
  378. */
  379. cc.ONE_MINUS_CONSTANT_COLOR = 0x8002;
  380. /**
  381. * Check webgl error.Error will be shown in console if exists.
  382. * @function
  383. */
  384. cc.checkGLErrorDebug = function () {
  385. if (cc.renderMode == cc._RENDER_TYPE_WEBGL) {
  386. var _error = cc._renderContext.getError();
  387. if (_error) {
  388. cc.log(cc._LogInfos.checkGLErrorDebug, _error);
  389. }
  390. }
  391. };
  392. //Possible device orientations
  393. /**
  394. * Device oriented vertically, home button on the bottom (UIDeviceOrientationPortrait)
  395. * @constant
  396. * @type Number
  397. */
  398. cc.DEVICE_ORIENTATION_PORTRAIT = 0;
  399. /**
  400. * Device oriented horizontally, home button on the right (UIDeviceOrientationLandscapeLeft)
  401. * @constant
  402. * @type Number
  403. */
  404. cc.DEVICE_ORIENTATION_LANDSCAPE_LEFT = 1;
  405. /**
  406. * Device oriented vertically, home button on the top (UIDeviceOrientationPortraitUpsideDown)
  407. * @constant
  408. * @type Number
  409. */
  410. cc.DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 2;
  411. /**
  412. * Device oriented horizontally, home button on the left (UIDeviceOrientationLandscapeRight)
  413. * @constant
  414. * @type Number
  415. */
  416. cc.DEVICE_ORIENTATION_LANDSCAPE_RIGHT = 3;
  417. /**
  418. * In browsers, we only support 2 orientations by change window size.
  419. * @constant
  420. * @type Number
  421. */
  422. cc.DEVICE_MAX_ORIENTATIONS = 2;
  423. // ------------------- vertex attrib flags -----------------------------
  424. /**
  425. * @constant
  426. * @type {Number}
  427. */
  428. cc.VERTEX_ATTRIB_FLAG_NONE = 0;
  429. /**
  430. * @constant
  431. * @type {Number}
  432. */
  433. cc.VERTEX_ATTRIB_FLAG_POSITION = 1 << 0;
  434. /**
  435. * @constant
  436. * @type {Number}
  437. */
  438. cc.VERTEX_ATTRIB_FLAG_COLOR = 1 << 1;
  439. /**
  440. * @constant
  441. * @type {Number}
  442. */
  443. cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 1 << 2;
  444. /**
  445. * @constant
  446. * @type {Number}
  447. */
  448. cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = ( cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS );
  449. /**
  450. * GL server side states
  451. * @constant
  452. * @type {Number}
  453. */
  454. cc.GL_ALL = 0;
  455. //-------------Vertex Attributes-----------
  456. /**
  457. * @constant
  458. * @type {Number}
  459. */
  460. cc.VERTEX_ATTRIB_POSITION = 0;
  461. /**
  462. * @constant
  463. * @type {Number}
  464. */
  465. cc.VERTEX_ATTRIB_COLOR = 1;
  466. /**
  467. * @constant
  468. * @type {Number}
  469. */
  470. cc.VERTEX_ATTRIB_TEX_COORDS = 2;
  471. /**
  472. * @constant
  473. * @type {Number}
  474. */
  475. cc.VERTEX_ATTRIB_MAX = 3;
  476. //------------Uniforms------------------
  477. /**
  478. * @constant
  479. * @type {Number}
  480. */
  481. cc.UNIFORM_PMATRIX = 0;
  482. /**
  483. * @constant
  484. * @type {Number}
  485. */
  486. cc.UNIFORM_MVMATRIX = 1;
  487. /**
  488. * @constant
  489. * @type {Number}
  490. */
  491. cc.UNIFORM_MVPMATRIX = 2;
  492. /**
  493. * @constant
  494. * @type {Number}
  495. */
  496. cc.UNIFORM_TIME = 3;
  497. /**
  498. * @constant
  499. * @type {Number}
  500. */
  501. cc.UNIFORM_SINTIME = 4;
  502. /**
  503. * @constant
  504. * @type {Number}
  505. */
  506. cc.UNIFORM_COSTIME = 5;
  507. /**
  508. * @constant
  509. * @type {Number}
  510. */
  511. cc.UNIFORM_RANDOM01 = 6;
  512. /**
  513. * @constant
  514. * @type {Number}
  515. */
  516. cc.UNIFORM_SAMPLER = 7;
  517. /**
  518. * @constant
  519. * @type {Number}
  520. */
  521. cc.UNIFORM_MAX = 8;
  522. //------------Shader Name---------------
  523. /**
  524. * @constant
  525. * @type {String}
  526. */
  527. cc.SHADER_POSITION_TEXTURECOLOR = "ShaderPositionTextureColor";
  528. /**
  529. * @constant
  530. * @type {String}
  531. */
  532. cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest";
  533. /**
  534. * @constant
  535. * @type {String}
  536. */
  537. cc.SHADER_POSITION_COLOR = "ShaderPositionColor";
  538. /**
  539. * @constant
  540. * @type {String}
  541. */
  542. cc.SHADER_POSITION_TEXTURE = "ShaderPositionTexture";
  543. /**
  544. * @constant
  545. * @type {String}
  546. */
  547. cc.SHADER_POSITION_TEXTURE_UCOLOR = "ShaderPositionTexture_uColor";
  548. /**
  549. * @constant
  550. * @type {String}
  551. */
  552. cc.SHADER_POSITION_TEXTUREA8COLOR = "ShaderPositionTextureA8Color";
  553. /**
  554. * @constant
  555. * @type {String}
  556. */
  557. cc.SHADER_POSITION_UCOLOR = "ShaderPosition_uColor";
  558. /**
  559. * @constant
  560. * @type {String}
  561. */
  562. cc.SHADER_POSITION_LENGTHTEXTURECOLOR = "ShaderPositionLengthTextureColor";
  563. //------------uniform names----------------
  564. /**
  565. * @constant
  566. * @type {String}
  567. */
  568. cc.UNIFORM_PMATRIX_S = "CC_PMatrix";
  569. /**
  570. * @constant
  571. * @type {String}
  572. */
  573. cc.UNIFORM_MVMATRIX_S = "CC_MVMatrix";
  574. /**
  575. * @constant
  576. * @type {String}
  577. */
  578. cc.UNIFORM_MVPMATRIX_S = "CC_MVPMatrix";
  579. /**
  580. * @constant
  581. * @type {String}
  582. */
  583. cc.UNIFORM_TIME_S = "CC_Time";
  584. /**
  585. * @constant
  586. * @type {String}
  587. */
  588. cc.UNIFORM_SINTIME_S = "CC_SinTime";
  589. /**
  590. * @constant
  591. * @type {String}
  592. */
  593. cc.UNIFORM_COSTIME_S = "CC_CosTime";
  594. /**
  595. * @constant
  596. * @type {String}
  597. */
  598. cc.UNIFORM_RANDOM01_S = "CC_Random01";
  599. /**
  600. * @constant
  601. * @type {String}
  602. */
  603. cc.UNIFORM_SAMPLER_S = "CC_Texture0";
  604. /**
  605. * @constant
  606. * @type {String}
  607. */
  608. cc.UNIFORM_ALPHA_TEST_VALUE_S = "CC_alpha_value";
  609. //------------Attribute names--------------
  610. /**
  611. * @constant
  612. * @type {String}
  613. */
  614. cc.ATTRIBUTE_NAME_COLOR = "a_color";
  615. /**
  616. * @constant
  617. * @type {String}
  618. */
  619. cc.ATTRIBUTE_NAME_POSITION = "a_position";
  620. /**
  621. * @constant
  622. * @type {String}
  623. */
  624. cc.ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
  625. /**
  626. * default size for font size
  627. * @constant
  628. * @type Number
  629. */
  630. cc.ITEM_SIZE = 32;
  631. /**
  632. * default tag for current item
  633. * @constant
  634. * @type Number
  635. */
  636. cc.CURRENT_ITEM = 0xc0c05001;
  637. /**
  638. * default tag for zoom action tag
  639. * @constant
  640. * @type Number
  641. */
  642. cc.ZOOM_ACTION_TAG = 0xc0c05002;
  643. /**
  644. * default tag for normal
  645. * @constant
  646. * @type Number
  647. */
  648. cc.NORMAL_TAG = 8801;
  649. /**
  650. * default selected tag
  651. * @constant
  652. * @type Number
  653. */
  654. cc.SELECTED_TAG = 8802;
  655. /**
  656. * default disabled tag
  657. * @constant
  658. * @type Number
  659. */
  660. cc.DISABLE_TAG = 8803;
  661. // Array utils
  662. /**
  663. * Verify Array's Type
  664. * @param {Array} arr
  665. * @param {function} type
  666. * @return {Boolean}
  667. * @function
  668. */
  669. cc.arrayVerifyType = function (arr, type) {
  670. if (arr && arr.length > 0) {
  671. for (var i = 0; i < arr.length; i++) {
  672. if (!(arr[i] instanceof type)) {
  673. cc.log("element type is wrong!");
  674. return false;
  675. }
  676. }
  677. }
  678. return true;
  679. };
  680. /**
  681. * Searches for the first occurance of object and removes it. If object is not found the function has no effect.
  682. * @function
  683. * @param {Array} arr Source Array
  684. * @param {*} delObj remove object
  685. */
  686. cc.arrayRemoveObject = function (arr, delObj) {
  687. for (var i = 0, l = arr.length; i < l; i++) {
  688. if (arr[i] == delObj) {
  689. arr.splice(i, 1);
  690. break;
  691. }
  692. }
  693. };
  694. /**
  695. * Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be removed.
  696. * @function
  697. * @param {Array} arr Source Array
  698. * @param {Array} minusArr minus Array
  699. */
  700. cc.arrayRemoveArray = function (arr, minusArr) {
  701. for (var i = 0, l = minusArr.length; i < l; i++) {
  702. cc.arrayRemoveObject(arr, minusArr[i]);
  703. }
  704. };
  705. /**
  706. * Inserts some objects at index
  707. * @function
  708. * @param {Array} arr
  709. * @param {Array} addObjs
  710. * @param {Number} index
  711. * @return {Array}
  712. */
  713. cc.arrayAppendObjectsToIndex = function(arr, addObjs,index){
  714. arr.splice.apply(arr, [index, 0].concat(addObjs));
  715. return arr;
  716. };
  717. /**
  718. * Copy an array's item to a new array (its performance is better than Array.slice)
  719. * @param {Array} arr
  720. * @return {Array}
  721. */
  722. cc.copyArray = function(arr){
  723. var i, len = arr.length, arr_clone = new Array(len);
  724. for (i = 0; i < len; i += 1)
  725. arr_clone[i] = arr[i];
  726. return arr_clone;
  727. };