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