import { _decorator, Component, Label, Node, Sprite, SpriteFrame, UITransform } from 'cc'; import { GuestLoginView } from '../../login/scripts/GuestLoginView'; import type { LoginState } from '../../login/scripts/GuestLoginPresenter'; import { StartupProgress } from './StartupProgress'; const { ccclass, property } = _decorator; /** 加载展示与认证解耦;仅显隐控件,不禁用拥有请求生命周期的节点。 / Display controls without disabling the node that owns authentication. */ @ccclass('StartupLoadingView') export class StartupLoadingView extends Component { static readonly READY = 'startup-preparation-ready'; @property({ type: GuestLoginView }) login: GuestLoginView | null = null; @property({ type: Node }) panel: Node | null = null; @property({ type: [Node] }) loginControls: Node[] = []; @property({ type: Sprite }) meter: Sprite | null = null; @property({ type: Label }) percentage: Label | null = null; @property({ type: Label }) caption: Label | null = null; @property({ type: SpriteFrame }) frameArtwork: SpriteFrame | null = null; @property({ type: SpriteFrame }) fillArtwork: SpriteFrame | null = null; private readonly progress = new StartupProgress(); private notified = false; private completedFor = 0; private visible = false; private readonly previousVisibility = new Map(); onLoad(): void { if (!this.login || !this.panel || !this.meter || !this.percentage || !this.caption) { throw new Error('Startup loading controls are not fully bound.'); } this.login.node.on(GuestLoginView.STATE_CHANGED, this.onLoginState, this); this.show(false); } onEnable(): void { this.show(this.progress.phase !== 'idle' && !this.notified); if (this.visible) this.draw(); } private onLoginState(state: LoginState): void { if (state.phase === 'signing-in') { this.notified = false; this.completedFor = 0; this.progress.begin(); this.show(this.enabledInHierarchy); this.draw(); } else if (state.phase === 'authenticated') { this.progress.finish(); this.draw(); } else { this.progress.reset(); this.show(false); } } update(dt: number): void { if (this.progress.phase === 'idle') return; this.progress.tick(dt); this.draw(); if (this.progress.phase === 'complete') this.completedFor += Math.max(0, Math.min(.1, dt)); if (this.progress.phase === 'complete' && this.completedFor >= .12 && !this.notified) { this.notified = true; // 当前无主场景,收尾后返回既有认证成功落点;后续由就绪接口承接。 / Until a game scene exists, retain the existing authenticated landing state. this.show(false); this.node.emit(StartupLoadingView.READY); } } private draw(): void { // 认证状态可更新文字,但加载层收尾前不显示到同一位置。 / Keep the latest auth message hidden until the loading overlay finishes. if (this.visible && this.login?.statusLabel) this.login.statusLabel.node.active = false; if (this.meter) { this.meter.node.active = this.progress.value > 0; this.meter.getComponent(UITransform)?.setContentSize(906 * this.progress.value, 102); } if (this.percentage) this.percentage.string = `${Math.floor(this.progress.value * 100)}%`; if (this.caption) this.caption.string = 'Getting things ready'; } private show(visible: boolean): void { if (this.panel) this.panel.active = visible; if (visible === this.visible) return; this.visible = visible; for (const control of this.loginControls) { if (!control.isValid) continue; if (visible) { this.previousVisibility.set(control, control.active); control.active = false; } else { control.active = control === this.login?.statusLabel?.node ? this.login.statusLabel.string.length > 0 : (this.previousVisibility.get(control) ?? control.active); } } if (!visible) this.previousVisibility.clear(); } onDisable(): void { this.show(false); } onDestroy(): void { if (this.login?.isValid) this.login.node.off(GuestLoginView.STATE_CHANGED, this.onLoginState, this); } }