mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 17:42:41 +08:00
Improve cc.Node for eventManager support and fix for JSB
This commit is contained in:
+22
-1
@@ -242,6 +242,9 @@ var Node = cc.Class({
|
||||
this._name = typeof name !== 'undefined' ? name : 'New Node';
|
||||
this._activeInHierarchy = false;
|
||||
|
||||
// Support for Scheduler, ActionManager and EventManager
|
||||
this.__instanceId = this._id || cc.ClassManager.getNewInstanceId();
|
||||
|
||||
// cache component
|
||||
this._widget = null;
|
||||
|
||||
@@ -837,7 +840,23 @@ var Node = cc.Class({
|
||||
|
||||
});
|
||||
|
||||
Node.EventType = EventType;
|
||||
// In JSB, when inner sg node being replaced, the system event listeners will be cleared.
|
||||
// We need a mechanisme to guarentee the persistence of system event listeners.
|
||||
if (cc.sys.isNative) {
|
||||
cc.js.getset(Node.prototype, '_sgNode', function () {
|
||||
return this.__sgNode;
|
||||
}, function (value) {
|
||||
this.__sgNode = value;
|
||||
if (this._touchListener) {
|
||||
cc.eventManager.removeListener(this._touchListener);
|
||||
cc.eventManager.addListener(this._touchListener, this);
|
||||
}
|
||||
if (this._mouseListener) {
|
||||
cc.eventManager.removeListener(this._mouseListener);
|
||||
cc.eventManager.addListener(this._mouseListener, this);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @event position-changed
|
||||
@@ -861,4 +880,6 @@ Node.EventType = EventType;
|
||||
* @event opacity-changed
|
||||
*/
|
||||
|
||||
Node.EventType = EventType;
|
||||
|
||||
cc.Node = module.exports = Node;
|
||||
|
||||
@@ -146,18 +146,8 @@ cc.EventListener = cc._Class.extend(/** @lends cc.EventListener# */{
|
||||
* @private
|
||||
*/
|
||||
_setSceneGraphPriority: function (node) {
|
||||
if (cc.Node && node instanceof cc.Node) {
|
||||
this._target = node;
|
||||
this._node = node._sgNode;
|
||||
}
|
||||
else if (cc.Component && node instanceof cc.Component) {
|
||||
this._target = node;
|
||||
this._node = node.node._sgNode;
|
||||
}
|
||||
else {
|
||||
this._target = node;
|
||||
this._node = node;
|
||||
}
|
||||
this._target = node;
|
||||
this._node = node;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
+31
-3
@@ -499,6 +499,12 @@ cc.Label.prototype.setOverflow = function () {};
|
||||
cc.Label.prototype.enableWrapText = function () {};
|
||||
cc.Label.prototype.setLineHeight = function () {};
|
||||
|
||||
// cc.Event#getCurrentTarget
|
||||
cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget;
|
||||
cc.Event.prototype.getCurrentTarget = function () {
|
||||
return this._currentTarget || this._getCurrentTarget();
|
||||
};
|
||||
|
||||
// cc.eventManager.addListener
|
||||
cc.eventManager.addListener = function(listener, nodeOrPriority) {
|
||||
if(!(listener instanceof cc.EventListener)) {
|
||||
@@ -513,13 +519,35 @@ cc.eventManager.addListener = function(listener, nodeOrPriority) {
|
||||
|
||||
cc.eventManager.addEventListenerWithFixedPriority(listener, nodeOrPriority);
|
||||
} else {
|
||||
var node = nodeOrPriority;
|
||||
if (nodeOrPriority instanceof cc.Component) {
|
||||
nodeOrPriority = nodeOrPriority.node._sgNode;
|
||||
node = nodeOrPriority.node._sgNode;
|
||||
}
|
||||
if (nodeOrPriority instanceof cc.Node) {
|
||||
nodeOrPriority = nodeOrPriority._sgNode;
|
||||
node = nodeOrPriority._sgNode;
|
||||
}
|
||||
cc.eventManager.addEventListenerWithSceneGraphPriority(listener, nodeOrPriority);
|
||||
// rebind target
|
||||
if (node !== nodeOrPriority) {
|
||||
var keys = Object.keys(listener);
|
||||
// Overwrite all functions
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
var key = keys[i];
|
||||
var value = listener[key];
|
||||
if (typeof value === 'function') {
|
||||
// var _value = value;
|
||||
listener[key] = function (event1, event2) {
|
||||
// event must be the last argument, and arguments count could be 1 or 2
|
||||
var event = event2 || event1;
|
||||
// Replace event's _currentTarget
|
||||
if (event) {
|
||||
event._currentTarget = nodeOrPriority;
|
||||
}
|
||||
value.call(this, event1, event2);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node);
|
||||
}
|
||||
|
||||
return listener;
|
||||
|
||||
Reference in New Issue
Block a user