Merge pull request #1265 from pandamicro/v1.3

Forbid adding event listener with Component
This commit is contained in:
Wang Nan
2016-11-08 11:24:14 +08:00
committed by GitHub
5 changed files with 75 additions and 64 deletions
+1
View File
@@ -253,6 +253,7 @@ cc._LogInfos = {
addListener_2: "Invalid parameters.",
addListener_3: "listener must be a cc.EventListener object when adding a fixed priority listener",
addListener_4: "The listener has been registered, please don't register it again.",
addListener_5: "Unsupported listener target.",
_forceAddEventListener: "Invalid scene graph priority!",
_updateListeners: "If program goes here, there should be event in dispatch.",
_updateListeners_2: "_inDispatch should be 1 here."
@@ -341,46 +341,46 @@ _ccsg.Node.RenderCmd.prototype = {
},
_updateDisplayColor: function (parentColor) {
var node = this._node;
var locDispColor = this._displayedColor, locRealColor = node._realColor;
var i, len, selChildren, item;
var node = this._node;
var locDispColor = this._displayedColor, locRealColor = node._realColor;
var i, len, selChildren, item;
this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty);
if (this._cascadeColorEnabledDirty && !node._cascadeColorEnabled) {
locDispColor.r = locRealColor.r;
locDispColor.g = locRealColor.g;
locDispColor.b = locRealColor.b;
var whiteColor = new cc.Color(255, 255, 255, 255);
selChildren = node._children;
for (i = 0, len = selChildren.length; i < len; i++) {
item = selChildren[i];
if (item && item._renderCmd)
item._renderCmd._updateDisplayColor(whiteColor);
}
this._cascadeColorEnabledDirty = false;
} else {
if (parentColor === undefined) {
var locParent = node._parent;
if (locParent && locParent._cascadeColorEnabled)
parentColor = locParent.getDisplayedColor();
else
parentColor = cc.Color.WHITE;
}
locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
if (node._cascadeColorEnabled) {
selChildren = node._children;
for (i = 0, len = selChildren.length; i < len; i++) {
item = selChildren[i];
if (item && item._renderCmd){
item._renderCmd._updateDisplayColor(locDispColor);
item._renderCmd._updateColor();
}
}
}
}
this._dirtyFlag &= ~dirtyFlags.colorDirty;
},
if (this._cascadeColorEnabledDirty && !node._cascadeColorEnabled) {
locDispColor.r = locRealColor.r;
locDispColor.g = locRealColor.g;
locDispColor.b = locRealColor.b;
var whiteColor = new cc.Color(255, 255, 255, 255);
selChildren = node._children;
for (i = 0, len = selChildren.length; i < len; i++) {
item = selChildren[i];
if (item && item._renderCmd)
item._renderCmd._updateDisplayColor(whiteColor);
}
this._cascadeColorEnabledDirty = false;
} else {
if (parentColor === undefined) {
var locParent = node._parent;
if (locParent && locParent._cascadeColorEnabled)
parentColor = locParent.getDisplayedColor();
else
parentColor = cc.Color.WHITE;
}
locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
if (node._cascadeColorEnabled) {
selChildren = node._children;
for (i = 0, len = selChildren.length; i < len; i++) {
item = selChildren[i];
if (item && item._renderCmd){
item._renderCmd._updateDisplayColor(locDispColor);
item._renderCmd._updateColor();
}
}
}
}
this._dirtyFlag &= ~dirtyFlags.colorDirty;
},
_updateDisplayOpacity: function (parentOpacity) {
var node = this._node;
-3
View File
@@ -724,9 +724,6 @@ var Component = cc.Class({
}
this.__eventTargets.length = 0;
// Remove all listeners
cc.eventManager.removeListeners(this);
//
if (CC_EDITOR && !CC_TEST) {
_Scene.AssetsWatcher.stop(this);
+18 -2
View File
@@ -145,6 +145,10 @@ cc.eventManager = {
* @param {Boolean} recursive
*/
pauseTarget: function (node, recursive) {
if (!(node instanceof cc._BaseNode || node instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
var listeners = this._nodeListenersMap[node.__instanceId], i, len;
if (listeners) {
for ( i = 0, len = listeners.length; i < len; i++)
@@ -165,6 +169,10 @@ cc.eventManager = {
* @param {Boolean} recursive
*/
resumeTarget: function (node, recursive) {
if (!(node instanceof cc._BaseNode || node instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
var listeners = this._nodeListenersMap[node.__instanceId], i, len;
if (listeners){
for ( i = 0, len = listeners.length; i < len; i++)
@@ -712,11 +720,15 @@ cc.eventManager = {
*/
addListener: function (listener, nodeOrPriority) {
cc.assert(listener && nodeOrPriority, cc._LogInfos.EventManager.addListener_2);
if(!(listener instanceof cc.EventListener)){
if (!(cc.js.isNumber(nodeOrPriority) || nodeOrPriority instanceof cc._BaseNode || nodeOrPriority instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
if (!(listener instanceof cc.EventListener)) {
cc.assert(!cc.js.isNumber(nodeOrPriority), cc._LogInfos.EventManager.addListener_3);
listener = cc.EventListener.create(listener);
} else {
if(listener._isRegistered()){
if (listener._isRegistered()) {
cc.log(cc._LogInfos.EventManager.addListener_4);
return;
}
@@ -872,6 +884,10 @@ cc.eventManager = {
*/
removeListeners: function (listenerType, recursive) {
var i, _t = this;
if (!(cc.js.isNumber(listenerType) || listenerType instanceof cc._BaseNode || listenerType instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
if (listenerType.__instanceId !== undefined) {
// Ensure the node is removed from these immediately also.
// Don't want any dangling pointers or the possibility of dealing with deleted objects..
+17 -20
View File
@@ -133,19 +133,20 @@ cc.eventManager.addListener = function(listener, nodeOrPriority) {
if (typeof nodeOrPriority === 'number') {
if (nodeOrPriority === 0) {
cc.log('0 priority is forbidden for fixed priority since it\'s used for scene graph based priority.');
cc.log(cc._LogInfos.EventManager.addListener);
return;
}
cc.eventManager.addEventListenerWithFixedPriority(listener, nodeOrPriority);
} else {
var node = nodeOrPriority;
if (nodeOrPriority instanceof cc.Component) {
node = nodeOrPriority.node._sgNode;
}
if (nodeOrPriority instanceof cc.Node) {
if (nodeOrPriority instanceof cc._BaseNode) {
node = nodeOrPriority._sgNode;
}
else if (!(node instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node);
}
@@ -153,28 +154,26 @@ cc.eventManager.addListener = function(listener, nodeOrPriority) {
};
cc.eventManager._removeListeners = cc.eventManager.removeListeners;
cc.eventManager.removeListeners = function (target, recursive) {
if (target instanceof cc.Component) {
target = target.node._sgNode;
}
if (target instanceof cc.Node) {
if (target instanceof cc._BaseNode) {
target = target._sgNode;
}
else if (!(target instanceof _ccsg.Node)) {
return;
if (target instanceof _ccsg.Node || cc.js.isNumber(target)) {
this._removeListeners(target, recursive || false);
}
else {
cc.warn(cc._LogInfos.EventManager.addListener_5);
}
this._removeListeners(target, recursive || false);
};
cc.eventManager._pauseTarget = cc.eventManager.pauseTarget;
cc.eventManager.pauseTarget = function (target, recursive) {
var sgTarget = target;
target._eventPaused = true;
if (target instanceof cc.Component) {
sgTarget = target.node._sgNode;
}
else if (target instanceof cc.Node) {
if (target instanceof cc._BaseNode) {
sgTarget = target._sgNode;
}
else if (!(sgTarget instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
@@ -193,13 +192,11 @@ cc.eventManager.pauseTarget = function (target, recursive) {
cc.eventManager._resumeTarget = cc.eventManager.resumeTarget;
cc.eventManager.resumeTarget = function (target, recursive) {
target._eventPaused = false;
if (target instanceof cc.Component) {
target = target.node._sgNode;
}
if (target instanceof cc.Node) {
if (target instanceof cc._BaseNode) {
target = target._sgNode;
}
else if (!(target instanceof _ccsg.Node)) {
cc.warn(cc._LogInfos.EventManager.addListener_5);
return;
}
this._resumeTarget(target, recursive || false);