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  * copy an new object
 29  * @function
 30  * @param {object|Array} obj source object
 31  * @return {Array|object}
 32  */
 33 cc.clone = function (obj) {
 34     // Cloning is better if the new object is having the same prototype chain
 35     // as the copied obj (or otherwise, the cloned object is certainly going to
 36     // have a different hidden class). Play with C1/C2 of the
 37     // PerformanceVirtualMachineTests suite to see how this makes an impact
 38     // under extreme conditions.
 39     //
 40     // Object.create(Object.getPrototypeOf(obj)) doesn't work well because the
 41     // prototype lacks a link to the constructor (Carakan, V8) so the new
 42     // object wouldn't have the hidden class that's associated with the
 43     // constructor (also, for whatever reasons, utilizing
 44     // Object.create(Object.getPrototypeOf(obj)) + Object.defineProperty is even
 45     // slower than the original in V8). Therefore, we call the constructor, but
 46     // there is a big caveat - it is possible that the this.init() in the
 47     // constructor would throw with no argument. It is also possible that a
 48     // derived class forgets to set "constructor" on the prototype. We ignore
 49     // these possibities for and the ultimate solution is a standardized
 50     // Object.clone(<object>).
 51     var newObj = (obj.constructor) ? new obj.constructor : {};
 52 
 53         // Assuming that the constuctor above initialized all properies on obj, the
 54     // following keyed assignments won't turn newObj into dictionary mode
 55     // becasue they're not *appending new properties* but *assigning existing
 56     // ones* (note that appending indexed properties is another story). See
 57     // CCClass.js for a link to the devils when the assumption fails.
 58     for (var key in obj) {
 59         var copy = obj[key];
 60         // Beware that typeof null == "object" !
 61         if (((typeof copy) == "object") && copy &&
 62             !(copy instanceof cc.Node) && !(copy instanceof HTMLElement)) {
 63             newObj[key] = cc.clone(copy);
 64         } else {
 65             newObj[key] = copy;
 66         }
 67     }
 68     return newObj;
 69 };
 70 
 71 /**
 72  * Function added for JS bindings compatibility. Not needed in cocos2d-html5.
 73  * @function
 74  * @param {object} jsObj subclass
 75  * @param {object} superclass
 76  */
 77 cc.associateWithNative = function (jsObj, superclass) {
 78 };
 79 
 80 /**
 81  * Is show bebug info on web page
 82  * @constant
 83  * @type {Boolean}
 84  */
 85 cc.IS_SHOW_DEBUG_ON_PAGE = cc.IS_SHOW_DEBUG_ON_PAGE || false;
 86 
 87 cc._logToWebPage = function (message) {
 88     var logList = document.getElementById("logInfoList");
 89     if (!logList) {
 90         var logDiv = document.createElement("Div");
 91         logDiv.setAttribute("id", "logInfoDiv");
 92         cc.canvas.parentNode.appendChild(logDiv);
 93         logDiv.setAttribute("width", "300");
 94         logDiv.setAttribute("height", cc.canvas.height);
 95         logDiv.style.zIndex = "99999";
 96         logDiv.style.position = "absolute";
 97         logDiv.style.top = "0";
 98         logDiv.style.left = "0";
 99 
100         logList = document.createElement("ul");
101         logDiv.appendChild(logList);
102         logList.setAttribute("id", "logInfoList");
103         logList.style.height = "450px";
104         logList.style.color = "#fff";
105         logList.style.textAlign = "left";
106         logList.style.listStyle = "disc outside";
107         logList.style.fontSize = "12px";
108         logList.style.fontFamily = "arial";
109         logList.style.padding = "0 0 0 20px";
110         logList.style.margin = "0";
111         logList.style.textShadow = "0 0 3px #000";
112         logList.style.zIndex = "99998";
113         logList.style.position = "absolute";
114         logList.style.top = "0";
115         logList.style.left = "0";
116         logList.style.overflowY = "hidden";
117 
118         var tempDiv = document.createElement("Div");
119         logDiv.appendChild(tempDiv);
120         tempDiv.style.width = "300px";
121         tempDiv.style.height = cc.canvas.height + "px";
122         tempDiv.style.opacity = "0.1";
123         tempDiv.style.background = "#fff";
124         tempDiv.style.border = "1px solid #dfdfdf";
125         tempDiv.style.borderRadius = "8px";
126     }
127     var addMessage = document.createElement("li");
128     //var now = new Date();
129     //addMessage.innerHTML = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " " + now.getMilliseconds() + " " + message;
130     addMessage.innerHTML = message;
131     if (logList.childNodes.length == 0) {
132         logList.appendChild(addMessage);
133     } else {
134         logList.insertBefore(addMessage, logList.childNodes[0]);
135     }
136 };
137 
138 /**
139  * Output Debug message.
140  * @function
141  * @param {String} message
142  */
143 cc.log = function (message) {
144     if (!cc.IS_SHOW_DEBUG_ON_PAGE) {
145         console.log(message);
146     } else {
147         cc._logToWebPage(message);
148     }
149 };
150 
151 /**
152  * Pop out a message box
153  * @param {String} message
154  * @function
155  */
156 cc.MessageBox = function (message) {
157     console.log(message);
158 };
159 
160 /**
161  * Output Assert message.
162  * @function
163  * @param {Boolean} cond If cond is false, assert.
164  * @param {String} message
165  */
166 cc.Assert = function (cond, message) {
167     if (console.assert)
168         console.assert(cond, message);
169     else {
170         if (!cond) {
171             if (message)
172                 alert(message);
173         }
174     }
175 };
176 
177 /**
178  * Update Debug setting.
179  * @function
180  */
181 cc.initDebugSetting = function () {
182     // cocos2d debug
183     if (cc.COCOS2D_DEBUG == 0) {
184         cc.log = function () {
185         };
186         cc.logINFO = function () {
187         };
188         cc.logERROR = function () {
189         };
190         cc.Assert = function () {
191         };
192     } else if (cc.COCOS2D_DEBUG == 1) {
193         cc.logINFO = cc.log;
194         cc.logERROR = function () {
195         };
196     } else if (cc.COCOS2D_DEBUG > 1) {
197         cc.logINFO = cc.log;
198         cc.logERROR = cc.log;
199     }// COCOS2D_DEBUG
200 };
201 
202 // Enum the language type supportted now
203 /**
204  * English language code
205  * @constant
206  * @type Number
207  */
208 cc.LANGUAGE_ENGLISH = 0;
209 
210 /**
211  * Chinese language code
212  * @constant
213  * @type Number
214  */
215 cc.LANGUAGE_CHINESE = 1;
216 
217 /**
218  * French language code
219  * @constant
220  * @type Number
221  */
222 cc.LANGUAGE_FRENCH = 2;
223 
224 /**
225  * Italian language code
226  * @constant
227  * @type Number
228  */
229 cc.LANGUAGE_ITALIAN = 3;
230 
231 /**
232  * German language code
233  * @constant
234  * @type Number
235  */
236 cc.LANGUAGE_GERMAN = 4;
237 
238 /**
239  * Spanish language code
240  * @constant
241  * @type Number
242  */
243 cc.LANGUAGE_SPANISH = 5;
244 
245 /**
246  * Russian language code
247  * @constant
248  * @type Number
249  */
250 cc.LANGUAGE_RUSSIAN = 6;
251 
252 /**
253  * Korean language code
254  * @constant
255  * @type Number
256  */
257 cc.LANGUAGE_KOREAN = 7;
258 
259 /**
260  * Japanese language code
261  * @constant
262  * @type Number
263  */
264 cc.LANGUAGE_JAPANESE = 8;
265 
266 /**
267  * Hungarian language code
268  * @constant
269  * @type Number
270  */
271 cc.LANGUAGE_HUNGARIAN = 9;
272 
273 /**
274  * Portuguese language code
275  * @constant
276  * @type Number
277  */
278 cc.LANGUAGE_PORTUGUESE = 10;
279 
280 /**
281  * Arabic language code
282  * @constant
283  * @type Number
284  */
285 cc.LANGUAGE_ARABIC = 11;
286 
287 /**
288  * Norwegian language code
289  * @constant
290  * @type Number
291  */
292 cc.LANGUAGE_NORWEGIAN = 12;
293 
294 /**
295  * Polish language code
296  * @constant
297  * @type Number
298  */
299 cc.LANGUAGE_POLISH = 13;
300 
301 
302