axmol/scripting/javascript/bindings/js/jsb_cocos2d.js

530 lines
14 KiB
JavaScript
Raw Normal View History

2012-11-22 16:11:47 +08:00
//
// cocos2d constants
//
var cc = cc || {};
2012-11-22 16:11:47 +08:00
cc.DIRECTOR_PROJECTION_2D = 0;
cc.DIRECTOR_PROJECTION_3D = 1;
cc.TEXTURE_PIXELFORMAT_RGBA8888 = 0;
cc.TEXTURE_PIXELFORMAT_RGB888 = 1;
cc.TEXTURE_PIXELFORMAT_RGB565 = 2;
cc.TEXTURE_PIXELFORMAT_A8 = 3;
cc.TEXTURE_PIXELFORMAT_I8 = 4;
cc.TEXTURE_PIXELFORMAT_AI88 = 5;
cc.TEXTURE_PIXELFORMAT_RGBA4444 = 6;
cc.TEXTURE_PIXELFORMAT_RGB5A1 = 7;
cc.TEXTURE_PIXELFORMAT_PVRTC4 = 8;
cc.TEXTURE_PIXELFORMAT_PVRTC4 = 9;
cc.TEXTURE_PIXELFORMAT_DEFAULT = cc.TEXTURE_PIXELFORMAT_RGBA8888;
2012-11-23 10:05:18 +08:00
cc.TEXT_ALIGNMENT_LEFT = 0;
2012-11-22 16:11:47 +08:00
cc.TEXT_ALIGNMENT_CENTER = 1;
cc.TEXT_ALIGNMENT_RIGHT = 2;
cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0;
cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 1;
cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 2;
cc.IMAGE_FORMAT_JPEG = 0;
cc.IMAGE_FORMAT_PNG = 0;
cc.PROGRESS_TIMER_TYPE_RADIAL = 0;
cc.PROGRESS_TIMER_TYPE_BAR = 1;
cc.PARTICLE_TYPE_FREE = 0;
cc.PARTICLE_TYPE_RELATIVE = 1;
cc.PARTICLE_TYPE_GROUPED = 2;
cc.PARTICLE_DURATION_INFINITY = -1;
cc.PARTICLE_MODE_GRAVITY = 0;
cc.PARTICLE_MODE_RADIUS = 1;
cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE = -1;
cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS = -1;
cc.TOUCH_ALL_AT_ONCE = 0;
cc.TOUCH_ONE_BY_ONE = 1;
cc.TMX_TILE_HORIZONTAL_FLAG = 0x80000000;
cc.TMX_TILE_VERTICAL_FLAG = 0x40000000;
cc.TMX_TILE_DIAGONAL_FLAG = 0x20000000;
cc.TRANSITION_ORIENTATION_LEFT_OVER = 0;
cc.TRANSITION_ORIENTATION_RIGHT_OVER = 1;
cc.TRANSITION_ORIENTATION_UP_OVER = 0;
cc.TRANSITION_ORIENTATION_DOWN_OVER = 1;
cc.RED = {r:255, g:0, b:0};
cc.GREEN = {r:0, g:255, b:0};
cc.BLUE = {r:0, g:0, b:255};
cc.BLACK = {r:0, g:0, b:0};
cc.WHITE = {r:255, g:255, b:255};
cc.POINT_ZERO = {x:0, y:0};
// XXX: This definition is different than cocos2d-html5
fixed #1617: Some improvements for JS Bindings. 1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff [Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android. Then cast it to unsigned int, the value will be 0. The schedule will not be able to work. I don't know why this occurs only on android. [Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++. 2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject. 3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks. 4) Improvments for JSTouchDelegate. Added four functions as follows: // Set the touch delegate to map by using the key (pJSObj). static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate); // Get the touch delegate by the key (pJSObj). static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj); // Remove the delegate by the key (pJSObj). static void removeDelegateForJSObject(JSObject* pJSObj); void unregisterTouchDelegate(); And exported cc.unregisterTouchDelegate(); to js. Fix a memory leak for JSTouchDelegate by making it as an autorelease object. 5) Don't add js callback function to the reserved slot of object. [Reason]: The target object may execute more than one schedule. Therefore, previous js callback function will be replaced by the current one. For example: this.scheduleOnce(function() { temporary function 1 }, 0.5); this.scheduleOnce(function() { temporary function 2 }, 0.5); In this case, the temporary function 1 will be removed from reserved slot 0. And temporary function 2 will be set to reserved slot 0 of this object. If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked, crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce. [Solution] Because one schedule corresponds to one JSScheduleWrapper, we root the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
// cc.REPEAT_FOREVER = - 1;
// We can't assign -1 to cc.REPEAT_FOREVER, since it will be a very big double value after
// converting it to double by JS_ValueToNumber on android.
// Then cast it to unsigned int, the value will be 0. The schedule will not be able to work.
// I don't know why this occurs only on android.
// So instead of passing -1 to it, I assign it with max value of unsigned int in c++.
cc.REPEAT_FOREVER = 0xffffffff;
cc.MENU_STATE_WAITING = 0;
cc.MENU_STATE_TRACKING_TOUCH = 1;
cc.MENU_HANDLER_PRIORITY = -128;
cc.DEFAULT_PADDING = 5;
2012-11-23 10:05:18 +08:00
// reusable objects
cc._reuse_p = [ {x:0, y:0}, {x:0,y:0}, {x:0,y:0}, {x:0,y:0} ];
2012-11-22 16:11:47 +08:00
cc._reuse_p_index = 0;
cc._reuse_size = {width:0, height:0};
cc._reuse_rect = {x:0, y:0, width:0, height:0};
cc._reuse_color3b = {r:255, g:255, b:255 };
cc._reuse_color4b = {r:255, g:255, b:255, a:255 };
cc.log = cc.log || log;
//
// Color 3B
//
2012-11-23 10:05:18 +08:00
cc.c3b = function( r, g, b )
{
2012-11-22 16:11:47 +08:00
return {r:r, g:g, b:b };
};
2012-11-23 10:05:18 +08:00
cc._c3b = function( r, g, b )
{
2012-11-22 16:11:47 +08:00
cc._reuse_color3b.r = r;
cc._reuse_color3b.g = g;
cc._reuse_color3b.b = b;
return cc._reuse_color3b;
};
//
// Color 4B
//
2012-11-23 10:05:18 +08:00
cc.c4b = function( r, g, b, a )
{
2012-11-22 16:11:47 +08:00
return {r:r, g:g, b:b, a:a };
};
2012-11-23 10:05:18 +08:00
cc._c4b = function( r, g, b, a )
{
2012-11-22 16:11:47 +08:00
cc._reuse_color4b.r = r;
cc._reuse_color4b.g = g;
cc._reuse_color4b.b = b;
cc._reuse_color4b.a = a;
return cc._reuse_color4b;
};
// compatibility
cc.c4 = cc.c4b;
cc._c4 = cc._c4b;
//
// Color 4F
//
2012-11-23 10:05:18 +08:00
cc.c4f = function( r, g, b, a )
{
2012-11-22 16:11:47 +08:00
return {r:r, g:g, b:b, a:a };
};
//
// Point
//
cc.p = function( x, y )
{
return {x:x, y:y};
};
2012-11-23 10:05:18 +08:00
cc._p = function( x, y )
{
if( cc._reuse_p_index == cc._reuse_p.length )
2012-11-22 16:11:47 +08:00
cc._reuse_p_index = 0;
var p = cc._reuse_p[ cc._reuse_p_index];
cc._reuse_p_index++;
p.x = x;
p.y = y;
return p;
};
cc.pointEqualToPoint = function (point1, point2) {
return ((point1.x == point2.x) && (point1.y == point2.y));
};
//
// Grid
//
2012-11-23 10:05:18 +08:00
cc._g = function( x, y )
{
2012-11-22 16:11:47 +08:00
cc._reuse_grid.x = x;
cc._reuse_grid.y = y;
return cc._reuse_grid;
};
//
// Size
//
2012-11-23 10:05:18 +08:00
cc.size = function(w,h)
{
2012-11-22 16:11:47 +08:00
return {width:w, height:h};
};
2012-11-23 10:05:18 +08:00
cc._size = function(w,h)
{
2012-11-22 16:11:47 +08:00
cc._reuse_size.width = w;
cc._reuse_size.height = h;
return cc._reuse_size;
};
2012-11-23 10:05:18 +08:00
cc.sizeEqualToSize = function (size1, size2)
{
2012-11-22 16:11:47 +08:00
return ((size1.width == size2.width) && (size1.height == size2.height));
};
//
// Rect
//
2012-11-23 10:05:18 +08:00
cc.rect = function(x,y,w,h)
{
2012-11-22 16:11:47 +08:00
return {x:x, y:y, width:w, height:h};
};
cc._rect = function(x,y,w,h)
{
cc._reuse_rect.x = x;
cc._reuse_rect.y = y;
cc._reuse_rect.width = w;
cc._reuse_rect.height = h;
return cc._reuse_rect;
};
cc.rectEqualToRect = function (rect1, rect2) {
return ( rect1.x==rect2.x && rect1.y==rect2.y && rect1.width==rect2.width && rect1.height==rect2.height);
};
cc.rectContainsRect = function (rect1, rect2) {
if ((rect1.x >= rect2.x) || (rect1.y >= rect2.y) ||
( rect1.x + rect1.width <= rect2.x + rect2.width) ||
( rect1.y + rect1.height <= rect2.y + rect2.height))
return false;
return true;
};
cc.rectGetMaxX = function (rect) {
return (rect.x + rect.width);
};
cc.rectGetMidX = function (rect) {
return (rect.x + rect.width / 2.0);
};
cc.rectGetMinX = function (rect) {
return rect.x;
};
cc.rectGetMaxY = function (rect) {
return(rect.y + rect.height);
};
cc.rectGetMidY = function (rect) {
return rect.y + rect.height / 2.0;
};
cc.rectGetMinY = function (rect) {
return rect.y;
};
cc.rectContainsPoint = function (rect, point) {
var ret = false;
if (point.x >= rect.x && point.x <= rect.x + rect.width &&
point.y >= rect.y && point.y <= rect.y + rect.height) {
ret = true;
}
return ret;
};
2012-11-23 10:05:18 +08:00
cc.rectIntersectsRect = function( rectA, rectB )
{
var bool = ! ( rectA.x > rectB.x + rectB.width ||
rectA.x + rectA.width < rectB.x ||
rectA.y > rectB.y +rectB.height ||
rectA.y + rectA.height < rectB.y );
2012-11-22 16:11:47 +08:00
return bool;
};
cc.rectUnion = function (rectA, rectB) {
var rect = cc.rect(0, 0, 0, 0);
rect.x = Math.min(rectA.x, rectB.x);
rect.y = Math.min(rectA.y, rectB.y);
rect.width = Math.max(rectA.x + rectA.width, rectB.x + rectB.width) - rect.x;
rect.height = Math.max(rectA.y + rectA.height, rectB.y + rectB.height) - rect.y;
return rect;
};
cc.rectIntersection = function (rectA, rectB) {
var intersection = cc.rect(
Math.max(rectA.x, rectB.x),
Math.max(rectA.y, rectB.y),
0, 0);
2012-11-23 10:05:18 +08:00
intersection.width = Math.min(rectA.x+rectA.width, rectB.x+rectB.width) - intersection.x;
intersection.height = Math.min(rectA.y+rectA.height, rectB.y+rectB.height) - intersection.y;
2012-11-22 16:11:47 +08:00
return intersection;
};
//
// Array: for cocos2d-html5 compatibility
//
fixed #1617: Some improvements for JS Bindings. 1) Changed cc.REPEAT_FOREVER = - 1 to cc.REPEAT_FOREVER = 0xffffffff [Reason]: If cc.REPEAT_FOREVER = -1, it will be a very big double value after converting it to double by JS_ValueToNumber on android. Then cast it to unsigned int, the value will be 0. The schedule will not be able to work. I don't know why this occurs only on android. [Solution]: Instead of passing -1 to it, I assign it with max value of unsigned int in c++. 2) Added two helper function, cc.ArrayGetIndexOfObject and cc.ArrayContainsObject. 3) Added JSScheduleWrapper::removeTargetForNativeNode to avoid memory leaks. 4) Improvments for JSTouchDelegate. Added four functions as follows: // Set the touch delegate to map by using the key (pJSObj). static void setDelegateForJSObject(JSObject* pJSObj, JSTouchDelegate* pDelegate); // Get the touch delegate by the key (pJSObj). static JSTouchDelegate* getDelegateForJSObject(JSObject* pJSObj); // Remove the delegate by the key (pJSObj). static void removeDelegateForJSObject(JSObject* pJSObj); void unregisterTouchDelegate(); And exported cc.unregisterTouchDelegate(); to js. Fix a memory leak for JSTouchDelegate by making it as an autorelease object. 5) Don't add js callback function to the reserved slot of object. [Reason]: The target object may execute more than one schedule. Therefore, previous js callback function will be replaced by the current one. For example: this.scheduleOnce(function() { temporary function 1 }, 0.5); this.scheduleOnce(function() { temporary function 2 }, 0.5); In this case, the temporary function 1 will be removed from reserved slot 0. And temporary function 2 will be set to reserved slot 0 of this object. If gc is triggered before the JSScheduleWrapper::scheduleFunc is invoked, crash will happen. You could simply reproduce it by adding jsc.garbageCollect(); after scheduleOnce. [Solution] Because one schedule corresponds to one JSScheduleWrapper, we root the js callback function in JSScheduleWrapper::setJSCallbackFunc and unroot it at the destructor of JSScheduleWrapper.
2012-12-18 11:56:44 +08:00
/**
* Returns index of first occurence of object, -1 if value not found.
* @function
* @param {Array} arr Source Array
* @param {*} findObj find object
* @return {Number} index of first occurence of value
*/
cc.ArrayGetIndexOfObject = function (arr, findObj) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == findObj)
return i;
}
return -1;
};
/**
* Returns a Boolean value that indicates whether value is present in the array.
* @function
* @param {Array} arr
* @param {*} findObj
* @return {Boolean}
*/
cc.ArrayContainsObject = function (arr, findObj) {
return cc.ArrayGetIndexOfObject(arr, findObj) != -1;
};
2012-11-22 16:11:47 +08:00
cc.ArrayRemoveObject = function (arr, delObj) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == delObj) {
arr.splice(i, 1);
}
}
};
//
// Helpers
//
2012-11-23 10:05:18 +08:00
cc.dump = function(obj)
{
for( var i in obj )
cc.log( i + " = " + obj[i] );
2012-11-22 16:11:47 +08:00
};
// dump config info, but only in debug mode
var sys = sys || undefined;
2012-11-23 10:05:18 +08:00
cc.dumpConfig = function()
{
if (sys) {
cc.dump(sys);
cc.dump(sys.capabilities);
}
2012-11-22 16:11:47 +08:00
};
//
// Bindings Overrides
//
// MenuItemToggle
cc.MenuItemToggle.create = function( /* var args */) {
var n = arguments.length;
if (typeof arguments[n-2] === 'function' || typeof arguments[n-1] === 'function') {
var args = Array.prototype.slice.call(arguments);
var obj = null;
if( typeof arguments[n-2] === 'function' )
obj = args.pop();
var func = args.pop();
// create it with arguments,
var item = cc.MenuItemToggle._create.apply(this, args);
// then set the callback
if( obj !== null )
item.setCallback(func, obj);
else
item.setCallback(func);
return item;
} else {
return cc.MenuItemToggle._create.apply(this, arguments);
}
};
// LabelAtlas
cc.LabelAtlas.create = function( a,b,c,d,e ) {
var n = arguments.length;
if ( n == 5) {
return cc.LabelAtlas._create(a,b,c,d,e.charCodeAt(0));
} else {
return cc.LabelAtlas._create.apply(this, arguments);
}
};
cc.LayerMultiplex.create = cc.LayerMultiplex.createWithArray;
2012-11-23 10:05:18 +08:00
2012-11-22 16:11:47 +08:00
/**
* Associates a base class with a native superclass
* @function
* @param {object} jsobj subclass
* @param {object} klass superclass
*/
2012-11-23 10:05:18 +08:00
cc.associateWithNative = function( jsobj, superclass_or_instance ) {
2012-11-22 16:11:47 +08:00
try {
// Used when subclassing using the "extend" method
var native = new superclass_or_instance();
2012-11-23 10:05:18 +08:00
__associateObjWithNative( jsobj, native );
} catch(err) {
2012-11-22 16:11:47 +08:00
// Used when subclassing using the goog.inherits method
2012-11-23 10:05:18 +08:00
__associateObjWithNative( jsobj, superclass_or_instance );
}
2012-11-22 16:11:47 +08:00
};
//
// JSB supports 2 official ways to create subclasses
//
// 1) Google "subclasses" borrowed from closure library
// This is the recommended way to do it
//
cc.inherits = function (childCtor, parentCtor) {
/** @constructor */
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
2012-11-23 10:05:18 +08:00
childCtor.prototype = new tempCtor();
childCtor.prototype.constructor = childCtor;
2012-11-22 16:11:47 +08:00
// Copy "static" method, but doesn't generate subclasses.
// for( var i in parentCtor ) {
// childCtor[ i ] = parentCtor[ i ];
// }
};
2012-11-23 10:05:18 +08:00
cc.base = function(me, opt_methodName, var_args) {
var caller = arguments.callee.caller;
if (caller.superClass_) {
// This is a constructor. Call the superclass constructor.
ret = caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1));
return ret;
}
var args = Array.prototype.slice.call(arguments, 2);
var foundCaller = false;
for (var ctor = me.constructor;
ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
if (ctor.prototype[opt_methodName] === caller) {
foundCaller = true;
} else if (foundCaller) {
return ctor.prototype[opt_methodName].apply(me, args);
}
}
// If we did not find the caller in the prototype chain,
// then one of two things happened:
// 1) The caller is an instance method.
// 2) This method was not called by the right caller.
if (me[opt_methodName] === caller) {
return me.constructor.prototype[opt_methodName].apply(me, args);
} else {
throw Error(
'cc.base called from a method of one name ' +
'to a method of a different name');
}
2012-11-22 16:11:47 +08:00
};
//
// 2) Using "extend" subclassing
// Simple JavaScript Inheritance By John Resig http://ejohn.org/
//
2012-11-23 10:05:18 +08:00
cc.Class = function(){};
2012-11-22 16:11:47 +08:00
cc.Class.extend = function (prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing && this.ctor)
this.ctor.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
2012-11-23 10:05:18 +08:00
cc.Node.prototype.ctor = function() {};
2012-11-22 16:11:47 +08:00
cc.Node.extend = cc.Class.extend;
cc.Layer.extend = cc.Class.extend;
cc.LayerGradient.extend = cc.Class.extend;
cc.LayerColor.extend = cc.Class.extend;
cc.Sprite.extend = cc.Class.extend;
cc.MenuItemFont.extend = cc.Class.extend;
cc.Scene.extend = cc.Class.extend;
2012-12-12 01:13:06 +08:00
cc.DrawNode.extend = cc.Class.extend;
// Cocos2d-html5 supports multi scene resources preloading.
// This is a compatible function for JSB.
cc.Loader = cc.Class.extend({
initWith:function (resources, selector, target) {
if (selector) {
this._selector = selector;
this._target = target;
}
this._selector.call(this._target);
}
});
cc.Loader.preload = function (resources, selector, target) {
if (!this._instance) {
this._instance = new cc.Loader();
}
this._instance.initWith(resources, selector, target);
return this._instance;
};