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     var newObj = (obj instanceof Array) ? [] : {};
 35     for (var key in obj) {
 36         var copy = obj[key];
 37         if (copy instanceof Array) {
 38             newObj[key] = cc.clone(copy);
 39         } else if (((typeof copy) == "object") && !(copy instanceof cc.Node)
 40             && !(copy instanceof HTMLElement)) {
 41             newObj[key] = cc.clone(copy);
 42         } else {
 43             newObj[key] = copy;
 44         }
 45     }
 46     return newObj;
 47 };
 48 
 49 /**
 50  * Function added for JS bindings compatibility. Not needed in cocos2d-html5.
 51  * @function
 52  * @param {object} jsobj subclass
 53  * @param {object} klass superclass
 54  */
 55 cc.associateWithNative = function (jsobj, superclass) {
 56 };
 57 
 58 /**
 59  * Is show bebug info on web page
 60  * @constant
 61  * @type {Boolean}
 62  */
 63 cc.IS_SHOW_DEBUG_ON_PAGE = cc.IS_SHOW_DEBUG_ON_PAGE || false;
 64 
 65 cc._logToWebPage = function (message) {
 66     var logList = document.getElementById("logInfoList");
 67     if (!logList) {
 68         var logDiv = document.createElement("Div");
 69         logDiv.setAttribute("id", "logInfoDiv");
 70         cc.canvas.parentNode.appendChild(logDiv);
 71         logDiv.setAttribute("width", "300");
 72         logDiv.setAttribute("height", cc.canvas.height);
 73         logDiv.style.zIndex = "99999";
 74         logDiv.style.position = "absolute";
 75         logDiv.style.top = "0";
 76         logDiv.style.left = "0";
 77 
 78         logList = document.createElement("ul");
 79         logDiv.appendChild(logList);
 80         logList.setAttribute("id", "logInfoList");
 81         logList.style.height = "450px";
 82         logList.style.color = "#fff";
 83         logList.style.textAlign = "left";
 84         logList.style.listStyle = "disc outside";
 85         logList.style.fontSize = "12px";
 86         logList.style.fontFamily = "arial";
 87         logList.style.padding = "0 0 0 20px";
 88         logList.style.margin = "0";
 89         logList.style.textShadow = "0 0 3px #000";
 90         logList.style.zIndex = "99998";
 91         logList.style.position = "absolute";
 92         logList.style.top = "0";
 93         logList.style.left = "0";
 94         logList.style.overflowY = "hidden";
 95 
 96         var tempDiv = document.createElement("Div");
 97         logDiv.appendChild(tempDiv);
 98         tempDiv.style.width = "300px";
 99         tempDiv.style.height = cc.canvas.height + "px";
100         tempDiv.style.opacity = "0.1";
101         tempDiv.style.background = "#fff";
102         tempDiv.style.border = "1px solid #dfdfdf";
103         tempDiv.style.borderRadius = "8px";
104     }
105     var addMessage = document.createElement("li");
106     //var now = new Date();
107     //addMessage.innerHTML = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " " + now.getMilliseconds() + " " + message;
108     addMessage.innerHTML = message;
109     if (logList.childNodes.length == 0) {
110         logList.appendChild(addMessage);
111     } else {
112         logList.insertBefore(addMessage, logList.childNodes[0]);
113     }
114 };
115 
116 /**
117  * Output Debug message.
118  * @function
119  * @param {String} message
120  */
121 cc.log = function (message) {
122     if (!cc.IS_SHOW_DEBUG_ON_PAGE) {
123         console.log(message);
124     } else {
125         cc._logToWebPage(message);
126     }
127 };
128 
129 /**
130  * Pop out a message box
131  * @param {String} message
132  * @function
133  */
134 cc.MessageBox = function (message) {
135     console.log(message);
136 };
137 
138 /**
139  * Output Assert message.
140  * @function
141  * @param {Boolean} cond If cond is false, assert.
142  * @param {String} message
143  */
144 cc.Assert = function (cond, message) {
145     if ((typeof console.assert) == "function") {
146         console.assert(cond, message);
147     } else {
148         if (!cond) {
149             if (message) {
150                 alert(message);
151             }
152         }
153     }
154 }
155 
156 /**
157  * Update Debug setting.
158  * @function
159  */
160 cc.initDebugSetting = function () {
161     // cocos2d debug
162     if (cc.COCOS2D_DEBUG == 0) {
163         cc.log = function () {
164         };
165         cc.logINFO = function () {
166         };
167         cc.logERROR = function () {
168         };
169         cc.Assert = function () {
170         };
171     }
172     else if (cc.COCOS2D_DEBUG == 1) {
173         cc.logINFO = cc.log;
174         cc.logERROR = function () {
175         };
176     }
177     else if (cc.COCOS2D_DEBUG > 1) {
178         cc.logINFO = cc.log;
179         cc.logERROR = cc.log;
180     }// COCOS2D_DEBUG
181 }
182 
183 // Enum the language type supportted now
184 /**
185  * English language code
186  * @constant
187  * @type Number
188  */
189 cc.LANGUAGE_ENGLISH = 0;
190 
191 /**
192  * Chinese language code
193  * @constant
194  * @type Number
195  */
196 cc.LANGUAGE_CHINESE = 1;
197 
198 /**
199  * French language code
200  * @constant
201  * @type Number
202  */
203 cc.LANGUAGE_FRENCH = 2;
204 
205 /**
206  * Italian language code
207  * @constant
208  * @type Number
209  */
210 cc.LANGUAGE_ITALIAN = 3;
211 
212 /**
213  * German language code
214  * @constant
215  * @type Number
216  */
217 cc.LANGUAGE_GERMAN = 4;
218 
219 /**
220  * Spanish language code
221  * @constant
222  * @type Number
223  */
224 cc.LANGUAGE_SPANISH = 5;
225 
226 /**
227  * Russian language code
228  * @constant
229  * @type Number
230  */
231 cc.LANGUAGE_RUSSIAN = 6;
232