Facade.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { ViewManager } from "./manager/ViewManager";
  2. import BaseMediator from "./base/BaseMediator";
  3. import BaseScene from "./base/BaseScene";
  4. import { BaseView } from "./base/BaseView";
  5. import { OPEN_VIEW_OPTION } from "./Constants";
  6. import BaseCommand from "./base/BaseCommand";
  7. import CommandManager from "./manager/CommandManager";
  8. import ModelManager from "./manager/ModelManager";
  9. import BaseModel from "./base/BaseModel";
  10. import NotificationManager from "./manager/NotificationManager";
  11. import FrameworkCfg from "./FrameworkCfg";
  12. export class Facade {
  13. /** 实例对象 */
  14. private static _instance: Facade = new Facade();
  15. /** 框架是否被初始化 */
  16. private static _isInit: boolean = false;
  17. private constructor() {
  18. }
  19. public static getInstance(): Facade {
  20. return this._instance;
  21. }
  22. /**
  23. * 初始化框架配置
  24. * @param {boolean} debug 是否是调试状态
  25. * @param {cc.Size} designResolution 设计分辨率
  26. * @param {boolean} fitHeight 是否高适配
  27. * @param {boolean} fitWidth 是否宽适配
  28. */
  29. public init(debug: boolean, designResolution: cc.Size, fitHeight: boolean, fitWidth: boolean): void {
  30. if (Facade._isInit) {
  31. console.warn("框架已经初始化,不需要重复初始化。");
  32. return;
  33. }
  34. Facade._isInit = true;
  35. FrameworkCfg.DEBUG = debug;
  36. FrameworkCfg.DESIGN_RESOLUTION = designResolution;
  37. FrameworkCfg.FIT_HEIGHT = fitHeight;
  38. FrameworkCfg.FIT_WIDTH = fitWidth;
  39. }
  40. /**
  41. * 运行场景
  42. * @param {{new(): BaseMediator}} mediator 场景mediator类型,类类型。
  43. * @param {{new(): BaseScene}} view 场景mediator类型,类类型。
  44. * @param {Object} data 自定义的任意类型透传数据。(可选)
  45. * @param {()=>void} cb 加载完成回调.
  46. */
  47. public runScene(mediator: { new(): BaseMediator }, view: { new(): BaseScene }, data?: any, cb?: () => void): void {
  48. if (Facade._isInit) {
  49. ViewManager.getInstance().__runScene__(mediator, view, data, cb);
  50. } else {
  51. console.warn("框架没有初始化,请先调用init接口进行初始化。");
  52. }
  53. }
  54. public loadTexture(name:string){
  55. return new Promise((resolve) => {
  56. let viewPathList = name.split('/');
  57. let bundleRes : string = viewPathList[0];
  58. let viewPath = "";
  59. for(let i = 1;i<viewPathList.length;i++){
  60. if(i<viewPathList.length-1){
  61. viewPath+=viewPathList[i]+"/";
  62. }else{
  63. viewPath+=viewPathList[i];
  64. }
  65. }
  66. // console.log("loadTexture: ",viewPath);
  67. cc.assetManager.loadBundle(bundleRes,(err,bundle)=>{
  68. bundle.load(viewPath,cc.SpriteFrame, function (err, spriteFrame) {
  69. if (err) {
  70. console.log("err", err);
  71. } else {
  72. resolve(spriteFrame);
  73. }
  74. });
  75. })
  76. });
  77. }
  78. /**
  79. * 运行分包场景
  80. * @param bundleRes 分包名
  81. * @param sceneName 场景名
  82. * @param progress 加载进度
  83. * @param cb 加载成功回调
  84. */
  85. public runBundleScene(bundleRes:string,sceneName:string,progress?:Function){
  86. cc.assetManager.loadBundle(bundleRes,(err,bundle)=>{
  87. if(progress){
  88. bundle.preloadScene(sceneName, progress.bind(this) , (res) => {
  89. cc.director.loadScene(sceneName);
  90. });
  91. }else{
  92. cc.director.loadScene(sceneName);
  93. }
  94. })
  95. }
  96. /**
  97. * 返回上一场景
  98. * @returns {boolean}是否存在上一个场景
  99. */
  100. public backScene(): boolean {
  101. return ViewManager.getInstance().__backScene__();
  102. }
  103. /**
  104. * 打开view界面,弹出界面
  105. * @param {{new(): BaseMediator}} mediator 界面mediator类型,类类型。
  106. * @param {{new(): BaseView}} view view 场景mediator类型,类类型。
  107. * @param {Object} data 自定义的任意类型透传数据。(可选)
  108. * @param {boolean} hasShowBlackBg 弹框是否有黑色背景(可选)
  109. * @param {()=>void} cb 加载完成回调.
  110. */
  111. public popView(mediator: { new(): BaseMediator }, view: { new(): BaseView }, data?: any, hasShowBlackBg: boolean = false, cb?: () => void): void {
  112. ViewManager.getInstance().__showView__(mediator, view, data, OPEN_VIEW_OPTION.OVERLAY, 0, cb, hasShowBlackBg);
  113. }
  114. /**
  115. * 创建view层,此接口用于初始不会被关闭和再次打开的常驻界面,所以它也不会受到pooView影响和管理。
  116. * @param {{new(): BaseMediator}} mediator 界面mediator类型,类类型。
  117. * @param {{new(): BaseView}} view view 场景mediator类型,类类型。
  118. * @param {number} zOrder ui层级
  119. * @param {Object} data 自定义的任意类型透传数据。(可选)
  120. * @param {boolean} hasShowBlackBg 弹框是否有黑色背景(可选)
  121. * @param {()=>void} cb 加载完成回调.
  122. */
  123. public addLayer(mediator: { new(): BaseMediator }, view: { new(): BaseView }, zOrder?: number, data?: any, hasShowBlackBg: boolean = false, cb?: () => void): void {
  124. ViewManager.getInstance().__showView__(mediator, view, data, OPEN_VIEW_OPTION.LAYER, zOrder, cb, hasShowBlackBg);
  125. }
  126. /**
  127. * 撤销命令
  128. * @param {{new (): BaseCommand}} command 命令对象
  129. * @param {Object} body 命令参数
  130. */
  131. public __undoCommand__(command: { new(): BaseCommand }, body?: any): void {
  132. CommandManager.getInstance().__undoCommand__(command, body);
  133. }
  134. /**
  135. * 注册数据model
  136. * @param {{new (): BaseModel}} model
  137. */
  138. public registerModel(model: { new(): BaseModel }): void {
  139. ModelManager.getInstance().registerModel(model);
  140. }
  141. /**
  142. * 获取model对象
  143. * @param {{new (): BaseModel}} model
  144. */
  145. public getModel<T extends BaseModel>(model: { new(): T }): T {
  146. return ModelManager.getInstance().getModel(model);
  147. }
  148. }
  149. /** 导入到全局属性mvc中的对外接口和属性等api */
  150. (<any>(window)).mvc = {
  151. appFacade: Facade.getInstance(),
  152. };