From 8367e8b0366a25cb4468fc6c51cbdb696319713e Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 9 Aug 2019 16:58:45 +0800 Subject: [PATCH] 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 --- EngineErrorMap.md | 4 ++ cocos/3d/ui/components/canvas-component.ts | 2 +- cocos/3d/ui/components/ui-component.ts | 1 - .../platform/event-manager/event-manager.ts | 24 +++++++++-- cocos/core/utils/interfaces.ts | 14 ------- cocos/renderer/ui/ui.ts | 2 +- cocos/scene-graph/base-node.ts | 42 ++++++------------- cocos/scene-graph/deprecated.ts | 12 ++++++ cocos/scene-graph/index.ts | 2 + cocos/scene-graph/node.ts | 1 + 10 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 cocos/scene-graph/deprecated.ts diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 1fc829030e..ad533b05e7 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -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. diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index d88f2a141d..8c7c7588e2 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -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(); diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts index f4dd876811..3c2a151ae5 100644 --- a/cocos/3d/ui/components/ui-component.ts +++ b/cocos/3d/ui/components/ui-component.ts @@ -134,7 +134,6 @@ export class UIComponent extends Component { } private _sortSiblings () { - const siblings = this.node.parent && this.node.parent.children as Mutable; if (siblings) { siblings.sort((a: INode, b: INode) => { diff --git a/cocos/core/platform/event-manager/event-manager.ts b/cocos/core/platform/event-manager/event-manager.ts index aa2164aac7..29cb2c36c8 100644 --- a/cocos/core/platform/event-manager/event-manager.ts +++ b/cocos/core/platform/event-manager/event-manager.ts @@ -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) { diff --git a/cocos/core/utils/interfaces.ts b/cocos/core/utils/interfaces.ts index 4d51f2fe5c..3714367799 100644 --- a/cocos/core/utils/interfaces.ts +++ b/cocos/core/utils/interfaces.ts @@ -97,20 +97,6 @@ export interface IBaseNode { */ children: Readonly; - /** - * @en All children nodes. - * @zh 节点的子节点数量。 - * @property childrenCount - * @type {Number} - * @readOnly - * @example - * ``` - * var count = node.childrenCount; - * cc.log("Node Children Count: " + count); - * ``` - */ - childrenCount: Readonly; - /** * @en * The local active state of this node.
diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 5c52abdf87..241c56b6ef 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -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) { diff --git a/cocos/scene-graph/base-node.ts b/cocos/scene-graph/base-node.ts index 46b2d76cf9..b339c91739 100644 --- a/cocos/scene-graph/base-node.ts +++ b/cocos/scene-graph/base-node.ts @@ -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.
@@ -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); } diff --git a/cocos/scene-graph/deprecated.ts b/cocos/scene-graph/deprecated.ts new file mode 100644 index 0000000000..f6257817de --- /dev/null +++ b/cocos/scene-graph/deprecated.ts @@ -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; + } + } +]); \ No newline at end of file diff --git a/cocos/scene-graph/index.ts b/cocos/scene-graph/index.ts index bafabae178..08e1be2929 100644 --- a/cocos/scene-graph/index.ts +++ b/cocos/scene-graph/index.ts @@ -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'; \ No newline at end of file diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index c25a20e90b..a7e20c8fa8 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -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) {