2013-03-11 16:39:28 +08:00
|
|
|
//
|
|
|
|
// jsb_cocos2d_extension_manual.cpp
|
|
|
|
//
|
|
|
|
// Created by James Chen on 3/11/13.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "jsb_cocos2dx_extension_manual.h"
|
|
|
|
#include "cocos-ext.h"
|
|
|
|
#include "ScriptingCore.h"
|
|
|
|
#include "cocos2d_specifics.hpp"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
class JSB_ScrollViewDelegate
|
|
|
|
: public CCNode
|
|
|
|
, public CCScrollViewDelegate
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_ScrollViewDelegate()
|
|
|
|
: m_pJSDelegate(NULL)
|
|
|
|
, m_bNeedUnroot(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_ScrollViewDelegate()
|
|
|
|
{
|
|
|
|
if (m_bNeedUnroot)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_RemoveObjectRoot(cx, &m_pJSDelegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
virtual void scrollViewDidScroll(CCScrollView* view)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(view);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "scrollViewDidScroll", 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void scrollViewDidZoom(CCScrollView* view)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(view);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "scrollViewDidZoom", 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
|
|
|
m_pJSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
|
|
|
js_proxy_t* p = jsb_get_js_proxy(m_pJSDelegate);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
m_bNeedUnroot = true;
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_AddNamedObjectRoot(cx, &m_pJSDelegate, "TableViewDelegate");
|
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
JSObject* m_pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
bool m_bNeedUnroot;
|
2013-03-11 16:39:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCScrollView_setDelegate(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-03-11 16:39:28 +08:00
|
|
|
cocos2d::extension::CCScrollView* cobj = (cocos2d::extension::CCScrollView *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
// save the delegate
|
|
|
|
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
|
2013-03-14 18:14:16 +08:00
|
|
|
JSB_ScrollViewDelegate* nativeDelegate = new JSB_ScrollViewDelegate();
|
2013-03-11 16:39:28 +08:00
|
|
|
nativeDelegate->setJSDelegate(jsDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
JSB_ScrollViewDelegate* oldDelegate = (JSB_ScrollViewDelegate*)cobj->getDelegate();
|
|
|
|
if (oldDelegate)
|
|
|
|
{
|
|
|
|
oldDelegate->removeFromParent();
|
|
|
|
}
|
|
|
|
cobj->addChild(nativeDelegate);
|
2013-03-11 16:39:28 +08:00
|
|
|
cobj->setDelegate(nativeDelegate);
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
nativeDelegate->release();
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
class JSB_TableViewDelegate
|
|
|
|
: public CCNode
|
|
|
|
, public CCTableViewDelegate
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_TableViewDelegate()
|
|
|
|
: m_pJSDelegate(NULL)
|
|
|
|
, m_bNeedUnroot(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_TableViewDelegate()
|
|
|
|
{
|
|
|
|
if (m_bNeedUnroot)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_RemoveObjectRoot(cx, &m_pJSDelegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
virtual void scrollViewDidScroll(CCScrollView* view)
|
|
|
|
{
|
|
|
|
callJSDelegate(view, "scrollViewDidScroll");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void scrollViewDidZoom(CCScrollView* view)
|
|
|
|
{
|
|
|
|
callJSDelegate(view, "scrollViewDidZoom");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell)
|
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellTouched");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void tableCellHighlight(CCTableView* table, CCTableViewCell* cell)
|
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellHighlight");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell)
|
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellUnhighlight");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void tableCellWillRecycle(CCTableView* table, CCTableViewCell* cell)
|
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellWillRecycle");
|
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
|
|
|
m_pJSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
|
|
|
js_proxy_t* p = jsb_get_js_proxy(m_pJSDelegate);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
m_bNeedUnroot = true;
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_AddNamedObjectRoot(cx, &m_pJSDelegate, "TableViewDelegate");
|
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
void callJSDelegate(CCScrollView* view, std::string jsFunctionName)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(view);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), jsFunctionName.c_str(), 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void callJSDelegate(CCTableView* table, CCTableViewCell* cell, std::string jsFunctionName)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(table);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * pCellProxy = jsb_get_native_proxy(cell);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!pCellProxy) return;
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
jsval args[2];
|
|
|
|
args[0] = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
args[1] = OBJECT_TO_JSVAL(pCellProxy->obj);
|
2013-03-11 16:39:28 +08:00
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), jsFunctionName.c_str(), 2, args, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* m_pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
bool m_bNeedUnroot;
|
2013-03-11 16:39:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCTableView_setDelegate(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-03-11 16:39:28 +08:00
|
|
|
cocos2d::extension::CCTableView* cobj = (cocos2d::extension::CCTableView *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
// save the delegate
|
|
|
|
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
|
2013-03-14 18:14:16 +08:00
|
|
|
JSB_TableViewDelegate* nativeDelegate = new JSB_TableViewDelegate();
|
2013-03-11 16:39:28 +08:00
|
|
|
nativeDelegate->setJSDelegate(jsDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
JSB_TableViewDelegate* oldDelegate = (JSB_TableViewDelegate*)cobj->getDelegate();
|
|
|
|
if (oldDelegate)
|
|
|
|
{
|
|
|
|
oldDelegate->removeFromParent();
|
|
|
|
}
|
|
|
|
cobj->addChild(nativeDelegate);
|
2013-03-11 16:39:28 +08:00
|
|
|
cobj->setDelegate(nativeDelegate);
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
nativeDelegate->release();
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
class JSB_TableViewDataSource
|
|
|
|
: public CCNode
|
|
|
|
, public CCTableViewDataSource
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_TableViewDataSource()
|
|
|
|
: m_pJSTableViewDataSource(NULL)
|
|
|
|
, m_bNeedUnroot(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_TableViewDataSource()
|
|
|
|
{
|
|
|
|
if (m_bNeedUnroot)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_RemoveObjectRoot(cx, &m_pJSTableViewDataSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
virtual CCSize tableCellSizeForIndex(CCTableView *table, unsigned int idx)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
jsval ret;
|
2013-04-04 22:57:43 +08:00
|
|
|
bool ok = callJSDelegate(table, idx, "tableCellSizeForIndex", ret);
|
|
|
|
if (!ok) {
|
|
|
|
ok = callJSDelegate(table, "cellSizeForTable", ret);
|
|
|
|
}
|
|
|
|
if (ok) {
|
2013-03-11 16:39:28 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
CCSize size;
|
|
|
|
JSBool isSucceed = jsval_to_ccsize(cx, ret, &size);
|
|
|
|
if (isSucceed) return size;
|
|
|
|
}
|
|
|
|
return CCSizeZero;
|
2013-04-04 22:57:43 +08:00
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx)
|
|
|
|
{
|
|
|
|
jsval ret;
|
|
|
|
bool ok = callJSDelegate(table, idx, "tableCellAtIndex", ret);
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
cocos2d::extension::CCTableViewCell* arg0;
|
|
|
|
do {
|
|
|
|
js_proxy_t *proxy;
|
|
|
|
JSObject *tmpObj = JSVAL_TO_OBJECT(ret);
|
2013-06-04 12:53:42 +08:00
|
|
|
proxy = jsb_get_js_proxy(tmpObj);
|
2013-03-11 16:39:28 +08:00
|
|
|
arg0 = (cocos2d::extension::CCTableViewCell*)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( arg0, cx, NULL, "Invalid Native Object");
|
|
|
|
} while (0);
|
|
|
|
return arg0;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned int numberOfCellsInTableView(CCTableView *table)
|
|
|
|
{
|
|
|
|
jsval ret;
|
|
|
|
bool ok = callJSDelegate(table, "numberOfCellsInTableView", ret);
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
uint32_t count = 0;
|
|
|
|
JSBool isSucceed = jsval_to_uint32(cx, ret, &count);
|
|
|
|
if (isSucceed) return count;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setTableViewDataSource(JSObject* pJSSource)
|
|
|
|
{
|
|
|
|
m_pJSTableViewDataSource = pJSSource;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
|
|
|
js_proxy_t* p = jsb_get_js_proxy(m_pJSTableViewDataSource);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
m_bNeedUnroot = true;
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_AddNamedObjectRoot(cx, &m_pJSTableViewDataSource, "TableViewDataSource");
|
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool callJSDelegate(CCTableView* table, std::string jsFunctionName, jsval& retVal)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(table);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return false;
|
|
|
|
|
|
|
|
JSBool hasAction;
|
|
|
|
jsval temp_retval;
|
|
|
|
jsval dataVal = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JSObject* obj = m_pJSTableViewDataSource;
|
|
|
|
|
|
|
|
if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) {
|
|
|
|
if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(temp_retval == JSVAL_VOID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
|
|
|
|
1, &dataVal, &retVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool callJSDelegate(CCTableView* table, int idx, std::string jsFunctionName, jsval& retVal)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(table);
|
2013-03-11 16:39:28 +08:00
|
|
|
if (!p) return false;
|
|
|
|
|
|
|
|
|
|
|
|
JSBool hasAction;
|
|
|
|
jsval temp_retval;
|
|
|
|
jsval dataVal[2];
|
|
|
|
dataVal[0] = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
dataVal[1] = INT_TO_JSVAL(idx);
|
|
|
|
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JSObject* obj = m_pJSTableViewDataSource;
|
|
|
|
|
|
|
|
if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) {
|
|
|
|
if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(temp_retval == JSVAL_VOID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
|
|
|
|
2, dataVal, &retVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSObject* m_pJSTableViewDataSource;
|
2013-06-05 14:31:07 +08:00
|
|
|
bool m_bNeedUnroot;
|
2013-03-11 16:39:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCTableView_setDataSource(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-03-11 16:39:28 +08:00
|
|
|
cocos2d::extension::CCTableView* cobj = (cocos2d::extension::CCTableView *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
JSB_TableViewDataSource* pNativeSource = new JSB_TableViewDataSource();
|
2013-03-11 16:39:28 +08:00
|
|
|
pNativeSource->setTableViewDataSource(JSVAL_TO_OBJECT(argv[0]));
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
JSB_TableViewDataSource* oldDataSource = (JSB_TableViewDataSource*)cobj->getDataSource();
|
|
|
|
if (oldDataSource)
|
|
|
|
{
|
|
|
|
oldDataSource->removeFromParent();
|
|
|
|
}
|
|
|
|
cobj->addChild(pNativeSource);
|
2013-03-11 16:39:28 +08:00
|
|
|
cobj->setDataSource(pNativeSource);
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
pNativeSource->release();
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportError(cx, "wrong number of arguments");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCTableView_create(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSBool ok = JS_TRUE;
|
|
|
|
if (argc == 3 || argc == 2) {
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
JSB_TableViewDataSource* pNativeSource = new JSB_TableViewDataSource();
|
2013-03-11 16:39:28 +08:00
|
|
|
pNativeSource->setTableViewDataSource(JSVAL_TO_OBJECT(argv[0]));
|
|
|
|
|
|
|
|
cocos2d::CCSize arg1;
|
|
|
|
ok &= jsval_to_ccsize(cx, argv[1], &arg1);
|
|
|
|
cocos2d::extension::CCTableView* ret = NULL;
|
|
|
|
ret = new CCTableView();
|
|
|
|
ret->autorelease();
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
JSB_TableViewDataSource* oldDataSource = (JSB_TableViewDataSource*)ret->getDataSource();
|
|
|
|
if (oldDataSource)
|
|
|
|
{
|
|
|
|
oldDataSource->removeFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->setDataSource(pNativeSource);
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
jsval jsret;
|
|
|
|
do {
|
|
|
|
if (ret) {
|
|
|
|
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::extension::CCTableView>(cx, ret);
|
|
|
|
jsret = OBJECT_TO_JSVAL(proxy->obj);
|
|
|
|
} else {
|
|
|
|
jsret = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
ret->initWithViewSize(arg1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cocos2d::CCNode* arg2;
|
|
|
|
do {
|
|
|
|
js_proxy_t *proxy;
|
|
|
|
JSObject *tmpObj = JSVAL_TO_OBJECT(argv[2]);
|
2013-06-04 12:53:42 +08:00
|
|
|
proxy = jsb_get_js_proxy(tmpObj);
|
2013-03-11 16:39:28 +08:00
|
|
|
arg2 = (cocos2d::CCNode*)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( arg2, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
} while (0);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
|
|
|
|
ret->initWithViewSize(arg1, arg2);
|
|
|
|
}
|
2013-04-04 22:57:43 +08:00
|
|
|
ret->reloadData();
|
2013-03-11 16:39:28 +08:00
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
ret->addChild(pNativeSource);
|
|
|
|
pNativeSource->release();
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JS_SET_RVAL(cx, vp, jsret);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportError(cx, "wrong number of arguments");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
class JSB_EditBoxDelegate
|
|
|
|
: public CCNode
|
|
|
|
, public CCEditBoxDelegate
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_EditBoxDelegate()
|
|
|
|
: m_pJSDelegate(NULL)
|
|
|
|
, m_bNeedUnroot(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_EditBoxDelegate()
|
|
|
|
{
|
|
|
|
if (m_bNeedUnroot)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_RemoveObjectRoot(cx, &m_pJSDelegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
virtual void editBoxEditingDidBegin(CCEditBox* editBox)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(editBox);
|
2013-03-14 18:14:16 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "editBoxEditingDidBegin", 1, &arg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void editBoxEditingDidEnd(CCEditBox* editBox)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(editBox);
|
2013-03-14 18:14:16 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "editBoxEditingDidEnd", 1, &arg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(editBox);
|
2013-03-14 18:14:16 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
jsval dataVal[2];
|
|
|
|
dataVal[0] = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
std::string arg1 = text;
|
|
|
|
dataVal[1] = std_string_to_jsval(ScriptingCore::getInstance()->getGlobalContext(), arg1);
|
|
|
|
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "editBoxTextChanged", 2, dataVal, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void editBoxReturn(CCEditBox* editBox)
|
|
|
|
{
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t * p = jsb_get_native_proxy(editBox);
|
2013-03-14 18:14:16 +08:00
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
jsval arg = OBJECT_TO_JSVAL(p->obj);
|
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(m_pJSDelegate), "editBoxReturn", 1, &arg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
|
|
|
m_pJSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
|
|
|
js_proxy_t* p = jsb_get_js_proxy(m_pJSDelegate);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
m_bNeedUnroot = true;
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_AddNamedObjectRoot(cx, &m_pJSDelegate, "TableViewDelegate");
|
|
|
|
}
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
JSObject* m_pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
bool m_bNeedUnroot;
|
2013-03-14 18:14:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCEditBox_setDelegate(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-06-04 12:53:42 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-03-14 18:14:16 +08:00
|
|
|
cocos2d::extension::CCEditBox* cobj = (cocos2d::extension::CCEditBox *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
// save the delegate
|
|
|
|
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
|
|
|
|
JSB_EditBoxDelegate* nativeDelegate = new JSB_EditBoxDelegate();
|
|
|
|
nativeDelegate->setJSDelegate(jsDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
JSB_EditBoxDelegate* oldDelegate = (JSB_EditBoxDelegate*)cobj->getDelegate();
|
|
|
|
if (oldDelegate)
|
|
|
|
{
|
|
|
|
oldDelegate->removeFromParent();
|
|
|
|
}
|
|
|
|
cobj->addChild(nativeDelegate);
|
2013-03-14 18:14:16 +08:00
|
|
|
cobj->setDelegate(nativeDelegate);
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
nativeDelegate->release();
|
|
|
|
|
2013-03-14 18:14:16 +08:00
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
extern JSObject* jsb_CCScrollView_prototype;
|
|
|
|
extern JSObject* jsb_CCTableView_prototype;
|
2013-03-14 18:14:16 +08:00
|
|
|
extern JSObject* jsb_CCEditBox_prototype;
|
2013-03-11 16:39:28 +08:00
|
|
|
|
|
|
|
void register_all_cocos2dx_extension_manual(JSContext* cx, JSObject* global)
|
|
|
|
{
|
|
|
|
JS_DefineFunction(cx, jsb_CCScrollView_prototype, "setDelegate", js_cocos2dx_CCScrollView_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_CCTableView_prototype, "setDelegate", js_cocos2dx_CCTableView_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_CCTableView_prototype, "setDataSource", js_cocos2dx_CCTableView_setDataSource, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-03-15 11:53:56 +08:00
|
|
|
JS_DefineFunction(cx, jsb_CCEditBox_prototype, "setDelegate", js_cocos2dx_CCEditBox_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-03-11 16:39:28 +08:00
|
|
|
|
|
|
|
JSObject *tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.TableView; })()"));
|
|
|
|
JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCTableView_create, 3, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
}
|