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
|
2013-06-20 22:51:54 +08:00
|
|
|
: public Object
|
2013-06-20 14:33:59 +08:00
|
|
|
, public ScrollViewDelegate
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_ScrollViewDelegate()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _JSDelegate(NULL)
|
|
|
|
, _needUnroot(false)
|
2013-06-05 14:31:07 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_ScrollViewDelegate()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_needUnroot)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void scrollViewDidScroll(ScrollView* view)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "scrollViewDidScroll", 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void scrollViewDidZoom(ScrollView* view)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "scrollViewDidZoom", 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_JSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
2013-06-15 14:03:30 +08:00
|
|
|
js_proxy_t* p = jsb_get_js_proxy(_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
if (!p)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_needUnroot = true;
|
2013-06-05 14:31:07 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_AddNamedObjectRoot(cx, &_JSDelegate, "TableViewDelegate");
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* _JSDelegate;
|
|
|
|
bool _needUnroot;
|
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-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::ScrollView* cobj = (cocos2d::extension::ScrollView *)(proxy ? proxy->ptr : NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
// 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
|
|
|
|
2013-06-20 22:51:54 +08:00
|
|
|
cobj->setUserObject(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-20 22:51:54 +08:00
|
|
|
|
|
|
|
#define KEY_TABLEVIEW_DATA_SOURCE "TableViewDataSource"
|
|
|
|
#define KEY_TABLEVIEW_DELEGATE "TableViewDelegate"
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
class JSB_TableViewDelegate
|
2013-06-20 22:51:54 +08:00
|
|
|
: public Object
|
2013-06-20 14:33:59 +08:00
|
|
|
, public TableViewDelegate
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_TableViewDelegate()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _JSDelegate(NULL)
|
|
|
|
, _needUnroot(false)
|
2013-06-05 14:31:07 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_TableViewDelegate()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_needUnroot)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void scrollViewDidScroll(ScrollView* view)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(view, "scrollViewDidScroll");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void scrollViewDidZoom(ScrollView* view)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(view, "scrollViewDidZoom");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void tableCellTouched(TableView* table, TableViewCell* cell)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellTouched");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void tableCellHighlight(TableView* table, TableViewCell* cell)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellHighlight");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellUnhighlight");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
callJSDelegate(table, cell, "tableCellWillRecycle");
|
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_JSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
2013-06-15 14:03:30 +08:00
|
|
|
js_proxy_t* p = jsb_get_js_proxy(_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
if (!p)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_needUnroot = true;
|
2013-06-05 14:31:07 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_AddNamedObjectRoot(cx, &_JSDelegate, "TableViewDelegate");
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2013-06-20 14:33:59 +08:00
|
|
|
void callJSDelegate(ScrollView* view, std::string jsFunctionName)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 1, &arg, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void callJSDelegate(TableView* table, TableViewCell* cell, std::string jsFunctionName)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 2, args, NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* _JSDelegate;
|
|
|
|
bool _needUnroot;
|
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-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::TableView* cobj = (cocos2d::extension::TableView *)(proxy ? proxy->ptr : NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
// 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
|
|
|
|
2013-06-20 22:51:54 +08:00
|
|
|
Dictionary* userDict = static_cast<Dictionary*>(cobj->getUserObject());
|
|
|
|
if (NULL == userDict)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
2013-06-20 22:51:54 +08:00
|
|
|
userDict = new Dictionary();
|
|
|
|
cobj->setUserObject(userDict);
|
|
|
|
userDict->release();
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-06-20 22:51:54 +08:00
|
|
|
|
|
|
|
userDict->setObject(nativeDelegate, KEY_TABLEVIEW_DELEGATE);
|
|
|
|
|
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
|
2013-06-20 22:51:54 +08:00
|
|
|
: public Object
|
2013-06-20 14:33:59 +08:00
|
|
|
, public TableViewDataSource
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_TableViewDataSource()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _JSTableViewDataSource(NULL)
|
|
|
|
, _needUnroot(false)
|
2013-06-05 14:31:07 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_TableViewDataSource()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_needUnroot)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &_JSTableViewDataSource);
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual Size tableCellSizeForIndex(TableView *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);
|
2013-08-09 15:00:06 +08:00
|
|
|
if (!ok)
|
|
|
|
{
|
2013-04-04 22:57:43 +08:00
|
|
|
ok = callJSDelegate(table, "cellSizeForTable", ret);
|
|
|
|
}
|
2013-08-09 15:00:06 +08:00
|
|
|
if (ok)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-20 14:33:59 +08:00
|
|
|
Size size;
|
2013-03-11 16:39:28 +08:00
|
|
|
JSBool isSucceed = jsval_to_ccsize(cx, ret, &size);
|
|
|
|
if (isSucceed) return size;
|
|
|
|
}
|
2013-07-12 14:47:36 +08:00
|
|
|
return Size::ZERO;
|
2013-04-04 22:57:43 +08:00
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual TableViewCell* tableCellAtIndex(TableView *table, unsigned int idx)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
jsval ret;
|
|
|
|
bool ok = callJSDelegate(table, idx, "tableCellAtIndex", ret);
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::TableViewCell* arg0;
|
2013-03-11 16:39:28 +08:00
|
|
|
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-06-20 14:33:59 +08:00
|
|
|
arg0 = (cocos2d::extension::TableViewCell*)(proxy ? proxy->ptr : NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
JSB_PRECONDITION2( arg0, cx, NULL, "Invalid Native Object");
|
|
|
|
} while (0);
|
|
|
|
return arg0;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual unsigned int numberOfCellsInTableView(TableView *table)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_JSTableViewDataSource = pJSSource;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
2013-06-15 14:03:30 +08:00
|
|
|
js_proxy_t* p = jsb_get_js_proxy(_JSTableViewDataSource);
|
2013-06-05 14:31:07 +08:00
|
|
|
if (!p)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_needUnroot = true;
|
2013-06-05 14:31:07 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_AddNamedObjectRoot(cx, &_JSTableViewDataSource, "TableViewDataSource");
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-03-11 16:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-20 14:33:59 +08:00
|
|
|
bool callJSDelegate(TableView* table, std::string jsFunctionName, jsval& retVal)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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();
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* obj = _JSTableViewDataSource;
|
2013-03-11 16:39:28 +08:00
|
|
|
|
2013-08-09 15:00:06 +08:00
|
|
|
if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction)
|
|
|
|
{
|
|
|
|
if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval))
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 15:00:06 +08:00
|
|
|
if(temp_retval == JSVAL_VOID)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 15:00:06 +08:00
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
|
|
|
|
1, &dataVal, &retVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool callJSDelegate(TableView* table, int idx, std::string jsFunctionName, jsval& retVal)
|
2013-03-11 16:39:28 +08:00
|
|
|
{
|
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();
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* obj = _JSTableViewDataSource;
|
2013-03-11 16:39:28 +08:00
|
|
|
|
2013-08-09 15:00:06 +08:00
|
|
|
if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction)
|
|
|
|
{
|
|
|
|
if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval))
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 15:00:06 +08:00
|
|
|
|
|
|
|
if(temp_retval == JSVAL_VOID)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-09 15:00:06 +08:00
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
|
|
|
|
2, dataVal, &retVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* _JSTableViewDataSource;
|
|
|
|
bool _needUnroot;
|
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-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::TableView* cobj = (cocos2d::extension::TableView *)(proxy ? proxy->ptr : NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
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
|
|
|
|
2013-06-20 22:51:54 +08:00
|
|
|
Dictionary* userDict = static_cast<Dictionary*>(cobj->getUserObject());
|
|
|
|
if (NULL == userDict)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
2013-06-20 22:51:54 +08:00
|
|
|
userDict = new Dictionary();
|
|
|
|
cobj->setUserObject(userDict);
|
|
|
|
userDict->release();
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-06-20 22:51:54 +08:00
|
|
|
|
|
|
|
userDict->setObject(pNativeSource, KEY_TABLEVIEW_DATA_SOURCE);
|
|
|
|
|
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;
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 3 || argc == 2)
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
|
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-20 14:33:59 +08:00
|
|
|
cocos2d::Size arg1;
|
2013-03-11 16:39:28 +08:00
|
|
|
ok &= jsval_to_ccsize(cx, argv[1], &arg1);
|
2013-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::TableView* ret = NULL;
|
|
|
|
ret = new TableView();
|
2013-03-11 16:39:28 +08:00
|
|
|
ret->autorelease();
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
ret->setDataSource(pNativeSource);
|
|
|
|
|
2013-03-11 16:39:28 +08:00
|
|
|
jsval jsret;
|
|
|
|
do {
|
2013-08-09 15:00:06 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::extension::TableView>(cx, ret);
|
2013-03-11 16:39:28 +08:00
|
|
|
jsret = OBJECT_TO_JSVAL(proxy->obj);
|
2013-08-09 15:00:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
jsret = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
ret->initWithViewSize(arg1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
cocos2d::Node* arg2;
|
2013-08-09 15:00:06 +08:00
|
|
|
do
|
|
|
|
{
|
2013-03-11 16:39:28 +08:00
|
|
|
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-06-20 14:33:59 +08:00
|
|
|
arg2 = (cocos2d::Node*)(proxy ? proxy->ptr : NULL);
|
2013-03-11 16:39:28 +08:00
|
|
|
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-20 22:51:54 +08:00
|
|
|
Dictionary* userDict = new Dictionary();
|
|
|
|
userDict->setObject(pNativeSource, KEY_TABLEVIEW_DATA_SOURCE);
|
|
|
|
ret->setUserObject(userDict);
|
|
|
|
userDict->release();
|
|
|
|
|
2013-06-05 14:31:07 +08:00
|
|
|
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
|
2013-06-20 22:51:54 +08:00
|
|
|
: public Object
|
2013-06-20 14:33:59 +08:00
|
|
|
, public EditBoxDelegate
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-05 14:31:07 +08:00
|
|
|
JSB_EditBoxDelegate()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _JSDelegate(NULL)
|
|
|
|
, _needUnroot(false)
|
2013-06-05 14:31:07 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_EditBoxDelegate()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_needUnroot)
|
2013-06-05 14:31:07 +08:00
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_RemoveObjectRoot(cx, &_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void editBoxEditingDidBegin(EditBox* editBox)
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxEditingDidBegin", 1, &arg, NULL);
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void editBoxEditingDidEnd(EditBox* editBox)
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxEditingDidEnd", 1, &arg, NULL);
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void editBoxTextChanged(EditBox* editBox, const std::string& text)
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
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);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxTextChanged", 2, dataVal, NULL);
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void editBoxReturn(EditBox* editBox)
|
2013-03-14 18:14:16 +08:00
|
|
|
{
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxReturn", 1, &arg, NULL);
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setJSDelegate(JSObject* pJSDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_JSDelegate = pJSDelegate;
|
2013-06-05 14:31:07 +08:00
|
|
|
|
|
|
|
// Check whether the js delegate is a pure js object.
|
2013-06-15 14:03:30 +08:00
|
|
|
js_proxy_t* p = jsb_get_js_proxy(_JSDelegate);
|
2013-06-05 14:31:07 +08:00
|
|
|
if (!p)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_needUnroot = true;
|
2013-06-05 14:31:07 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-06-15 14:03:30 +08:00
|
|
|
JS_AddNamedObjectRoot(cx, &_JSDelegate, "TableViewDelegate");
|
2013-06-05 14:31:07 +08:00
|
|
|
}
|
2013-03-14 18:14:16 +08:00
|
|
|
}
|
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
JSObject* _JSDelegate;
|
|
|
|
bool _needUnroot;
|
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-06-20 14:33:59 +08:00
|
|
|
cocos2d::extension::EditBox* cobj = (cocos2d::extension::EditBox *)(proxy ? proxy->ptr : NULL);
|
2013-03-14 18:14:16 +08:00
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
2013-03-14 18:14:16 +08:00
|
|
|
// 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
|
|
|
|
2013-06-20 22:51:54 +08:00
|
|
|
cobj->setUserObject(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-08-09 10:58:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class JSB_ControlButtonTarget : public Object
|
|
|
|
{
|
2013-07-25 20:52:16 +08:00
|
|
|
public:
|
2013-08-09 10:58:25 +08:00
|
|
|
JSB_ControlButtonTarget()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~JSB_ControlButtonTarget()
|
|
|
|
{
|
|
|
|
CCLOGINFO("In the destruction of JSB_ControlButtonTarget ...");
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
if (_needUnroot)
|
|
|
|
{
|
|
|
|
JS_RemoveObjectRoot(cx, &_jsTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_RemoveObjectRoot(cx, &_jsFunc);
|
|
|
|
|
|
|
|
for (auto iter = _jsNativeTargetMap.begin(); iter != _jsNativeTargetMap.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (this == iter->second)
|
|
|
|
{
|
|
|
|
_jsNativeTargetMap.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onEvent(Object *controlButton, Control::EventType event)
|
|
|
|
{
|
2013-07-25 20:52:16 +08:00
|
|
|
js_proxy_t * p;
|
|
|
|
JS_GET_PROXY(p, controlButton);
|
2013-08-09 15:00:06 +08:00
|
|
|
if (!p)
|
|
|
|
{
|
2013-07-25 20:52:16 +08:00
|
|
|
log("Failed to get proxy for control button");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval dataVal[2];
|
|
|
|
dataVal[0] = OBJECT_TO_JSVAL(p->obj);
|
2013-08-05 10:03:02 +08:00
|
|
|
int arg1 = (int)event;
|
2013-07-25 20:52:16 +08:00
|
|
|
dataVal[1] = INT_TO_JSVAL(arg1);
|
2013-08-09 10:58:25 +08:00
|
|
|
jsval jsRet;
|
|
|
|
|
|
|
|
ScriptingCore::getInstance()->executeJSFunctionWithThisObj(OBJECT_TO_JSVAL(_jsTarget), OBJECT_TO_JSVAL(_jsFunc), 2, dataVal, &jsRet);
|
2013-07-25 20:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setJSTarget(JSObject* pJSTarget)
|
|
|
|
{
|
|
|
|
_jsTarget = pJSTarget;
|
2013-08-09 10:58:25 +08:00
|
|
|
|
|
|
|
js_proxy_t* p = jsb_get_js_proxy(_jsTarget);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
|
|
|
JS_AddNamedObjectRoot(cx, &_jsTarget, "JSB_ControlButtonTarget, target");
|
|
|
|
_needUnroot = true;
|
|
|
|
}
|
2013-07-25 20:52:16 +08:00
|
|
|
}
|
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
void setJSAction(JSObject* jsFunc)
|
|
|
|
{
|
2013-07-25 20:52:16 +08:00
|
|
|
_jsFunc = jsFunc;
|
2013-08-09 10:58:25 +08:00
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
|
2013-08-09 10:58:25 +08:00
|
|
|
JS_AddNamedObjectRoot(cx, &_jsFunc, "JSB_ControlButtonTarget, func");
|
|
|
|
}
|
|
|
|
|
|
|
|
void setEventType(Control::EventType type)
|
|
|
|
{
|
|
|
|
_type = type;
|
2013-07-25 20:52:16 +08:00
|
|
|
}
|
2013-08-09 10:58:25 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
static std::multimap<JSObject*, JSB_ControlButtonTarget*> _jsNativeTargetMap;
|
|
|
|
JSObject* _jsFunc;
|
|
|
|
Control::EventType _type;
|
2013-07-25 20:52:16 +08:00
|
|
|
private:
|
|
|
|
JSObject* _jsTarget;
|
2013-08-05 10:03:02 +08:00
|
|
|
bool _needUnroot;
|
2013-07-25 20:52:16 +08:00
|
|
|
};
|
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
std::multimap<JSObject*, JSB_ControlButtonTarget*> JSB_ControlButtonTarget::_jsNativeTargetMap;
|
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
static JSBool js_cocos2dx_CCControl_addTargetWithActionForControlEvents(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-08-09 10:58:25 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-07-25 20:52:16 +08:00
|
|
|
cocos2d::extension::Control* cobj = (cocos2d::extension::Control *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
|
|
|
JSBool ok = JS_TRUE;
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 3)
|
|
|
|
{
|
2013-08-09 10:58:25 +08:00
|
|
|
JSObject* jsDelegate = JSVAL_TO_OBJECT(argv[0]);
|
|
|
|
JSObject* jsFunc = JSVAL_TO_OBJECT(argv[1]);
|
2013-08-05 10:03:02 +08:00
|
|
|
Control::EventType arg2;
|
2013-07-25 20:52:16 +08:00
|
|
|
ok &= jsval_to_int32(cx, argv[2], (int32_t *)&arg2);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing control event");
|
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
// Check whether the target already exists.
|
|
|
|
auto range = JSB_ControlButtonTarget::_jsNativeTargetMap.equal_range(jsDelegate);
|
|
|
|
for (auto it=range.first; it!=range.second; ++it)
|
|
|
|
{
|
|
|
|
if (it->second->_jsFunc == jsFunc && arg2 == it->second->_type)
|
|
|
|
{
|
|
|
|
// Return true directly.
|
|
|
|
JS_SET_RVAL(cx, vp, JSVAL_VOID);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
// save the delegate
|
|
|
|
JSB_ControlButtonTarget* nativeDelegate = new JSB_ControlButtonTarget();
|
2013-08-09 10:58:25 +08:00
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
nativeDelegate->setJSTarget(jsDelegate);
|
2013-08-09 10:58:25 +08:00
|
|
|
nativeDelegate->setJSAction(jsFunc);
|
|
|
|
nativeDelegate->setEventType(arg2);
|
|
|
|
|
|
|
|
Array* nativeDelegateArray = static_cast<Array*>(cobj->getUserObject());
|
|
|
|
if (nullptr == nativeDelegateArray)
|
|
|
|
{
|
|
|
|
nativeDelegateArray = new Array();
|
|
|
|
cobj->setUserObject(nativeDelegateArray); // The reference of nativeDelegateArray is added to 2
|
|
|
|
nativeDelegateArray->release(); // Release nativeDelegateArray to make the reference to 1
|
|
|
|
}
|
|
|
|
|
|
|
|
nativeDelegateArray->addObject(nativeDelegate); // The reference of nativeDelegate is added to 2
|
|
|
|
nativeDelegate->release(); // Release nativeDelegate to make the reference to 1
|
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
cobj->addTargetWithActionForControlEvents(nativeDelegate, cccontrol_selector(JSB_ControlButtonTarget::onEvent), arg2);
|
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
JSB_ControlButtonTarget::_jsNativeTargetMap.insert(std::make_pair(jsDelegate, nativeDelegate));
|
|
|
|
|
2013-07-25 20:52: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, 3);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool js_cocos2dx_CCControl_removeTargetWithActionForControlEvents(JSContext *cx, uint32_t argc, jsval *vp)
|
|
|
|
{
|
|
|
|
jsval *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2013-08-09 10:58:25 +08:00
|
|
|
js_proxy_t *proxy = jsb_get_js_proxy(obj);
|
2013-07-25 20:52:16 +08:00
|
|
|
cocos2d::extension::Control* cobj = (cocos2d::extension::Control *)(proxy ? proxy->ptr : NULL);
|
|
|
|
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");
|
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
JSBool ok = JS_TRUE;
|
2013-08-09 15:00:06 +08:00
|
|
|
if (argc == 3)
|
|
|
|
{
|
2013-08-09 10:58:25 +08:00
|
|
|
Control::EventType arg2;
|
|
|
|
ok &= jsval_to_int32(cx, argv[2], (int32_t *)&arg2);
|
|
|
|
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing control event");
|
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
obj = JSVAL_TO_OBJECT(argv[0]);
|
2013-08-09 10:58:25 +08:00
|
|
|
JSObject* jsFunc = JSVAL_TO_OBJECT(argv[1]);
|
2013-07-25 20:52:16 +08:00
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
JSB_ControlButtonTarget* nativeTargetToRemoved = nullptr;
|
2013-07-25 20:52:16 +08:00
|
|
|
|
2013-08-09 10:58:25 +08:00
|
|
|
auto range = JSB_ControlButtonTarget::_jsNativeTargetMap.equal_range(obj);
|
|
|
|
for (auto it=range.first; it!=range.second; ++it)
|
|
|
|
{
|
|
|
|
if (it->second->_jsFunc == jsFunc && arg2 == it->second->_type)
|
|
|
|
{
|
|
|
|
nativeTargetToRemoved = it->second;
|
|
|
|
JSB_ControlButtonTarget::_jsNativeTargetMap.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cobj->removeTargetWithActionForControlEvents(nativeTargetToRemoved, cccontrol_selector(JSB_ControlButtonTarget::onEvent), arg2);
|
|
|
|
|
2013-07-25 20:52:16 +08:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2013-03-14 18:14:16 +08:00
|
|
|
|
2013-06-20 14:58:33 +08:00
|
|
|
extern JSObject* jsb_ScrollView_prototype;
|
|
|
|
extern JSObject* jsb_TableView_prototype;
|
|
|
|
extern JSObject* jsb_EditBox_prototype;
|
2013-07-25 20:52:16 +08:00
|
|
|
extern JSObject* jsb_Control_prototype;
|
2013-03-11 16:39:28 +08:00
|
|
|
|
|
|
|
void register_all_cocos2dx_extension_manual(JSContext* cx, JSObject* global)
|
|
|
|
{
|
2013-06-20 14:58:33 +08:00
|
|
|
JS_DefineFunction(cx, jsb_ScrollView_prototype, "setDelegate", js_cocos2dx_CCScrollView_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_TableView_prototype, "setDelegate", js_cocos2dx_CCTableView_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_TableView_prototype, "setDataSource", js_cocos2dx_CCTableView_setDataSource, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_EditBox_prototype, "setDelegate", js_cocos2dx_CCEditBox_setDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
|
2013-07-25 20:52:16 +08:00
|
|
|
JS_DefineFunction(cx, jsb_Control_prototype, "addTargetWithActionForControlEvents", js_cocos2dx_CCControl_addTargetWithActionForControlEvents, 3, JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
JS_DefineFunction(cx, jsb_Control_prototype, "removeTargetWithActionForControlEvents", js_cocos2dx_CCControl_removeTargetWithActionForControlEvents, 3, 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);
|
|
|
|
}
|