mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
Improve the usage of addEvent & removeEvent of CCArmatureDisplay.
This commit is contained in:
@@ -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
|
||||
DRAGONBONES_NAMESPACE_END
|
||||
|
||||
@@ -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<void(EventObject*)>& 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<void(EventObject*)> _eventCallback;
|
||||
};
|
||||
|
||||
/** @private */
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<void (dragonBones::EventObject *)> arg0;
|
||||
do {
|
||||
if(JS_TypeOfValue(cx, args.get(0)) == JSTYPE_FUNCTION)
|
||||
{
|
||||
JS::RootedObject jstarget(cx, args.thisv().toObjectOrNull());
|
||||
std::shared_ptr<JSFunctionWrapper> 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<dragonBones::EventObject>(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),
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user