Skip to content

Commit

Permalink
[v3.8.6] Fix pointerEventProcessorList may not be re-sorted. (#18046)
Browse files Browse the repository at this point in the history
* [v3.8.6] Fix pointerEventProcessorList may not be re-sorted.

This bug was reported at https://forum.cocos.org/t/topic/163982/10

* legacyCC -> cclegacy, fix Node.isNode compilation error in node-event-processor.ts

* Fix cclegacy in node.jsb.ts and fix _onPostActivated.

* Revert

* Update root.jsb.ts
  • Loading branch information
dumganhar authored Dec 17, 2024
1 parent 55edb5f commit 0738856
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 57 deletions.
8 changes: 4 additions & 4 deletions cocos/2d/event/pointer-event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class PointerEventDispatcher implements IEventDispatcher {

constructor () {
input._registerEventDispatcher(this);

NodeEventProcessor.callbacksInvoker.on(DispatcherEventType.ADD_POINTER_EVENT_PROCESSOR, this.addPointerEventProcessor, this);
NodeEventProcessor.callbacksInvoker.on(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this.removePointerEventProcessor, this);
NodeEventProcessor.callbacksInvoker.on(DispatcherEventType.MARK_LIST_DIRTY, this._markListDirty, this);
const callbacksInvoker = NodeEventProcessor.callbacksInvoker;
callbacksInvoker.on(DispatcherEventType.ADD_POINTER_EVENT_PROCESSOR, this.addPointerEventProcessor, this);
callbacksInvoker.on(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this.removePointerEventProcessor, this);
callbacksInvoker.on(DispatcherEventType.MARK_LIST_DIRTY, this._markListDirty, this);
}

onThrowException (): void {
Expand Down
26 changes: 13 additions & 13 deletions cocos/root.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
THE SOFTWARE.
*/

import { legacyCC } from './core/global-exports';
import { cclegacy } from './core/global-exports';
import { DataPoolManager } from './3d/skeletal-animation/data-pool-manager';
import { Device, deviceManager } from './gfx';
import { settings, Settings, warnID, Pool, macro, log, cclegacy } from './core';
import { settings, Settings, warnID, Pool, macro, log } from './core';
import { PipelineEventProcessor } from './rendering/pipeline-event';
import type { Root as JsbRoot } from './root';

Expand Down Expand Up @@ -55,8 +55,8 @@ export interface IRootInfo {
const rootProto: any = Root.prototype;

rootProto._createBatcher2D = function () {
if (!this._batcher && legacyCC.internal.Batcher2D) {
this._batcher = new legacyCC.internal.Batcher2D(this);
if (!this._batcher && cclegacy.internal.Batcher2D) {
this._batcher = new cclegacy.internal.Batcher2D(this);
if (!this._batcher!.initialize()) {
this._batcher = null;
this.destroy();
Expand Down Expand Up @@ -92,7 +92,7 @@ Object.defineProperty(rootProto, 'pipelineEvent', {

rootProto._ctor = function (device: Device) {
this._device = device;
this._dataPoolMgr = legacyCC.internal.DataPoolManager && new legacyCC.internal.DataPoolManager(device) as DataPoolManager;
this._dataPoolMgr = cclegacy.internal.DataPoolManager && new cclegacy.internal.DataPoolManager(device) as DataPoolManager;
this._modelPools = new Map();
this._lightPools = new Map();
this._batcher = null;
Expand Down Expand Up @@ -196,19 +196,19 @@ rootProto.recycleLight = function (l) {
};

rootProto._onDirectorBeforeCommit = function () {
legacyCC.director.emit(legacyCC.Director.EVENT_BEFORE_COMMIT);
cclegacy.director.emit(cclegacy.Director.EVENT_BEFORE_COMMIT);
};

rootProto._onDirectorBeforeRender = function () {
legacyCC.director.emit(legacyCC.Director.EVENT_BEFORE_RENDER);
cclegacy.director.emit(cclegacy.Director.EVENT_BEFORE_RENDER);
};

rootProto._onDirectorAfterRender = function () {
legacyCC.director.emit(legacyCC.Director.EVENT_AFTER_RENDER);
cclegacy.director.emit(cclegacy.Director.EVENT_AFTER_RENDER);
};

rootProto._onDirectorPipelineChanged = function () {
const scene = legacyCC.director.getScene();
const scene = cclegacy.director.getScene();
if (scene) {
scene._activate();
}
Expand All @@ -217,25 +217,25 @@ rootProto._onDirectorPipelineChanged = function () {
const oldOnGlobalPipelineStateChanged = rootProto.onGlobalPipelineStateChanged;
rootProto.onGlobalPipelineStateChanged = function() {
oldOnGlobalPipelineStateChanged.call(this);
const builder = legacyCC.rendering.getCustomPipeline(macro.CUSTOM_PIPELINE_NAME);
const builder = cclegacy.rendering.getCustomPipeline(macro.CUSTOM_PIPELINE_NAME);
if (builder) {
if (typeof builder.onGlobalPipelineStateChanged === 'function') {
builder.onGlobalPipelineStateChanged();
}
legacyCC.rendering.forceResizeAllWindows();
cclegacy.rendering.forceResizeAllWindows();
}
}

const oldFrameMove = rootProto.frameMove;
rootProto.frameMove = function (deltaTime: number) {
oldFrameMove.call(this, deltaTime, legacyCC.director.getTotalFrames());
oldFrameMove.call(this, deltaTime, cclegacy.director.getTotalFrames());
};

const oldSetPipeline = rootProto.setRenderPipeline;
rootProto.setRenderPipeline = function (customPipeline: boolean) {
let ppl;
if (customPipeline) {
legacyCC.rendering.createCustomPipeline();
cclegacy.rendering.createCustomPipeline();
ppl = oldSetPipeline.call(this, null);
log(`Using custom pipeline: ${macro.CUSTOM_PIPELINE_NAME}`);
} else {
Expand Down
24 changes: 13 additions & 11 deletions cocos/scene-graph/node-event-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import { CallbacksInvoker } from '../core/event/callbacks-invoker';
import { Event, EventMouse, EventTouch, Touch } from '../input/types';
import { Vec2 } from '../core/math/vec2';
import { Node } from './node';
import { legacyCC } from '../core/global-exports';
import type { Node } from './node';
import { cclegacy } from '../core/global-exports';
import { Component } from './component';
import { NodeEventType } from './node-event';
import { InputEventType, SystemEventTypeUnion } from '../input/types/event-enum';
Expand Down Expand Up @@ -63,6 +63,8 @@ export enum DispatcherEventType {
MARK_LIST_DIRTY,
}

const globalCallbacksInvoker = new CallbacksInvoker<DispatcherEventType>();

/**
* @en The event processor for Node
* @zh 节点事件类。
Expand All @@ -75,7 +77,7 @@ export class NodeEventProcessor {
/**
* @internal
*/
public static callbacksInvoker = new CallbacksInvoker<DispatcherEventType>();
public static callbacksInvoker = globalCallbacksInvoker;

/**
* Whether the node event is enabled
Expand Down Expand Up @@ -159,7 +161,7 @@ export class NodeEventProcessor {
if (value) {
this._attachMask();
}
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY);
globalCallbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY);
if (recursive && children.length > 0) {
for (let i = 0; i < children.length; ++i) {
const child = children[i];
Expand All @@ -184,7 +186,7 @@ export class NodeEventProcessor {

if (this.capturingTarget) this.capturingTarget.clear();
if (this.bubblingTarget) this.bubblingTarget.clear();
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
globalCallbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
if (this._dispatchingTouch) {
// Dispatch touch cancel event when node is destroyed.
const cancelEvent = new EventTouch([this._dispatchingTouch], true, InputEventType.TOUCH_CANCEL);
Expand Down Expand Up @@ -244,7 +246,7 @@ export class NodeEventProcessor {
this.shouldHandleEventMouse = false;
}
if (!this._hasPointerListeners()) {
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
globalCallbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
}
}

Expand Down Expand Up @@ -363,15 +365,15 @@ export class NodeEventProcessor {
}

public onUpdatingSiblingIndex (): void {
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY);
globalCallbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY);
}

private _searchComponentsInParent<T extends Component> (ctor: Constructor<T> | null): IMask[] | null {
const node = this.node;
if (ctor) {
let index = 0;
let list: IMask[] = [];
for (let curr: Node | null = node; curr && Node.isNode(curr); curr = curr.parent, ++index) {
for (let curr: Node | null = node; curr && cclegacy.Node.isNode(curr); curr = curr.parent, ++index) {
const comp = curr.getComponent(ctor);
if (comp) {
const next = {
Expand Down Expand Up @@ -444,7 +446,7 @@ export class NodeEventProcessor {
this.shouldHandleEventMouse = true;
}
if ((isTouchEvent || isMouseEvent) && !this._hasPointerListeners()) {
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.ADD_POINTER_EVENT_PROCESSOR, this);
globalCallbacksInvoker.emit(DispatcherEventType.ADD_POINTER_EVENT_PROCESSOR, this);
}
}

Expand All @@ -463,7 +465,7 @@ export class NodeEventProcessor {
this.shouldHandleEventMouse = false;
}
if (!this._hasPointerListeners()) {
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
globalCallbacksInvoker.emit(DispatcherEventType.REMOVE_POINTER_EVENT_PROCESSOR, this);
}
});
return callbacksInvoker;
Expand Down Expand Up @@ -692,4 +694,4 @@ export class NodeEventProcessor {
// #endregion handle touch event
}

legacyCC.NodeEventProcessor = NodeEventProcessor;
cclegacy.NodeEventProcessor = NodeEventProcessor;
31 changes: 20 additions & 11 deletions cocos/scene-graph/node.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

import { EDITOR, EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { legacyCC } from '../core/global-exports';
import { cclegacy } from '../core/global-exports';
import { errorID, getError } from '../core/platform/debug';
import { Component } from './component';
import { NodeEventType } from './node-event';
Expand All @@ -38,14 +38,16 @@ import { nodePolyfill } from './node-dev';
import * as js from '../core/utils/js';
import { patch_cc_Node } from '../native-binding/decorators';
import type { Node as JsbNode } from './node';
import { DispatcherEventType, NodeEventProcessor } from './node-event-processor';

const reserveContentsForAllSyncablePrefabTag = Symbol('ReserveContentsForAllSyncablePrefab');

declare const jsb: any;
declare const EditorExtends: any;

export const Node: typeof JsbNode = jsb.Node;
export type Node = JsbNode;
legacyCC.Node = Node;
cclegacy.Node = Node;

const NodeCls: any = Node;

Expand Down Expand Up @@ -81,6 +83,7 @@ const TRANSFORMBIT_TRS = TransformBit.TRS;

const nodeProto: any = jsb.Node.prototype;
export const TRANSFORM_ON = 1 << 0;
const ACTIVE_ON = 1 << 1;
const Destroying = CCObject.Flags.Destroying;

// TODO: `_setTempFloatArray` is only implemented on Native platforms. @dumganhar
Expand Down Expand Up @@ -162,7 +165,7 @@ nodeProto.addComponent = function (typeOrClassName) {
if (typeof typeOrClassName === 'string') {
constructor = getClassByName(typeOrClassName);
if (!constructor) {
if (legacyCC._RF.peek()) {
if (cclegacy._RF.peek()) {
errorID(3808, typeOrClassName);
}
throw TypeError(getError(3807, typeOrClassName));
Expand Down Expand Up @@ -221,7 +224,7 @@ nodeProto.addComponent = function (typeOrClassName) {
}
this.emit(NodeEventType.COMPONENT_ADDED, component);
if (this._activeInHierarchy) {
legacyCC.director._nodeActivator.activateComp(component);
cclegacy.director._nodeActivator.activateComp(component);
}
if (EDITOR_NOT_IN_PREVIEW) {
component.resetInEditor?.();
Expand Down Expand Up @@ -419,7 +422,7 @@ nodeProto._onEditorAttached = function (attached: boolean) {
};

nodeProto._onRemovePersistRootNode = function () {
legacyCC.game.removePersistRootNode(this);
cclegacy.game.removePersistRootNode(this);
};

nodeProto._onDestroyComponents = function () {
Expand Down Expand Up @@ -496,11 +499,17 @@ nodeProto._onSiblingOrderChanged = function () {
};

nodeProto._onActivateNode = function (shouldActiveNow) {
legacyCC.director._nodeActivator.activateNode(this, shouldActiveNow);
cclegacy.director._nodeActivator.activateNode(this, shouldActiveNow);
};

nodeProto._onPostActivated = function (active: boolean) {
this._eventProcessor.setEnabled(active);
const eventProcessor = this._eventProcessor;
if (eventProcessor.isEnabled === active) {
NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY);
}

eventProcessor.setEnabled(active);

if (active) {
// in case transform updated during deactivated period
this.invalidateChildren(TransformBit.TRS);
Expand Down Expand Up @@ -598,7 +607,7 @@ NodeCls._findChildComponents = function (children, constructor, components) {
// @ts-ignore
NodeCls.isNode = function (obj: unknown): obj is jsb.Node {
// @ts-ignore
return obj instanceof jsb.Node && (obj.constructor === jsb.Node || !(obj instanceof legacyCC.Scene));
return obj instanceof jsb.Node && (obj.constructor === jsb.Node || !(obj instanceof cclegacy.Scene));
};

let _tempQuat = new Quat();
Expand Down Expand Up @@ -1259,7 +1268,7 @@ nodeProto[serializeTag] = function (serializationOutput: SerializationOutput, co
};

nodeProto._onActiveNode = function (shouldActiveNow: boolean) {
legacyCC.director._nodeActivator.activateNode(this, shouldActiveNow);
cclegacy.director._nodeActivator.activateNode(this, shouldActiveNow);
};

nodeProto._onBatchCreated = function (dontSyncChildPrefab: boolean) {
Expand Down Expand Up @@ -1323,7 +1332,7 @@ nodeProto._onLocalPositionRotationScaleUpdated = function (px, py, pz, rx, ry, r

nodeProto._instantiate = function (cloned: Node, isSyncedNode: boolean) {
if (!cloned) {
cloned = legacyCC.instantiate._clone(this, this);
cloned = cclegacy.instantiate._clone(this, this);
}

// TODO(PP_Pro): after we support editorOnly tag, we could remove this any type assertion.
Expand Down Expand Up @@ -1372,7 +1381,7 @@ nodeProto._ctor = function (name?: string) {
this.__editorExtras__ = { editorOnly: true };

this._components = [];
this._eventProcessor = new legacyCC.NodeEventProcessor(this);
this._eventProcessor = new NodeEventProcessor(this);
this._uiProps = new NodeUIProperties(this);

const sharedArrayBuffer = this._initAndReturnSharedBuffer();
Expand Down
Loading

0 comments on commit 0738856

Please sign in to comment.