From d0057ba96b463bc273768419655a221183023e2d Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 17 Dec 2024 12:56:49 +0800 Subject: [PATCH] legacyCC -> cclegacy, fix Node.isNode compilation error in node-event-processor.ts --- cocos/2d/event/pointer-event-dispatcher.ts | 8 ++--- cocos/root.jsb.ts | 4 +-- cocos/scene-graph/node-event-processor.ts | 24 +++++++------- cocos/scene-graph/node.jsb.ts | 11 +++++-- cocos/scene-graph/node.ts | 37 ++++++++++++---------- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/cocos/2d/event/pointer-event-dispatcher.ts b/cocos/2d/event/pointer-event-dispatcher.ts index ec4e08f3354..b113538cf38 100644 --- a/cocos/2d/event/pointer-event-dispatcher.ts +++ b/cocos/2d/event/pointer-event-dispatcher.ts @@ -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 { diff --git a/cocos/root.jsb.ts b/cocos/root.jsb.ts index 95f388bb2cd..73195d253a7 100644 --- a/cocos/root.jsb.ts +++ b/cocos/root.jsb.ts @@ -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'; diff --git a/cocos/scene-graph/node-event-processor.ts b/cocos/scene-graph/node-event-processor.ts index 5dae5eb9ec3..e4b7bb464d6 100644 --- a/cocos/scene-graph/node-event-processor.ts +++ b/cocos/scene-graph/node-event-processor.ts @@ -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'; @@ -63,6 +63,8 @@ export enum DispatcherEventType { MARK_LIST_DIRTY, } +const globalCallbacksInvoker = new CallbacksInvoker(); + /** * @en The event processor for Node * @zh 节点事件类。 @@ -75,7 +77,7 @@ export class NodeEventProcessor { /** * @internal */ - public static callbacksInvoker = new CallbacksInvoker(); + public static callbacksInvoker = globalCallbacksInvoker; /** * Whether the node event is enabled @@ -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]; @@ -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); @@ -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); } } @@ -363,7 +365,7 @@ export class NodeEventProcessor { } public onUpdatingSiblingIndex (): void { - NodeEventProcessor.callbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY); + globalCallbacksInvoker.emit(DispatcherEventType.MARK_LIST_DIRTY); } private _searchComponentsInParent (ctor: Constructor | null): IMask[] | null { @@ -371,7 +373,7 @@ export class NodeEventProcessor { 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 = { @@ -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); } } @@ -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; @@ -692,4 +694,4 @@ export class NodeEventProcessor { // #endregion handle touch event } -legacyCC.NodeEventProcessor = NodeEventProcessor; +cclegacy.NodeEventProcessor = NodeEventProcessor; diff --git a/cocos/scene-graph/node.jsb.ts b/cocos/scene-graph/node.jsb.ts index 4b99c37f42b..a494d3cb9ab 100644 --- a/cocos/scene-graph/node.jsb.ts +++ b/cocos/scene-graph/node.jsb.ts @@ -38,6 +38,7 @@ 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'); @@ -500,7 +501,13 @@ nodeProto._onActivateNode = function (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); @@ -1372,7 +1379,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(); diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index c8477d68380..6865fba97dc 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -26,7 +26,7 @@ import { ccclass, editable, serializable, type } from 'cc.decorator'; import { DEV, DEBUG, EDITOR, EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; import { Layers } from './layers'; import { NodeUIProperties } from './node-ui-properties'; -import { legacyCC } from '../core/global-exports'; +import { cclegacy } from '../core/global-exports'; import { nodePolyfill } from './node-dev'; import { ISchedulable } from '../core/scheduler'; import { approx, EPSILON, Mat3, mat4, Mat4, quat, Quat, v3, Vec3 } from '../core/math'; @@ -36,11 +36,14 @@ import { errorID, warnID, error, log, getError } from '../core/platform/debug'; import { Component } from './component'; import { property } from '../core/data/decorators/property'; import { CCObject, js } from '../core'; -import type { Scene } from './scene'; import { PrefabInfo, PrefabInstance } from './prefab/prefab-info'; import { NodeEventType } from './node-event'; import { Event } from '../input/types'; -import type { NodeEventProcessor } from './node-event-processor'; +import { DispatcherEventType, NodeEventProcessor } from './node-event-processor'; + +import type { Scene } from './scene'; +import type { Director } from '../game/director'; +import type { Game } from '../game/game'; const Destroying = CCObject.Flags.Destroying; const DontDestroy = CCObject.Flags.DontDestroy; @@ -191,7 +194,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { if (parent) { const couldActiveInScene = parent._activeInHierarchy; if (couldActiveInScene) { - legacyCC.director._nodeActivator.activateNode(this, isActive); + (cclegacy.director as Director)._nodeActivator.activateNode(this, isActive); } } } @@ -382,7 +385,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { public set id (v: string) { this._id = v; } protected _id: string = idGenerator.getNewId(); - protected _eventProcessor: NodeEventProcessor = new (legacyCC.NodeEventProcessor as typeof NodeEventProcessor)(this); + protected _eventProcessor: NodeEventProcessor = new NodeEventProcessor(this); protected _eventMask = 0; protected _siblingIndex = 0; @@ -974,7 +977,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { if (typeof typeOrClassName === 'string') { constructor = js.getClassByName(typeOrClassName) as Constructor | undefined; if (!constructor) { - if (legacyCC._RF.peek()) { + if (cclegacy._RF.peek()) { errorID(3808, typeOrClassName); } throw TypeError(getError(3807, typeOrClassName)); @@ -991,7 +994,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { if (typeof constructor !== 'function') { throw TypeError(getError(3809)); } - if (!js.isChildClassOf(constructor, legacyCC.Component)) { + if (!js.isChildClassOf(constructor, cclegacy.Component)) { throw TypeError(getError(3810)); } @@ -1038,7 +1041,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { } this.emit(NodeEventType.COMPONENT_ADDED, component); if (this._activeInHierarchy) { - legacyCC.director._nodeActivator.activateComp(component); + (cclegacy.director as Director)._nodeActivator.activateComp(component); } if (EDITOR_NOT_IN_PREVIEW) { component.resetInEditor?.(); @@ -1329,7 +1332,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { protected _instantiate (cloned?: Node | null, isSyncedNode: boolean = false): Node { if (!cloned) { - cloned = legacyCC.instantiate._clone(this, this) as Node; + cloned = cclegacy.instantiate._clone(this, this) as Node; } const newPrefabInfo = cloned._prefab; @@ -1353,15 +1356,15 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { protected _onHierarchyChangedBase (oldParent: this | null): void { const newParent = this._parent; - if (this._persistNode && !(newParent instanceof legacyCC.Scene)) { - legacyCC.game.removePersistRootNode(this); + if (this._persistNode && !(newParent instanceof cclegacy.Scene)) { + cclegacy.game.removePersistRootNode(this); if (EDITOR) { warnID(1623); } } if (EDITOR) { - const scene = legacyCC.director.getScene() as this | null; + const scene = (cclegacy.director as Director).getScene() as this | null; const inCurrentSceneBefore = oldParent && oldParent.isChildOf(scene); const inCurrentSceneNow = newParent && newParent.isChildOf(scene); if (!inCurrentSceneBefore && inCurrentSceneNow) { @@ -1382,7 +1385,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { const shouldActiveNow = this._active && !!(newParent && newParent._activeInHierarchy); if (this._activeInHierarchy !== shouldActiveNow) { - legacyCC.director._nodeActivator.activateNode(this, shouldActiveNow); + (cclegacy.director as Director)._nodeActivator.activateNode(this, shouldActiveNow); } } @@ -1401,7 +1404,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { // remove from persist if (this._persistNode) { - legacyCC.game.removePersistRootNode(this); + (cclegacy.game as Game).removePersistRootNode(this); } if (!destroyByParent) { @@ -1560,7 +1563,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { * @zh 指定对象是否是普通的节点?如果传入 [[Scene]] 会返回 false。 */ public static isNode (obj: unknown): obj is Node { - return obj instanceof Node && (obj.constructor === Node || !(obj instanceof legacyCC.Scene)); + return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cclegacy.Scene)); } protected _onPreDestroy (): boolean { @@ -2700,7 +2703,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { public getPathInHierarchy (): string { let result = this.name; let curNode: Node | null = this.parent; - while (curNode && !(curNode instanceof legacyCC.Scene)) { + while (curNode && !(curNode instanceof cclegacy.Scene)) { result = `${curNode.name}/${result}`; curNode = curNode.parent; } @@ -2711,4 +2714,4 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { nodePolyfill(Node); -legacyCC.Node = Node; +cclegacy.Node = Node;