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 var cc = cc || {};
 28 cc._tmp = cc._tmp || {};
 29 
 30 /**
 31  * Function added for JS bindings compatibility. Not needed in cocos2d-html5.
 32  * @function
 33  * @param {object} jsObj subclass
 34  * @param {object} superclass
 35  */
 36 cc.associateWithNative = function (jsObj, superclass) {
 37 };
 38 
 39 /**
 40  * Key map for keyboard event
 41  *
 42  * @constant
 43  * @type {Object}
 44  * @example
 45     cc.eventManager.addListener({
 46         event: cc.EventListener.KEYBOARD,
 47         onKeyPressed:  function(keyCode, event){
 48             if (cc.KEY["a"] == keyCode) {
 49                 cc.log("A is pressed");
 50             }
 51         }
 52     }, this);
 53  */
 54 cc.KEY = {
 55     none:0,
 56 
 57     // android
 58     back:6,
 59     menu:18,
 60 
 61     backspace:8,
 62     tab:9,
 63 
 64     enter:13,
 65 
 66     shift:16, //should use shiftkey instead
 67     ctrl:17, //should use ctrlkey
 68     alt:18, //should use altkey
 69     pause:19,
 70     capslock:20,
 71 
 72     escape:27,
 73     space:32,
 74     pageup:33,
 75     pagedown:34,
 76     end:35,
 77     home:36,
 78     left:37,
 79     up:38,
 80     right:39,
 81     down:40,
 82     select:41,
 83 
 84     insert:45,
 85     Delete:46,
 86     0:48,
 87     1:49,
 88     2:50,
 89     3:51,
 90     4:52,
 91     5:53,
 92     6:54,
 93     7:55,
 94     8:56,
 95     9:57,
 96     a:65,
 97     b:66,
 98     c:67,
 99     d:68,
100     e:69,
101     f:70,
102     g:71,
103     h:72,
104     i:73,
105     j:74,
106     k:75,
107     l:76,
108     m:77,
109     n:78,
110     o:79,
111     p:80,
112     q:81,
113     r:82,
114     s:83,
115     t:84,
116     u:85,
117     v:86,
118     w:87,
119     x:88,
120     y:89,
121     z:90,
122 
123     num0:96,
124     num1:97,
125     num2:98,
126     num3:99,
127     num4:100,
128     num5:101,
129     num6:102,
130     num7:103,
131     num8:104,
132     num9:105,
133     '*':106,
134     '+':107,
135     '-':109,
136     'numdel':110,
137     '/':111,
138     f1:112, //f1-f12 don't work on ie
139     f2:113,
140     f3:114,
141     f4:115,
142     f5:116,
143     f6:117,
144     f7:118,
145     f8:119,
146     f9:120,
147     f10:121,
148     f11:122,
149     f12:123,
150 
151     numlock:144,
152     scrolllock:145,
153 
154     ';':186,
155     semicolon:186,
156     equal:187,
157     '=':187,
158     ',':188,
159     comma:188,
160     dash:189,
161     '.':190,
162     period:190,
163     forwardslash:191,
164     grave:192,
165     '[':219,
166     openbracket:219,
167     backslash:220,
168     ']':221,
169     closebracket:221,
170     quote:222,
171 
172     // gamepad control
173     dpadLeft:1000,
174     dpadRight:1001,
175     dpadUp:1003,
176     dpadDown:1004,
177     dpadCenter:1005
178 };
179 
180 /**
181  * Image Format:JPG
182  * @constant
183  * @type {Number}
184  */
185 cc.FMT_JPG = 0;
186 
187 /**
188  * Image Format:PNG
189  * @constant
190  * @type {Number}
191  */
192 cc.FMT_PNG = 1;
193 
194 /**
195  * Image Format:TIFF
196  * @constant
197  * @type {Number}
198  */
199 cc.FMT_TIFF = 2;
200 
201 /**
202  * Image Format:RAWDATA
203  * @constant
204  * @type {Number}
205  */
206 cc.FMT_RAWDATA = 3;
207 
208 /**
209  * Image Format:WEBP
210  * @constant
211  * @type {Number}
212  */
213 cc.FMT_WEBP = 4;
214 
215 /**
216  * Image Format:UNKNOWN
217  * @constant
218  * @type {Number}
219  */
220 cc.FMT_UNKNOWN = 5;
221 
222 /**
223  * get image format by image data
224  * @function
225  * @param {Array} imgData
226  * @returns {Number}
227  */
228 cc.getImageFormatByData = function (imgData) {
229 	// if it is a png file buffer.
230     if (imgData.length > 8 && imgData[0] === 0x89
231         && imgData[1] === 0x50
232         && imgData[2] === 0x4E
233         && imgData[3] === 0x47
234         && imgData[4] === 0x0D
235         && imgData[5] === 0x0A
236         && imgData[6] === 0x1A
237         && imgData[7] === 0x0A) {
238         return cc.FMT_PNG;
239     }
240 
241 	// if it is a tiff file buffer.
242     if (imgData.length > 2 && ((imgData[0] === 0x49 && imgData[1] === 0x49)
243         || (imgData[0] === 0x4d && imgData[1] === 0x4d)
244         || (imgData[0] === 0xff && imgData[1] === 0xd8))) {
245         return cc.FMT_TIFF;
246     }
247 	return cc.FMT_UNKNOWN;
248 };
249 
250 /**
251  * Another way to subclass: Using Google Closure.
252  * The following code was copied + pasted from goog.base / goog.inherits
253  * @function
254  * @param {Function} childCtor
255  * @param {Function} parentCtor
256  */
257 cc.inherits = function (childCtor, parentCtor) {
258     function tempCtor() {}
259     tempCtor.prototype = parentCtor.prototype;
260     childCtor.superClass_ = parentCtor.prototype;
261     childCtor.prototype = new tempCtor();
262     childCtor.prototype.constructor = childCtor;
263 
264     // Copy "static" method, but doesn't generate subclasses.
265 // for( var i in parentCtor ) {
266 // childCtor[ i ] = parentCtor[ i ];
267 // }
268 };
269 
270 /**
271  * @deprecated since v3.0, please use cc.Class.extend and _super
272  * @cc.Class.extend
273  */
274 cc.base = function(me, opt_methodName, var_args) {
275     var caller = arguments.callee.caller;
276     if (caller.superClass_) {
277         // This is a constructor. Call the superclass constructor.
278         ret = caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1));
279         return ret;
280     }
281 
282     var args = Array.prototype.slice.call(arguments, 2);
283     var foundCaller = false;
284     for (var ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
285         if (ctor.prototype[opt_methodName] === caller) {
286             foundCaller = true;
287         } else if (foundCaller) {
288             return ctor.prototype[opt_methodName].apply(me, args);
289         }
290     }
291 
292     // If we did not find the caller in the prototype chain,
293     // then one of two things happened:
294     // 1) The caller is an instance method.
295     // 2) This method was not called by the right caller.
296     if (me[opt_methodName] === caller) {
297         return me.constructor.prototype[opt_methodName].apply(me, args);
298     } else {
299         throw Error(
300             'cc.base called from a method of one name ' +
301                 'to a method of a different name');
302     }
303 };
304