Skip to content
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

Modify the calculation of renderOrder in EventManager. #4522

Merged
merged 11 commits into from
Jun 12, 2019
45 changes: 23 additions & 22 deletions cocos2d/core/event-manager/CCEventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ var eventManager = {
_dirtyListeners: {},
_inDispatch: 0,
_isEnabled: false,
_renderOrderMap: {},

_internalCustomListenerIDs:[],

Expand Down Expand Up @@ -226,10 +225,11 @@ var eventManager = {
},

_updateDirtyFlagForSceneGraph: function () {
var locDirtyListeners = this._dirtyListeners
let locDirtyListeners = this._dirtyListeners
for (var selKey in locDirtyListeners) {
this._setDirty(selKey, this.DIRTY_SCENE_GRAPH_PRIORITY);
}

this._dirtyListeners = {};
},

Expand Down Expand Up @@ -281,7 +281,7 @@ var eventManager = {
var dirtyFlag = this.DIRTY_NONE, locFlagMap = this._priorityDirtyFlagMap;
if (locFlagMap[listenerID])
dirtyFlag = locFlagMap[listenerID];

if (dirtyFlag !== this.DIRTY_NONE) {
// Clear the dirty flag first, if `rootNode` is null, then set its dirty flag of scene graph priority
locFlagMap[listenerID] = this.DIRTY_NONE;
Expand All @@ -292,12 +292,12 @@ var eventManager = {
if (dirtyFlag & this.DIRTY_SCENE_GRAPH_PRIORITY){
var rootEntity = cc.director.getScene();
if(rootEntity)
this._sortListenersOfSceneGraphPriority(listenerID, rootEntity);
this._sortListenersOfSceneGraphPriority(listenerID);
}
}
},

_sortListenersOfSceneGraphPriority: function (listenerID, rootNode) {
_sortListenersOfSceneGraphPriority: function (listenerID) {
var listeners = this._getListeners(listenerID);
if (!listeners)
return;
Expand All @@ -311,18 +311,28 @@ var eventManager = {
},

_sortEventListenersOfSceneGraphPriorityDes: function (l1, l2) {
var orders = eventManager._renderOrderMap;
var node1 = l1._getSceneGraphPriority(),
node2 = l2._getSceneGraphPriority(),
order1 = orders[node1._id],
order2 = orders[node2._id];
let node1 = l1._getSceneGraphPriority(),
node2 = l2._getSceneGraphPriority();

if (!l2 || !node2 || !order2)
if (!l2 || !node2 || node2.parent === null)
return -1;
else if (!l1 || !node1 || !order1)
else if (!l1 || !node1 || node1.parent === null)
return 1;

let p1 = node1, p2 = node2, ex = false;
while (p1.parent._id !== p2.parent._id) {
p1 = p1.parent.parent === null ? (ex = true) && node2 : p1.parent;
p2 = p2.parent.parent === null ? (ex = true) && node1 : p2.parent;
}

if (p1._id === p2._id) {
if (p1._id === node2._id)
return -1;
if (p1._id === node1._id)
return 1;
}

return order2 - order1;
return ex ? p1._localZOrder - p2._localZOrder : p2._localZOrder - p1._localZOrder;
},

_sortListenersOfFixedPriority: function (listenerID) {
Expand Down Expand Up @@ -590,7 +600,6 @@ var eventManager = {
cc.js.array.remove(listeners, listener);
if (listeners.length === 0)
delete this._nodeListenersMap[node._id];
delete this._renderOrderMap[node._id];
}
},

Expand Down Expand Up @@ -645,13 +654,6 @@ var eventManager = {
return a - b;
},

_updateRenderOrder: function (node, order) {
let selListeners = this._nodeListenersMap[node._id];
if (selListeners !== undefined) {
this._renderOrderMap[node._id] = order;
}
},

/**
* !#en Query whether the specified event listener id has been added.
* !#zh 查询指定的事件 ID 是否存在
Expand Down Expand Up @@ -871,7 +873,6 @@ var eventManager = {
for (i = 0; i < listenersCopy.length; i++)
_t.removeListener(listenersCopy[i]);
delete _t._nodeListenersMap[listenerType._id];
delete _t._renderOrderMap[listenerType._id];
}

// Bug fix: ensure there are no references to the node in the list of listeners to be added.
Expand Down
3 changes: 2 additions & 1 deletion cocos2d/core/node-activator.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,15 @@ var NodeActivator = cc.Class({
--originCount;
}
}
node._childArrivalOrder = node._children.length;
// activate children recursively
for (let i = 0, len = node._children.length; i < len; ++i) {
let child = node._children[i];
child._localZOrder = (child._localZOrder & 0xffff0000) | (i + 1);
if (child._active) {
this._activateNodeRecursively(child, preloadInvoker, onLoadInvoker, onEnableInvoker);
}
}

node._onPostActivated(true);
},

Expand Down
4 changes: 0 additions & 4 deletions cocos2d/core/renderer/render-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const FINAL = 1 << 9;

let _batcher;
let _cullingMask = 0;
let _renderQueueIndex = 0;
const EventManager = require("../event-manager/CCEventManager");

function RenderFlow () {
this._func = init;
Expand Down Expand Up @@ -92,7 +90,6 @@ _proto._children = function (node) {
let children = node._children;
for (let i = 0, l = children.length; i < l; i++) {
let c = children[i];
EventManager._updateRenderOrder(c, ++_renderQueueIndex);

// Advance the modification of the flag to avoid node attribute modification is invalid when opacity === 0.
c._renderFlag |= worldDirtyFlag;
Expand Down Expand Up @@ -194,7 +191,6 @@ RenderFlow.visit = function (scene) {
_batcher.walking = true;

_cullingMask = 1 << scene.groupIndex;
_renderQueueIndex = 0;

if (scene._renderFlag & WORLD_TRANSFORM) {
_batcher.worldMatDirty ++;
Expand Down