1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * RGB color composed of bytes 3 bytes 29 * @Class 30 * @Construct 31 * @param {Number | cc.Color3B} r1 red value (0 to 255) or destination color of new color 32 * @param {Number} g1 green value (0 to 255) 33 * @param {Number} b1 blue value (0 to 255) 34 * @example 35 * //create an empty color 36 * var color1 = new cc.Color3B(); 37 * 38 * //create a red color 39 * var redColor = new cc.Color3B(255,0,0); 40 * 41 * //create a new color with color 42 * var newColor = new cc.Color3B(redColor); 43 */ 44 cc.Color3B = function (r1, g1, b1) { 45 switch (arguments.length) { 46 case 0: 47 this.r = 0; 48 this.g = 0; 49 this.b = 0; 50 break; 51 case 1: 52 if (r1 && r1 instanceof cc.Color3B) { 53 this.r = (0 | r1.r) || 0; 54 this.g = (0 | r1.g) || 0; 55 this.b = (0 | r1.b) || 0; 56 } else { 57 this.r = 0; 58 this.g = 0; 59 this.b = 0; 60 } 61 break; 62 case 3: 63 this.r = (0 | r1) || 0; 64 this.g = (0 | g1) || 0; 65 this.b = (0 | b1) || 0; 66 break; 67 default: 68 throw "unknown argument type"; 69 break; 70 } 71 }; 72 73 /** 74 * helper macro that creates an ccColor3B type 75 * @function 76 * @param {Number} r red value (0 to 255) 77 * @param {Number} g green value (0 to 255) 78 * @param {Number} b blue value (0 to 255) 79 * @return {Number,Number,Number} 80 */ 81 cc.c3b = function (r, g, b) { 82 return new cc.Color3B(r, g, b); 83 }; 84 85 cc.integerToColor3B = function (intValue) { 86 intValue = intValue || 0; 87 88 var offset = 0xff; 89 var retColor = new cc.Color3B(); 90 retColor.r = intValue & (offset); 91 retColor.g = (intValue >> 8) & offset; 92 retColor.b = (intValue >> 16) & offset; 93 return retColor; 94 }; 95 96 // compatibility 97 cc.c3 = cc.c3b; 98 99 100 //ccColor3B predefined colors 101 Object.defineProperties(cc, { 102 WHITE:{ 103 get:function () { 104 return cc.c3b(255, 255, 255); 105 } 106 }, 107 YELLOW:{ 108 get:function () { 109 return cc.c3b(255, 255, 0); 110 } 111 }, 112 BLUE:{ 113 get:function () { 114 return cc.c3b(0, 0, 255); 115 } 116 }, 117 GREEN:{ 118 get:function () { 119 return cc.c3b(0, 255, 0); 120 } 121 }, 122 RED:{ 123 get:function () { 124 return cc.c3b(255, 0, 0); 125 } 126 }, 127 MAGENTA:{ 128 get:function () { 129 return cc.c3b(255, 0, 255); 130 } 131 }, 132 BLACK:{ 133 get:function () { 134 return cc.c3b(0, 0, 0); 135 } 136 }, 137 ORANGE:{ 138 get:function () { 139 return cc.c3b(255, 127, 0); 140 } 141 }, 142 GRAY:{ 143 get:function () { 144 return cc.c3b(166, 166, 166); 145 } 146 } 147 }); 148 149 /** 150 * White color (255,255,255) 151 * @constant 152 * @type {Number,Number,Number} 153 */ 154 cc.white = function () { 155 return new cc.Color3B(255, 255, 255); 156 }; 157 158 /** 159 * Yellow color (255,255,0) 160 * @constant 161 * @type {Number,Number,Number} 162 */ 163 cc.yellow = function () { 164 return new cc.Color3B(255, 255, 0); 165 }; 166 167 /** 168 * Blue color (0,0,255) 169 * @constant 170 * @type {Number,Number,Number} 171 */ 172 cc.blue = function () { 173 return new cc.Color3B(0, 0, 255); 174 }; 175 176 /** 177 * Green Color (0,255,0) 178 * @constant 179 * @type {Number,Number,Number} 180 */ 181 cc.green = function () { 182 return new cc.Color3B(0, 255, 0); 183 }; 184 185 /** 186 * Red Color (255,0,0,) 187 * @constant 188 * @type {Number,Number,Number} 189 */ 190 cc.red = function () { 191 return new cc.Color3B(255, 0, 0); 192 }; 193 194 /** 195 * Magenta Color (255,0,255) 196 * @constant 197 * @type {Number,Number,Number} 198 */ 199 cc.magenta = function () { 200 return new cc.Color3B(255, 0, 255); 201 }; 202 203 /** 204 * Black Color (0,0,0) 205 * @constant 206 * @type {Number,Number,Number} 207 */ 208 cc.black = function () { 209 return new cc.Color3B(0, 0, 0); 210 }; 211 212 /** 213 * Orange Color (255,127,0) 214 * @constant 215 * @type {Number,Number,Number} 216 */ 217 cc.orange = function () { 218 return new cc.Color3B(255, 127, 0); 219 }; 220 221 /** 222 * Gray Color (166,166,166) 223 * @constant 224 * @type {Number,Number,Number} 225 */ 226 cc.gray = function () { 227 return new cc.Color3B(166, 166, 166); 228 }; 229 230 /** 231 * RGBA color composed of 4 bytes 232 * @Class 233 * @Construct 234 * @param {Number} r1 red value (0 to 255) 235 * @param {Number} g1 green value (0 to 255) 236 * @param {Number} b1 blue value (0 to 255) 237 * @param {Number} a1 Alpha value (0 to 255) 238 * @example 239 * //create a red color 240 * var redColor = new cc.Color4B(255,0,0,255); 241 */ 242 cc.Color4B = function (r1, g1, b1, a1) { 243 this.r = 0 | r1; 244 this.g = 0 | g1; 245 this.b = 0 | b1; 246 this.a = 0 | a1; 247 }; 248 249 /** 250 * helper macro that creates an ccColor4B type 251 * @function 252 * @param {Number} r red value (0 to 255) 253 * @param {Number} g green value (0 to 255) 254 * @param {Number} b blue value (0 to 255) 255 * @param {Number} a Alpha value (0 to 255) 256 * @return {Number,Number,Number,Number} 257 */ 258 cc.c4b = function (r, g, b, a) { 259 return new cc.Color4B(r, g, b, a); 260 }; 261 262 // backwards compatibility 263 cc.c4 = cc.c4b; 264 265 /** 266 * RGBA color composed of 4 floats 267 * @Class 268 * @Construct 269 * @param {Number} r1 red value (0 to 1) 270 * @param {Number} g1 green value (0 to 1) 271 * @param {Number} b1 blue value (0 to 1) 272 * @param {Number} a1 Alpha value (0 to 1) 273 * @example 274 * //create a red color 275 * var redColor = new cc.Color4F(1,0,0,1); 276 */ 277 cc.Color4F = function (r1, g1, b1, a1) { 278 this.r = r1; 279 this.g = g1; 280 this.b = b1; 281 this.a = a1; 282 }; 283 284 285 /** 286 * helper macro that creates an ccColor4F type 287 * @Class 288 * @Construct 289 * @param {Number} r red value (0 to 1) 290 * @param {Number} g green value (0 to 1) 291 * @param {Number} b blue value (0 to 1) 292 * @param {Number} a Alpha value (0 to 1) 293 * @example 294 * //create a red color 295 * var redColor = cc.c4f(1,0,0,1); 296 */ 297 cc.c4f = function (r, g, b, a) { 298 return new cc.Color4F(r, g, b, a); 299 }; 300 301 /** 302 * Returns a cc.Color4F from a cc.Color3B. Alpha will be 1. 303 * @function 304 * @param {cc.Color3B} c color 305 * @return {cc.Color4F} 306 */ 307 cc.c4FFromccc3B = function (c) { 308 return new cc.Color4F(c.r / 255.0, c.g / 255.0, c.b / 255.0, 1.0); 309 }; 310 311 /** 312 * Returns a cc.Color4F from a cc.Color4B. 313 * @function 314 * @param {cc.Color4B} c Color 315 * @return {cc.Color4F} 316 */ 317 cc.c4FFromccc4B = function (c) { 318 return new cc.Color4F(c.r / 255.0, c.g / 255.0, c.b / 255.0, c.a / 255.0); 319 }; 320 321 /** 322 * returns YES if both cc.Color4F are equal. Otherwise it returns NO. 323 * @param {cc.Color4F} a color1 324 * @param {cc.Color4F} b color2 325 * @return {Boolean} 326 */ 327 cc.c4FEqual = function (a, b) { 328 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; 329 }; 330 331 /** 332 * A vertex composed of 2 floats: x, y 333 * @Class 334 * @Construct 335 * @param {Number} x1 336 * @param {Number} y1 337 */ 338 cc.Vertex2F = function (x1, y1) { 339 this.x = x1 || 0; 340 this.y = y1 || 0; 341 }; 342 343 /** 344 * helper macro that creates an Vertex2F type 345 * @function 346 * @param {Number} x 347 * @param {Number} y 348 * @return {cc.Vertex2F} 349 */ 350 cc.Vertex2 = function (x, y) { 351 return new cc.Vertex2F(x, y); 352 }; 353 354 /** 355 * A vertex composed of 3 floats: x, y, z 356 * @Class 357 * @Construct 358 * @param {Number} x1 359 * @param {Number} y1 360 * @param {Number} z1 361 */ 362 cc.Vertex3F = function (x1, y1, z1) { 363 this.x = x1 || 0; 364 this.y = y1 || 0; 365 this.z = z1 || 0; 366 }; 367 368 /** 369 * helper macro that creates an Vertex3F type 370 * @function 371 * @param {Number} x 372 * @param {Number} y 373 * @param {Number} z 374 * @return {cc.Vertex3F} 375 */ 376 cc.vertex3 = function (x, y, z) { 377 return new cc.Vertex3F(x, y, z); 378 }; 379 380 /** 381 * A texcoord composed of 2 floats: u, y 382 * @Class 383 * @Construct 384 * @param {Number} u1 385 * @param {Number} v1 386 */ 387 cc.Tex2F = function (u1, v1) { 388 this.u = u1 || 0; 389 this.v = v1 || 0; 390 }; 391 392 /** 393 * helper macro that creates an Tex2F type 394 * @function 395 * @param {Number} u 396 * @param {Number} v 397 * @return {cc.Tex2F} 398 */ 399 cc.tex2 = function (u, v) { 400 return new cc.Tex2F(u, v); 401 }; 402 403 /** 404 * Point Sprite component 405 * @Class 406 * @Construct 407 * @param {cc.Vertex2F} pos1 408 * @param {cc.Color4B} color1 409 * @param {Number} size1 410 */ 411 cc.PointSprite = function (pos1, color1, size1) { 412 this.pos = pos1 || new cc.Vertex2F(0, 0); 413 this.color = color1 || new cc.Color4B(0, 0, 0, 0); 414 this.size = size1 || 0; 415 }; 416 417 /** 418 * A 2D Quad. 4 * 2 floats 419 * @Class 420 * @Construct 421 * @param {cc.Vertex2F} tl1 422 * @param {cc.Vertex2F} tr1 423 * @param {cc.Vertex2F} bl1 424 * @param {cc.Vertex2F} br1 425 */ 426 cc.Quad2 = function (tl1, tr1, bl1, br1) { 427 this.tl = tl1 || new cc.Vertex2F(0, 0); 428 this.tr = tr1 || new cc.Vertex2F(0, 0); 429 this.bl = bl1 || new cc.Vertex2F(0, 0); 430 this.br = br1 || new cc.Vertex2F(0, 0); 431 }; 432 433 /** 434 * A 3D Quad. 4 * 3 floats 435 * @Class 436 * @Construct 437 * @param {cc.Vertex3F} bl1 438 * @param {cc.Vertex3F} br1 439 * @param {cc.Vertex3F} tl1 440 * @param {cc.Vertex3F} tr1 441 */ 442 cc.Quad3 = function (bl1, br1, tl1, tr1) { 443 this.bl = bl1 || new cc.Vertex3F(0, 0, 0); 444 this.br = br1 || new cc.Vertex3F(0, 0, 0); 445 this.tl = tl1 || new cc.Vertex3F(0, 0, 0); 446 this.tr = tr1 || new cc.Vertex3F(0, 0, 0); 447 }; 448 449 /** 450 * A 2D grid size 451 * @Class 452 * @Construct 453 * @param {Number} x1 454 * @param {Number} y1 455 */ 456 cc.GridSize = function (x1, y1) { 457 this.x = x1; 458 this.y = y1; 459 }; 460 461 /** 462 * helper function to create a cc.GridSize 463 * @function 464 * @param {Number} x 465 * @param {Number} y 466 * @return {cc.GridSize} 467 */ 468 cc.g = function (x, y) { 469 return new cc.GridSize(x, y); 470 }; 471 472 /** 473 * a Point with a vertex point, a tex coord point and a color 4B 474 * @Class 475 * @Construct 476 * @param {cc.Vertex2F} vertices1 477 * @param {cc.Color4B} colors1 478 * @param {cc.Tex2F} texCoords1 479 */ 480 cc.V2F_C4B_T2F = function (vertices1, colors1, texCoords1) { 481 this.vertices = vertices1 || new cc.Vertex2F(0, 0); 482 this.colors = colors1 || new cc.Color4B(0, 0, 0, 0); 483 this.texCoords = texCoords1 || new cc.Tex2F(0, 0); 484 }; 485 486 /** 487 * a Point with a vertex point, a tex coord point and a color 4F 488 * @Class 489 * @Construct 490 * @param {cc.Vertex2F} vertices1 491 * @param {cc.Color4F} colors1 492 * @param {cc.Tex2F} texCoords1 493 */ 494 cc.V2F_C4F_T2F = function (vertices1, colors1, texCoords1) { 495 this.vertices = vertices1 || new cc.Vertex2F(0, 0); 496 this.colors = colors1 || new cc.Color4F(0, 0, 0, 0); 497 this.texCoords = texCoords1 || new cc.Tex2F(0, 0); 498 }; 499 500 /** 501 * a Point with a vertex point, a tex coord point and a color 4B 502 * @Class 503 * @Construct 504 * @param {cc.Vertex3F} vertices1 505 * @param {cc.Color4B} colors1 506 * @param {cc.Tex2F} texCoords1 507 */ 508 cc.V3F_C4B_T2F = function (vertices1, colors1, texCoords1) { 509 this.vertices = vertices1 || new cc.Vertex3F(0, 0, 0); 510 this.colors = colors1 || new cc.Color4B(0, 0, 0, 0); 511 this.texCoords = texCoords1 || new cc.Tex2F(0, 0); 512 }; 513 514 /** 515 * 4 ccVertex2FTex2FColor4B Quad 516 * @Class 517 * @Construct 518 * @param {cc.V2F_C4B_T2F} bl1 bottom left 519 * @param {cc.V2F_C4B_T2F} br1 bottom right 520 * @param {cc.V2F_C4B_T2F} tl1 top left 521 * @param {cc.V2F_C4B_T2F} tr1 top right 522 */ 523 cc.V2F_C4B_T2F_Quad = function (bl1, br1, tl1, tr1) { 524 this.bl = bl1 || new cc.V2F_C4B_T2F(); 525 this.br = br1 || new cc.V2F_C4B_T2F(); 526 this.tl = tl1 || new cc.V2F_C4B_T2F(); 527 this.tr = tr1 || new cc.V2F_C4B_T2F(); 528 }; 529 530 /** 531 * helper function to create a cc.V2F_C4B_T2F_Quad 532 * @function 533 * @return {cc.V2F_C4B_T2F_Quad} 534 */ 535 cc.V2F_C4B_T2F_QuadZero = function () { 536 return new cc.V2F_C4B_T2F_Quad( 537 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 538 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 539 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 540 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)) 541 ); 542 }; 543 544 /** 545 * 4 ccVertex3FTex2FColor4B 546 * @Class 547 * @Construct 548 * @param {cc.V3F_C4B_T2F} tl1 top left 549 * @param {cc.V3F_C4B_T2F} bl1 bottom left 550 * @param {cc.V3F_C4B_T2F} tr1 top right 551 * @param {cc.V3F_C4B_T2F} br1 bottom right 552 */ 553 cc.V3F_C4B_T2F_Quad = function (tl1, bl1, tr1, br1) { 554 this.tl = tl1 || new cc.V3F_C4B_T2F(); 555 this.bl = bl1 || new cc.V3F_C4B_T2F(); 556 this.tr = tr1 || new cc.V3F_C4B_T2F(); 557 this.br = br1 || new cc.V3F_C4B_T2F(); 558 }; 559 560 /** 561 * helper function to create a cc.V3F_C4B_T2F_Quad 562 * @function 563 * @return {cc.V3F_C4B_T2F_Quad} 564 */ 565 cc.V3F_C4B_T2F_QuadZero = function () { 566 return new cc.V3F_C4B_T2F_Quad( 567 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 568 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 569 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0)), 570 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), new cc.Color4B(0, 0, 0, 255), new cc.Tex2F(0, 0))); 571 }; 572 573 /** 574 * 4 ccVertex2FTex2FColor4F Quad 575 * @Class 576 * @Construct 577 * @param {cc.V2F_C4F_T2F} bl1 bottom left 578 * @param {cc.V2F_C4F_T2F} br1 bottom right 579 * @param {cc.V2F_C4F_T2F} tl1 top left 580 * @param {cc.V2F_C4F_T2F} tr1 top right 581 * Constructor 582 */ 583 cc.V2F_C4F_T2F_Quad = function (bl1, br1, tl1, tr1) { 584 this.bl = bl1 || new cc.V2F_C4F_T2F(); 585 this.br = br1 || new cc.V2F_C4F_T2F(); 586 this.tl = tl1 || new cc.V2F_C4F_T2F(); 587 this.tr = tr1 || new cc.V2F_C4F_T2F(); 588 }; 589 590 /** 591 * Blend Function used for textures 592 * @Class 593 * @Construct 594 * @param {Number} src1 source blend function 595 * @param {Number} dst1 destination blend function 596 */ 597 cc.BlendFunc = function (src1, dst1) { 598 this.src = src1; 599 this.dst = dst1; 600 }; 601 602 603 /** 604 * convert Color3B to a string of color for style. 605 * e.g. Color3B(255,6,255) to : "#ff06ff" 606 * @param clr 607 * @return {String} 608 */ 609 cc.convertColor3BtoHexString = function (clr) { 610 var hR = clr.r.toString(16); 611 var hG = clr.g.toString(16); 612 var hB = clr.b.toString(16); 613 var stClr = "#" + (clr.r < 16 ? ("0" + hR) : hR) + (clr.g < 16 ? ("0" + hG) : hG) + (clr.b < 16 ? ("0" + hB) : hB); 614 return stClr; 615 }; 616 617 /** 618 * convert a string of color for style to Color3B. 619 * e.g. "#ff06ff" to : Color3B(255,6,255) 620 * @param clr 621 * @return {String} 622 */ 623 cc.convertHexNumToColor3B = function(clrSt) 624 { 625 var nAr = clrSt.substr(1).split(""); 626 var r = parseInt("0x"+nAr[0]+nAr[1]); 627 var g = parseInt("0x"+nAr[2]+nAr[3]); 628 var b = parseInt("0x"+nAr[4]+nAr[5]); 629 return new cc.Color3B(r,g,b); 630 }; 631 632 633 /** 634 * text alignment : left 635 * @constant 636 * @type Number 637 */ 638 cc.TEXT_ALIGNMENT_LEFT = 0; 639 640 /** 641 * text alignment : center 642 * @constant 643 * @type Number 644 */ 645 cc.TEXT_ALIGNMENT_CENTER = 1; 646 647 /** 648 * text alignment : right 649 * @constant 650 * @type Number 651 */ 652 cc.TEXT_ALIGNMENT_RIGHT = 2; 653 654 /** 655 * text alignment : top 656 * @constant 657 * @type Number 658 */ 659 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0; 660 661 /** 662 * text alignment : center 663 * @constant 664 * @type Number 665 */ 666 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1; 667 668 /** 669 * text alignment : bottom 670 * @constant 671 * @type Number 672 */ 673 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2; 674