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