Animation ported to JS

This commit is contained in:
Ricardo Quesada 2015-12-03 15:02:39 -08:00
parent cc6e72fe7c
commit 3100cba5a1
2 changed files with 49 additions and 40 deletions

View File

@ -82,7 +82,7 @@ AnimationFrame* AnimationFrame::clone() const
// implementation of Animation
Animation* Animation::create(void)
Animation* Animation::create()
{
Animation *animation = new (std::nothrow) Animation();
animation->init();

View File

@ -391,36 +391,40 @@ bool js_cocos2dx_CCSpawn_create(JSContext *cx, uint32_t argc, jsval *vp)
return false;
}
// TODO: This function is deprecated. The new API is "new Sprite" instead of "Sprite.create"
// TODO: This function is deprecated. The new API is "new MenuItemToggle" instead of "MenuItemToggle.create"
// There are not js tests for this function. Impossible to know weather it works Ok.
bool js_cocos2dx_CCMenuItemToggle_create(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc >= 1) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
cocos2d::MenuItemToggle* ret = new (std::nothrow) cocos2d::MenuItemToggle;
ret->initWithItem(nullptr);
if (ret->initWithItem(nullptr))
{
for (uint32_t i=0; i < argc; i++) {
js_proxy_t *proxy;
JS::RootedObject tmpObj(cx, args.get(i).toObjectOrNull());
proxy = jsb_get_js_proxy(tmpObj);
cocos2d::MenuItem* item = (cocos2d::MenuItem*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, item)
ret->addSubItem(item);
for (uint32_t i=0; i < argc; i++) {
js_proxy_t *proxy;
JS::RootedObject tmpObj(cx, args.get(i).toObjectOrNull());
proxy = jsb_get_js_proxy(tmpObj);
cocos2d::MenuItem* item = (cocos2d::MenuItem*)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, item)
ret->addSubItem(item);
}
ret->setSelectedIndex(0);
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::MenuItemToggle>(ret);
// link the native object with the javascript object
JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::MenuItemToggle"));
args.rval().set(OBJECT_TO_JSVAL(jsobj));
return true;
}
ret->setSelectedIndex(0);
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::MenuItemToggle>(ret);
// link the native object with the javascript object
JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::MenuItemToggle"));
args.rval().set(OBJECT_TO_JSVAL(jsobj));
return true;
}
JS_ReportError(cx, "wrong number of arguments");
return false;
}
// TODO: This function is deprecated. The new API is "new Animation" instead of "Animation.create"
// There are not js tests for this function. Impossible to know weather it works Ok.
bool js_cocos2dx_CCAnimation_create(JSContext *cx, uint32_t argc, jsval *vp)
{
bool ok = true;
@ -428,7 +432,8 @@ bool js_cocos2dx_CCAnimation_create(JSContext *cx, uint32_t argc, jsval *vp)
if (argc <= 3) {
cocos2d::Animation* ret = nullptr;
double arg1 = 0.0f;
if (argc == 2) {
if (argc == 2)
{
Vector<SpriteFrame*> arg0;
if (argc > 0) {
ok &= jsval_to_ccvector(cx, args.get(0), &arg0);
@ -437,8 +442,11 @@ bool js_cocos2dx_CCAnimation_create(JSContext *cx, uint32_t argc, jsval *vp)
JS::RootedValue jsarg1(cx, args.get(1));
ok &= JS::ToNumber(cx, jsarg1, &arg1);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
ret = cocos2d::Animation::createWithSpriteFrames(arg0, arg1);
} else if (argc == 3) {
ret = new (std::nothrow) cocos2d::Animation;
ok &= ret->initWithSpriteFrames(arg0, arg1);
}
else if (argc == 3)
{
Vector<AnimationFrame*> arg0;
if (argc > 0) {
ok &= jsval_to_ccvector(cx, args.get(0), &arg0);
@ -449,32 +457,33 @@ bool js_cocos2dx_CCAnimation_create(JSContext *cx, uint32_t argc, jsval *vp)
ok &= JS::ToNumber(cx, jsarg1, &arg1);
ok &= jsval_to_uint32(cx, args.get(2), &loops);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
ret = cocos2d::Animation::create(arg0, arg1, loops);
} else if (argc == 1) {
ret = new (std::nothrow) cocos2d::Animation;
ok &= ret->initWithAnimationFrames(arg0, arg1, loops);
}
else if (argc == 1)
{
Vector<SpriteFrame*> arg0;
if (argc > 0) {
ok &= jsval_to_ccvector(cx, args.get(0), &arg0);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
}
ret = cocos2d::Animation::createWithSpriteFrames(arg0);
} else if (argc == 0) {
ret = cocos2d::Animation::create();
ret = new (std::nothrow) cocos2d::Animation;
ok &= ret->initWithSpriteFrames(arg0);
}
jsval jsret;
if (ret) {
js_proxy_t *proxy = jsb_get_native_proxy(ret);
if (proxy) {
jsret = OBJECT_TO_JSVAL(proxy->obj);
} else {
// create a new js obj of that class
proxy = js_get_or_create_proxy<cocos2d::Animation>(cx, ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
}
} else {
jsret = JSVAL_NULL;
else if (argc == 0)
{
ret = new (std::nothrow) cocos2d::Animation;
ok = ret->init();
}
if (ok)
{
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::Animation>(ret);
// link the native object with the javascript object
JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, ret, typeClass, "cocos2d::Animation"));
args.rval().set(OBJECT_TO_JSVAL(jsobj));
return true;
}
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments");
return false;