From c473adea7ce0b91d6cc0bbf0052818fc5cd19265 Mon Sep 17 00:00:00 2001 From: zhangbin Date: Thu, 22 Dec 2016 16:38:21 +0800 Subject: [PATCH] Improve the usage of addEvent & removeEvent of CCArmatureDisplay. --- .../cocos2dx/CCArmatureDisplay.cpp | 13 ++- .../dragonbones/cocos2dx/CCArmatureDisplay.h | 14 +++- .../api/jsb_cocos2dx_dragonbones_auto_api.js | 28 +++++++ .../auto/jsb_cocos2dx_dragonbones_auto.cpp | 83 +++++++++++++++++++ .../auto/jsb_cocos2dx_dragonbones_auto.hpp | 3 + .../js-bindings/script/jsb_dragonbones.js | 23 ----- 6 files changed, 137 insertions(+), 27 deletions(-) diff --git a/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.cpp b/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.cpp index 536915b765..8ffea05f46 100644 --- a/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.cpp +++ b/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.cpp @@ -20,7 +20,8 @@ CCArmatureDisplay* CCArmatureDisplay::create() CCArmatureDisplay::CCArmatureDisplay() : _armature(nullptr), - _dispatcher(nullptr) + _dispatcher(nullptr), + _eventCallback(nullptr) { _dispatcher = new cocos2d::EventDispatcher(); this->setEventDispatcher(_dispatcher); @@ -39,7 +40,13 @@ void CCArmatureDisplay::_onClear() void CCArmatureDisplay::_dispatchEvent(EventObject* value) { - _dispatcher->dispatchCustomEvent(value->type, value); + if (_eventCallback) { + _eventCallback(value); + } + + if (_dispatcher->isEnabled()) { + _dispatcher->dispatchCustomEvent(value->type, value); + } } void CCArmatureDisplay::dispose() @@ -216,4 +223,4 @@ cocos2d::PolygonInfo& DBCCSprite::getPolygonInfoModify() return this->_polyInfo; } -DRAGONBONES_NAMESPACE_END \ No newline at end of file +DRAGONBONES_NAMESPACE_END diff --git a/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.h b/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.h index 45f79366c9..7e5100e9a2 100644 --- a/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.h +++ b/native/cocos/editor-support/dragonbones/cocos2dx/CCArmatureDisplay.h @@ -44,7 +44,7 @@ public: inline bool hasEvent(const std::string& type) const override { - return _dispatcher->isEnabled(); + return _eventCallback || _dispatcher->isEnabled(); } inline Armature* getArmature() const override @@ -56,6 +56,18 @@ public: { return _armature->getAnimation(); } + +CC_CONSTRUCTOR_ACCESS: + // methods added for js bindings + void setEventCallback(const std::function& callback) { + this->_eventCallback = callback; + } + inline bool hasEventCallback() { return this->_eventCallback ? true : false; } + inline void clearEventCallback() { this->_eventCallback = nullptr; } + +private: + // added for js bindings + std::function _eventCallback; }; /** @private */ diff --git a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_dragonbones_auto_api.js b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_dragonbones_auto_api.js index 0e3dd93bdc..fe8b9b83a1 100644 --- a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_dragonbones_auto_api.js +++ b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_dragonbones_auto_api.js @@ -1602,6 +1602,34 @@ dispose : function ( { }, +/** + * @method hasEventCallback + * @return {bool} + */ +hasEventCallback : function ( +) +{ + return false; +}, + +/** + * @method setEventCallback + * @param {function} arg0 + */ +setEventCallback : function ( +func +) +{ +}, + +/** + * @method clearEventCallback + */ +clearEventCallback : function ( +) +{ +}, + /** * @method addEvent * @param {String} arg0 diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp index 0d9fd34bb7..fe14ad24c0 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.cpp @@ -6282,6 +6282,86 @@ bool js_cocos2dx_dragonbones_CCArmatureDisplay_dispose(JSContext *cx, uint32_t a JS_ReportError(cx, "js_cocos2dx_dragonbones_CCArmatureDisplay_dispose : wrong number of arguments: %d, was expecting %d", argc, 0); return false; } +bool js_cocos2dx_dragonbones_CCArmatureDisplay_hasEventCallback(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + dragonBones::CCArmatureDisplay* cobj = (dragonBones::CCArmatureDisplay *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_dragonbones_CCArmatureDisplay_hasEventCallback : Invalid Native Object"); + if (argc == 0) { + bool ret = cobj->hasEventCallback(); + JS::RootedValue jsret(cx); + jsret = BOOLEAN_TO_JSVAL(ret); + args.rval().set(jsret); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_dragonbones_CCArmatureDisplay_hasEventCallback : wrong number of arguments: %d, was expecting %d", argc, 0); + return false; +} +bool js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + bool ok = true; + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + dragonBones::CCArmatureDisplay* cobj = (dragonBones::CCArmatureDisplay *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback : Invalid Native Object"); + if (argc == 1) { + std::function arg0; + do { + if(JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION) + { + JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull()); + std::shared_ptr func(new JSFunctionWrapper(cx, jstarget, args.get(0), args.thisv())); + auto lambda = [=](dragonBones::EventObject* larg0) -> void { + JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET + jsval largv[1]; + if (larg0) { + largv[0] = OBJECT_TO_JSVAL(js_get_or_create_jsobject(cx, (dragonBones::EventObject*)larg0)); + } else { + largv[0] = JSVAL_NULL; + }; + JS::RootedValue rval(cx); + bool succeed = func->invoke(1, &largv[0], &rval); + if (!succeed && JS_IsExceptionPending(cx)) { + JS_ReportPendingException(cx); + } + }; + arg0 = lambda; + } + else + { + arg0 = nullptr; + } + } while(0) + ; + JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback : Error processing arguments"); + cobj->setEventCallback(arg0); + args.rval().setUndefined(); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback : wrong number of arguments: %d, was expecting %d", argc, 1); + return false; +} +bool js_cocos2dx_dragonbones_CCArmatureDisplay_clearEventCallback(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + dragonBones::CCArmatureDisplay* cobj = (dragonBones::CCArmatureDisplay *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_dragonbones_CCArmatureDisplay_clearEventCallback : Invalid Native Object"); + if (argc == 0) { + cobj->clearEventCallback(); + args.rval().setUndefined(); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_dragonbones_CCArmatureDisplay_clearEventCallback : wrong number of arguments: %d, was expecting %d", argc, 0); + return false; +} bool js_cocos2dx_dragonbones_CCArmatureDisplay_addEvent(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); @@ -6412,6 +6492,9 @@ void js_register_cocos2dx_dragonbones_CCArmatureDisplay(JSContext *cx, JS::Handl JS_FN("advanceTimeBySelf", js_cocos2dx_dragonbones_CCArmatureDisplay_advanceTimeBySelf, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("removeEventListener", js_cocos2dx_dragonbones_CCArmatureDisplay_removeEvent, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("dispose", js_cocos2dx_dragonbones_CCArmatureDisplay_dispose, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("hasEventCallback", js_cocos2dx_dragonbones_CCArmatureDisplay_hasEventCallback, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("setEventCallback", js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("clearEventCallback", js_cocos2dx_dragonbones_CCArmatureDisplay_clearEventCallback, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("addEventListener", js_cocos2dx_dragonbones_CCArmatureDisplay_addEvent, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("hasEvent", js_cocos2dx_dragonbones_CCArmatureDisplay_hasEvent, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("armature", js_cocos2dx_dragonbones_CCArmatureDisplay_getArmature, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.hpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.hpp index 338f679315..b40a4d1ebc 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.hpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_dragonbones_auto.hpp @@ -324,6 +324,9 @@ void register_all_cocos2dx_dragonbones(JSContext* cx, JS::HandleObject obj); bool js_cocos2dx_dragonbones_CCArmatureDisplay_advanceTimeBySelf(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_dragonbones_CCArmatureDisplay_removeEvent(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_dragonbones_CCArmatureDisplay_dispose(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_dragonbones_CCArmatureDisplay_hasEventCallback(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_dragonbones_CCArmatureDisplay_setEventCallback(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_dragonbones_CCArmatureDisplay_clearEventCallback(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_dragonbones_CCArmatureDisplay_addEvent(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_dragonbones_CCArmatureDisplay_hasEvent(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_dragonbones_CCArmatureDisplay_getArmature(JSContext *cx, uint32_t argc, jsval *vp); diff --git a/native/cocos/scripting/js-bindings/script/jsb_dragonbones.js b/native/cocos/scripting/js-bindings/script/jsb_dragonbones.js index 77ee93b37a..2972c8d096 100644 --- a/native/cocos/scripting/js-bindings/script/jsb_dragonbones.js +++ b/native/cocos/scripting/js-bindings/script/jsb_dragonbones.js @@ -21,19 +21,8 @@ */ var proto = dragonBones.CCArmatureDisplay.prototype; - proto.animation = proto.getAnimation; -proto.addEvent = function(type, listener, target) { - this.addEventListener(type, function(event) { - listener.call(target, { type : event.type, detail: event }); - }); -}; - -proto.removeEvent = function(type) { - this.removeEventListener(type); -}; - var slotProto = dragonBones.Slot.prototype; cc.defineGetterSetter(slotProto, 'childArmature', slotProto.getChildArmature, slotProto.setChildArmature); @@ -42,18 +31,6 @@ cc.defineGetterSetter(armatureProto, 'animation', armatureProto.getAnimation, nu cc.defineGetterSetter(armatureProto, 'display', armatureProto.getDisplay, null); cc.defineGetterSetter(armatureProto, 'name', armatureProto.getName, null); -armatureProto.addEventListener = function (type, listener, target) { - var display = this.display; - jsb.registerNativeRef(this, display); - display.addEvent(type, listener, target); -}; - -armatureProto.removeEventListener = function (type) { - var display = this.display; - jsb.unregisterNativeRef(this, display); - display.removeEvent(type); -}; - var animationStateProto = dragonBones.AnimationState.prototype; cc.defineGetterSetter(animationStateProto, 'name', animationStateProto.getName);