增加loading界面

This commit is contained in:
zpx committed 2026-11-20 16:29:14 +08:00
1 parent a3f61a29d0
commit e84e14783e
69 files changed
+1394 -31

No files matched your search

@@ -0,0 +1,93 @@
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<Node, boolean>();
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);
}
}
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "e4def002-3baf-403c-a08f-77f1340f98bc",
"files": [],
"subMetas": {},
"userData": {}
}
+30
View File
@@ -0,0 +1,30 @@
export type PreparationPhase = 'idle' | 'loading' | 'finishing' | 'complete';
/** 未知耗时只估算到上限,真实完成才允许满格。 / Estimated waiting progress is capped until actual completion. */
export class StartupProgress {
phase: PreparationPhase = 'idle';
value = 0;
private elapsed = 0;
private finishElapsed = 0;
private finishFrom = 0;
begin(): void { this.reset(); this.phase = 'loading'; this.value = .03; }
reset(): void { this.phase = 'idle'; this.value = 0; this.elapsed = 0; this.finishElapsed = 0; this.finishFrom = 0; }
finish(): void {
if (this.phase !== 'loading') return;
this.phase = 'finishing'; this.finishFrom = this.value;
}
tick(seconds: number): number {
const dt = Number.isFinite(seconds) ? Math.min(.1, Math.max(0, seconds)) : 0;
if (this.phase === 'loading') {
this.elapsed += dt;
const target = .08 + .82 * (1 - Math.exp(-this.elapsed / 8));
this.value = Math.min(.9, this.value + Math.max(0, target - this.value) * (1 - Math.exp(-dt * 7)));
} else if (this.phase === 'finishing') {
this.finishElapsed += dt;
const t = Math.min(1, this.finishElapsed / .24);
this.value = this.finishFrom + (1 - this.finishFrom) * (1 - (1 - t) ** 3);
if (t === 1) { this.value = 1; this.phase = 'complete'; }
}
return this.value;
}
}
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "bd350eaf-d6b7-4516-9d17-78534f4862a6",
"files": [],
"subMetas": {},
"userData": {}
}