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 cc.Color = function (r, g, b, a) { 28 this.r = r || 0; 29 this.g = g || 0; 30 this.b = b || 0; 31 this.a = a || 255; 32 }; 33 34 /** 35 * Generate a color object based on multiple forms of parameters 36 * @example 37 * 38 * // 1. All channels seperately as parameters 39 * var color1 = cc.color(255, 255, 255, 255); 40 * 41 * // 2. Convert a hex string to a color 42 * var color2 = cc.color("#000000"); 43 * 44 * // 3. An color object as parameter 45 * var color3 = cc.color({r: 255, g: 255, b: 255, a: 255}); 46 * 47 * Alpha channel is optional. Default value is 255 48 * 49 * @class cc.Color 50 * @constructor 51 * @param {Number|String|cc.Color} r 52 * @param {Number} g 53 * @param {Number} b 54 * @param {Number} [a=255] 55 * @returns {cc.Color} 56 */ 57 cc.color = function (r, g, b, a) { 58 if (r === undefined) 59 return {r: 0, g: 0, b: 0, a: 255}; 60 if (typeof r === "string") 61 return cc.hexToColor(r); 62 if (typeof r === "object") 63 return {r: r.r, g: r.g, b: r.b, a: r.a || 255}; 64 return {r: r, g: g, b: b, a: a || 255 }; 65 }; 66 67 /** 68 * returns true if both ccColor3B are equal. Otherwise it returns false. 69 * @function 70 * @param {cc.Color} color1 71 * @param {cc.Color} color2 72 * @return {Boolean} true if both ccColor3B are equal. Otherwise it returns false. 73 */ 74 cc.colorEqual = function (color1, color2) { 75 return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; 76 }; 77 78 /** 79 * the device accelerometer reports values for each axis in units of g-force 80 */ 81 cc.Acceleration = function (x, y, z, timestamp) { 82 this.x = x || 0; 83 this.y = y || 0; 84 this.z = z || 0; 85 this.timestamp = timestamp || 0; 86 }; 87 88 cc.Vertex2F = function (x1, y1) { 89 this.x = x1 || 0; 90 this.y = y1 || 0; 91 }; 92 93 /** 94 * Helper macro that creates an Vertex2F type composed of 2 floats: x, y 95 * @class cc.Vertex2F 96 * @constructor 97 * @param {Number} x 98 * @param {Number} y 99 * @return {cc.Vertex2F} 100 */ 101 cc.vertex2 = function (x, y) { 102 return new cc.Vertex2F(x, y); 103 }; 104 105 cc.Vertex3F = function (x1, y1, z1) { 106 this.x = x1 || 0; 107 this.y = y1 || 0; 108 this.z = z1 || 0; 109 }; 110 111 /** 112 * Helper macro that creates an Vertex3F type composed of 3 floats: x, y, z 113 * @class cc.Vertex3F 114 * @constructor 115 * @param {Number} x 116 * @param {Number} y 117 * @param {Number} z 118 * @return {cc.Vertex3F} 119 */ 120 cc.vertex3 = function (x, y, z) { 121 return new cc.Vertex3F(x, y, z); 122 }; 123 124 cc.Tex2F = function (u1, v1) { 125 this.u = u1 || 0; 126 this.v = v1 || 0; 127 }; 128 129 /** 130 * Helper macro that creates an Tex2F type: A texcoord composed of 2 floats: u, y 131 * @class cc.Tex2F 132 * @constructor 133 * @param {Number} u 134 * @param {Number} v 135 * @return {cc.Tex2F} 136 */ 137 cc.tex2 = function (u, v) { 138 return new cc.Tex2F(u, v); 139 }; 140 141 /** 142 * Blend Function used for textures 143 * @Class cc.BlendFunc 144 * @Constructor 145 * @param {Number} src1 source blend function 146 * @param {Number} dst1 destination blend function 147 */ 148 cc.BlendFunc = function (src1, dst1) { 149 this.src = src1; 150 this.dst = dst1; 151 }; 152 153 cc.blendFuncDisable = function () { 154 return new cc.BlendFunc(cc.ONE, cc.ZERO); 155 }; 156 157 /** 158 * convert a string of color for style to Color. 159 * e.g. "#ff06ff" to : cc.color(255,6,255) 160 * @function 161 * @param {String} hex 162 * @return {cc.Color} 163 */ 164 cc.hexToColor = function (hex) { 165 hex = hex.replace(/^#?/, "0x"); 166 var c = parseInt(hex); 167 var r = c >> 16; 168 var g = (c >> 8) % 256; 169 var b = c % 256; 170 return cc.color(r, g, b); 171 }; 172 173 /** 174 * convert Color to a string of color for style. 175 * e.g. cc.color(255,6,255) to : "#ff06ff" 176 * @function 177 * @param {cc.Color} color 178 * @return {String} 179 */ 180 cc.colorToHex = function (color) { 181 var hR = color.r.toString(16), hG = color.g.toString(16), hB = color.b.toString(16); 182 return "#" + (color.r < 16 ? ("0" + hR) : hR) + (color.g < 16 ? ("0" + hG) : hG) + (color.b < 16 ? ("0" + hB) : hB); 183 }; 184 185 /** 186 * text alignment : left 187 * @constant 188 * @type Number 189 */ 190 cc.TEXT_ALIGNMENT_LEFT = 0; 191 192 /** 193 * text alignment : center 194 * @constant 195 * @type Number 196 */ 197 cc.TEXT_ALIGNMENT_CENTER = 1; 198 199 /** 200 * text alignment : right 201 * @constant 202 * @type Number 203 */ 204 cc.TEXT_ALIGNMENT_RIGHT = 2; 205 206 /** 207 * text alignment : top 208 * @constant 209 * @type Number 210 */ 211 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0; 212 213 /** 214 * text alignment : center 215 * @constant 216 * @type Number 217 */ 218 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1; 219 220 /** 221 * text alignment : bottom 222 * @constant 223 * @type Number 224 */ 225 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2; 226 227 cc._Dictionary = cc.Class.extend({ 228 _keyMapTb: null, 229 _valueMapTb: null, 230 __currId: 0, 231 232 ctor: function () { 233 this._keyMapTb = {}; 234 this._valueMapTb = {}; 235 this.__currId = 2 << (0 | (Math.random() * 10)); 236 }, 237 238 __getKey: function () { 239 this.__currId++; 240 return "key_" + this.__currId; 241 }, 242 243 setObject: function (value, key) { 244 if (key == null) 245 return; 246 247 var keyId = this.__getKey(); 248 this._keyMapTb[keyId] = key; 249 this._valueMapTb[keyId] = value; 250 }, 251 252 objectForKey: function (key) { 253 if (key == null) 254 return null; 255 256 var locKeyMapTb = this._keyMapTb; 257 for (var keyId in locKeyMapTb) { 258 if (locKeyMapTb[keyId] === key) 259 return this._valueMapTb[keyId]; 260 } 261 return null; 262 }, 263 264 valueForKey: function (key) { 265 return this.objectForKey(key); 266 }, 267 268 removeObjectForKey: function (key) { 269 if (key == null) 270 return; 271 272 var locKeyMapTb = this._keyMapTb; 273 for (var keyId in locKeyMapTb) { 274 if (locKeyMapTb[keyId] === key) { 275 delete this._valueMapTb[keyId]; 276 delete locKeyMapTb[keyId]; 277 return; 278 } 279 } 280 }, 281 282 removeObjectsForKeys: function (keys) { 283 if (keys == null) 284 return; 285 286 for (var i = 0; i < keys.length; i++) 287 this.removeObjectForKey(keys[i]); 288 }, 289 290 allKeys: function () { 291 var keyArr = [], locKeyMapTb = this._keyMapTb; 292 for (var key in locKeyMapTb) 293 keyArr.push(locKeyMapTb[key]); 294 return keyArr; 295 }, 296 297 removeAllObjects: function () { 298 this._keyMapTb = {}; 299 this._valueMapTb = {}; 300 }, 301 302 count: function () { 303 return this.allKeys().length; 304 } 305 }); 306 307 cc.FontDefinition = function () { 308 var _t = this; 309 _t.fontName = "Arial"; 310 _t.fontSize = 12; 311 _t.textAlign = cc.TEXT_ALIGNMENT_CENTER; 312 _t.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP; 313 _t.fillStyle = cc.color(255, 255, 255, 255); 314 _t.boundingWidth = 0; 315 _t.boundingHeight = 0; 316 317 _t.strokeEnabled = false; 318 _t.strokeStyle = cc.color(255, 255, 255, 255); 319 _t.lineWidth = 1; 320 321 _t.shadowEnabled = false; 322 _t.shadowOffsetX = 0; 323 _t.shadowOffsetY = 0; 324 _t.shadowBlur = 0; 325 _t.shadowOpacity = 1.0; 326 }; 327 328 if (cc._renderType === cc._RENDER_TYPE_WEBGL) { 329 cc.assert(typeof cc._tmp.WebGLColor === "function", cc._LogInfos.MissingFile, "CCTypesWebGL.js"); 330 cc._tmp.WebGLColor(); 331 delete cc._tmp.WebGLColor; 332 } 333 334 cc.assert(typeof cc._tmp.PrototypeColor === "function", cc._LogInfos.MissingFile, "CCTypesPropertyDefine.js"); 335 cc._tmp.PrototypeColor(); 336 delete cc._tmp.PrototypeColor; 337 338