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 * The color class 29 * @param {Number} r 0 to 255 30 * @param {Number} g 0 to 255 31 * @param {Number} b 0 to 255 32 * @param {Number} a 0 to 255 33 * @constructor 34 */ 35 cc.Color = function (r, g, b, a) { 36 this.r = r || 0; 37 this.g = g || 0; 38 this.b = b || 0; 39 this.a = a || 0; 40 }; 41 42 /** 43 * 44 * @param {Number|String|cc.Color} r 45 * @param {Number} g 46 * @param {Number} b 47 * @param {Number} a 48 * @returns {cc.Color} 49 */ 50 cc.color = function (r, g, b, a) { 51 if (r === undefined) 52 return {r: 0, g: 0, b: 0, a: 255}; 53 if (typeof r === "string") 54 return cc.hexToColor(r); 55 if (typeof r === "object") 56 return {r: r.r, g: r.g, b: r.b, a: r.a}; 57 return {r: r, g: g, b: b, a: a }; 58 }; 59 60 /** 61 * returns true if both ccColor3B are equal. Otherwise it returns false. 62 * @param {cc.Color} color1 63 * @param {cc.Color} color2 64 * @return {Boolean} true if both ccColor3B are equal. Otherwise it returns false. 65 */ 66 cc.colorEqual = function(color1, color2){ 67 return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; 68 }; 69 70 /** 71 * the device accelerometer reports values for each axis in units of g-force 72 */ 73 cc.Acceleration = function (x, y, z, timestamp) { 74 this.x = x || 0; 75 this.y = y || 0; 76 this.z = z || 0; 77 this.timestamp = timestamp || 0; 78 }; 79 80 /** 81 * A vertex composed of 2 floats: x, y 82 * @Class 83 * @Construct 84 * @param {Number} x1 85 * @param {Number} y1 86 */ 87 cc.Vertex2F = function (x1, y1) { 88 this.x = x1 || 0; 89 this.y = y1 || 0; 90 }; 91 92 /** 93 * helper macro that creates an Vertex2F type 94 * @function 95 * @param {Number} x 96 * @param {Number} y 97 * @return {cc.Vertex2F} 98 */ 99 cc.Vertex2 = function (x, y) { 100 return new cc.Vertex2F(x, y); 101 }; 102 103 /** 104 * A vertex composed of 3 floats: x, y, z 105 * @Class 106 * @Construct 107 * @param {Number} x1 108 * @param {Number} y1 109 * @param {Number} z1 110 */ 111 cc.Vertex3F = function (x1, y1, z1) { 112 this.x = x1 || 0; 113 this.y = y1 || 0; 114 this.z = z1 || 0; 115 }; 116 117 /** 118 * helper macro that creates an Vertex3F type 119 * @function 120 * @param {Number} x 121 * @param {Number} y 122 * @param {Number} z 123 * @return {cc.Vertex3F} 124 */ 125 cc.vertex3 = function (x, y, z) { 126 return new cc.Vertex3F(x, y, z); 127 }; 128 129 /** 130 * A texcoord composed of 2 floats: u, y 131 * @Class 132 * @Construct 133 * @param {Number} u1 134 * @param {Number} v1 135 */ 136 cc.Tex2F = function (u1, v1) { 137 this.u = u1 || 0; 138 this.v = v1 || 0; 139 }; 140 141 /** 142 * helper macro that creates an Tex2F type 143 * @function 144 * @param {Number} u 145 * @param {Number} v 146 * @return {cc.Tex2F} 147 */ 148 cc.tex2 = function (u, v) { 149 return new cc.Tex2F(u, v); 150 }; 151 152 /** 153 * A 2D Quad. 4 * 2 floats 154 * @Class 155 * @Construct 156 * @param {cc.Vertex2F} tl1 157 * @param {cc.Vertex2F} tr1 158 * @param {cc.Vertex2F} bl1 159 * @param {cc.Vertex2F} br1 160 */ 161 cc.Quad2 = function (tl1, tr1, bl1, br1) { 162 this.tl = tl1 || new cc.Vertex2F(0, 0); 163 this.tr = tr1 || new cc.Vertex2F(0, 0); 164 this.bl = bl1 || new cc.Vertex2F(0, 0); 165 this.br = br1 || new cc.Vertex2F(0, 0); 166 }; 167 168 /** 169 * A 3D Quad. 4 * 3 floats 170 * @Class 171 * @Construct 172 * @param {cc.Vertex3F} bl1 173 * @param {cc.Vertex3F} br1 174 * @param {cc.Vertex3F} tl1 175 * @param {cc.Vertex3F} tr1 176 */ 177 cc.Quad3 = function (bl1, br1, tl1, tr1) { 178 this.bl = bl1 || new cc.Vertex3F(0, 0, 0); 179 this.br = br1 || new cc.Vertex3F(0, 0, 0); 180 this.tl = tl1 || new cc.Vertex3F(0, 0, 0); 181 this.tr = tr1 || new cc.Vertex3F(0, 0, 0); 182 }; 183 184 185 /** 186 * a Point with a vertex point, a tex coord point and a color 4B 187 * @Class 188 * @Construct 189 * @param {cc.Vertex2F} vertices1 190 * @param {cc.Color} colors1 191 * @param {cc.Tex2F} texCoords1 192 */ 193 cc.V2F_C4B_T2F = function (vertices1, colors1, texCoords1) { 194 this.vertices = vertices1 || new cc.Vertex2F(0, 0); 195 this.colors = colors1 || cc.color(0, 0, 0, 0); 196 this.texCoords = texCoords1 || new cc.Tex2F(0, 0); 197 }; 198 199 /** 200 * a Point with a vertex point, a tex coord point and a color 4B 201 * @Class 202 * @Construct 203 * @param {cc.Vertex3F} vertices1 204 * @param {cc.Color} colors1 205 * @param {cc.Tex2F} texCoords1 206 */ 207 cc.V3F_C4B_T2F = function (vertices1, colors1, texCoords1) { 208 this.vertices = vertices1 || new cc.Vertex3F(0, 0, 0); 209 this.colors = colors1 || cc.color(0, 0, 0, 0); 210 this.texCoords = texCoords1 || new cc.Tex2F(0, 0); 211 }; 212 213 /** 214 * A Triangle of ccV2F_C4B_T2F 215 * @Class 216 * @Construct 217 * @param {cc.V2F_C4B_T2F} a 218 * @param {cc.V2F_C4B_T2F} b 219 * @param {cc.V2F_C4B_T2F} c 220 */ 221 cc.V2F_C4B_T2F_Triangle = function (a, b, c) { 222 this.a = a || new cc.V2F_C4B_T2F(); 223 this.b = b || new cc.V2F_C4B_T2F(); 224 this.c = c || new cc.V2F_C4B_T2F(); 225 }; 226 227 /** 228 * 4 ccVertex2FTex2FColor4B Quad 229 * @Class 230 * @Construct 231 * @param {cc.V2F_C4B_T2F} bl1 bottom left 232 * @param {cc.V2F_C4B_T2F} br1 bottom right 233 * @param {cc.V2F_C4B_T2F} tl1 top left 234 * @param {cc.V2F_C4B_T2F} tr1 top right 235 */ 236 cc.V2F_C4B_T2F_Quad = function (bl1, br1, tl1, tr1) { 237 this.bl = bl1 || new cc.V2F_C4B_T2F(); 238 this.br = br1 || new cc.V2F_C4B_T2F(); 239 this.tl = tl1 || new cc.V2F_C4B_T2F(); 240 this.tr = tr1 || new cc.V2F_C4B_T2F(); 241 }; 242 243 /** 244 * helper function to create a cc.V2F_C4B_T2F_Quad 245 * @function 246 * @return {cc.V2F_C4B_T2F_Quad} 247 */ 248 cc.V2F_C4B_T2F_QuadZero = function () { 249 return new cc.V2F_C4B_T2F_Quad( 250 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 251 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 252 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 253 new cc.V2F_C4B_T2F(new cc.Vertex2F(0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)) 254 ); 255 }; 256 257 /** 258 * 4 ccVertex3FTex2FColor4B 259 * @Class 260 * @Construct 261 * @param {cc.V3F_C4B_T2F} tl1 top left 262 * @param {cc.V3F_C4B_T2F} bl1 bottom left 263 * @param {cc.V3F_C4B_T2F} tr1 top right 264 * @param {cc.V3F_C4B_T2F} br1 bottom right 265 */ 266 cc.V3F_C4B_T2F_Quad = function (tl1, bl1, tr1, br1) { 267 this.tl = tl1 || new cc.V3F_C4B_T2F(); 268 this.bl = bl1 || new cc.V3F_C4B_T2F(); 269 this.tr = tr1 || new cc.V3F_C4B_T2F(); 270 this.br = br1 || new cc.V3F_C4B_T2F(); 271 }; 272 273 /** 274 * helper function to create a cc.V3F_C4B_T2F_Quad 275 * @function 276 * @return {cc.V3F_C4B_T2F_Quad} 277 */ 278 cc.V3F_C4B_T2F_QuadZero = function () { 279 return new cc.V3F_C4B_T2F_Quad( 280 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 281 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 282 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0)), 283 new cc.V3F_C4B_T2F(new cc.Vertex3F(0, 0, 0), cc.color(0, 0, 0, 255), new cc.Tex2F(0, 0))); 284 }; 285 286 cc.V3F_C4B_T2F_QuadCopy = function (sourceQuad) { 287 if (!sourceQuad) 288 return cc.V3F_C4B_T2F_QuadZero(); 289 var tl = sourceQuad.tl, bl = sourceQuad.bl, tr = sourceQuad.tr, br = sourceQuad.br; 290 return new cc.V3F_C4B_T2F_Quad( 291 new cc.V3F_C4B_T2F(new cc.Vertex3F(sourceQuad.tl.vertices.x, sourceQuad.tl.vertices.y, sourceQuad.tl.vertices.z), 292 cc.color(sourceQuad.tl.colors.r, sourceQuad.tl.colors.g, sourceQuad.tl.colors.b, sourceQuad.tl.colors.a), 293 new cc.Tex2F(sourceQuad.tl.texCoords.u, sourceQuad.tl.texCoords.v)), 294 new cc.V3F_C4B_T2F(new cc.Vertex3F(sourceQuad.bl.vertices.x, sourceQuad.bl.vertices.y, sourceQuad.bl.vertices.z), 295 cc.color(sourceQuad.bl.colors.r, sourceQuad.bl.colors.g, sourceQuad.bl.colors.b, sourceQuad.bl.colors.a), 296 new cc.Tex2F(sourceQuad.bl.texCoords.u, sourceQuad.bl.texCoords.v)), 297 new cc.V3F_C4B_T2F(new cc.Vertex3F(sourceQuad.tr.vertices.x, sourceQuad.tr.vertices.y, sourceQuad.tr.vertices.z), 298 cc.color(sourceQuad.tr.colors.r, sourceQuad.tr.colors.g, sourceQuad.tr.colors.b, sourceQuad.tr.colors.a), 299 new cc.Tex2F(sourceQuad.tr.texCoords.u, sourceQuad.tr.texCoords.v)), 300 new cc.V3F_C4B_T2F(new cc.Vertex3F(sourceQuad.br.vertices.x, sourceQuad.br.vertices.y, sourceQuad.br.vertices.z), 301 cc.color(sourceQuad.br.colors.r, sourceQuad.br.colors.g, sourceQuad.br.colors.b, sourceQuad.br.colors.a), 302 new cc.Tex2F(sourceQuad.br.texCoords.u, sourceQuad.br.texCoords.v))); 303 }; 304 305 cc.V3F_C4B_T2F_QuadsCopy = function (sourceQuads) { 306 if (!sourceQuads) 307 return []; 308 309 var retArr = []; 310 for (var i = 0; i < sourceQuads.length; i++) { 311 retArr.push(cc.V3F_C4B_T2F_QuadCopy(sourceQuads[i])); 312 } 313 return retArr; 314 }; 315 316 /** 317 * Blend Function used for textures 318 * @Class 319 * @Construct 320 * @param {Number} src1 source blend function 321 * @param {Number} dst1 destination blend function 322 */ 323 cc.BlendFunc = function (src1, dst1) { 324 this.src = src1; 325 this.dst = dst1; 326 }; 327 328 cc.BlendFuncDisable = function () { 329 return new cc.BlendFunc(cc.ONE, cc.ZERO); 330 }; 331 332 /** 333 * texture coordinates for a quad 334 * @param {cc.Tex2F} bl 335 * @param {cc.Tex2F} br 336 * @param {cc.Tex2F} tl 337 * @param {cc.Tex2F} tr 338 * @constructor 339 */ 340 cc.T2F_Quad = function(bl, br, tl, tr){ 341 this.bl = bl; 342 this.br = br; 343 this.tl = tl; 344 this.tr = tr; 345 }; 346 347 /** 348 * struct that holds the size in pixels, texture coordinates and delays for animated cc.ParticleSystem 349 * @param {cc.T2F_Quad} texCoords 350 * @param delay 351 * @param size 352 * @constructor 353 */ 354 cc.AnimationFrameData = function(texCoords, delay, size){ 355 this.texCoords = texCoords; 356 this.delay = delay; 357 this.size = size; 358 }; 359 360 if (cc._renderType === cc._RENDER_TYPE_WEBGL) { 361 //redefine some types with ArrayBuffer for WebGL 362 cc.color = function (r, g, b, a, arrayBuffer, offset) { 363 if (r === undefined) 364 return new cc.Color(0,0,0,255, arrayBuffer, offset); 365 if (typeof r === "string"){ 366 var color = cc.hexToColor(r); 367 return new cc.Color(color.r, color.g, color.b, color.a); 368 } 369 if (typeof r === "object") 370 return new cc.Color(r.r, r.g, r.b, r.a, r.arrayBuffer, r.offset); 371 return new cc.Color(r, g, b, a, arrayBuffer, offset); 372 }; 373 //redefine cc.Color 374 cc.Color = function (r, g, b, a, arrayBuffer, offset) { 375 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Color.BYTES_PER_ELEMENT); 376 this._offset = offset || 0; 377 378 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = Uint8Array.BYTES_PER_ELEMENT; 379 this._rU8 = new Uint8Array(locArrayBuffer, locOffset, 1); 380 this._gU8 = new Uint8Array(locArrayBuffer, locOffset + locElementLen, 1); 381 this._bU8 = new Uint8Array(locArrayBuffer, locOffset + locElementLen * 2, 1); 382 this._aU8 = new Uint8Array(locArrayBuffer, locOffset + locElementLen * 3, 1); 383 384 this._rU8[0] = r || 0; 385 this._gU8[0] = g || 0; 386 this._bU8[0] = b || 0; 387 this._aU8[0] = a || 0; 388 389 if (a === undefined) { 390 this.a_undefined = true; 391 } 392 }; 393 cc.Color.BYTES_PER_ELEMENT = 4; 394 395 window._p = cc.Color.prototype; 396 _p._getR = function(){ 397 return this._rU8[0]; 398 }; 399 _p._setR = function(value){ 400 this._rU8[0] = value < 0 ? 0 : value; 401 }; 402 _p._getG = function(){ 403 return this._gU8[0]; 404 }; 405 _p._setG = function(value){ 406 this._gU8[0] = value < 0 ? 0 : value; 407 }; 408 _p._getB = function(){ 409 return this._bU8[0]; 410 }; 411 _p._setB = function(value){ 412 this._bU8[0] = value < 0 ? 0 : value; 413 }; 414 _p._getA = function(){ 415 return this._aU8[0]; 416 }; 417 _p._setA = function(value){ 418 this._aU8[0] = value < 0 ? 0 : value; 419 }; 420 /** @expose */ 421 _p.r; 422 cc.defineGetterSetter(_p, "r", _p._getR,_p._setR); 423 /** @expose */ 424 _p.g; 425 cc.defineGetterSetter(_p, "g", _p._getG,_p._setG); 426 /** @expose */ 427 _p.b; 428 cc.defineGetterSetter(_p, "b", _p._getB,_p._setB); 429 /** @expose */ 430 _p.a; 431 cc.defineGetterSetter(_p, "a", _p._getA,_p._setA); 432 delete window._p; 433 434 //redefine cc.Vertex2F 435 cc.Vertex2F = function (x, y, arrayBuffer, offset) { 436 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Vertex2F.BYTES_PER_ELEMENT); 437 this._offset = offset || 0; 438 439 this._xF32 = new Float32Array(this._arrayBuffer, this._offset, 1); 440 this._yF32 = new Float32Array(this._arrayBuffer, this._offset + 4, 1); 441 this._xF32[0] = x || 0; 442 this._yF32[0] = y || 0; 443 }; 444 cc.Vertex2F.BYTES_PER_ELEMENT = 8; 445 Object.defineProperties(cc.Vertex2F.prototype, { 446 x: { 447 get: function () { 448 return this._xF32[0]; 449 }, 450 set: function (xValue) { 451 this._xF32[0] = xValue; 452 }, 453 enumerable: true 454 }, 455 y: { 456 get: function () { 457 return this._yF32[0]; 458 }, 459 set: function (yValue) { 460 this._yF32[0] = yValue; 461 }, 462 enumerable: true 463 } 464 }); 465 466 // redefine cc.Vertex3F 467 cc.Vertex3F = function (x, y, z, arrayBuffer, offset) { 468 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Vertex3F.BYTES_PER_ELEMENT); 469 this._offset = offset || 0; 470 471 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset; 472 this._xF32 = new Float32Array(locArrayBuffer, locOffset, 1); 473 this._xF32[0] = x || 0; 474 this._yF32 = new Float32Array(locArrayBuffer, locOffset + Float32Array.BYTES_PER_ELEMENT, 1); 475 this._yF32[0] = y || 0; 476 this._zF32 = new Float32Array(locArrayBuffer, locOffset + Float32Array.BYTES_PER_ELEMENT * 2, 1); 477 this._zF32[0] = z || 0; 478 }; 479 cc.Vertex3F.BYTES_PER_ELEMENT = 12; 480 Object.defineProperties(cc.Vertex3F.prototype, { 481 x: { 482 get: function () { 483 return this._xF32[0]; 484 }, 485 set: function (xValue) { 486 this._xF32[0] = xValue; 487 }, 488 enumerable: true 489 }, 490 y: { 491 get: function () { 492 return this._yF32[0]; 493 }, 494 set: function (yValue) { 495 this._yF32[0] = yValue; 496 }, 497 enumerable: true 498 }, 499 z: { 500 get: function () { 501 return this._zF32[0]; 502 }, 503 set: function (zValue) { 504 this._zF32[0] = zValue; 505 }, 506 enumerable: true 507 } 508 }); 509 510 // redefine cc.Tex2F 511 cc.Tex2F = function (u, v, arrayBuffer, offset) { 512 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Tex2F.BYTES_PER_ELEMENT); 513 this._offset = offset || 0; 514 515 this._uF32 = new Float32Array(this._arrayBuffer, this._offset, 1); 516 this._vF32 = new Float32Array(this._arrayBuffer, this._offset + 4, 1); 517 this._uF32[0] = u || 0; 518 this._vF32[0] = v || 0; 519 }; 520 cc.Tex2F.BYTES_PER_ELEMENT = 8; 521 Object.defineProperties(cc.Tex2F.prototype, { 522 u: { 523 get: function () { 524 return this._uF32[0]; 525 }, 526 set: function (xValue) { 527 this._uF32[0] = xValue; 528 }, 529 enumerable: true 530 }, 531 v: { 532 get: function () { 533 return this._vF32[0]; 534 }, 535 set: function (yValue) { 536 this._vF32[0] = yValue; 537 }, 538 enumerable: true 539 } 540 }); 541 542 //redefine cc.Quad2 543 cc.Quad2 = function (tl, tr, bl, br, arrayBuffer, offset) { 544 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Quad2.BYTES_PER_ELEMENT); 545 this._offset = offset || 0; 546 547 var locArrayBuffer = this._arrayBuffer, locElementLen = cc.Vertex2F.BYTES_PER_ELEMENT; 548 this._tl = tl ? new cc.Vertex2F(tl.x, tl.y, locArrayBuffer, 0) : new cc.Vertex2F(0, 0, locArrayBuffer, 0); 549 this._tr = tr ? new cc.Vertex2F(tr.x, tr.y, locArrayBuffer, locElementLen) : new cc.Vertex2F(0, 0, locArrayBuffer, locElementLen); 550 this._bl = bl ? new cc.Vertex2F(bl.x, bl.y, locArrayBuffer, locElementLen * 2) : new cc.Vertex2F(0, 0, locArrayBuffer, locElementLen * 2); 551 this._br = br ? new cc.Vertex2F(br.x, br.y, locArrayBuffer, locElementLen * 3) : new cc.Vertex2F(0, 0, locArrayBuffer, locElementLen * 3); 552 }; 553 cc.Quad2.BYTES_PER_ELEMENT = 32; 554 Object.defineProperties(cc.Quad2.prototype, { 555 tl: { 556 get: function () { 557 return this._tl; 558 }, 559 set: function (tlValue) { 560 this._tl.x = tlValue.x; 561 this._tl.y = tlValue.y; 562 }, 563 enumerable: true 564 }, 565 tr: { 566 get: function () { 567 return this._tr; 568 }, 569 set: function (trValue) { 570 this._tr.x = trValue.x; 571 this._tr.y = trValue.y; 572 }, 573 enumerable: true 574 }, 575 bl: { 576 get: function () { 577 return this._bl; 578 }, 579 set: function (blValue) { 580 this._bl.x = blValue.x; 581 this._bl.y = blValue.y; 582 }, 583 enumerable: true 584 }, 585 br: { 586 get: function () { 587 return this._br; 588 }, 589 set: function (brValue) { 590 this._br.x = brValue.x; 591 this._br.y = brValue.y; 592 }, 593 enumerable: true 594 } 595 }); 596 597 //redefine cc.V3F_C4B_T2F 598 cc.V3F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) { 599 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V3F_C4B_T2F.BYTES_PER_ELEMENT); 600 this._offset = offset || 0; 601 602 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.Vertex3F.BYTES_PER_ELEMENT; 603 this._vertices = vertices ? new cc.Vertex3F(vertices.x, vertices.y, vertices.z, locArrayBuffer, locOffset) : 604 new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 605 this._colors = colors ? cc.color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset + locElementLen) : 606 cc.color(0, 0, 0, 0, locArrayBuffer, locOffset + locElementLen); 607 this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset + locElementLen + cc.Color.BYTES_PER_ELEMENT) : 608 new cc.Tex2F(0, 0, locArrayBuffer, locOffset + locElementLen + cc.Color.BYTES_PER_ELEMENT); 609 }; 610 cc.V3F_C4B_T2F.BYTES_PER_ELEMENT = 24; 611 Object.defineProperties(cc.V3F_C4B_T2F.prototype, { 612 vertices: { 613 get: function () { 614 return this._vertices; 615 }, 616 set: function (verticesValue) { 617 var locVertices = this._vertices; 618 locVertices.x = verticesValue.x; 619 locVertices.y = verticesValue.y; 620 locVertices.z = verticesValue.z; 621 }, 622 enumerable: true 623 }, 624 colors: { 625 get: function () { 626 return this._colors; 627 }, 628 set: function (colorValue) { 629 var locColors = this._colors; 630 locColors.r = colorValue.r; 631 locColors.g = colorValue.g; 632 locColors.b = colorValue.b; 633 locColors.a = colorValue.a; 634 }, 635 enumerable: true 636 }, 637 texCoords: { 638 get: function () { 639 return this._texCoords; 640 }, 641 set: function (texValue) { 642 this._texCoords.u = texValue.u; 643 this._texCoords.v = texValue.v; 644 }, 645 enumerable: true 646 } 647 }); 648 649 //redefine cc.V3F_C4B_T2F_Quad 650 cc.V3F_C4B_T2F_Quad = function (tl, bl, tr, br, arrayBuffer, offset) { 651 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT); 652 this._offset = offset || 0; 653 654 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT; 655 this._tl = tl ? new cc.V3F_C4B_T2F(tl.vertices, tl.colors, tl.texCoords, locArrayBuffer, locOffset) : 656 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 657 this._bl = bl ? new cc.V3F_C4B_T2F(bl.vertices, bl.colors, bl.texCoords, locArrayBuffer, locOffset + locElementLen) : 658 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset + locElementLen); 659 this._tr = tr ? new cc.V3F_C4B_T2F(tr.vertices, tr.colors, tr.texCoords, locArrayBuffer, locOffset + locElementLen * 2) : 660 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset + locElementLen * 2); 661 this._br = br ? new cc.V3F_C4B_T2F(br.vertices, br.colors, br.texCoords, locArrayBuffer, locOffset + locElementLen * 3) : 662 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset + locElementLen * 3); 663 }; 664 cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT = 96; 665 Object.defineProperties(cc.V3F_C4B_T2F_Quad.prototype, { 666 tl:{ 667 get: function () { 668 return this._tl; 669 }, 670 set: function (tlValue) { 671 var locTl = this._tl; 672 locTl.vertices = tlValue.vertices; 673 locTl.colors = tlValue.colors; 674 locTl.texCoords = tlValue.texCoords; 675 }, 676 enumerable: true 677 }, 678 bl:{ 679 get: function () { 680 return this._bl; 681 }, 682 set: function (blValue) { 683 var locBl = this._bl; 684 locBl.vertices = blValue.vertices; 685 locBl.colors = blValue.colors; 686 locBl.texCoords = blValue.texCoords; 687 }, 688 enumerable: true 689 }, 690 tr:{ 691 get: function () { 692 return this._tr; 693 }, 694 set: function (trValue) { 695 var locTr = this._tr; 696 locTr.vertices = trValue.vertices; 697 locTr.colors = trValue.colors; 698 locTr.texCoords = trValue.texCoords; 699 }, 700 enumerable: true 701 }, 702 br:{ 703 get: function () { 704 return this._br; 705 }, 706 set: function (brValue) { 707 var locBr = this._br; 708 locBr.vertices = brValue.vertices; 709 locBr.colors = brValue.colors; 710 locBr.texCoords = brValue.texCoords; 711 }, 712 enumerable: true 713 }, 714 arrayBuffer:{ 715 get: function () { 716 return this._arrayBuffer; 717 }, 718 enumerable: true 719 } 720 }); 721 cc.V3F_C4B_T2F_QuadZero = function(){ 722 return new cc.V3F_C4B_T2F_Quad(); 723 }; 724 725 cc.V3F_C4B_T2F_QuadCopy = function (sourceQuad) { 726 if (!sourceQuad) 727 return cc.V3F_C4B_T2F_QuadZero(); 728 729 //return new cc.V3F_C4B_T2F_Quad(sourceQuad,tl,sourceQuad,bl,sourceQuad.tr,sourceQuad.br,null,0); 730 var srcTL = sourceQuad.tl, srcBL = sourceQuad.bl, srcTR = sourceQuad.tr, srcBR = sourceQuad.br; 731 return { 732 tl: {vertices: {x: srcTL.vertices.x, y: srcTL.vertices.y, z: srcTL.vertices.z}, 733 colors: {r: srcTL.colors.r, g: srcTL.colors.g, b: srcTL.colors.b, a: srcTL.colors.a}, 734 texCoords: {u: srcTL.texCoords.u, v: srcTL.texCoords.v}}, 735 bl: {vertices: {x: srcBL.vertices.x, y: srcBL.vertices.y, z: srcBL.vertices.z}, 736 colors: {r: srcBL.colors.r, g: srcBL.colors.g, b: srcBL.colors.b, a: srcBL.colors.a}, 737 texCoords: {u: srcBL.texCoords.u, v: srcBL.texCoords.v}}, 738 tr: {vertices: {x: srcTR.vertices.x, y: srcTR.vertices.y, z: srcTR.vertices.z}, 739 colors: {r: srcTR.colors.r, g: srcTR.colors.g, b: srcTR.colors.b, a: srcTR.colors.a}, 740 texCoords: {u: srcTR.texCoords.u, v: srcTR.texCoords.v}}, 741 br: {vertices: {x: srcBR.vertices.x, y: srcBR.vertices.y, z: srcBR.vertices.z}, 742 colors: {r: srcBR.colors.r, g: srcBR.colors.g, b: srcBR.colors.b, a: srcBR.colors.a}, 743 texCoords: {u: srcBR.texCoords.u, v: srcBR.texCoords.v}} 744 }; 745 }; 746 747 //redefine cc.V2F_C4B_T2F 748 cc.V2F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) { 749 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V2F_C4B_T2F.BYTES_PER_ELEMENT); 750 this._offset = offset || 0; 751 752 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.Vertex2F.BYTES_PER_ELEMENT; 753 this._vertices = vertices ? new cc.Vertex2F(vertices.x, vertices.y, locArrayBuffer, locOffset) : 754 new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 755 this._colors = colors ? cc.color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset + locElementLen) : 756 cc.color(0, 0, 0, 0, locArrayBuffer, locOffset + locElementLen); 757 this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset + locElementLen + cc.Color.BYTES_PER_ELEMENT) : 758 new cc.Tex2F(0, 0, locArrayBuffer, locOffset + locElementLen + cc.Color.BYTES_PER_ELEMENT); 759 }; 760 cc.V2F_C4B_T2F.BYTES_PER_ELEMENT = 20; 761 Object.defineProperties(cc.V2F_C4B_T2F.prototype, { 762 vertices: { 763 get: function () { 764 return this._vertices; 765 }, 766 set: function (verticesValue) { 767 this._vertices.x = verticesValue.x; 768 this._vertices.y = verticesValue.y; 769 }, 770 enumerable: true 771 }, 772 colors: { 773 get: function () { 774 return this._colors; 775 }, 776 set: function (colorValue) { 777 var locColors = this._colors; 778 locColors.r = colorValue.r; 779 locColors.g = colorValue.g; 780 locColors.b = colorValue.b; 781 locColors.a = colorValue.a; 782 }, 783 enumerable: true 784 }, 785 texCoords: { 786 get: function () { 787 return this._texCoords; 788 }, 789 set: function (texValue) { 790 this._texCoords.u = texValue.u; 791 this._texCoords.v = texValue.v; 792 }, 793 enumerable: true 794 } 795 }); 796 797 //redefine cc.V2F_C4B_T2F_Triangle 798 cc.V2F_C4B_T2F_Triangle = function (a, b, c, arrayBuffer, offset) { 799 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT); 800 this._offset = offset || 0; 801 802 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT; 803 this._a = a ? new cc.V2F_C4B_T2F(a.vertices, a.colors, a.texCoords, locArrayBuffer, locOffset) : 804 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 805 this._b = b ? new cc.V2F_C4B_T2F(b.vertices, b.colors, b.texCoords, locArrayBuffer, locOffset + locElementLen) : 806 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset + locElementLen); 807 this._c = c ? new cc.V2F_C4B_T2F(c.vertices, c.colors, c.texCoords, locArrayBuffer, locOffset + locElementLen * 2) : 808 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset + locElementLen * 2); 809 }; 810 cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT = 60; 811 Object.defineProperties(cc.V2F_C4B_T2F_Triangle.prototype, { 812 a:{ 813 get: function () { 814 return this._a; 815 }, 816 set: function (aValue) { 817 var locA = this._a; 818 locA.vertices = aValue.vertices; 819 locA.colors = aValue.colors; 820 locA.texCoords = aValue.texCoords; 821 }, 822 enumerable: true 823 }, 824 b:{ 825 get: function () { 826 return this._b; 827 }, 828 set: function (bValue) { 829 var locB = this._b; 830 locB.vertices = bValue.vertices; 831 locB.colors = bValue.colors; 832 locB.texCoords = bValue.texCoords; 833 }, 834 enumerable: true 835 }, 836 c:{ 837 get: function () { 838 return this._c; 839 }, 840 set: function (cValue) { 841 var locC = this._c; 842 locC.vertices = cValue.vertices; 843 locC.colors = cValue.colors; 844 locC.texCoords = cValue.texCoords; 845 }, 846 enumerable: true 847 } 848 }); 849 } 850 851 /** 852 * convert a string of color for style to Color. 853 * e.g. "#ff06ff" to : cc.color(255,6,255) 854 * @param {String} hex 855 * @return {cc.Color} 856 */ 857 cc.hexToColor = function (hex) { 858 hex = hex.replace(/^#?/, "0x"); 859 var c = parseInt(hex); 860 var r = c >> 16; 861 var g = (c >> 8) % 256; 862 var b = c % 256; 863 return cc.color(r, g, b); 864 }; 865 866 /** 867 * convert Color to a string of color for style. 868 * e.g. cc.color(255,6,255) to : "#ff06ff" 869 * @param {cc.Color} color 870 * @return {String} 871 */ 872 cc.colorToHex = function (color) { 873 var hR = color.r.toString(16); 874 var hG = color.g.toString(16); 875 var hB = color.b.toString(16); 876 var hex = "#" + (color.r < 16 ? ("0" + hR) : hR) + (color.g < 16 ? ("0" + hG) : hG) + (color.b < 16 ? ("0" + hB) : hB); 877 return hex; 878 }; 879 880 /** 881 * text alignment : left 882 * @constant 883 * @type Number 884 */ 885 cc.TEXT_ALIGNMENT_LEFT = 0; 886 887 /** 888 * text alignment : center 889 * @constant 890 * @type Number 891 */ 892 cc.TEXT_ALIGNMENT_CENTER = 1; 893 894 /** 895 * text alignment : right 896 * @constant 897 * @type Number 898 */ 899 cc.TEXT_ALIGNMENT_RIGHT = 2; 900 901 /** 902 * text alignment : top 903 * @constant 904 * @type Number 905 */ 906 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0; 907 908 /** 909 * text alignment : center 910 * @constant 911 * @type Number 912 */ 913 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1; 914 915 /** 916 * text alignment : bottom 917 * @constant 918 * @type Number 919 */ 920 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2; 921 922 cc._Dictionary = cc.Class.extend({ 923 _keyMapTb: null, 924 _valueMapTb: null, 925 __currId: 0, 926 927 ctor: function () { 928 this._keyMapTb = {}; 929 this._valueMapTb = {}; 930 this.__currId = 2 << (0 | (Math.random() * 10)); 931 }, 932 933 __getKey: function () { 934 this.__currId++; 935 return "key_" + this.__currId; 936 }, 937 938 setObject: function (value, key) { 939 if (key == null) 940 return; 941 942 var keyId = this.__getKey(); 943 this._keyMapTb[keyId] = key; 944 this._valueMapTb[keyId] = value; 945 }, 946 947 objectForKey: function (key) { 948 if (key == null) 949 return null; 950 951 var locKeyMapTb = this._keyMapTb; 952 for (var keyId in locKeyMapTb) { 953 if (locKeyMapTb[keyId] === key) 954 return this._valueMapTb[keyId]; 955 } 956 return null; 957 }, 958 959 valueForKey: function (key) { 960 return this.objectForKey(key); 961 }, 962 963 removeObjectForKey: function (key) { 964 if (key == null) 965 return; 966 967 var locKeyMapTb = this._keyMapTb; 968 for (var keyId in locKeyMapTb) { 969 if (locKeyMapTb[keyId] === key) { 970 delete this._valueMapTb[keyId]; 971 delete locKeyMapTb[keyId]; 972 return; 973 } 974 } 975 }, 976 977 removeObjectsForKeys: function (keys) { 978 if (keys == null) 979 return; 980 981 for (var i = 0; i < keys.length; i++) 982 this.removeObjectForKey(keys[i]); 983 }, 984 985 allKeys: function () { 986 var keyArr = [], locKeyMapTb = this._keyMapTb; 987 for (var key in locKeyMapTb) 988 keyArr.push(locKeyMapTb[key]); 989 return keyArr; 990 }, 991 992 removeAllObjects: function () { 993 this._keyMapTb = {}; 994 this._valueMapTb = {}; 995 }, 996 997 count: function() { 998 return this.allKeys().length; 999 } 1000 }); 1001 1002 cc.FontDefinition = function () { 1003 this.fontName = "Arial"; 1004 this.fontSize = 12; 1005 this.textAlign = cc.TEXT_ALIGNMENT_CENTER; 1006 this.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP; 1007 this.fillStyle = cc.color(255, 255, 255, 255); 1008 this.boundingWidth = 0; 1009 this.boundingHeight = 0; 1010 1011 this.strokeEnabled = false; 1012 this.strokeStyle = cc.color(255, 255, 255, 255); 1013 this.lineWidth = 1; 1014 1015 this.shadowEnabled = false; 1016 this.shadowOffsetX = 0; 1017 this.shadowOffsetY = 0; 1018 this.shadowBlur = 0; 1019 this.shadowOpacity = 1.0; 1020 }; 1021 1022 1023 /** 1024 * White color (255, 255, 255, 255) 1025 * @returns {cc.Color} 1026 * @private 1027 */ 1028 cc.color._getWhite = function(){ 1029 return cc.color(255, 255, 255); 1030 }; 1031 1032 /** 1033 * Yellow color (255, 255, 0, 255) 1034 * @returns {cc.Color} 1035 * @private 1036 */ 1037 cc.color._getYellow = function () { 1038 return cc.color(255, 255, 0); 1039 }; 1040 1041 /** 1042 * Blue color (0, 0, 255, 255) 1043 * @type {cc.Color} 1044 * @private 1045 */ 1046 cc.color._getBlue = function () { 1047 return cc.color(0, 0, 255); 1048 }; 1049 1050 /** 1051 * Green Color (0, 255, 0, 255) 1052 * @type {cc.Color} 1053 * @private 1054 */ 1055 cc.color._getGreen = function () { 1056 return cc.color(0, 255, 0); 1057 }; 1058 1059 /** 1060 * Red Color (255, 0, 0, 255) 1061 * @type {cc.Color} 1062 * @private 1063 */ 1064 cc.color._getRed = function () { 1065 return cc.color(255, 0, 0); 1066 }; 1067 1068 /** 1069 * Magenta Color (255, 0, 255, 255) 1070 * @type {cc.Color} 1071 * @private 1072 */ 1073 cc.color._getMagenta = function () { 1074 return cc.color(255, 0, 255); 1075 }; 1076 1077 /** 1078 * Black Color (0, 0, 0, 255) 1079 * @type {cc.Color} 1080 * @private 1081 */ 1082 cc.color._getBlack = function () { 1083 return cc.color(0, 0, 0); 1084 }; 1085 1086 /** 1087 * Orange Color (255, 127, 0, 255) 1088 * @type {cc.Color} 1089 * @private 1090 */ 1091 cc.color._getOrange = function () { 1092 return cc.color(255, 127, 0); 1093 }; 1094 1095 /** 1096 * Gray Color (166, 166, 166, 255) 1097 * @type {cc.Color} 1098 * @private 1099 */ 1100 cc.color._getGray = function () { 1101 return cc.color(166, 166, 166); 1102 }; 1103 window._p = cc.color; 1104 /** @expose */ 1105 _p.WHITE; 1106 cc.defineGetterSetter(_p, "WHITE", _p._getWhite); 1107 /** @expose */ 1108 _p.YELLOW; 1109 cc.defineGetterSetter(_p, "YELLOW", _p._getYellow); 1110 /** @expose */ 1111 _p.BLUE; 1112 cc.defineGetterSetter(_p, "BLUE", _p._getBlue); 1113 /** @expose */ 1114 _p.GREEN; 1115 cc.defineGetterSetter(_p, "GREEN", _p._getGreen); 1116 /** @expose */ 1117 _p.RED; 1118 cc.defineGetterSetter(_p, "RED", _p._getRed); 1119 /** @expose */ 1120 _p.MAGENTA; 1121 cc.defineGetterSetter(_p, "MAGENTA", _p._getMagenta); 1122 /** @expose */ 1123 _p.BLACK; 1124 cc.defineGetterSetter(_p, "BLACK", _p._getBlack); 1125 /** @expose */ 1126 _p.ORANGE; 1127 cc.defineGetterSetter(_p, "ORANGE", _p._getOrange); 1128 /** @expose */ 1129 _p.GRAY; 1130 cc.defineGetterSetter(_p, "GRAY", _p._getGray); 1131 delete window._p; 1132