axmol/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manu...

1045 lines
37 KiB
C++
Raw Normal View History

2015-05-05 10:50:19 +08:00
/*
* Created by James Chen on 3/11/13.
* Copyright (c) 2013-2014 Chukong Technologies Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.h"
2015-05-05 10:50:19 +08:00
#include "extensions/cocos-ext.h"
#include "scripting/js-bindings/manual/ScriptingCore.h"
#include "scripting/js-bindings/manual/cocos2d_specifics.hpp"
#include "scripting/js-bindings/auto/jsb_cocos2dx_auto.hpp"
2015-05-05 10:50:19 +08:00
#include <thread>
2016-04-18 15:09:21 +08:00
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "renderer/CCTextureCache.h"
#include "renderer/CCTextureCube.h"
2015-05-05 10:50:19 +08:00
USING_NS_CC;
USING_NS_CC_EXT;
class JSB_ScrollViewDelegate
: public Ref
, public ScrollViewDelegate
{
public:
JSB_ScrollViewDelegate()
{
_JSDelegate = nullptr;
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void scrollViewDidScroll(ScrollView* view) override
{
js_proxy_t * p = jsb_get_native_proxy(view);
if (!p) return;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval arg = OBJECT_TO_JSVAL(p->obj);
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "scrollViewDidScroll", 1, &arg);
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void scrollViewDidZoom(ScrollView* view) override
{
js_proxy_t * p = jsb_get_native_proxy(view);
if (!p) return;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval arg = OBJECT_TO_JSVAL(p->obj);
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "scrollViewDidZoom", 1, &arg);
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
void setJSDelegate(JS::HandleObject pJSDelegate)
2015-05-05 10:50:19 +08:00
{
_JSDelegate = pJSDelegate;
2015-05-05 10:50:19 +08:00
}
private:
JS::Heap<JSObject*> _JSDelegate;
2015-05-05 10:50:19 +08:00
};
static bool js_cocos2dx_CCScrollView_setDelegate(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::ScrollView* cobj = (cocos2d::extension::ScrollView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
if (argc == 1)
{
// save the delegate
JS::RootedObject jsDelegate(cx, args.get(0).toObjectOrNull());
JSB_ScrollViewDelegate* nativeDelegate = new (std::nothrow) JSB_ScrollViewDelegate();
2015-05-05 10:50:19 +08:00
nativeDelegate->setJSDelegate(jsDelegate);
2016-04-18 15:09:21 +08:00
JS_SetProperty(cx, obj, "_delegate", args.get(0));
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
cobj->setUserObject(nativeDelegate);
cobj->setDelegate(nativeDelegate);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
nativeDelegate->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return false;
}
#define KEY_TABLEVIEW_DATA_SOURCE "TableViewDataSource"
#define KEY_TABLEVIEW_DELEGATE "TableViewDelegate"
class JSB_TableViewDelegate
: public Ref
, public TableViewDelegate
{
public:
JSB_TableViewDelegate()
{
_JSDelegate = nullptr;
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void scrollViewDidScroll(ScrollView* view) override
{
callJSDelegate(view, "scrollViewDidScroll");
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void scrollViewDidZoom(ScrollView* view) override
{
callJSDelegate(view, "scrollViewDidZoom");
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void tableCellTouched(TableView* table, TableViewCell* cell) override
{
callJSDelegate(table, cell, "tableCellTouched");
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void tableCellHighlight(TableView* table, TableViewCell* cell) override
{
callJSDelegate(table, cell, "tableCellHighlight");
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell) override
{
callJSDelegate(table, cell, "tableCellUnhighlight");
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell) override
{
callJSDelegate(table, cell, "tableCellWillRecycle");
}
2016-04-18 15:09:21 +08:00
void setJSDelegate(JS::HandleObject pJSDelegate)
2015-05-05 10:50:19 +08:00
{
_JSDelegate = pJSDelegate;
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
private:
void callJSDelegate(ScrollView* view, std::string jsFunctionName)
{
js_proxy_t * p = jsb_get_native_proxy(view);
if (!p) return;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval arg = OBJECT_TO_JSVAL(p->obj);
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 1, &arg);
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
void callJSDelegate(TableView* table, TableViewCell* cell, std::string jsFunctionName)
{
js_proxy_t * p = jsb_get_native_proxy(table);
if (!p) return;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
js_proxy_t * pCellProxy = jsb_get_native_proxy(cell);
if (!pCellProxy) return;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval args[2];
args[0] = OBJECT_TO_JSVAL(p->obj);
args[1] = OBJECT_TO_JSVAL(pCellProxy->obj);
2016-04-18 15:09:21 +08:00
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 2, args);
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
JS::Heap<JSObject*> _JSDelegate;
2015-05-05 10:50:19 +08:00
};
static bool js_cocos2dx_CCTableView_setDelegate(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::TableView* cobj = (cocos2d::extension::TableView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
if (argc == 1)
{
// save the delegate
JS::RootedObject jsDelegate(cx, args.get(0).toObjectOrNull());
JSB_TableViewDelegate* nativeDelegate = new (std::nothrow) JSB_TableViewDelegate();
2015-05-05 10:50:19 +08:00
nativeDelegate->setJSDelegate(jsDelegate);
2016-04-18 15:09:21 +08:00
JS_SetProperty(cx, obj, "_delegate", args.get(0));
2016-04-18 15:09:21 +08:00
auto userDict = static_cast<JSBinding::DictionaryRef*>(cobj->getUserObject());
2015-05-05 10:50:19 +08:00
if (NULL == userDict)
{
userDict = new (std::nothrow) JSBinding::DictionaryRef();
2015-05-05 10:50:19 +08:00
cobj->setUserObject(userDict);
userDict->release();
}
2016-04-18 15:09:21 +08:00
userDict->data.insert(KEY_TABLEVIEW_DELEGATE, nativeDelegate);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
cobj->setDelegate(nativeDelegate);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
nativeDelegate->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return false;
}
class JSB_TableViewDataSource
: public Ref
, public TableViewDataSource
{
public:
JSB_TableViewDataSource()
{
_JSTableViewDataSource = nullptr;
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx) override
{
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
JS::RootedValue ret(cx);
bool ok = callJSDelegate(table, idx, "tableCellSizeForIndex", &ret);
2016-04-18 15:09:21 +08:00
if (!ok)
2015-05-05 10:50:19 +08:00
{
ok = callJSDelegate(table, "cellSizeForTable", &ret);
}
2016-04-18 15:09:21 +08:00
if (ok)
2015-05-05 10:50:19 +08:00
{
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
Size size;
bool isSucceed = jsval_to_ccsize(cx, ret, &size);
if (isSucceed) return size;
}
return Size::ZERO;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx) override
{
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
JS::RootedValue ret(cx);
bool ok = callJSDelegate(table, idx, "tableCellAtIndex", &ret);
if (ok)
{
cocos2d::extension::TableViewCell* arg0;
do {
js_proxy_t *proxy;
JS::RootedObject tmpObj(cx, ret.toObjectOrNull());
2015-05-05 10:50:19 +08:00
proxy = jsb_get_js_proxy(tmpObj);
arg0 = (cocos2d::extension::TableViewCell*)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( arg0, cx, NULL, "Invalid Native Object");
} while (0);
return arg0;
}
return NULL;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual ssize_t numberOfCellsInTableView(TableView *table) override
{
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
JS::RootedValue ret(cx);
bool ok = callJSDelegate(table, "numberOfCellsInTableView", &ret);
if (ok)
{
ssize_t count = 0;
bool isSucceed = jsval_to_ssize(cx, ret, &count);
if (isSucceed) return count;
}
return 0;
}
2016-04-18 15:09:21 +08:00
void setTableViewDataSource(JS::HandleObject pJSSource)
2015-05-05 10:50:19 +08:00
{
_JSTableViewDataSource = pJSSource;
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
private:
bool callJSDelegate(TableView* table, std::string jsFunctionName, JS::MutableHandleValue retVal)
{
js_proxy_t * p = jsb_get_native_proxy(table);
if (!p) return false;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
bool hasAction;
JS::RootedValue temp_retval(cx);
jsval dataVal = OBJECT_TO_JSVAL(p->obj);
2016-04-18 15:09:21 +08:00
JS::RootedObject obj(cx, _JSTableViewDataSource);
2015-05-05 10:50:19 +08:00
JSAutoCompartment ac(cx, obj);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
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;
}
JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
JS::HandleValueArray::fromMarkedLocation(1, &dataVal), retVal);
return true;
}
return false;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
bool callJSDelegate(TableView* table, ssize_t idx, std::string jsFunctionName, JS::MutableHandleValue retVal)
{
js_proxy_t * p = jsb_get_native_proxy(table);
if (!p) return false;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
bool hasAction;
JS::RootedValue temp_retval(cx);
jsval dataVal[2];
dataVal[0] = OBJECT_TO_JSVAL(p->obj);
dataVal[1] = ssize_to_jsval(cx,idx);
2016-04-18 15:09:21 +08:00
JS::RootedObject obj(cx, _JSTableViewDataSource);
2015-05-05 10:50:19 +08:00
JSAutoCompartment ac(cx, obj);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction)
{
if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval))
{
return false;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
if(temp_retval == JSVAL_VOID)
{
return false;
}
bool ret = JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
JS::HandleValueArray::fromMarkedLocation(2, dataVal), retVal);
return ret == true ? true : false;
}
return false;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
private:
JS::Heap<JSObject*> _JSTableViewDataSource;
2015-05-05 10:50:19 +08:00
};
static bool js_cocos2dx_CCTableView_setDataSource(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::TableView* cobj = (cocos2d::extension::TableView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if (argc == 1)
{
JSB_TableViewDataSource* pNativeSource = new (std::nothrow) JSB_TableViewDataSource();
JS::RootedObject jsdata(cx, args.get(0).toObjectOrNull());
pNativeSource->setTableViewDataSource(jsdata);
2016-04-18 15:09:21 +08:00
JS_SetProperty(cx, obj, "_dataSource", args.get(0));
2016-04-18 15:09:21 +08:00
auto userDict = static_cast<JSBinding::DictionaryRef*>(cobj->getUserObject());
2015-05-05 10:50:19 +08:00
if (NULL == userDict)
{
userDict = new (std::nothrow) JSBinding::DictionaryRef();
2015-05-05 10:50:19 +08:00
cobj->setUserObject(userDict);
userDict->release();
}
userDict->data.insert(KEY_TABLEVIEW_DATA_SOURCE, pNativeSource);
2015-05-05 10:50:19 +08:00
cobj->setDataSource(pNativeSource);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
pNativeSource->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
return true;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "wrong number of arguments");
return false;
}
static bool js_cocos2dx_CCTableView_create(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
bool ok = true;
if (argc == 3 || argc == 2)
{
2016-04-18 15:09:21 +08:00
JSB_TableViewDataSource* pNativeSource = new (std::nothrow) JSB_TableViewDataSource();
JS::RootedObject jsdata(cx, args.get(0).toObjectOrNull());
pNativeSource->setTableViewDataSource(jsdata);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
cocos2d::Size arg1;
ok &= jsval_to_ccsize(cx, args.get(1), &arg1);
cocos2d::extension::TableView* ret = NULL;
ret = new (std::nothrow) TableView();
2015-05-05 10:50:19 +08:00
ret->autorelease();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
ret->setDataSource(pNativeSource);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval jsret;
do {
if (ret)
{
JS::RootedObject jsobj(cx, js_get_or_create_jsobject<cocos2d::extension::TableView>(cx, ret));
jsret = OBJECT_TO_JSVAL(jsobj);
2016-04-18 15:09:21 +08:00
JS_SetProperty(cx, jsobj, "_dataSource", args.get(0));
2016-04-18 15:09:21 +08:00
}
2015-05-05 10:50:19 +08:00
else
{
jsret = JSVAL_NULL;
}
} while (0);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
if (argc == 2)
{
ret->initWithViewSize(arg1);
}
else
{
cocos2d::Node* arg2;
2016-04-18 15:09:21 +08:00
do
2015-05-05 10:50:19 +08:00
{
js_proxy_t *proxy;
JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull());
2015-05-05 10:50:19 +08:00
proxy = jsb_get_js_proxy(tmpObj);
arg2 = (cocos2d::Node*)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object");
} while (0);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
ret->initWithViewSize(arg1, arg2);
}
ret->reloadData();
2016-04-18 15:09:21 +08:00
JSBinding::DictionaryRef* userDict = new (std::nothrow) JSBinding::DictionaryRef();
userDict->data.insert(KEY_TABLEVIEW_DATA_SOURCE, pNativeSource);
2015-05-05 10:50:19 +08:00
ret->setUserObject(userDict);
userDict->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
pNativeSource->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().set(jsret);
return true;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "wrong number of arguments");
return false;
}
static bool js_cocos2dx_CCTableView_init(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::TableView* cobj = (cocos2d::extension::TableView *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_extension_TableView_dequeueCell : Invalid Native Object");
bool ok = true;
if (argc == 3 || argc == 2)
{
2016-04-18 15:09:21 +08:00
JSB_TableViewDataSource* pNativeSource = new (std::nothrow) JSB_TableViewDataSource();
JS::RootedObject jsdata(cx, args.get(0).toObjectOrNull());
pNativeSource->setTableViewDataSource(jsdata);
2015-05-05 10:50:19 +08:00
cobj->setDataSource(pNativeSource);
2016-04-18 15:09:21 +08:00
JS_SetProperty(cx, obj, "_dataSource", args.get(0));
2015-05-05 10:50:19 +08:00
cocos2d::Size arg1;
ok &= jsval_to_ccsize(cx, args.get(1), &arg1);
if (argc == 2)
{
cobj->initWithViewSize(arg1);
}
else
{
cocos2d::Node* arg2;
2016-04-18 15:09:21 +08:00
do
2015-05-05 10:50:19 +08:00
{
JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull());
2015-05-05 10:50:19 +08:00
proxy = jsb_get_js_proxy(tmpObj);
arg2 = (cocos2d::Node*)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object");
} while (0);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
cobj->initWithViewSize(arg1, arg2);
}
cobj->reloadData();
2016-04-18 15:09:21 +08:00
JSBinding::DictionaryRef* userDict = new (std::nothrow) JSBinding::DictionaryRef();
userDict->data.insert(KEY_TABLEVIEW_DATA_SOURCE, pNativeSource);
2015-05-05 10:50:19 +08:00
cobj->setUserObject(userDict);
userDict->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
pNativeSource->release();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
return true;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "wrong number of arguments");
return false;
}
class JSB_ControlButtonTarget : public Ref
{
public:
JSB_ControlButtonTarget()
: _callback(nullptr),
_type(Control::EventType::TOUCH_DOWN)
{
_jsFunc = nullptr;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual ~JSB_ControlButtonTarget()
{
CCLOGINFO("In the destruction of JSB_ControlButtonTarget ...");
2016-04-18 15:09:21 +08:00
if (_callback != nullptr)
2015-05-05 10:50:19 +08:00
{
CC_SAFE_DELETE(_callback);
2015-05-05 10:50:19 +08:00
}
for (auto iter = _jsNativeTargetMap.begin(); iter != _jsNativeTargetMap.end(); ++iter)
{
if (this == iter->second)
{
_jsNativeTargetMap.erase(iter);
break;
}
}
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
virtual void onEvent(Ref *controlButton, Control::EventType event)
{
js_proxy_t* p = jsb_get_native_proxy(controlButton);
2015-05-05 10:50:19 +08:00
if (!p)
{
log("Failed to get proxy for control button %p", controlButton);
2015-05-05 10:50:19 +08:00
return;
}
2016-04-18 15:09:21 +08:00
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
jsval dataVal[2];
dataVal[0] = OBJECT_TO_JSVAL(p->obj);
int arg1 = (int)event;
dataVal[1] = INT_TO_JSVAL(arg1);
JS::RootedValue jsRet(cx);
2016-04-18 15:09:21 +08:00
_callback->invoke(2, dataVal, &jsRet);
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
void setJSCallback(JS::HandleValue jsFunc, JS::HandleObject jsTarget)
2015-05-05 10:50:19 +08:00
{
if (_callback != nullptr)
2015-05-05 10:50:19 +08:00
{
CC_SAFE_DELETE(_callback);
2015-05-05 10:50:19 +08:00
}
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
_callback = new (std::nothrow) JSFunctionWrapper(cx, jsTarget, jsFunc);
_jsFunc = jsFunc.toObjectOrNull();
2015-05-05 10:50:19 +08:00
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
void setEventType(Control::EventType type)
{
_type = type;
}
public:
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
static std::multimap<JSObject*, JSB_ControlButtonTarget*> _jsNativeTargetMap;
JSFunctionWrapper *_callback;
2015-05-05 10:50:19 +08:00
Control::EventType _type;
JS::Heap<JSObject*> _jsFunc;
2015-05-05 10:50:19 +08:00
};
std::multimap<JSObject*, JSB_ControlButtonTarget*> JSB_ControlButtonTarget::_jsNativeTargetMap;
static bool js_cocos2dx_CCControl_addTargetWithActionForControlEvents(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::Control* cobj = (cocos2d::extension::Control *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
bool ok = true;
if (argc == 3)
{
JSObject* jsDelegate = args.get(0).toObjectOrNull();
JSObject* jsFunc = args.get(1).toObjectOrNull();
Control::EventType arg2;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
JSB_PRECONDITION2(ok, cx, false, "Error processing control event");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +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.get() == jsFunc && arg2 == it->second->_type)
2015-05-05 10:50:19 +08:00
{
// Return true directly.
args.rval().setUndefined();
return true;
}
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
// save the delegate
JSB_ControlButtonTarget* nativeDelegate = new (std::nothrow) JSB_ControlButtonTarget();
2016-04-18 15:09:21 +08:00
JS::RootedObject jscb(cx, jsDelegate);
nativeDelegate->setJSCallback(args.get(1), jscb);
2015-05-05 10:50:19 +08:00
nativeDelegate->setEventType(arg2);
auto nativeDelegateArray = static_cast<JSBinding::ArrayRef*>(cobj->getUserObject());
2015-05-05 10:50:19 +08:00
if (nullptr == nativeDelegateArray)
{
nativeDelegateArray = new (std::nothrow) JSBinding::ArrayRef();
2015-05-05 10:50:19 +08:00
cobj->setUserObject(nativeDelegateArray); // The reference of nativeDelegateArray is added to 2
nativeDelegateArray->release(); // Release nativeDelegateArray to make the reference to 1
}
2016-04-18 15:09:21 +08:00
nativeDelegateArray->data.pushBack(nativeDelegate); // The reference of nativeDelegate is added to 2
2015-05-05 10:50:19 +08:00
nativeDelegate->release(); // Release nativeDelegate to make the reference to 1
2016-04-18 15:09:21 +08:00
js_add_object_reference(args.thisv(), args.get(1));
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
cobj->addTargetWithActionForControlEvents(nativeDelegate, cccontrol_selector(JSB_ControlButtonTarget::onEvent), arg2);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JSB_ControlButtonTarget::_jsNativeTargetMap.insert(std::make_pair(jsDelegate, nativeDelegate));
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
static bool js_cocos2dx_CCControl_removeTargetWithActionForControlEvents(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::Control* cobj = (cocos2d::extension::Control *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
bool ok = true;
if (argc == 3)
{
Control::EventType arg2;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
JSB_PRECONDITION2(ok, cx, false, "Error processing control event");
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
obj = args.get(0).toObjectOrNull();
JSObject* jsFunc = args.get(1).toObjectOrNull();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JSB_ControlButtonTarget* nativeTargetToRemoved = nullptr;
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
auto range = JSB_ControlButtonTarget::_jsNativeTargetMap.equal_range(obj);
for (auto it = range.first; it != range.second; ++it)
{
if (it->second->_jsFunc.get() == jsFunc && arg2 == it->second->_type)
2015-05-05 10:50:19 +08:00
{
nativeTargetToRemoved = it->second;
JSB_ControlButtonTarget::_jsNativeTargetMap.erase(it);
break;
}
}
2016-04-18 15:09:21 +08:00
js_remove_object_reference(args.thisv(), args.get(1));
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
cobj->removeTargetWithActionForControlEvents(nativeTargetToRemoved, cccontrol_selector(JSB_ControlButtonTarget::onEvent), arg2);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool js_cocos2dx_extension_EventListenerAssetsManagerEx_init(JSContext *cx, uint32_t argc, jsval *vp)
2015-05-05 10:50:19 +08:00
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
2015-05-05 10:50:19 +08:00
bool ok = true;
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::extension::EventListenerAssetsManagerEx* cobj = (cocos2d::extension::EventListenerAssetsManagerEx *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_extension_EventListenerAssetsManagerEx_init : Invalid Native Object");
if (argc == 2) {
const cocos2d::extension::AssetsManagerEx* arg0 = nullptr;
std::function<void (cocos2d::extension::EventAssetsManagerEx *)> arg1;
2015-05-05 10:50:19 +08:00
do {
if (args.get(0).isNull()) { arg0 = nullptr; break; }
if (!args.get(0).isObject()) { ok = false; break; }
js_proxy_t *jsProxy;
JS::RootedObject tmpObj(cx, args.get(0).toObjectOrNull());
jsProxy = jsb_get_js_proxy(tmpObj);
arg0 = (const cocos2d::extension::AssetsManagerEx*)(jsProxy ? jsProxy->ptr : NULL);
JSB_PRECONDITION2( arg0, cx, false, "Invalid Native Object");
} while (0);
do {
if(JS_TypeOfValue(cx, args.get(1)) == JSTYPE_FUNCTION)
{
JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull());
std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, jstarget, args.get(1)));
auto lambda = [=](cocos2d::extension::EventAssetsManagerEx* larg0) -> void {
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
jsval largv[1];
do {
if (larg0) {
js_type_class_t* typeClass = js_get_type_from_native<cocos2d::extension::EventAssetsManagerEx>(larg0);
largv[0] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(cx, larg0, typeClass, "cocos2d::extension::EventAssetsManagerEx"));
} else {
largv[0] = JSVAL_NULL;
}
} while (0);
JS::RootedValue rval(cx);
bool succeed = func->invoke(1, &largv[0], &rval);
if (!succeed && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
removeJSObject(cx, larg0);
};
arg1 = lambda;
2015-05-05 10:50:19 +08:00
}
else
2015-05-05 10:50:19 +08:00
{
arg1 = nullptr;
2015-05-05 10:50:19 +08:00
}
} while(0)
;
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_extension_EventListenerAssetsManagerEx_init : Error processing arguments");
bool ret = cobj->init(arg0, arg1);
jsval jsret = JSVAL_NULL;
jsret = BOOLEAN_TO_JSVAL(ret);
args.rval().set(jsret);
2015-05-05 10:50:19 +08:00
return true;
}
2016-04-18 15:09:21 +08:00
JS_ReportError(cx, "js_cocos2dx_extension_EventListenerAssetsManagerEx_init : wrong number of arguments: %d, was expecting %d", argc, 2);
2015-05-05 10:50:19 +08:00
return false;
}
bool js_cocos2dx_extension_EventListenerAssetsManagerEx_create(JSContext *cx, uint32_t argc, jsval *vp)
2015-05-05 10:50:19 +08:00
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
bool ok = true;
if (argc == 2) {
cocos2d::extension::AssetsManagerEx* arg0 = nullptr;
std::function<void (cocos2d::extension::EventAssetsManagerEx *)> arg1;
2015-05-05 10:50:19 +08:00
do {
if (args.get(0).isNull()) { arg0 = nullptr; break; }
if (!args.get(0).isObject()) { ok = false; break; }
js_proxy_t *jsProxy;
JS::RootedObject tmpObj(cx, args.get(0).toObjectOrNull());
jsProxy = jsb_get_js_proxy(tmpObj);
arg0 = (cocos2d::extension::AssetsManagerEx*)(jsProxy ? jsProxy->ptr : NULL);
JSB_PRECONDITION2( arg0, cx, false, "Invalid Native Object");
2015-05-05 10:50:19 +08:00
} while (0);
do {
if(JS_TypeOfValue(cx, args.get(1)) == JSTYPE_FUNCTION)
{
JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull());
std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, jstarget, args.get(1)));
auto lambda = [=](cocos2d::extension::EventAssetsManagerEx* larg0) -> void {
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
jsval largv[1];
do {
if (larg0) {
js_type_class_t* typeClass = js_get_type_from_native<cocos2d::extension::EventAssetsManagerEx>(larg0);
largv[0] = OBJECT_TO_JSVAL(jsb_get_or_create_weak_jsobject(cx, larg0, typeClass, "cocos2d::extension::EventAssetsManagerEx"));
} else {
largv[0] = JSVAL_NULL;
}
} while (0);
JS::RootedValue rval(cx);
bool succeed = func->invoke(1, &largv[0], &rval);
if (!succeed && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
removeJSObject(cx, larg0);
};
arg1 = lambda;
}
else
{
arg1 = nullptr;
}
} while(0)
;
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_extension_EventListenerAssetsManagerEx_create : Error processing arguments");
cocos2d::extension::EventListenerAssetsManagerEx* ret = cocos2d::extension::EventListenerAssetsManagerEx::create(arg0, arg1);
jsval jsret = JSVAL_NULL;
if (ret) {
JS::RootedObject jsobj(cx, js_get_or_create_jsobject<cocos2d::extension::EventListenerAssetsManagerEx>(cx, ret));
jsret = OBJECT_TO_JSVAL(jsobj);
} else {
jsret = JSVAL_NULL;
}
args.rval().set(jsret);
2015-05-05 10:50:19 +08:00
return true;
}
JS_ReportError(cx, "js_cocos2dx_extension_EventListenerAssetsManagerEx_create : wrong number of arguments");
2015-05-05 10:50:19 +08:00
return false;
}
__JSDownloaderDelegator::__JSDownloaderDelegator(JSContext *cx, JS::HandleObject obj, const std::string &url, JS::HandleObject callback)
2015-05-05 10:50:19 +08:00
: _cx(cx)
, _url(url)
{
_obj = obj;
_jsCallback = callback;
2016-04-18 15:09:21 +08:00
JS::RootedValue target(cx, OBJECT_TO_JSVAL(obj));
if (!target.isNullOrUndefined())
{
js_add_object_root(target);
}
target.set(OBJECT_TO_JSVAL(callback));
if (!target.isNullOrUndefined())
{
js_add_object_root(target);
}
}
__JSDownloaderDelegator::~__JSDownloaderDelegator()
{
JS::RootedValue target(_cx, OBJECT_TO_JSVAL(_obj));
if (!target.isNullOrUndefined())
{
js_remove_object_root(target);
}
target.set(OBJECT_TO_JSVAL(_jsCallback));
if (!target.isNullOrUndefined())
{
js_remove_object_root(target);
}
2016-04-18 15:09:21 +08:00
2015-09-15 18:30:45 +08:00
_downloader->onTaskError = (nullptr);
_downloader->onDataTaskSuccess = (nullptr);
}
__JSDownloaderDelegator *__JSDownloaderDelegator::create(JSContext *cx, JS::HandleObject obj, const std::string &url, JS::HandleObject callback)
{
__JSDownloaderDelegator *delegate = new (std::nothrow) __JSDownloaderDelegator(cx, obj, url, callback);
delegate->autorelease();
return delegate;
}
void __JSDownloaderDelegator::startDownload()
{
2015-09-15 18:30:45 +08:00
if (auto texture = Director::getInstance()->getTextureCache()->getTextureForKey(_url))
2015-05-05 10:50:19 +08:00
{
2015-09-15 18:30:45 +08:00
onSuccess(texture);
2015-05-05 10:50:19 +08:00
}
else
{
Squashed commit of the following: commit 2c810a8e931e07b06498c07db37cbf20a2bcfc92 Merge: 61788a0 082caaf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 13 00:13:38 2015 -0700 Merge branch 'v3' into downloader_decouple commit 61788a0b34188fed5367cab121817db97198bcd8 Merge: 8c1e211 5e21580 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:45:12 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8c1e2119a5c671c00da5ce1fb244029f2bfab09f Merge: 2ea66af 8d73883 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:42:04 2015 -0700 Merge pull request #6 from minggo/downloader_decouple fix js binding error commit 8d7388302ff1d8da575b277e47a3f8ceb1017e98 Author: minggo <ming@cocos2d-x.org> Date: Thu Aug 13 11:09:58 2015 +0800 fix js binding error commit 2ea66af7684f5ec8a20067b1a49347920dbbbb40 Merge: 8cdb0b6 bc964e8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 09:14:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8cdb0b6e4b83210b1f68b4568c74ccb36f504806 Merge: 9272765 08eeca9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 20:11:46 2015 -0700 Merge pull request #5 from minggo/downloader_decouple fix lua-binding error commit 08eeca9d99b5160c48bac1bfecf03b459d05ed15 Author: minggo <ming@cocos2d-x.org> Date: Wed Aug 12 10:34:05 2015 +0800 fix lua-binding error commit 927276589d57432477f8ffc033e19a21a215b246 Merge: 8252464 6f4ed8a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 10:39:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8252464020dbe86da4e5e5aa6e4e45f6997f6b9b Merge: 5dddbb6 138df42 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 06:53:48 2015 -0700 Merge pull request #4 from minggo/downloader_decouple fix linking error in lua-tests on Android commit 138df42a3acfdcfae29eeab1b03ef5dd062f0145 Author: minggo <ming@cocos2d-x.org> Date: Tue Aug 11 16:23:53 2015 +0800 fix link error in lua-tests commit 5dddbb61a1bcda96aa93bfcbe78f42c639eb6247 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 23:22:50 2015 -0700 works for real on win32 commit ddadaf82231b60c8e343718561c0d78657afe7c2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 22:26:59 2015 -0700 compiles on win32 commit 733f1d4c2e4f8382ff19f1793f7f09950774fe78 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:57:49 2015 -0700 compiles on iOS!! commit f706f97962eb393bf7dfec79739ff8dfdec103d5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:27:03 2015 -0700 compiles and runs Ok on windows. Tested all downloadertests + assetmanagertest commit 5e333f4407952d9c2e71e18cb9393d8b6cd170b4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 20:24:28 2015 -0700 updates windows projects commit e77fd8a278594fefc24ff13cbee038508429294d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 19:56:10 2015 -0700 compiles on Android commit 5f515e3519a0688d11476ea6ef235007e8378c96 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:34:33 2015 -0700 trying to fix a compile issue on iOS WIP commit b144ea67dfe7a06e7bfb515adf16f536ba2ead98 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:25:11 2015 -0700 make bug fixes adds public `getHeader` commit a228799ba74eb8642c43d215a802f811a576a4aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 17:42:19 2015 -0700 some linux fixes commit 2cab23d87acc138823ebe9dcec9d42f827690d99 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:58:53 2015 -0700 Squashed commit of the following: commit 86227e9138c1fb523bd28186d355de65cc64c588 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:55:17 2015 -0700 works %100 and uses less memory than before commit a9cafa6f2fe7847e279934d2c162287680760dda Merge: 0bbc62e 0cbd71f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:49:33 2015 -0700 Merge branch 'v3' into cocos_net commit 0bbc62ee1fbfd107a935131d5d93994e87865afa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:46:55 2015 -0700 more tests... and works but there is huge bug. ProgData <vector> needs the position of DownloadUnits <unordered_map> but <unordered_map> does not guaranty the order. So in my next commit I"ll merge DownloadUnit with ProgressData commit 389ed8639a1942f628773616a8e340bc66a0c1d7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 13:47:07 2015 -0700 debugging info commit af0dc45bb9c4705934ced2c1d67e5afc06b6f317 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:47:31 2015 -0700 sync and async tests working ok commit 999fa7a7bfb28565492f47b39caef7f1a079c183 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:38:38 2015 -0700 adding tests commit 91ee294942510718400edc0d00d3c744d00339ee Merge: 76ab703 7f215e9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 10:03:50 2015 -0700 Merge branch 'v3' into cocos_net commit 76ab7032d3a21156f05395a021391a16649f755a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 00:22:20 2015 -0700 Downloader tests commit b65c4dad7089660bfe697c0dab3e9290d1a6d013 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 21:34:54 2015 -0700 compiles on Xcode 7 commit ec0d66340b8daf0b042d36333dff759cd424f465 Merge: 878e4b5 0d77838 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 11:02:24 2015 -0700 Merge branch 'v3' into cocos_net commit 878e4b5d0f87bc6d5d6e507102ab15e2c24a1c8f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 18:07:02 2015 -0700 working, yeah! commit ad1e7637a5ac2e6bff44d35429f5556ea1cb8a4c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 17:49:59 2015 -0700 code is more thread-safe friendly commit 8c6baaee6b898d4b90e5d8988a174d8a33702f60 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 16:39:02 2015 -0700 fixes callbacks commit ddab4d753106c6ff11304c1c69739c5611e3f20a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 15:43:51 2015 -0700 cleaner code runs callbacks in cocos2d thread only if needed commit ef75844aaa010633e6e27ae6e69460174460d5c7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:58:04 2015 -0700 removed weak_ptr<> from ProgressData that logic of the code is added in the callbacks commit d9c7436cbadc611592b3c80ca2a3a9edeaa00853 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:54:07 2015 -0700 checks whether or not the callbacks are in the cocos2d thread commit ae6594a6f866f6b5015ba293db23343887063d2f Merge: 4c61069 6831e24 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 10:48:34 2015 -0700 Merge branch 'v3' into cocos_net commit 4c61069a7e0e33fa0332df700cc8df4807178caf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 21:36:50 2015 -0700 almost there... Downloader has std::vector<FILE*> instead of FileDescriptors code cleaner... commit c34d04a8218d2be5c316e0487e5ac46792bbed76 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 20:38:44 2015 -0700 passes progressData to batch no need to alloc memory for each ProgressData commit f7e8bbd8b85f214b919bce6118d4158dd6d26368 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 17:47:23 2015 -0700 tidier code and more optimizations commit a486dc78aedcf9c6178e490292edd572f8a7b374 Merge: 4178327 a06421b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 14:03:22 2015 -0700 Merge branch 'v3' into cocos_net commit 417832705910a6ebd16a876cd98064ea16f0f45c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 18:13:13 2015 -0700 not finished yet... ...but trying to have a common inteface with batched and non-batched downloads commit 1d0e4652763c2e1511ac102a62defae6c6a89220 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 16:29:39 2015 -0700 ErrorCallback compiles starting the batching code commit 6645a287c47dc01cbf1f2d80e9c0fdd2b16e3e18 Merge: c180425 8d836da Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 10:28:51 2015 -0700 Merge branch 'v3' into cocos_net commit c180425d8181b8e6f53be766d8d2b94f908ae168 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 15:35:53 2015 -0700 More progress on migrating multiple-file downloader to the implementation file commit 5fa273e265963af3c0cb6338bf49dc5e1b54173b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 14:00:02 2015 -0700 Dowloader moved to `cocos2d::network` commit 14b1d0825106701b5435a266b255f09291b6486b Merge: 13cfc8d f744383 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:13:55 2015 -0700 Merge branch 'v3' into cocos_net commit 13cfc8de4eb4835ffb8f8d671d3d070c411b0a2e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:11:45 2015 -0700 DownloadUnits belong to network commit e6e17564ead1fb7f6f7df343b0627a104cd756f8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 12:23:36 2015 -0700 adds `check for option` commit 5cb76dfaaed42011a0c536d01b82a0a94487d6aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 10:37:51 2015 -0700 Downloader: using URLDownload in one case. 3 more to go. Not tested yet commit 9b26e9cfac425635027189ccab835054b68bc83d Merge: 20fe063 253d9c4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 08:35:53 2015 -0700 Merge branch 'v3' into cocos_net commit 20fe0636e36ae7f1da048545b3345f526d5b64de Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 17:36:07 2015 -0700 URLDownload: initial commit HttpAsyncConnection -> HttpAsyncConnection-apple: follows the cocos2d guidelines Adds `CCIRULDownload` interface. Adds `CCURLDownload` implementation based on libcurl. Not working yet. `Downloader` uses `URLDownload` for some of its functions. WIP commit 3a5d3b83838bee53e3bbea182f3ad09d7bec32d0 Merge: e408fe0 642ccac Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 14:00:43 2015 -0700 Merge remote-tracking branch 'cocos2d/v3' into cocos_net commit e408fe0b9303813d20bab2f7bf548f3b7af416a6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 13:59:46 2015 -0700 Adds more files to Xcode project ...even if they are not being compiled. Much easier to add missing functionality to the rest of the platforms commit 11d86acebeb61348e4ccf7e6af9a2eaf1b6bb534 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 11:36:57 2015 -0700 removes unneeded headers
2015-08-13 15:14:10 +08:00
_downloader = std::make_shared<cocos2d::network::Downloader>();
2015-09-15 18:30:45 +08:00
// _downloader->setConnectionTimeout(8);
_downloader->onTaskError = [this](const cocos2d::network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr)
{
this->onError();
};
2016-04-18 15:09:21 +08:00
2015-09-15 18:30:45 +08:00
_downloader->onDataTaskSuccess = [this](const cocos2d::network::DownloadTask& task,
std::vector<unsigned char>& data)
{
Image* img = new (std::nothrow) Image();
2015-09-15 18:30:45 +08:00
Texture2D *tex = nullptr;
do
{
if (false == img->initWithImageData(data.data(), data.size()))
2015-09-15 18:30:45 +08:00
{
break;
}
tex = Director::getInstance()->getTextureCache()->addImage(img, _url);
2015-09-15 18:30:45 +08:00
} while (0);
2016-04-18 15:09:21 +08:00
CC_SAFE_RELEASE(img);
2016-04-18 15:09:21 +08:00
2015-09-15 18:30:45 +08:00
if (tex)
{
this->onSuccess(tex);
}
else
{
this->onError();
}
};
2016-04-18 15:09:21 +08:00
2015-09-15 18:30:45 +08:00
_downloader->createDownloadDataTask(_url);
2015-05-05 10:50:19 +08:00
}
}
void __JSDownloaderDelegator::download()
2015-05-05 10:50:19 +08:00
{
retain();
startDownload();
}
void __JSDownloaderDelegator::downloadAsync()
{
retain();
auto t = std::thread(&__JSDownloaderDelegator::startDownload, this);
t.detach();
2015-05-05 10:50:19 +08:00
}
2015-09-15 18:30:45 +08:00
void __JSDownloaderDelegator::onError()
2015-05-05 10:50:19 +08:00
{
Director::getInstance()->getScheduler()->performFunctionInCocosThread([this]
{
JS::RootedValue callback(_cx, OBJECT_TO_JSVAL(_jsCallback));
if (!callback.isNull()) {
JS::RootedObject global(_cx, ScriptingCore::getInstance()->getGlobalObject());
JSAutoCompartment ac(_cx, global);
2016-04-18 15:09:21 +08:00
jsval succeed = BOOLEAN_TO_JSVAL(false);
JS::RootedValue retval(_cx);
JS_CallFunctionValue(_cx, global, callback, JS::HandleValueArray::fromMarkedLocation(1, &succeed), &retval);
}
release();
});
2015-05-05 10:50:19 +08:00
}
2015-09-15 18:30:45 +08:00
void __JSDownloaderDelegator::onSuccess(Texture2D *tex)
2015-05-05 10:50:19 +08:00
{
2015-09-15 18:30:45 +08:00
CCASSERT(tex, "__JSDownloaderDelegator::onSuccess must make sure tex not null!");
//Director::getInstance()->getScheduler()->performFunctionInCocosThread([this, tex]
2015-05-05 10:50:19 +08:00
{
JS::RootedObject global(_cx, ScriptingCore::getInstance()->getGlobalObject());
JSAutoCompartment ac(_cx, global);
2016-04-18 15:09:21 +08:00
jsval valArr[2];
if (tex)
{
valArr[0] = BOOLEAN_TO_JSVAL(true);
JS::RootedObject jsobj(_cx, js_get_or_create_jsobject<Texture2D>(_cx, tex));
valArr[1] = OBJECT_TO_JSVAL(jsobj);
}
else
{
valArr[0] = BOOLEAN_TO_JSVAL(false);
valArr[1] = JSVAL_NULL;
}
2016-04-18 15:09:21 +08:00
JS::RootedValue callback(_cx, OBJECT_TO_JSVAL(_jsCallback));
if (!callback.isNull())
{
JS::RootedValue retval(_cx);
JS_CallFunctionValue(_cx, global, callback, JS::HandleValueArray::fromMarkedLocation(2, valArr), &retval);
}
release();
2015-09-15 18:30:45 +08:00
}//);
2015-05-05 10:50:19 +08:00
}
// jsb.loadRemoteImg(url, function(succeed, result) {})
bool js_load_remote_image(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
if (argc == 2)
{
2015-05-05 10:50:19 +08:00
std::string url;
bool ok = jsval_to_std_string(cx, args.get(0), &url);
JSB_PRECONDITION2(ok, cx, false, "js_load_remote_image : Error processing arguments");
JS::RootedObject callback(cx, args.get(1).toObjectOrNull());
2016-04-18 15:09:21 +08:00
__JSDownloaderDelegator *delegate = __JSDownloaderDelegator::create(cx, obj, url, callback);
delegate->downloadAsync();
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
args.rval().setUndefined();
return true;
}
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "js_load_remote_image : wrong number of arguments");
return false;
}
extern JSObject* jsb_cocos2d_extension_ScrollView_prototype;
extern JSObject* jsb_cocos2d_extension_TableView_prototype;
extern JSObject* jsb_cocos2d_extension_Control_prototype;
extern JSObject* jsb_cocos2d_extension_AssetsManagerEx_prototype;
extern JSObject* jsb_cocos2d_extension_EventListenerAssetsManagerEx_prototype;
2015-05-05 10:50:19 +08:00
extern JSObject* jsb_cocos2d_extension_Manifest_prototype;
void register_all_cocos2dx_extension_manual(JSContext* cx, JS::HandleObject global)
{
JS::RootedObject ccObj(cx);
JS::RootedObject jsbObj(cx);
2015-05-05 10:50:19 +08:00
JS::RootedValue tmpVal(cx);
JS::RootedObject tmpObj(cx);
get_or_create_js_obj(cx, global, "cc", &ccObj);
get_or_create_js_obj(cx, global, "jsb", &jsbObj);
2016-04-18 15:09:21 +08:00
tmpObj.set(jsb_cocos2d_extension_AssetsManagerEx_prototype);
JS_DefineFunction(cx, tmpObj, "retain", js_cocos2dx_retain, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "release", js_cocos2dx_release, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
tmpObj.set(jsb_cocos2d_extension_Manifest_prototype);
JS_DefineFunction(cx, tmpObj, "retain", js_cocos2dx_retain, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "release", js_cocos2dx_release, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT);
2016-04-18 15:09:21 +08:00
JS_GetProperty(cx, ccObj, "EventListenerAssetsManager", &tmpVal);
tmpObj.set(tmpVal.toObjectOrNull());
JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_extension_EventListenerAssetsManagerEx_create, 2, JSPROP_READONLY | JSPROP_PERMANENT);
tmpObj.set(jsb_cocos2d_extension_EventListenerAssetsManagerEx_prototype);
JS_DefineFunction(cx, tmpObj, "init", js_cocos2dx_extension_EventListenerAssetsManagerEx_init, 2, JSPROP_ENUMERATE | JSPROP_PERMANENT);
2016-04-18 15:09:21 +08:00
tmpObj.set(jsb_cocos2d_extension_ScrollView_prototype);
JS_DefineFunction(cx, tmpObj, "setDelegate", js_cocos2dx_CCScrollView_setDelegate, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
2015-05-05 10:50:19 +08:00
JS::RootedObject tableview(cx, jsb_cocos2d_extension_TableView_prototype);
JS_DefineFunction(cx, tableview, "setDelegate", js_cocos2dx_CCTableView_setDelegate, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, tableview, "setDataSource", js_cocos2dx_CCTableView_setDataSource, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, tableview, "_init", js_cocos2dx_CCTableView_init, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS::RootedObject control(cx, jsb_cocos2d_extension_Control_prototype);
JS_DefineFunction(cx, control, "addTargetWithActionForControlEvents", js_cocos2dx_CCControl_addTargetWithActionForControlEvents, 3, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, control, "removeTargetWithActionForControlEvents", js_cocos2dx_CCControl_removeTargetWithActionForControlEvents, 3, JSPROP_ENUMERATE | JSPROP_PERMANENT);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_GetProperty(cx, ccObj, "TableView", &tmpVal);
tmpObj.set(tmpVal.toObjectOrNull());
2015-05-05 10:50:19 +08:00
JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCTableView_create, 3, JSPROP_READONLY | JSPROP_PERMANENT);
2016-04-18 15:09:21 +08:00
2015-05-05 10:50:19 +08:00
JS_DefineFunction(cx, jsbObj, "loadRemoteImg", js_load_remote_image, 2, JSPROP_READONLY | JSPROP_PERMANENT);
2016-04-18 15:09:21 +08:00
}