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 seperately 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 (typeof r === "string")
 68         return cc.hexToColor(r);
 69     if (typeof r === "object")
 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: (r.a == null) ? 255 : r.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  * @constructor
104  * @param {Number} x1
105  * @param {Number} y1
106  */
107 cc.Vertex2F = function (x1, y1) {
108     this.x = x1 || 0;
109     this.y = y1 || 0;
110 };
111 
112 /**
113  * Helper macro that creates an Vertex2F type composed of 2 floats: x, y
114  * @function
115  * @param {Number} x
116  * @param {Number} y
117  * @return {cc.Vertex2F}
118  */
119 cc.vertex2 = function (x, y) {
120     return new cc.Vertex2F(x, y);
121 };
122 
123 /**
124  * @class cc.Vertex3F
125  * @constructor
126  * @param {Number} x1
127  * @param {Number} y1
128  * @param {Number} z1
129  */
130 cc.Vertex3F = function (x1, y1, z1) {
131     this.x = x1 || 0;
132     this.y = y1 || 0;
133     this.z = z1 || 0;
134 };
135 
136 /**
137  * Helper macro that creates an Vertex3F type composed of 3 floats: x, y, z
138  * @function
139  * @param {Number} x
140  * @param {Number} y
141  * @param {Number} z
142  * @return {cc.Vertex3F}
143  */
144 cc.vertex3 = function (x, y, z) {
145     return new cc.Vertex3F(x, y, z);
146 };
147 
148 /**
149  * @class cc.Tex2F
150  * @constructor
151  * @param {Number} u1
152  * @param {Number} v1
153  */
154 cc.Tex2F = function (u1, v1) {
155     this.u = u1 || 0;
156     this.v = v1 || 0;
157 };
158 
159 /**
160  * Helper macro that creates an Tex2F type: A texcoord composed of 2 floats: u, y
161  * @function
162  * @param {Number} u
163  * @param {Number} v
164  * @return {cc.Tex2F}
165  */
166 cc.tex2 = function (u, v) {
167     return new cc.Tex2F(u, v);
168 };
169 
170 /**
171  * Blend Function used for textures
172  * @Class cc.BlendFunc
173  * @Constructor
174  * @param {Number} src1 source blend function
175  * @param {Number} dst1 destination blend function
176  */
177 cc.BlendFunc = function (src1, dst1) {
178     this.src = src1;
179     this.dst = dst1;
180 };
181 
182 /**
183  * @function
184  * @returns {cc.BlendFunc}
185  */
186 cc.blendFuncDisable = function () {
187     return new cc.BlendFunc(cc.ONE, cc.ZERO);
188 };
189 
190 /**
191  * convert a string of color for style to Color.
192  * e.g. "#ff06ff"  to : cc.color(255,6,255)
193  * @function
194  * @param {String} hex
195  * @return {cc.Color}
196  */
197 cc.hexToColor = function (hex) {
198     hex = hex.replace(/^#?/, "0x");
199     var c = parseInt(hex);
200     var r = c >> 16;
201     var g = (c >> 8) % 256;
202     var b = c % 256;
203     return cc.color(r, g, b);
204 };
205 
206 /**
207  * convert Color to a string of color for style.
208  * e.g.  cc.color(255,6,255)  to : "#ff06ff"
209  * @function
210  * @param {cc.Color} color
211  * @return {String}
212  */
213 cc.colorToHex = function (color) {
214     var hR = color.r.toString(16), hG = color.g.toString(16), hB = color.b.toString(16);
215     return "#" + (color.r < 16 ? ("0" + hR) : hR) + (color.g < 16 ? ("0" + hG) : hG) + (color.b < 16 ? ("0" + hB) : hB);
216 };
217 
218 /**
219  * text alignment : left
220  * @constant
221  * @type Number
222  */
223 cc.TEXT_ALIGNMENT_LEFT = 0;
224 
225 /**
226  * text alignment : center
227  * @constant
228  * @type Number
229  */
230 cc.TEXT_ALIGNMENT_CENTER = 1;
231 
232 /**
233  * text alignment : right
234  * @constant
235  * @type Number
236  */
237 cc.TEXT_ALIGNMENT_RIGHT = 2;
238 
239 /**
240  * text alignment : top
241  * @constant
242  * @type Number
243  */
244 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0;
245 
246 /**
247  * text alignment : center
248  * @constant
249  * @type Number
250  */
251 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1;
252 
253 /**
254  * text alignment : bottom
255  * @constant
256  * @type Number
257  */
258 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2;
259 
260 cc._Dictionary = cc.Class.extend({
261     _keyMapTb: null,
262     _valueMapTb: null,
263     __currId: 0,
264 
265     ctor: function () {
266         this._keyMapTb = {};
267         this._valueMapTb = {};
268         this.__currId = 2 << (0 | (Math.random() * 10));
269     },
270 
271     __getKey: function () {
272         this.__currId++;
273         return "key_" + this.__currId;
274     },
275 
276     setObject: function (value, key) {
277         if (key == null)
278             return;
279 
280         var keyId = this.__getKey();
281         this._keyMapTb[keyId] = key;
282         this._valueMapTb[keyId] = value;
283     },
284 
285     objectForKey: function (key) {
286         if (key == null)
287             return null;
288 
289         var locKeyMapTb = this._keyMapTb;
290         for (var keyId in locKeyMapTb) {
291             if (locKeyMapTb[keyId] === key)
292                 return this._valueMapTb[keyId];
293         }
294         return null;
295     },
296 
297     valueForKey: function (key) {
298         return this.objectForKey(key);
299     },
300 
301     removeObjectForKey: function (key) {
302         if (key == null)
303             return;
304 
305         var locKeyMapTb = this._keyMapTb;
306         for (var keyId in locKeyMapTb) {
307             if (locKeyMapTb[keyId] === key) {
308                 delete this._valueMapTb[keyId];
309                 delete locKeyMapTb[keyId];
310                 return;
311             }
312         }
313     },
314 
315     removeObjectsForKeys: function (keys) {
316         if (keys == null)
317             return;
318 
319         for (var i = 0; i < keys.length; i++)
320             this.removeObjectForKey(keys[i]);
321     },
322 
323     allKeys: function () {
324         var keyArr = [], locKeyMapTb = this._keyMapTb;
325         for (var key in locKeyMapTb)
326             keyArr.push(locKeyMapTb[key]);
327         return keyArr;
328     },
329 
330     removeAllObjects: function () {
331         this._keyMapTb = {};
332         this._valueMapTb = {};
333     },
334 
335     count: function () {
336         return this.allKeys().length;
337     }
338 });
339 
340 /**
341  * @class cc.FontDefinition
342  * @constructor
343  */
344 cc.FontDefinition = function () {
345     var _t = this;
346     _t.fontName = "Arial";
347     _t.fontSize = 12;
348     _t.textAlign = cc.TEXT_ALIGNMENT_CENTER;
349     _t.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
350     _t.fillStyle = cc.color(255, 255, 255, 255);
351     _t.boundingWidth = 0;
352     _t.boundingHeight = 0;
353 
354     _t.strokeEnabled = false;
355     _t.strokeStyle = cc.color(255, 255, 255, 255);
356     _t.lineWidth = 1;
357 
358     _t.shadowEnabled = false;
359     _t.shadowOffsetX = 0;
360     _t.shadowOffsetY = 0;
361     _t.shadowBlur = 0;
362     _t.shadowOpacity = 1.0;
363 };
364 
365 if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
366     cc.assert(typeof cc._tmp.WebGLColor === "function", cc._LogInfos.MissingFile, "CCTypesWebGL.js");
367     cc._tmp.WebGLColor();
368     delete cc._tmp.WebGLColor;
369 }
370 
371 cc.assert(typeof cc._tmp.PrototypeColor === "function", cc._LogInfos.MissingFile, "CCTypesPropertyDefine.js");
372 cc._tmp.PrototypeColor();
373 delete cc._tmp.PrototypeColor;
374 
375