Skip to content

Commit

Permalink
legacyCC -> cclegacy, fix Node.isNode compilation error in node-event…
Browse files Browse the repository at this point in the history
…-processor.ts
  • Loading branch information
dumganhar committed Dec 17, 2024
1 parent b0ecd26 commit d0057ba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 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
4 changes: 2 additions & 2 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
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;
11 changes: 9 additions & 2 deletions cocos/scene-graph/node.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 20 additions & 17 deletions cocos/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -974,7 +977,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
if (typeof typeOrClassName === 'string') {
constructor = js.getClassByName(typeOrClassName) as Constructor<T> | undefined;
if (!constructor) {
if (legacyCC._RF.peek()) {
if (cclegacy._RF.peek()) {
errorID(3808, typeOrClassName);
}
throw TypeError(getError(3807, typeOrClassName));
Expand All @@ -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));
}

Expand Down Expand Up @@ -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?.();
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -2711,4 +2714,4 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {

nodePolyfill(Node);

legacyCC.Node = Node;
cclegacy.Node = Node;

0 comments on commit d0057ba

Please sign in to comment.