axmol/scripting/javascript/bindings/js_bindings_ccbreader.cpp

331 lines
11 KiB
C++
Raw Normal View History

2012-09-19 09:19:28 +08:00
//
// js_bindings_ccbreader.cpp
// watermelon
//
// Created by Rohan Kuruvilla on 14/08/2012.
//
//
#include "js_bindings_ccbreader.h"
#include "ScriptingCore.h"
#include "js_bindings_config.h"
2012-09-19 09:19:28 +08:00
USING_NS_CC;
USING_NS_CC_EXT;
static void removeSelector(std::string &str) {
size_t found;
found = str.find(":");
while (found!=std::string::npos){
str.replace(found, found+1, "");
found = str.find(":");
}
}
2012-09-19 09:19:28 +08:00
SEL_MenuHandler CCBScriptCallbackProxy::onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget,
const char * pSelectorName) {
this->callBackProp = pSelectorName;
removeSelector(this->callBackProp);
2012-09-19 09:19:28 +08:00
return menu_selector(CCBScriptCallbackProxy::menuItemCallback);
}
SEL_CallFuncN CCBScriptCallbackProxy::onResolveCCBCCCallFuncSelector(cocos2d::CCObject * pTarget,
const char * pSelectorName) {
}
2012-09-19 09:19:28 +08:00
SEL_CCControlHandler CCBScriptCallbackProxy::onResolveCCBCCControlSelector(CCObject * pTarget,
const char * pSelectorName) {
2012-09-19 09:19:28 +08:00
this->callBackProp = pSelectorName;
removeSelector(this->callBackProp);
2012-09-19 09:19:28 +08:00
return cccontrol_selector(CCBScriptCallbackProxy::controlCallback);
}
bool CCBScriptCallbackProxy::onAssignCCBMemberVariable(CCObject * pTarget,
const char * pMemberVariableName,
2012-09-19 09:19:28 +08:00
CCNode * pNode) {
return true;
2012-09-19 09:19:28 +08:00
}
void CCBScriptCallbackProxy::onNodeLoaded(CCNode * pNode,
CCNodeLoader * pNodeLoader) {}
CCBSelectorResolver * CCBScriptCallbackProxy::createNew() {
CCBScriptCallbackProxy * ret = new CCBScriptCallbackProxy();
ret->setJSOwner(this->owner);
return dynamic_cast<CCBSelectorResolver *>(ret);
}
void CCBScriptCallbackProxy::menuItemCallback(CCObject *pSender) {
2013-03-14 18:14:16 +08:00
ScriptingCore::getInstance()->executeFunctionWithOwner(owner, callBackProp.c_str() );
2012-09-19 09:19:28 +08:00
}
void CCBScriptCallbackProxy::controlCallback(CCObject *pSender, CCControlEvent event) {
2013-03-14 18:14:16 +08:00
ScriptingCore::getInstance()->executeFunctionWithOwner(owner, callBackProp.c_str() );
2012-09-19 09:19:28 +08:00
}
void CCBScriptCallbackProxy::setCallbackProperty(const char *prop) {
callBackProp = prop;
}
void CCBScriptCallbackProxy::setJSOwner(jsval ownr) {
owner = ownr;
}
jsval CCBScriptCallbackProxy::getJSOwner() {
return owner;
}
JSBool js_cocos2dx_CCBAnimationManager_animationCompleteCallback(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc >= 1) {
jsval *argv = JS_ARGV(cx, vp);
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy;
JS_GET_NATIVE_PROXY(proxy, obj);
cocos2d::extension::CCBAnimationManager *node = (cocos2d::extension::CCBAnimationManager *)(proxy ? proxy->ptr : NULL);
JSCCBAnimationWrapper *tmpCobj = new JSCCBAnimationWrapper();
tmpCobj->autorelease();
tmpCobj->setJSCallbackThis(argv[0]);
if(argc >= 2) {
tmpCobj->setJSCallbackFunc(argv[1]);
}
node->setAnimationCompletedCallback(tmpCobj, callfunc_selector(JSCCBAnimationWrapper::animationCompleteCallback));
JS_SetReservedSlot(proxy->obj, 0, argv[0]);
JS_SetReservedSlot(proxy->obj, 1, argv[1]);
return JS_TRUE;
}
return JS_FALSE;
}
JSBool js_cocos2dx_CCBReader_readNodeGraphFromFile(JSContext *cx, uint32_t argc, jsval *vp)
{
jsval *argv = JS_ARGV(cx, vp);
JSBool ok = JS_TRUE;
JSObject *obj;
cocos2d::extension::CCBReader* cobj;
obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy; JS_GET_NATIVE_PROXY(proxy, obj);
cobj = (cocos2d::extension::CCBReader *)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, cobj)
2012-09-19 09:19:28 +08:00
if (argc == 2) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
cocos2d::CCObject* arg1;
do {
js_proxy_t *proxy;
JSObject *tmpObj = JSVAL_TO_OBJECT(argv[1]);
JS_GET_NATIVE_PROXY(proxy, tmpObj);
arg1 = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, arg1)
} while (0);
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCNode* ret = cobj->readNodeGraphFromFile(arg0, arg1);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCNode>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
}
} while (0);
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
if (argc == 1) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCNode* ret = cobj->readNodeGraphFromFile(arg0);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCNode>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
}
} while (0);
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
if (argc == 3) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
cocos2d::CCObject* arg1;
do {
js_proxy_t *proxy;
JSObject *tmpObj = JSVAL_TO_OBJECT(argv[1]);
JS_GET_NATIVE_PROXY(proxy, tmpObj);
arg1 = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, arg1)
} while (0);
cocos2d::CCSize arg2;
ok &= jsval_to_ccsize(cx, argv[2], &arg2);
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCNode* ret = cobj->readNodeGraphFromFile(arg0, arg1, arg2);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCNode>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
}
} while (0);
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
return JS_FALSE;
2012-09-19 09:19:28 +08:00
}
JSBool js_cocos2dx_CCBReader_createSceneWithNodeGraphFromFile(JSContext *cx, uint32_t argc, jsval *vp)
2012-09-19 09:19:28 +08:00
{
jsval *argv = JS_ARGV(cx, vp);
JSBool ok = JS_TRUE;
JSObject *obj;
cocos2d::extension::CCBReader* cobj;
obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy; JS_GET_NATIVE_PROXY(proxy, obj);
cobj = (cocos2d::extension::CCBReader *)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, cobj)
if (argc == 2) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
cocos2d::CCObject* arg1;
do {
2012-09-19 09:19:28 +08:00
js_proxy_t *proxy;
JSObject *tmpObj = JSVAL_TO_OBJECT(argv[1]);
JS_GET_NATIVE_PROXY(proxy, tmpObj);
arg1 = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, arg1)
} while (0);
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCScene* ret = cobj->createSceneWithNodeGraphFromFile(arg0, arg1);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCScene>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
}
} while (0);
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
if (argc == 1) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCScene* ret = cobj->createSceneWithNodeGraphFromFile(arg0);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCScene>(cx, ret);
2012-09-19 09:19:28 +08:00
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
}
} while (0);
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
if (argc == 3) {
const char* arg0;
std::string arg0_tmp; ok &= jsval_to_std_string(cx, argv[0], &arg0_tmp); arg0 = arg0_tmp.c_str();
cocos2d::CCObject* arg1;
do {
js_proxy_t *proxy;
JSObject *tmpObj = JSVAL_TO_OBJECT(argv[1]);
JS_GET_NATIVE_PROXY(proxy, tmpObj);
arg1 = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, arg1)
} while (0);
cocos2d::CCSize arg2;
ok &= jsval_to_ccsize(cx, argv[2], &arg2);
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cocos2d::CCScene* ret = cobj->createSceneWithNodeGraphFromFile(arg0, arg1, arg2);
jsval jsret; do {
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCScene>(cx, ret);
2012-09-19 09:19:28 +08:00
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
jsret = JSVAL_NULL;
2012-09-19 09:19:28 +08:00
}
} while (0);
2012-09-19 09:19:28 +08:00
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
}
return JS_FALSE;
}
2012-09-19 09:19:28 +08:00
JSBool js_CocosBuilder_create(JSContext *cx, uint32_t argc, jsval *vp)
{
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
ccNodeLoaderLibrary->registerCCNodeLoader("", JSLayerLoader::loader());
cocos2d::extension::CCBReader * ret = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary);
ret->autorelease();
jsval jsret;
if (ret) {
js_proxy_t *proxy;
JS_GET_PROXY(proxy, ret);
if (proxy) {
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
// create a new js obj of that class
proxy = js_get_or_create_proxy<cocos2d::extension::CCBReader>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
}
} else {
jsret = JSVAL_NULL;
2012-09-19 09:19:28 +08:00
}
JS_SET_RVAL(cx, vp, jsret);
return JS_TRUE;
2012-09-19 09:19:28 +08:00
}
extern JSObject* jsb_CCBReader_prototype;
extern JSObject* jsb_CCBAnimationManager_prototype;
2012-09-19 09:19:28 +08:00
void register_CCBuilderReader(JSContext *cx, JSObject *obj) {
jsval nsval;
JSObject *ns;
JS_GetProperty(cx, obj, "cc", &nsval);
if (nsval == JSVAL_VOID) {
ns = JS_NewObject(cx, NULL, NULL, NULL);
nsval = OBJECT_TO_JSVAL(ns);
JS_SetProperty(cx, obj, "cc", &nsval);
} else {
JS_ValueToObject(cx, nsval, &ns);
}
obj = ns;
JSObject *tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, obj, "(function () { return cc._Reader; })()"));
JS_DefineFunction(cx, tmpObj, "create", js_CocosBuilder_create, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "loadScene", js_cocos2dx_CCBReader_createSceneWithNodeGraphFromFile, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, jsb_CCBReader_prototype, "load", js_cocos2dx_CCBReader_readNodeGraphFromFile, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, jsb_CCBAnimationManager_prototype, "setCompletedAnimationCallback", js_cocos2dx_CCBAnimationManager_animationCompleteCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT);
2012-09-19 09:19:28 +08:00
}