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