| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import {
- _decorator,
- BlockInputEvents,
- Button,
- Camera,
- Canvas,
- Component,
- director,
- Director,
- find,
- Label,
- Node,
- ProgressBar,
- UITransform,
- } from 'cc';
- import { AppKernel } from '../../app/AppKernel';
- import { LaunchProgress, LaunchStateMachine } from './LaunchStateMachine';
- import { LaunchDialog } from './states/LaunchState';
- import { UILayer } from '@scripts/framework/ui/UILayer';
- const { ccclass, property } = _decorator;
- @ccclass('LaunchController')
- export class LaunchController extends Component {
- @property(Canvas)
- canvas: Canvas | null = null;
- @property(Label)
- progressLabel: Label | null = null;
- @property(Label)
- progressMsg: Label | null = null;
- @property(ProgressBar)
- progressBar: ProgressBar | null = null;
- @property(Node)
- dialogNode: Node | null = null;
- private launchStateMachine: LaunchStateMachine | null = null;
- protected onLoad(): void {
- director.addPersistRootNode(this.node);
- AppKernel.initialize();
- this.initEvents();
- this.launchStateMachine = new LaunchStateMachine({
- kernel: AppKernel.Instance,
- error: null,
- totalProgress: 0,
- progress: (value) => this.updateProgress(value),
- dialog: (message) => this.showDialog(message),
- });
- }
- private initEvents(): void {
- AppKernel.events.once('launch:completed', this.onLaunchCompleted, this);
- }
- private readonly onLaunchCompleted = (): void => {
- if (!this.node.isValid) return;
- this.node.active = false;
- if (director.isPersistRootNode(this.node)) {
- director.removePersistRootNode(this.node);
- }
- this.node.destroy();
- };
- protected async start(): Promise<void> {
- await this.launchStateMachine?.run();
- }
- protected onDestroy(): void {
- AppKernel.events.offTarget(this);
- }
- private updateProgress(value: LaunchProgress): void {
- if (value.progress !== undefined) {
- const progress = value.progress || 0;
- this.progressBar.progress = progress;
- this.progressLabel.string = `${Math.floor(progress * 100)}%`;
- }
- if (this.progressMsg) this.progressMsg.string = value.title || "";
- }
- private showDialog(dialog: LaunchDialog): Promise<boolean> {
- AppKernel.ui.bindLayer(UILayer.System, this.dialogNode);
- return new Promise<boolean>((resolve, reject) => {
- AppKernel.ui.dialog({
- message: dialog.message,
- lblConfirm: dialog.lblOK,
- callBack: (res) => {
- resolve(res);
- return true
- }
- })
- })
- }
- }
|