import { _decorator, Component } from 'cc'; import { UICloseReason } from './UILayer'; import { AppKernel } from '../../app/AppKernel'; import type { UIViewID } from '../../define/UIDefine'; const { ccclass } = _decorator; @ccclass('UIView') export class UIView extends Component { /** UIManager 用它识别并接管场景中预放置的 UIView。 */ readonly viewId: UIViewID | null = null; public getViewID(): UIViewID { if (!this.viewId) { return (this?.node?.name || "") as UIViewID; } return this.viewId; } /** * 在界面创建完成后、显示前执行;首次打开与缓存界面再次打开时, * this 都是同一个实际的 UIView 实例。 */ async internalOpenBefore(params: TParams): Promise { return (await this.onBeforeOpen(params)) !== false; } internalCreate() { this.onCreate(); } async internalOpen(params: TParams): Promise { this.node.setSiblingIndex(this.node.parent.children.length); await this.onOpen(params); } async internalBeforeClose(reason: UICloseReason): Promise { return this.onBeforeClose(reason); } async internalClose(reason: UICloseReason): Promise { await this.onClose(reason); } internalDispose(): void { this.onDispose(); } protected onDestroy(): void { AppKernel.events.offTarget(this); } protected async closeSelf() { await AppKernel.ui.close(this.getViewID()); } protected onBeforeOpen(_params: TParams): boolean | void | Promise { return true; } protected onCreate(): void | Promise { } protected onOpen(_params: TParams): void | Promise { } protected onFocus(): void { } protected onBlur(): void { } protected onBeforeClose(_reason: UICloseReason): boolean | Promise { return true; } protected onClose(_reason: UICloseReason): void | Promise { } protected onDispose(): void { } }