LaunchController.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. _decorator,
  3. BlockInputEvents,
  4. Button,
  5. Camera,
  6. Canvas,
  7. Component,
  8. director,
  9. Director,
  10. find,
  11. Label,
  12. Node,
  13. ProgressBar,
  14. UITransform,
  15. } from 'cc';
  16. import { AppKernel } from '../../app/AppKernel';
  17. import { LaunchProgress, LaunchStateMachine } from './LaunchStateMachine';
  18. import { LaunchDialog } from './states/LaunchState';
  19. import { UILayer } from '@scripts/framework/ui/UILayer';
  20. const { ccclass, property } = _decorator;
  21. @ccclass('LaunchController')
  22. export class LaunchController extends Component {
  23. @property(Canvas)
  24. canvas: Canvas | null = null;
  25. @property(Label)
  26. progressLabel: Label | null = null;
  27. @property(Label)
  28. progressMsg: Label | null = null;
  29. @property(ProgressBar)
  30. progressBar: ProgressBar | null = null;
  31. @property(Node)
  32. dialogNode: Node | null = null;
  33. private launchStateMachine: LaunchStateMachine | null = null;
  34. protected onLoad(): void {
  35. director.addPersistRootNode(this.node);
  36. AppKernel.initialize();
  37. this.initEvents();
  38. this.launchStateMachine = new LaunchStateMachine({
  39. kernel: AppKernel.Instance,
  40. error: null,
  41. totalProgress: 0,
  42. progress: (value) => this.updateProgress(value),
  43. dialog: (message) => this.showDialog(message),
  44. });
  45. }
  46. private initEvents(): void {
  47. AppKernel.events.once('launch:completed', this.onLaunchCompleted, this);
  48. }
  49. private readonly onLaunchCompleted = (): void => {
  50. if (!this.node.isValid) return;
  51. this.node.active = false;
  52. if (director.isPersistRootNode(this.node)) {
  53. director.removePersistRootNode(this.node);
  54. }
  55. this.node.destroy();
  56. };
  57. protected async start(): Promise<void> {
  58. await this.launchStateMachine?.run();
  59. }
  60. protected onDestroy(): void {
  61. AppKernel.events.offTarget(this);
  62. }
  63. private updateProgress(value: LaunchProgress): void {
  64. if (value.progress !== undefined) {
  65. const progress = value.progress || 0;
  66. this.progressBar.progress = progress;
  67. this.progressLabel.string = `${Math.floor(progress * 100)}%`;
  68. }
  69. if (this.progressMsg) this.progressMsg.string = value.title || "";
  70. }
  71. private showDialog(dialog: LaunchDialog): Promise<boolean> {
  72. AppKernel.ui.bindLayer(UILayer.System, this.dialogNode);
  73. return new Promise<boolean>((resolve, reject) => {
  74. AppKernel.ui.dialog({
  75. message: dialog.message,
  76. lblConfirm: dialog.lblOK,
  77. callBack: (res) => {
  78. resolve(res);
  79. return true
  80. }
  81. })
  82. })
  83. }
  84. }