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 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 * Color class, please use cc.color() to construct a color 29 * @class cc.Color 30 * @param {Number} r 31 * @param {Number} g 32 * @param {Number} b 33 * @param {Number} a 34 * @see cc.color 35 */ 36 cc.Color = function (r, g, b, a) { 37 this.r = r || 0; 38 this.g = g || 0; 39 this.b = b || 0; 40 this.a = (a == null) ? 255 : a; 41 }; 42 43 /** 44 * Generate a color object based on multiple forms of parameters 45 * @example 46 * 47 * // 1. All channels separately as parameters 48 * var color1 = cc.color(255, 255, 255, 255); 49 * 50 * // 2. Convert a hex string to a color 51 * var color2 = cc.color("#000000"); 52 * 53 * // 3. An color object as parameter 54 * var color3 = cc.color({r: 255, g: 255, b: 255, a: 255}); 55 * 56 * Alpha channel is optional. Default value is 255 57 * 58 * @param {Number|String|cc.Color} r 59 * @param {Number} [g] 60 * @param {Number} [b] 61 * @param {Number} [a=255] 62 * @return {cc.Color} 63 */ 64 cc.color = function (r, g, b, a) { 65 if (r === undefined) 66 return {r: 0, g: 0, b: 0, a: 255}; 67 if (cc.isString(r)) 68 return cc.hexToColor(r); 69 if (cc.isObject(r)) 70 return {r: r.r, g: r.g, b: r.b, a: (r.a == null) ? 255 : r.a}; 71 return {r: r, g: g, b: b, a: (a == null ? 255 : a)}; 72 }; 73 74 /** 75 * returns true if both ccColor3B are equal. Otherwise it returns false. 76 * @function 77 * @param {cc.Color} color1 78 * @param {cc.Color} color2 79 * @return {Boolean} true if both ccColor3B are equal. Otherwise it returns false. 80 */ 81 cc.colorEqual = function (color1, color2) { 82 return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; 83 }; 84 85 /** 86 * the device accelerometer reports values for each axis in units of g-force 87 * @class cc.Acceleration 88 * @constructor 89 * @param {Number} x 90 * @param {Number} y 91 * @param {Number} z 92 * @param {Number} timestamp 93 */ 94 cc.Acceleration = function (x, y, z, timestamp) { 95 this.x = x || 0; 96 this.y = y || 0; 97 this.z = z || 0; 98 this.timestamp = timestamp || 0; 99 }; 100 101 /** 102 * @class cc.Vertex2F 103 * @param {Number} x 104 * @param {Number}y 105 * @param {Array} arrayBuffer 106 * @param {Number}offset 107 * @constructor 108 */ 109 cc.Vertex2F = function (x, y, arrayBuffer, offset) { 110 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Vertex2F.BYTES_PER_ELEMENT); 111 this._offset = offset || 0; 112 113 this._view = new Float32Array(this._arrayBuffer, this._offset, 2); 114 this._view[0] = x || 0; 115 this._view[1] = y || 0; 116 }; 117 /** 118 * @constant 119 * @type {number} 120 */ 121 cc.Vertex2F.BYTES_PER_ELEMENT = 8; 122 123 _p = cc.Vertex2F.prototype; 124 _p._getX = function () { 125 return this._view[0]; 126 }; 127 _p._setX = function (xValue) { 128 this._view[0] = xValue; 129 }; 130 _p._getY = function () { 131 return this._view[1]; 132 }; 133 _p._setY = function (yValue) { 134 this._view[1] = yValue; 135 }; 136 /** @expose */ 137 _p.x; 138 cc.defineGetterSetter(_p, "x", _p._getX, _p._setX); 139 /** @expose */ 140 _p.y; 141 cc.defineGetterSetter(_p, "y", _p._getY, _p._setY); 142 143 /** 144 * @class cc.Vertex3F 145 * @param {Number} x 146 * @param {Number} y 147 * @param {Number}z 148 * @param {Array} arrayBuffer 149 * @param {Number} offset 150 * @constructor 151 */ 152 cc.Vertex3F = function (x, y, z, arrayBuffer, offset) { 153 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Vertex3F.BYTES_PER_ELEMENT); 154 this._offset = offset || 0; 155 156 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset; 157 this._view = new Float32Array(locArrayBuffer, locOffset, 3); 158 this._view[0] = x || 0; 159 this._view[1] = y || 0; 160 this._view[2] = z || 0; 161 }; 162 /** 163 * @constant 164 * @type {number} 165 */ 166 cc.Vertex3F.BYTES_PER_ELEMENT = 12; 167 168 _p = cc.Vertex3F.prototype; 169 _p._getX = function () { 170 return this._view[0]; 171 }; 172 _p._setX = function (xValue) { 173 this._view[0] = xValue; 174 }; 175 _p._getY = function () { 176 return this._view[1]; 177 }; 178 _p._setY = function (yValue) { 179 this._view[1] = yValue; 180 }; 181 _p._getZ = function () { 182 return this._view[2]; 183 }; 184 _p._setZ = function (zValue) { 185 this._view[2] = zValue; 186 }; 187 /** @expose */ 188 _p.x; 189 cc.defineGetterSetter(_p, "x", _p._getX, _p._setX); 190 /** @expose */ 191 _p.y; 192 cc.defineGetterSetter(_p, "y", _p._getY, _p._setY); 193 /** @expose */ 194 _p.z; 195 cc.defineGetterSetter(_p, "z", _p._getZ, _p._setZ); 196 197 /** 198 * @class cc.Tex2F 199 * @param {Number} u 200 * @param {Number} v 201 * @param {Array} arrayBuffer 202 * @param {Number} offset 203 * @constructor 204 */ 205 cc.Tex2F = function (u, v, arrayBuffer, offset) { 206 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Tex2F.BYTES_PER_ELEMENT); 207 this._offset = offset || 0; 208 209 this._view = new Float32Array(this._arrayBuffer, this._offset, 2); 210 this._view[0] = u || 0; 211 this._view[1] = v || 0; 212 }; 213 /** 214 * @constants 215 * @type {number} 216 */ 217 cc.Tex2F.BYTES_PER_ELEMENT = 8; 218 219 _p = cc.Tex2F.prototype; 220 _p._getU = function () { 221 return this._view[0]; 222 }; 223 _p._setU = function (xValue) { 224 this._view[0] = xValue; 225 }; 226 _p._getV = function () { 227 return this._view[1]; 228 }; 229 _p._setV = function (yValue) { 230 this._view[1] = yValue; 231 }; 232 /** @expose */ 233 _p.u; 234 cc.defineGetterSetter(_p, "u", _p._getU, _p._setU); 235 /** @expose */ 236 _p.v; 237 cc.defineGetterSetter(_p, "v", _p._getV, _p._setV); 238 239 /** 240 * @class cc.Quad2 241 * @param {cc.Vertex2F} tl 242 * @param {cc.Vertex2F} tr 243 * @param {cc.Vertex2F} bl 244 * @param {cc.Vertex2F} br 245 * @param {Array} arrayBuffer 246 * @param {Number} offset 247 * @constructor 248 */ 249 cc.Quad2 = function (tl, tr, bl, br, arrayBuffer, offset) { 250 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Quad2.BYTES_PER_ELEMENT); 251 this._offset = offset || 0; 252 253 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.Vertex2F.BYTES_PER_ELEMENT; 254 this._tl = tl ? new cc.Vertex2F(tl.x, tl.y, locArrayBuffer, locOffset) : new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 255 locOffset += locElementLen; 256 this._tr = tr ? new cc.Vertex2F(tr.x, tr.y, locArrayBuffer, locOffset) : new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 257 locOffset += locElementLen; 258 this._bl = bl ? new cc.Vertex2F(bl.x, bl.y, locArrayBuffer, locOffset) : new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 259 locOffset += locElementLen; 260 this._br = br ? new cc.Vertex2F(br.x, br.y, locArrayBuffer, locOffset) : new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 261 }; 262 /** 263 * @constant 264 * @type {number} 265 */ 266 cc.Quad2.BYTES_PER_ELEMENT = 32; 267 268 _p = cc.Quad2.prototype; 269 _p._getTL = function () { 270 return this._tl; 271 }; 272 _p._setTL = function (tlValue) { 273 this._tl._view[0] = tlValue.x; 274 this._tl._view[1] = tlValue.y; 275 }; 276 _p._getTR = function () { 277 return this._tr; 278 }; 279 _p._setTR = function (trValue) { 280 this._tr._view[0] = trValue.x; 281 this._tr._view[1] = trValue.y; 282 }; 283 _p._getBL = function() { 284 return this._bl; 285 }; 286 _p._setBL = function (blValue) { 287 this._bl._view[0] = blValue.x; 288 this._bl._view[1] = blValue.y; 289 }; 290 _p._getBR = function () { 291 return this._br; 292 }; 293 _p._setBR = function (brValue) { 294 this._br._view[0] = brValue.x; 295 this._br._view[1] = brValue.y; 296 }; 297 298 /** @expose */ 299 _p.tl; 300 cc.defineGetterSetter(_p, "tl", _p._getTL, _p._setTL); 301 /** @expose */ 302 _p.tr; 303 cc.defineGetterSetter(_p, "tr", _p._getTR, _p._setTR); 304 /** @expose */ 305 _p.bl; 306 cc.defineGetterSetter(_p, "bl", _p._getBL, _p._setBL); 307 /** @expose */ 308 _p.br; 309 cc.defineGetterSetter(_p, "br", _p._getBR, _p._setBR); 310 311 /** 312 * A 3D Quad. 4 * 3 floats 313 * @Class cc.Quad3 314 * @Construct 315 * @param {cc.Vertex3F} bl 316 * @param {cc.Vertex3F} br 317 * @param {cc.Vertex3F} tl 318 * @param {cc.Vertex3F} tr 319 */ 320 cc.Quad3 = function (bl, br, tl, tr, arrayBuffer, offset) { 321 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.Quad3.BYTES_PER_ELEMENT); 322 this._offset = offset || 0; 323 324 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.Vertex3F.BYTES_PER_ELEMENT; 325 this.bl = bl ? new cc.Vertex3F(bl.x, bl.y, bl.z, locArrayBuffer, locOffset) : new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 326 locOffset += locElementLen; 327 this.br = br ? new cc.Vertex3F(br.x, br.y, br.z, locArrayBuffer, locOffset) : new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 328 locOffset += locElementLen; 329 this.tl = tl ? new cc.Vertex3F(tl.x, tl.y, tl.z, locArrayBuffer, locOffset) : new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 330 locOffset += locElementLen; 331 this.tr = tr ? new cc.Vertex3F(tr.x, tr.y, tr.z, locArrayBuffer, locOffset) : new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 332 }; 333 /** 334 * @constant 335 * @type {number} 336 */ 337 cc.Quad3.BYTES_PER_ELEMENT = 48; 338 339 /** 340 * @class cc.V3F_C4B_T2F 341 * @param {cc.Vertex3F} vertices 342 * @param {cc.Color} colors 343 * @param {cc.Tex2F} texCoords 344 * @param {Array} arrayBuffer 345 * @param {Number} offset 346 * @constructor 347 */ 348 cc.V3F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) { 349 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V3F_C4B_T2F.BYTES_PER_ELEMENT); 350 this._offset = offset || 0; 351 352 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset; 353 this._vertices = vertices ? new cc.Vertex3F(vertices.x, vertices.y, vertices.z, locArrayBuffer, locOffset) : 354 new cc.Vertex3F(0, 0, 0, locArrayBuffer, locOffset); 355 356 locOffset += cc.Vertex3F.BYTES_PER_ELEMENT; 357 this._colors = colors ? new cc.Color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) : 358 new cc.Color(0, 0, 0, 0, locArrayBuffer, locOffset); 359 360 locOffset += cc.Color.BYTES_PER_ELEMENT; 361 this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset) : 362 new cc.Tex2F(0, 0, locArrayBuffer, locOffset); 363 }; 364 /** 365 * @constant 366 * @type {number} 367 */ 368 cc.V3F_C4B_T2F.BYTES_PER_ELEMENT = 24; 369 370 _p = cc.V3F_C4B_T2F.prototype; 371 _p._getVertices = function () { 372 return this._vertices; 373 }; 374 _p._setVertices = function (verticesValue) { 375 var locVertices = this._vertices; 376 locVertices._view[0] = verticesValue.x; 377 locVertices._view[1] = verticesValue.y; 378 locVertices._view[2] = verticesValue.z; 379 }; 380 _p._getColor = function () { 381 return this._colors; 382 }; 383 _p._setColor = function (colorValue) { 384 var locColors = this._colors; 385 locColors._view[0] = colorValue.r; 386 locColors._view[1] = colorValue.g; 387 locColors._view[2] = colorValue.b; 388 locColors._view[3] = colorValue.a; 389 }; 390 _p._getTexCoords = function () { 391 return this._texCoords; 392 }; 393 _p._setTexCoords = function (texValue) { 394 this._texCoords._view[0] = texValue.u; 395 this._texCoords._view[1] = texValue.v; 396 }; 397 /** @expose */ 398 _p.vertices; 399 cc.defineGetterSetter(_p, "vertices", _p._getVertices, _p._setVertices); 400 /** @expose */ 401 _p.colors; 402 cc.defineGetterSetter(_p, "colors", _p._getColor, _p._setColor); 403 /** @expose */ 404 _p.texCoords; 405 cc.defineGetterSetter(_p, "texCoords", _p._getTexCoords, _p._setTexCoords); 406 407 /** 408 * @cc.class cc.V3F_C4B_T2F_Quad 409 * @param {cc.V3F_C4B_T2F} tl 410 * @param {cc.V3F_C4B_T2F} bl 411 * @param {cc.V3F_C4B_T2F} tr 412 * @param {cc.V3F_C4B_T2F} br 413 * @param {Array} arrayBuffer 414 * @param {Number} offset 415 * @constructor 416 */ 417 cc.V3F_C4B_T2F_Quad = function (tl, bl, tr, br, arrayBuffer, offset) { 418 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT); 419 this._offset = offset || 0; 420 421 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.V3F_C4B_T2F.BYTES_PER_ELEMENT; 422 this._tl = tl ? new cc.V3F_C4B_T2F(tl.vertices, tl.colors, tl.texCoords, locArrayBuffer, locOffset) : 423 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 424 locOffset += locElementLen; 425 this._bl = bl ? new cc.V3F_C4B_T2F(bl.vertices, bl.colors, bl.texCoords, locArrayBuffer, locOffset) : 426 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 427 locOffset += locElementLen; 428 this._tr = tr ? new cc.V3F_C4B_T2F(tr.vertices, tr.colors, tr.texCoords, locArrayBuffer, locOffset) : 429 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 430 locOffset += locElementLen; 431 this._br = br ? new cc.V3F_C4B_T2F(br.vertices, br.colors, br.texCoords, locArrayBuffer, locOffset) : 432 new cc.V3F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 433 }; 434 /** 435 * @constant 436 * @type {number} 437 */ 438 cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT = 96; 439 _p = cc.V3F_C4B_T2F_Quad.prototype; 440 _p._getTL = function () { 441 return this._tl; 442 }; 443 _p._setTL = function (tlValue) { 444 var locTl = this._tl; 445 locTl.vertices = tlValue.vertices; 446 locTl.colors = tlValue.colors; 447 locTl.texCoords = tlValue.texCoords; 448 }; 449 _p._getBL = function () { 450 return this._bl; 451 }; 452 _p._setBL = function (blValue) { 453 var locBl = this._bl; 454 locBl.vertices = blValue.vertices; 455 locBl.colors = blValue.colors; 456 locBl.texCoords = blValue.texCoords; 457 }; 458 _p._getTR = function () { 459 return this._tr; 460 }; 461 _p._setTR = function (trValue) { 462 var locTr = this._tr; 463 locTr.vertices = trValue.vertices; 464 locTr.colors = trValue.colors; 465 locTr.texCoords = trValue.texCoords; 466 }; 467 _p._getBR = function () { 468 return this._br; 469 }; 470 _p._setBR = function (brValue) { 471 var locBr = this._br; 472 locBr.vertices = brValue.vertices; 473 locBr.colors = brValue.colors; 474 locBr.texCoords = brValue.texCoords; 475 }; 476 _p._getArrayBuffer = function () { 477 return this._arrayBuffer; 478 }; 479 480 /** @expose */ 481 _p.tl; 482 cc.defineGetterSetter(_p, "tl", _p._getTL, _p._setTL); 483 /** @expose */ 484 _p.tr; 485 cc.defineGetterSetter(_p, "tr", _p._getTR, _p._setTR); 486 /** @expose */ 487 _p.bl; 488 cc.defineGetterSetter(_p, "bl", _p._getBL, _p._setBL); 489 /** @expose */ 490 _p.br; 491 cc.defineGetterSetter(_p, "br", _p._getBR, _p._setBR); 492 /** @expose */ 493 _p.arrayBuffer; 494 cc.defineGetterSetter(_p, "arrayBuffer", _p._getArrayBuffer, null); 495 496 /** 497 * @function 498 * @returns {cc.V3F_C4B_T2F_Quad} 499 */ 500 cc.V3F_C4B_T2F_QuadZero = function () { 501 return new cc.V3F_C4B_T2F_Quad(); 502 }; 503 504 /** 505 * @function 506 * @param {cc.V3F_C4B_T2F_Quad} sourceQuad 507 * @return {cc.V3F_C4B_T2F_Quad} 508 */ 509 cc.V3F_C4B_T2F_QuadCopy = function (sourceQuad) { 510 if (!sourceQuad) 511 return cc.V3F_C4B_T2F_QuadZero(); 512 513 //return new cc.V3F_C4B_T2F_Quad(sourceQuad,tl,sourceQuad,bl,sourceQuad.tr,sourceQuad.br,null,0); 514 var srcTL = sourceQuad.tl, srcBL = sourceQuad.bl, srcTR = sourceQuad.tr, srcBR = sourceQuad.br; 515 return { 516 tl: {vertices: {x: srcTL.vertices.x, y: srcTL.vertices.y, z: srcTL.vertices.z}, 517 colors: {r: srcTL.colors.r, g: srcTL.colors.g, b: srcTL.colors.b, a: srcTL.colors.a}, 518 texCoords: {u: srcTL.texCoords.u, v: srcTL.texCoords.v}}, 519 bl: {vertices: {x: srcBL.vertices.x, y: srcBL.vertices.y, z: srcBL.vertices.z}, 520 colors: {r: srcBL.colors.r, g: srcBL.colors.g, b: srcBL.colors.b, a: srcBL.colors.a}, 521 texCoords: {u: srcBL.texCoords.u, v: srcBL.texCoords.v}}, 522 tr: {vertices: {x: srcTR.vertices.x, y: srcTR.vertices.y, z: srcTR.vertices.z}, 523 colors: {r: srcTR.colors.r, g: srcTR.colors.g, b: srcTR.colors.b, a: srcTR.colors.a}, 524 texCoords: {u: srcTR.texCoords.u, v: srcTR.texCoords.v}}, 525 br: {vertices: {x: srcBR.vertices.x, y: srcBR.vertices.y, z: srcBR.vertices.z}, 526 colors: {r: srcBR.colors.r, g: srcBR.colors.g, b: srcBR.colors.b, a: srcBR.colors.a}, 527 texCoords: {u: srcBR.texCoords.u, v: srcBR.texCoords.v}} 528 }; 529 }; 530 531 /** 532 * @function 533 * @param {Array} sourceQuads 534 * @returns {Array} 535 */ 536 cc.V3F_C4B_T2F_QuadsCopy = function (sourceQuads) { 537 if (!sourceQuads) 538 return []; 539 540 var retArr = []; 541 for (var i = 0; i < sourceQuads.length; i++) { 542 retArr.push(cc.V3F_C4B_T2F_QuadCopy(sourceQuads[i])); 543 } 544 return retArr; 545 }; 546 547 //redefine cc.V2F_C4B_T2F 548 /** 549 * @class cc.V2F_C4B_T2F 550 * @param {cc.Vertex2F} vertices 551 * @param {cc.Color} colors 552 * @param {cc.Tex2F} texCoords 553 * @param {Array} arrayBuffer 554 * @param {Number} offset 555 * @constructor 556 */ 557 cc.V2F_C4B_T2F = function (vertices, colors, texCoords, arrayBuffer, offset) { 558 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V2F_C4B_T2F.BYTES_PER_ELEMENT); 559 this._offset = offset || 0; 560 561 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset; 562 this._vertices = vertices ? new cc.Vertex2F(vertices.x, vertices.y, locArrayBuffer, locOffset) : 563 new cc.Vertex2F(0, 0, locArrayBuffer, locOffset); 564 locOffset += cc.Vertex2F.BYTES_PER_ELEMENT; 565 this._colors = colors ? cc.color(colors.r, colors.g, colors.b, colors.a, locArrayBuffer, locOffset) : 566 cc.color(0, 0, 0, 0, locArrayBuffer, locOffset); 567 locOffset += cc.Color.BYTES_PER_ELEMENT; 568 this._texCoords = texCoords ? new cc.Tex2F(texCoords.u, texCoords.v, locArrayBuffer, locOffset) : 569 new cc.Tex2F(0, 0, locArrayBuffer, locOffset); 570 }; 571 572 /** 573 * @constant 574 * @type {number} 575 */ 576 cc.V2F_C4B_T2F.BYTES_PER_ELEMENT = 20; 577 _p = cc.V2F_C4B_T2F.prototype; 578 _p._getVertices = function () { 579 return this._vertices; 580 }; 581 _p._setVertices = function (verticesValue) { 582 this._vertices._view[0] = verticesValue.x; 583 this._vertices._view[1] = verticesValue.y; 584 }; 585 _p._getColor = function () { 586 return this._colors; 587 }; 588 _p._setColor = function (colorValue) { 589 var locColors = this._colors; 590 locColors._view[0] = colorValue.r; 591 locColors._view[1] = colorValue.g; 592 locColors._view[2] = colorValue.b; 593 locColors._view[3] = colorValue.a; 594 }; 595 _p._getTexCoords = function () { 596 return this._texCoords; 597 }; 598 _p._setTexCoords = function (texValue) { 599 this._texCoords._view[0] = texValue.u; 600 this._texCoords._view[1] = texValue.v; 601 }; 602 603 /** @expose */ 604 _p.vertices; 605 cc.defineGetterSetter(_p, "vertices", _p._getVertices, _p._setVertices); 606 /** @expose */ 607 _p.colors; 608 cc.defineGetterSetter(_p, "colors", _p._getColor, _p._setColor); 609 /** @expose */ 610 _p.texCoords; 611 cc.defineGetterSetter(_p, "texCoords", _p._getTexCoords, _p._setTexCoords); 612 613 //redefine cc.V2F_C4B_T2F_Triangle 614 /** 615 * @class cc.V2F_C4B_T2F_Triangle 616 * @param {cc.V2F_C4B_T2F} a 617 * @param {cc.V2F_C4B_T2F} b 618 * @param {cc.V2F_C4B_T2F} c 619 * @param {Array} arrayBuffer 620 * @param {Number} offset 621 * @constructor 622 */ 623 cc.V2F_C4B_T2F_Triangle = function (a, b, c, arrayBuffer, offset) { 624 this._arrayBuffer = arrayBuffer || new ArrayBuffer(cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT); 625 this._offset = offset || 0; 626 627 var locArrayBuffer = this._arrayBuffer, locOffset = this._offset, locElementLen = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT; 628 this._a = a ? new cc.V2F_C4B_T2F(a.vertices, a.colors, a.texCoords, locArrayBuffer, locOffset) : 629 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 630 locOffset += locElementLen; 631 this._b = b ? new cc.V2F_C4B_T2F(b.vertices, b.colors, b.texCoords, locArrayBuffer, locOffset) : 632 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 633 locOffset += locElementLen; 634 this._c = c ? new cc.V2F_C4B_T2F(c.vertices, c.colors, c.texCoords, locArrayBuffer, locOffset) : 635 new cc.V2F_C4B_T2F(null, null, null, locArrayBuffer, locOffset); 636 }; 637 /** 638 * @constant 639 * @type {number} 640 */ 641 cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT = 60; 642 _p = cc.V2F_C4B_T2F_Triangle.prototype; 643 _p._getA = function () { 644 return this._a; 645 }; 646 _p._setA = function (aValue) { 647 var locA = this._a; 648 locA.vertices = aValue.vertices; 649 locA.colors = aValue.colors; 650 locA.texCoords = aValue.texCoords; 651 }; 652 _p._getB = function () { 653 return this._b; 654 }; 655 _p._setB = function (bValue) { 656 var locB = this._b; 657 locB.vertices = bValue.vertices; 658 locB.colors = bValue.colors; 659 locB.texCoords = bValue.texCoords; 660 }; 661 _p._getC = function () { 662 return this._c; 663 }; 664 _p._setC = function (cValue) { 665 var locC = this._c; 666 locC.vertices = cValue.vertices; 667 locC.colors = cValue.colors; 668 locC.texCoords = cValue.texCoords; 669 }; 670 671 /** @expose */ 672 _p.a; 673 cc.defineGetterSetter(_p, "a", _p._getA, _p._setA); 674 /** @expose */ 675 _p.b; 676 cc.defineGetterSetter(_p, "b", _p._getB, _p._setB); 677 /** @expose */ 678 _p.c; 679 cc.defineGetterSetter(_p, "c", _p._getC, _p._setC); 680 681 /** 682 * Helper macro that creates an Vertex2F type composed of 2 floats: x, y 683 * @function 684 * @param {Number} x 685 * @param {Number} y 686 * @return {cc.Vertex2F} 687 */ 688 cc.vertex2 = function (x, y) { 689 return new cc.Vertex2F(x, y); 690 }; 691 692 /** 693 * Helper macro that creates an Vertex3F type composed of 3 floats: x, y, z 694 * @function 695 * @param {Number} x 696 * @param {Number} y 697 * @param {Number} z 698 * @return {cc.Vertex3F} 699 */ 700 cc.vertex3 = function (x, y, z) { 701 return new cc.Vertex3F(x, y, z); 702 }; 703 704 /** 705 * Helper macro that creates an Tex2F type: A texcoord composed of 2 floats: u, y 706 * @function 707 * @param {Number} u 708 * @param {Number} v 709 * @return {cc.Tex2F} 710 */ 711 cc.tex2 = function (u, v) { 712 return new cc.Tex2F(u, v); 713 }; 714 715 /** 716 * Blend Function used for textures 717 * @Class cc.BlendFunc 718 * @Constructor 719 * @param {Number} src1 source blend function 720 * @param {Number} dst1 destination blend function 721 */ 722 cc.BlendFunc = function (src1, dst1) { 723 this.src = src1; 724 this.dst = dst1; 725 }; 726 727 /** 728 * @function 729 * @returns {cc.BlendFunc} 730 */ 731 cc.blendFuncDisable = function () { 732 return new cc.BlendFunc(cc.ONE, cc.ZERO); 733 }; 734 735 /** 736 * convert a string of color for style to Color. 737 * e.g. "#ff06ff" to : cc.color(255,6,255) 738 * @function 739 * @param {String} hex 740 * @return {cc.Color} 741 */ 742 cc.hexToColor = function (hex) { 743 hex = hex.replace(/^#?/, "0x"); 744 var c = parseInt(hex); 745 var r = c >> 16; 746 var g = (c >> 8) % 256; 747 var b = c % 256; 748 return cc.color(r, g, b); 749 }; 750 751 /** 752 * convert Color to a string of color for style. 753 * e.g. cc.color(255,6,255) to : "#ff06ff" 754 * @function 755 * @param {cc.Color} color 756 * @return {String} 757 */ 758 cc.colorToHex = function (color) { 759 var hR = color.r.toString(16), hG = color.g.toString(16), hB = color.b.toString(16); 760 return "#" + (color.r < 16 ? ("0" + hR) : hR) + (color.g < 16 ? ("0" + hG) : hG) + (color.b < 16 ? ("0" + hB) : hB); 761 }; 762 763 /** 764 * text alignment : left 765 * @constant 766 * @type Number 767 */ 768 cc.TEXT_ALIGNMENT_LEFT = 0; 769 770 /** 771 * text alignment : center 772 * @constant 773 * @type Number 774 */ 775 cc.TEXT_ALIGNMENT_CENTER = 1; 776 777 /** 778 * text alignment : right 779 * @constant 780 * @type Number 781 */ 782 cc.TEXT_ALIGNMENT_RIGHT = 2; 783 784 /** 785 * text alignment : top 786 * @constant 787 * @type Number 788 */ 789 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0; 790 791 /** 792 * text alignment : center 793 * @constant 794 * @type Number 795 */ 796 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1; 797 798 /** 799 * text alignment : bottom 800 * @constant 801 * @type Number 802 */ 803 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2; 804 805 cc._Dictionary = cc.Class.extend({ 806 _keyMapTb: null, 807 _valueMapTb: null, 808 __currId: 0, 809 810 ctor: function () { 811 this._keyMapTb = {}; 812 this._valueMapTb = {}; 813 this.__currId = 2 << (0 | (Math.random() * 10)); 814 }, 815 816 __getKey: function () { 817 this.__currId++; 818 return "key_" + this.__currId; 819 }, 820 821 setObject: function (value, key) { 822 if (key == null) 823 return; 824 825 var keyId = this.__getKey(); 826 this._keyMapTb[keyId] = key; 827 this._valueMapTb[keyId] = value; 828 }, 829 830 objectForKey: function (key) { 831 if (key == null) 832 return null; 833 834 var locKeyMapTb = this._keyMapTb; 835 for (var keyId in locKeyMapTb) { 836 if (locKeyMapTb[keyId] === key) 837 return this._valueMapTb[keyId]; 838 } 839 return null; 840 }, 841 842 valueForKey: function (key) { 843 return this.objectForKey(key); 844 }, 845 846 removeObjectForKey: function (key) { 847 if (key == null) 848 return; 849 850 var locKeyMapTb = this._keyMapTb; 851 for (var keyId in locKeyMapTb) { 852 if (locKeyMapTb[keyId] === key) { 853 delete this._valueMapTb[keyId]; 854 delete locKeyMapTb[keyId]; 855 return; 856 } 857 } 858 }, 859 860 removeObjectsForKeys: function (keys) { 861 if (keys == null) 862 return; 863 864 for (var i = 0; i < keys.length; i++) 865 this.removeObjectForKey(keys[i]); 866 }, 867 868 allKeys: function () { 869 var keyArr = [], locKeyMapTb = this._keyMapTb; 870 for (var key in locKeyMapTb) 871 keyArr.push(locKeyMapTb[key]); 872 return keyArr; 873 }, 874 875 removeAllObjects: function () { 876 this._keyMapTb = {}; 877 this._valueMapTb = {}; 878 }, 879 880 count: function () { 881 return this.allKeys().length; 882 } 883 }); 884 885 /** 886 * Common usage: 887 * 888 * var fontDef = new cc.FontDefinition(); 889 * fontDef.fontName = "Arial"; 890 * fontDef.fontSize = 12; 891 * ... 892 * 893 * OR using inline definition useful for constructor injection 894 * 895 * var fontDef = new cc.FontDefinition({ 896 * fontName: "Arial", 897 * fontSize: 12 898 * }); 899 * 900 * 901 * 902 * @class cc.FontDefinition 903 * @param {Object} properties - (OPTIONAL) Allow inline FontDefinition 904 * @constructor 905 */ 906 cc.FontDefinition = function (properties) { 907 var _t = this; 908 _t.fontName = "Arial"; 909 _t.fontSize = 12; 910 _t.textAlign = cc.TEXT_ALIGNMENT_CENTER; 911 _t.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP; 912 _t.fillStyle = cc.color(255, 255, 255, 255); 913 _t.boundingWidth = 0; 914 _t.boundingHeight = 0; 915 916 _t.strokeEnabled = false; 917 _t.strokeStyle = cc.color(255, 255, 255, 255); 918 _t.lineWidth = 1; 919 _t.lineHeight = "normal"; 920 _t.fontStyle = "normal"; 921 _t.fontWeight = "normal"; 922 923 _t.shadowEnabled = false; 924 _t.shadowOffsetX = 0; 925 _t.shadowOffsetY = 0; 926 _t.shadowBlur = 0; 927 _t.shadowOpacity = 1.0; 928 929 //properties mapping: 930 if(properties && properties instanceof Object){ 931 for(var key in properties){ 932 _t[key] = properties[key]; 933 } 934 } 935 }; 936 /** 937 * Web ONLY 938 * */ 939 cc.FontDefinition.prototype._getCanvasFontStr = function(){ 940 var lineHeight = !this.lineHeight.charAt ? this.lineHeight+"px" : this.lineHeight; 941 return this.fontStyle + " " + this.fontWeight + " " + this.fontSize + "px/"+lineHeight+" '" + this.fontName + "'"; 942 }; 943 944 cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () { 945 if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) { 946 cc.assert(cc.isFunction(cc._tmp.PrototypeColor), cc._LogInfos.MissingFile, "CCTypesPropertyDefine.js"); 947 cc._tmp.PrototypeColor(); 948 delete cc._tmp.PrototypeColor; 949 } 950 }); 951