Fix EventManager listener order issue (#5127)

* Remove useless base node apis to reduce properties

* Add BaseNode _siblingIndex to avoid indexOf check

* Fix EventManager listener order issue

* Fix  ci

* Fix node removal issue

* Fix siblingIndex for deserialized node
This commit is contained in:
pandamicro
2019-08-09 16:58:45 +08:00
committed by GitHub
parent 9900d3f846
commit 8367e8b036
10 changed files with 55 additions and 49 deletions
+4
View File
@@ -1122,6 +1122,10 @@ _inDispatch should be 1 here.
event is undefined
### 3512
Event manager only support scene graph priority for ui nodes which contain UIComponent
### 3600
cc.Class will automatically call super constructor of %s, you should not call it manually.
+1 -1
View File
@@ -34,7 +34,7 @@ import { Vec3 } from '../../../core/math';
import { GFXClearFlag } from '../../../gfx/define';
import { Camera } from '../../../renderer';
import { UITransformComponent } from './ui-transfrom-component';
import { Node } from '../../../scene-graph';
import { Node } from '../../../scene-graph/node';
import { RenderTexture } from '../../../assets';
const _worldPos = new Vec3();
-1
View File
@@ -134,7 +134,6 @@ export class UIComponent extends Component {
}
private _sortSiblings () {
const siblings = this.node.parent && this.node.parent.children as Mutable<INode[]>;
if (siblings) {
siblings.sort((a: INode, b: INode) => {
@@ -35,6 +35,16 @@ import { EventListener, TouchOneByOne } from './event-listener';
import { INode } from '../../utils/interfaces';
const ListenerID = EventListener.ListenerID;
function checkUINode (node) {
while (node) {
if (node.getComponent('cc.CanvasComponent')) {
return true;
}
node = node.parent;
}
return false;
}
class _EventListenerVector {
public gt0Index = 0;
private _fixedListeners: EventListener[] = [];
@@ -274,6 +284,10 @@ class EventManager {
listener._setPaused(false);
this._addListener(listener);
} else {
if (!checkUINode(nodeOrPriority)) {
cc.logID(3512);
return;
}
listener._setSceneGraphPriority(nodeOrPriority);
listener._setFixedPriority(0);
listener._setRegistered(true);
@@ -744,10 +758,11 @@ class EventManager {
private _sortEventListenersOfSceneGraphPriorityDes (l1: EventListener, l2: EventListener) {
const node1 = l1._getSceneGraphPriority();
const node2 = l2._getSceneGraphPriority();
if (!l2 || !node2 || !node2._activeInHierarchy || node2.parent === null) {
// Event manager should only care about ui node in the current scene hierarchy
if (!l2 || !node2 || !node2._activeInHierarchy) {
return -1;
}
else if (!l1 || !node1 || !node1._activeInHierarchy || node1.parent === null) {
else if (!l1 || !node1 || !node1._activeInHierarchy) {
return 1;
}
@@ -766,7 +781,10 @@ class EventManager {
}
}
return ex ? p1._localZOrder - p2._localZOrder : p2._localZOrder - p1._localZOrder;
let priority1 = p1.getSiblingIndex();
let priority2 = p2.getSiblingIndex();
return ex ? priority1 - priority2 : priority2 - priority1;
}
private _sortListenersOfFixedPriority (listenerID: string) {
-14
View File
@@ -97,20 +97,6 @@ export interface IBaseNode {
*/
children: Readonly<this[]>;
/**
* @en All children nodes.
* @zh 节点的子节点数量。
* @property childrenCount
* @type {Number}
* @readOnly
* @example
* ```
* var count = node.childrenCount;
* cc.log("Node Children Count: " + count);
* ```
*/
childrenCount: Readonly<number>;
/**
* @en
* The local active state of this node.<br/>
+1 -1
View File
@@ -453,7 +453,7 @@ export class UI {
}
private _walk (node: INode, level = 0) {
const len = node.childrenCount;
const len = node.children.length;
this._preprocess(node);
if (len > 0) {
+13 -29
View File
@@ -173,23 +173,6 @@ export class BaseNode extends CCObject implements IBaseNode {
return this._children;
}
/**
* @en All children nodes.
* @zh 节点的子节点数量。
* @property childrenCount
* @type {Number}
* @readOnly
* @example
* ```
* var count = node.childrenCount;
* cc.log("Node Children Count: " + count);
* ```
*/
@property
get childrenCount () {
return this._children.length;
}
/**
* @en
* The local active state of this node.<br/>
@@ -350,12 +333,6 @@ export class BaseNode extends CCObject implements IBaseNode {
@property
protected _active = true;
/**
* @default 0
*/
@property
protected _level = 0;
/**
* @default []
* @readOnly
@@ -390,6 +367,9 @@ export class BaseNode extends CCObject implements IBaseNode {
*/
protected __eventTargets: any[] = [];
protected _siblingIndex: number = 0;
/**
* @method constructor
* @param {String} [name]
@@ -439,6 +419,8 @@ export class BaseNode extends CCObject implements IBaseNode {
}
this._parent = value;
// Reset sibling index
this._siblingIndex = 0;
this._onSetParent(oldParent, keepWorldTransform);
@@ -446,8 +428,8 @@ export class BaseNode extends CCObject implements IBaseNode {
if (CC_DEBUG && (value._objFlags & Deactivating)) {
cc.errorID(3821);
}
this._level = value._level + 1;
value._children.push(this);
this._siblingIndex = value._children.length - 1;
if (value.emit) {
value.emit(SystemEventType.CHILD_ADDED, this);
}
@@ -609,11 +591,7 @@ export class BaseNode extends CCObject implements IBaseNode {
* ```
*/
public getSiblingIndex () {
if (this._parent) {
return this._parent._children.indexOf(this);
} else {
return 0;
}
return this._siblingIndex;
}
/**
@@ -642,6 +620,7 @@ export class BaseNode extends CCObject implements IBaseNode {
} else {
siblings.push(this);
}
this._siblingIndex = index;
if (this._onSiblingIndexChanged) {
this._onSiblingIndexChanged(index);
}
@@ -1180,6 +1159,9 @@ export class BaseNode extends CCObject implements IBaseNode {
}
protected _onBatchCreated () {
if (this._parent) {
this._siblingIndex = this._parent.children.indexOf(this);
}
return;
}
@@ -1315,8 +1297,10 @@ export class BaseNode extends CCObject implements IBaseNode {
if (!destroyByParent) {
// remove from parent
if (parent) {
// During destroy process, siblingIndex is not relyable
const childIndex = parent._children.indexOf(this);
parent._children.splice(childIndex, 1);
this._siblingIndex = 0;
if (parent.emit) {
parent.emit('child-removed', this);
}
+12
View File
@@ -0,0 +1,12 @@
import { replaceProperty } from '../deprecated';
import { BaseNode } from './base-node';
replaceProperty(BaseNode.prototype, 'BaseNode', [
{
name: 'childrenCount',
custom: function () {
// @ts-ignore
return this.children.length;
}
}
]);
+2
View File
@@ -35,3 +35,5 @@ export { Layers } from './layers';
export { find } from './find';
export { PrivateNode } from './private-node';
export { default as NodeActivator } from './node-activator';
import './deprecated';
+1
View File
@@ -331,6 +331,7 @@ export class Node extends BaseNode implements INode {
}
public _onBatchCreated () {
super._onBatchCreated();
this._dirtyFlags = this._hasChangedFlags = TransformDirtyBit.TRS;
const len = this._children.length;
for (let i = 0; i < len; ++i) {