Skip to content

Fix spine animation listener binding issues #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frameworks/js-bindings/bindings/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ LOCAL_EXPORT_CFLAGS := -DCOCOS2D_JAVASCRIPT

LOCAL_C_INCLUDES := $(LOCAL_PATH)/manual \
$(LOCAL_PATH)/manual/cocostudio \
$(LOCAL_PATH)/manual/spine \
$(LOCAL_PATH)/auto \
$(LOCAL_PATH)/../cocos2d-x/cocos/2d \
$(LOCAL_PATH)/../cocos2d-x/cocos/base \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,30 +593,6 @@ class JSSkeletonAnimationWrapper: public JSCallbackWrapper
}
};

static bool jsb_cocos2dx_spine_setAnimationListener(JSContext *cx, uint32_t argc, jsval *vp)
{
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);

spine::SkeletonAnimation* node = (spine::SkeletonAnimation *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( node, cx, false, "Invalid Native Object");
if (argc == 2) {
JSSkeletonAnimationWrapper *tmpCobj = new JSSkeletonAnimationWrapper();
jsval *argv = JS_ARGV(cx, vp);

tmpCobj->setJSCallbackFunc(argv[1]);
tmpCobj->setJSCallbackThis(argv[0]);

//node->setAnimationListener(tmpCobj, animationStateEvent_selector(JSSkeletonAnimationWrapper::animationCallbackFunc));

JS_SET_RVAL(cx, vp, JSVAL_VOID);

return true;
}
JS_ReportError(cx, "Invalid number of arguments");
return false;
}

extern JSObject* jsb_spine_SkeletonRenderer_prototype;
extern JSObject* jsb_spine_SkeletonAnimation_prototype;

Expand All @@ -630,5 +606,4 @@ void register_all_cocos2dx_spine_manual(JSContext* cx, JSObject* global)
JS_DefineFunction(cx, jsb_spine_SkeletonAnimation_prototype, "getCurrent", jsb_cocos2dx_spine_getCurrent, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, jsb_spine_SkeletonAnimation_prototype, "setAnimation", jsb_cocos2dx_spine_setAnimation, 3, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, jsb_spine_SkeletonAnimation_prototype, "addAnimation", jsb_cocos2dx_spine_addAnimation, 4, JSPROP_ENUMERATE | JSPROP_PERMANENT);
JS_DefineFunction(cx, jsb_spine_SkeletonAnimation_prototype, "setAnimationListener", jsb_cocos2dx_spine_setAnimationListener, 2, JSPROP_ENUMERATE | JSPROP_PERMANENT);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@
#define __jsb_cocos2dx_spine_manual__


#include "jsapi.h"
#include "jsfriendapi.h"
#include "js/Value.h"
#include "js/TypeDecls.h"
#include "spine/spine-cocos2dx.h"

void register_all_cocos2dx_spine_manual(JSContext* cx, JSObject* global);

extern jsval speventdata_to_jsval(JSContext* cx, spEventData& v);
extern jsval spevent_to_jsval(JSContext* cx, spEvent& v);
extern jsval spbonedata_to_jsval(JSContext* cx, const spBoneData* v);
extern jsval spbone_to_jsval(JSContext* cx, spBone& v);
extern jsval spskeleton_to_jsval(JSContext* cx, spSkeleton& v);
extern jsval spattachment_to_jsval(JSContext* cx, spAttachment& v);
extern jsval spslotdata_to_jsval(JSContext* cx, spSlotData& v);
extern jsval spslot_to_jsval(JSContext* cx, spSlot& v);
extern jsval sptimeline_to_jsval(JSContext* cx, spTimeline& v);
extern jsval spanimationstate_to_jsval(JSContext* cx, spAnimationState& v);
extern jsval spanimation_to_jsval(JSContext* cx, spAnimation& v);
extern jsval sptrackentry_to_jsval(JSContext* cx, spTrackEntry& v);

#endif /* defined(__jsb_cocos2dx_spine_manual__) */
40 changes: 40 additions & 0 deletions frameworks/js-bindings/bindings/script/jsb_spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
* THE SOFTWARE.
*/

sp.ANIMATION_EVENT_TYPE = {
START: 0,
END: 1,
COMPLETE: 2,
EVENT: 3
};

sp.SkeletonAnimation.prototype._ctor = function(skeletonDataFile, atlasFile, scale) {
if(atlasFile) {
if (isNaN(scale)) {
Expand All @@ -28,7 +35,40 @@ sp.SkeletonAnimation.prototype._ctor = function(skeletonDataFile, atlasFile, sca

this.initWithFile(skeletonDataFile, atlasFile, scale);
this.initialize();

this._target = null;
this._callback = null;
}
};

sp.SkeletonAnimation.extend = cc.Class.extend;

// Temporary solution before upgrade the Spine API
sp.SkeletonAnimation.prototype.setAnimationListener = function (target, callback) {
this._target = target;
this._callback = callback;

this.setStartListener(function (trackIndex) {
if (this._target && this._callback) {
this._callback.call(this._target, this, trackIndex, sp.ANIMATION_EVENT_TYPE.START, null, 0);
}
});

this.setEndListener(function (trackIndex) {
if (this._target && this._callback) {
this._callback.call(this._target, this, trackIndex, sp.ANIMATION_EVENT_TYPE.END, null, 0);
}
});

this.setCompleteListener(function (trackIndex, loopCount) {
if (this._target && this._callback) {
this._callback.call(this._target, this, trackIndex, sp.ANIMATION_EVENT_TYPE.COMPLETE, null, loopCount);
}
});

this.setEventListener(function (trackIndex, event) {
if (this._target && this._callback) {
this._callback.call(this._target, this, trackIndex, sp.ANIMATION_EVENT_TYPE.EVENT, event, 0);
}
});
}
6 changes: 3 additions & 3 deletions tools/tojs/cocos2dx_spine.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android
cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android -I%(jsbdir)s/external/spidermonkey/include/android

cocos_flags = -DANDROID

Expand All @@ -18,14 +18,14 @@ cxxgenerator_headers =
# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s

headers = %(cocosdir)s/cocos/editor-support/spine/spine-cocos2dx.h
headers = %(cocosdir)s/cocos/editor-support/spine/spine-cocos2dx.h %(jsbdir)s/bindings/manual/spine/jsb_cocos2dx_spine_manual.h

classes = SkeletonRenderer SkeletonAnimation

classes_need_extend = SkeletonAnimation

skip = SkeletonRenderer::[createWithData findBone findSlot getAttachment],
SkeletonAnimation::[createWithData getCurrent setAnimation addAnimation setAnimationListener]
SkeletonAnimation::[createWithData getCurrent setAnimation addAnimation]

remove_prefix =

Expand Down