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  * 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;
 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  * Blend Function used for textures
154  * @Class
155  * @Construct
156  * @param {Number} src1 source blend function
157  * @param {Number} dst1 destination blend function
158  */
159 cc.BlendFunc = function (src1, dst1) {
160     this.src = src1;
161     this.dst = dst1;
162 };
163 
164 cc.blendFuncDisable = function () {
165     return new cc.BlendFunc(cc.ONE, cc.ZERO);
166 };
167 
168 /**
169  * convert a string of color for style to Color.
170  * e.g. "#ff06ff"  to : cc.color(255,6,255)
171  * @param {String} hex
172  * @return {cc.Color}
173  */
174 cc.hexToColor = function (hex) {
175     hex = hex.replace(/^#?/, "0x");
176     var c = parseInt(hex);
177     var r = c >> 16;
178     var g = (c >> 8) % 256;
179     var b = c % 256;
180     return cc.color(r, g, b);
181 };
182 
183 /**
184  * convert Color to a string of color for style.
185  * e.g.  cc.color(255,6,255)  to : "#ff06ff"
186  * @param {cc.Color} color
187  * @return {String}
188  */
189 cc.colorToHex = function (color) {
190     var hR = color.r.toString(16), hG = color.g.toString(16), hB = color.b.toString(16);
191     return "#" + (color.r < 16 ? ("0" + hR) : hR) + (color.g < 16 ? ("0" + hG) : hG) + (color.b < 16 ? ("0" + hB) : hB);
192 };
193 
194 /**
195  * text alignment : left
196  * @constant
197  * @type Number
198  */
199 cc.TEXT_ALIGNMENT_LEFT = 0;
200 
201 /**
202  * text alignment : center
203  * @constant
204  * @type Number
205  */
206 cc.TEXT_ALIGNMENT_CENTER = 1;
207 
208 /**
209  * text alignment : right
210  * @constant
211  * @type Number
212  */
213 cc.TEXT_ALIGNMENT_RIGHT = 2;
214 
215 /**
216  * text alignment : top
217  * @constant
218  * @type Number
219  */
220 cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0;
221 
222 /**
223  * text alignment : center
224  * @constant
225  * @type Number
226  */
227 cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1;
228 
229 /**
230  * text alignment : bottom
231  * @constant
232  * @type Number
233  */
234 cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2;
235 
236 cc._Dictionary = cc.Class.extend({
237     _keyMapTb: null,
238     _valueMapTb: null,
239     __currId: 0,
240 
241     ctor: function () {
242         this._keyMapTb = {};
243         this._valueMapTb = {};
244         this.__currId = 2 << (0 | (Math.random() * 10));
245     },
246 
247     __getKey: function () {
248         this.__currId++;
249         return "key_" + this.__currId;
250     },
251 
252     setObject: function (value, key) {
253         if (key == null)
254             return;
255 
256         var keyId = this.__getKey();
257         this._keyMapTb[keyId] = key;
258         this._valueMapTb[keyId] = value;
259     },
260 
261     objectForKey: function (key) {
262         if (key == null)
263             return null;
264 
265         var locKeyMapTb = this._keyMapTb;
266         for (var keyId in locKeyMapTb) {
267             if (locKeyMapTb[keyId] === key)
268                 return this._valueMapTb[keyId];
269         }
270         return null;
271     },
272 
273     valueForKey: function (key) {
274         return this.objectForKey(key);
275     },
276 
277     removeObjectForKey: function (key) {
278         if (key == null)
279             return;
280 
281         var locKeyMapTb = this._keyMapTb;
282         for (var keyId in locKeyMapTb) {
283             if (locKeyMapTb[keyId] === key) {
284                 delete this._valueMapTb[keyId];
285                 delete locKeyMapTb[keyId];
286                 return;
287             }
288         }
289     },
290 
291     removeObjectsForKeys: function (keys) {
292         if (keys == null)
293             return;
294 
295         for (var i = 0; i < keys.length; i++)
296             this.removeObjectForKey(keys[i]);
297     },
298 
299     allKeys: function () {
300         var keyArr = [], locKeyMapTb = this._keyMapTb;
301         for (var key in locKeyMapTb)
302             keyArr.push(locKeyMapTb[key]);
303         return keyArr;
304     },
305 
306     removeAllObjects: function () {
307         this._keyMapTb = {};
308         this._valueMapTb = {};
309     },
310 
311     count: function () {
312         return this.allKeys().length;
313     }
314 });
315 
316 cc.FontDefinition = function () {
317     var _t = this;
318     _t.fontName = "Arial";
319     _t.fontSize = 12;
320     _t.textAlign = cc.TEXT_ALIGNMENT_CENTER;
321     _t.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
322     _t.fillStyle = cc.color(255, 255, 255, 255);
323     _t.boundingWidth = 0;
324     _t.boundingHeight = 0;
325 
326     _t.strokeEnabled = false;
327     _t.strokeStyle = cc.color(255, 255, 255, 255);
328     _t.lineWidth = 1;
329 
330     _t.shadowEnabled = false;
331     _t.shadowOffsetX = 0;
332     _t.shadowOffsetY = 0;
333     _t.shadowBlur = 0;
334     _t.shadowOpacity = 1.0;
335 };
336 
337 if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
338     cc.assert(typeof cc._tmp.WebGLColor === "function", cc._LogInfos.MissingFile, "CCTypesWebGL.js");
339     cc._tmp.WebGLColor();
340     delete cc._tmp.WebGLColor;
341 }
342 
343 cc.assert(typeof cc._tmp.PrototypeColor === "function", cc._LogInfos.MissingFile, "CCTypesPropertyDefine.js");
344 cc._tmp.PrototypeColor();
345 delete cc._tmp.PrototypeColor;
346 
347