8d5d817b-4812-4ead-b28d-25e55575cdfd.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. "use strict";
  2. cc._RF.push(module, '8d5d8F7SBJOrbKNJeVVdc39', 'ViewManager');
  3. // lightMVC/core/manager/ViewManager.ts
  4. "use strict";
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.ViewManager = void 0;
  7. var Constants_1 = require("../Constants");
  8. var FrameworkCfg_1 = require("../FrameworkCfg");
  9. /**
  10. * mvc框架控制类
  11. * @author ituuz
  12. * @description 负责控制和维护框架各个节点和结构间的跳转和关联。
  13. */
  14. var ViewManager = /** @class */ (function () {
  15. /**
  16. * @constructor
  17. * @private
  18. */
  19. function ViewManager() {
  20. /** 上一场景类 */
  21. this._preSceneMediatorCls = null;
  22. this._preSceneViewCls = null;
  23. /** 当前场景类 */
  24. this._curSceneMediatorCls = null;
  25. this._curSceneViewCls = null;
  26. /***是否有黑色背景 */
  27. this._hasblackBg = false;
  28. this._popViewList = [];
  29. this._layerViewList = [];
  30. }
  31. /**
  32. * 单例获取类
  33. */
  34. ViewManager.getInstance = function () {
  35. return this._instance;
  36. };
  37. /**
  38. * 运行场景
  39. * @param {{new(): BaseMediator}} mediator 场景mediator类型,类类型。
  40. * @param {{new(): BaseScene}} view 场景mediator类型,类类型。
  41. * @param {Object} data 自定义的任意类型透传数据。(可选)
  42. * @param {()=>void} cb 加载完成回调.
  43. * @private
  44. */
  45. ViewManager.prototype.__runScene__ = function (mediator, view, data, cb) {
  46. var _this = this;
  47. // 创建并绑定场景
  48. var sceneMediator = new mediator();
  49. sceneMediator["__init__"]();
  50. sceneMediator.init(data);
  51. // 如果前一场景不为空则进行清理
  52. if (this._curScene) {
  53. this._curScene.destroy();
  54. }
  55. // 保存当前场景
  56. this._curScene = sceneMediator;
  57. if (this._curSceneMediatorCls != null && this._curSceneViewCls != null) {
  58. this._preSceneMediatorCls = this._curSceneMediatorCls;
  59. this._preSceneViewCls = this._curSceneViewCls;
  60. }
  61. this._curSceneMediatorCls = mediator;
  62. this._curSceneViewCls = view;
  63. // 处理场景显示逻辑
  64. var scenePath = (view).path();
  65. if (scenePath === "") {
  66. var ccs_1 = new cc.Scene();
  67. ccs_1.name = "Scene";
  68. var canvasNode = new cc.Node();
  69. canvasNode.name = "Canvas";
  70. var canvas = canvasNode.addComponent(cc.Canvas);
  71. canvas.designResolution = FrameworkCfg_1.default.DESIGN_RESOLUTION;
  72. canvas.fitHeight = FrameworkCfg_1.default.FIT_HEIGHT;
  73. canvas.fitWidth = FrameworkCfg_1.default.FIT_WIDTH;
  74. sceneMediator.view = canvasNode.addComponent(view);
  75. sceneMediator.view.__init__();
  76. ccs_1.addChild(canvasNode);
  77. // 这里延迟1ms是为了让场景在下一帧加载
  78. setTimeout(function () {
  79. _this.__closeAllView__();
  80. cc.director.runSceneImmediate(ccs_1);
  81. sceneMediator.viewDidAppear();
  82. cb && cb();
  83. }, 1);
  84. }
  85. else {
  86. cc.director.loadScene(scenePath, function () {
  87. _this.__closeAllView__();
  88. var canvas = cc.director.getScene().getChildByName('Canvas');
  89. if (canvas) {
  90. sceneMediator.view = canvas.addComponent(view);
  91. sceneMediator.view.__init__();
  92. sceneMediator.viewDidAppear();
  93. cb && cb();
  94. }
  95. else {
  96. console.log("场景中必须包含默认的Canvas节点!");
  97. }
  98. });
  99. }
  100. };
  101. /**
  102. * 返回上一场景
  103. * @returns {boolean}是否存在上一个场景
  104. */
  105. ViewManager.prototype.__backScene__ = function () {
  106. if (this._preSceneMediatorCls && this._preSceneViewCls) {
  107. this.__runScene__(this._preSceneMediatorCls, this._preSceneViewCls);
  108. return true;
  109. }
  110. return false;
  111. };
  112. /**
  113. * 打开view界面
  114. * @param {{new(): BaseMediator}} mediator 界面mediator类型,类类型。
  115. * @param {{new(): BaseView}} view view 场景mediator类型,类类型。
  116. * @param {boolean} hasShowBlackBg 是否有黑色背景图
  117. * @param {Object} data 自定义的任意类型透传数据。(可选)
  118. * @param {OPEN_VIEW_OPTION} option 打开ui的操作选项,枚举类型。
  119. * @param {number} zOrder 层级。
  120. * @param {()=>void} cb 加载完成回调.
  121. */
  122. ViewManager.prototype.__showView__ = function (mediator, view, data, option, zOrder, cb, hasShowBlackBg) {
  123. var _this = this;
  124. if (hasShowBlackBg === void 0) { hasShowBlackBg = false; }
  125. // 处理打开UI的其他操作
  126. this.openViewOptionHandler(option);
  127. // 创建并绑定view
  128. var viewMediator = new mediator();
  129. viewMediator["__init__"]();
  130. //设置是否有黑色背景
  131. this._hasblackBg = hasShowBlackBg;
  132. // 处理场景显示逻辑
  133. var viewPath = (view).path();
  134. var viewPathList = viewPath.split('/');
  135. var bundle = viewPathList[0];
  136. viewPath = "";
  137. for (var i = 1; i < viewPathList.length; i++) {
  138. if (i < viewPathList.length - 1) {
  139. viewPath += viewPathList[i] + "/";
  140. }
  141. else {
  142. viewPath += viewPathList[i];
  143. }
  144. }
  145. // console.log("bundle: ",bundle," viewPath: ",viewPath);
  146. if (viewPath === "") {
  147. var viewNode = new cc.Node();
  148. this.initViewMediator(viewMediator, viewNode, view, option);
  149. viewMediator.init(data);
  150. viewMediator.viewDidAppear();
  151. cb && cb();
  152. }
  153. else {
  154. if (bundle === "" || bundle === null) {
  155. cc.loader.loadRes(viewPath, cc.Prefab, function (err, prefab) {
  156. if (err) {
  157. console.error(err);
  158. return;
  159. }
  160. var viewNode = cc.instantiate(prefab);
  161. _this.initViewMediator(viewMediator, viewNode, view, option);
  162. viewMediator.init(data);
  163. viewMediator.viewDidAppear();
  164. cb && cb();
  165. });
  166. }
  167. else {
  168. cc.assetManager.loadBundle(bundle, function (err, bundle) {
  169. bundle.load(viewPath, cc.Prefab, function (err, prefab) {
  170. if (err) {
  171. console.error(err);
  172. return;
  173. }
  174. var viewNode = cc.instantiate(prefab);
  175. _this.initViewMediator(viewMediator, viewNode, view, option);
  176. viewMediator.init(data);
  177. viewMediator.viewDidAppear();
  178. cb && cb();
  179. });
  180. });
  181. }
  182. }
  183. };
  184. /**
  185. * 初始化ViewMediator
  186. * @param {BaseMediator} mediator ViewMediator
  187. * @param {cc.Node} viewNode view显示节点
  188. * @param {{new(): BaseView}} view view显示组件类
  189. * @param {OPEN_VIEW_OPTION} option 打开选项
  190. * @param {number} zOrder 层级排序
  191. */
  192. ViewManager.prototype.initViewMediator = function (mediator, viewNode, view, option, zOrder) {
  193. viewNode.zIndex = zOrder;
  194. mediator.view = viewNode.addComponent(view);
  195. //出现黑色背景图
  196. this.blackBg(viewNode);
  197. mediator.view.__init__();
  198. // 根据不同打开类型,存储到不同队列中。
  199. if (option === Constants_1.OPEN_VIEW_OPTION.OVERLAY || option === Constants_1.OPEN_VIEW_OPTION.SINGLE) {
  200. this._popViewList.push(mediator);
  201. }
  202. else if (option === Constants_1.OPEN_VIEW_OPTION.LAYER) {
  203. this._layerViewList.push(mediator);
  204. }
  205. };
  206. /**
  207. *
  208. * 是否显示黑色背景
  209. */
  210. ViewManager.prototype.blackBg = function (viewNode) {
  211. if (this._hasblackBg) {
  212. this._hasblackBg = false;
  213. var node = new cc.Node('myNode');
  214. node.name = "blackBg" + viewNode.name;
  215. var sprite_1 = node.addComponent(cc.Sprite);
  216. node.addComponent(cc.BlockInputEvents);
  217. cc.loader.loadRes("blackBg", cc.SpriteFrame, function (err, spriteFrame) {
  218. if (err) {
  219. console.error(err);
  220. return;
  221. }
  222. sprite_1.spriteFrame = spriteFrame;
  223. cc.director.getScene().getChildByName('Canvas').addChild(node);
  224. node.width = FrameworkCfg_1.default.DESIGN_RESOLUTION.width;
  225. node.height = FrameworkCfg_1.default.DESIGN_RESOLUTION.height;
  226. node.x = 0;
  227. node.y = 0;
  228. cc.director.getScene().getChildByName('Canvas').addChild(viewNode);
  229. });
  230. }
  231. else {
  232. cc.director.getScene().getChildByName('Canvas').addChild(viewNode);
  233. }
  234. };
  235. /**
  236. * 关闭指定View
  237. * @param view
  238. * @private
  239. */
  240. ViewManager.prototype.__closeView__ = function (view) {
  241. for (var i = 0; i < this._popViewList.length; i++) {
  242. if (this._popViewList[i].view === view) {
  243. this._popViewList.splice(i, 1);
  244. return;
  245. }
  246. }
  247. for (var i = 0; i < this._layerViewList.length; i++) {
  248. if (this._layerViewList[i].view === view) {
  249. this._layerViewList.splice(i, 1);
  250. return;
  251. }
  252. }
  253. };
  254. /**
  255. * 关闭所有弹出窗口
  256. * @private
  257. */
  258. ViewManager.prototype.__closeAllPopView__ = function () {
  259. for (var i = 0; i < this._popViewList.length; i++) {
  260. this._popViewList[i].view["__onClose__"]();
  261. }
  262. this._popViewList = [];
  263. };
  264. /**
  265. * 关闭所有添加层级
  266. * @private
  267. */
  268. ViewManager.prototype.__closeAllAddLayer__ = function () {
  269. for (var i = 0; i < this._layerViewList.length; i++) {
  270. this._layerViewList[i].view["__onClose__"]();
  271. }
  272. this._layerViewList = [];
  273. };
  274. /**
  275. * 关闭所有view
  276. * @private
  277. */
  278. ViewManager.prototype.__closeAllView__ = function () {
  279. this.__closeAllPopView__();
  280. this.__closeAllAddLayer__();
  281. };
  282. /**
  283. * 根据参数处理ui的打开方式
  284. * @param option
  285. * @private
  286. */
  287. ViewManager.prototype.openViewOptionHandler = function (option) {
  288. // 设置默认值
  289. if (!option) {
  290. option = Constants_1.OPEN_VIEW_OPTION.OVERLAY;
  291. }
  292. // 根据不同操作做不同处理
  293. if (option === Constants_1.OPEN_VIEW_OPTION.SINGLE) {
  294. // TODO:暂时不提供这种关闭其他view的打开方式,可以通过BaseView.closeAllPopView()来实现。
  295. }
  296. };
  297. Object.defineProperty(ViewManager.prototype, "popViewList", {
  298. /**************************** getter and setter ******************************/
  299. get: function () {
  300. return this._popViewList;
  301. },
  302. enumerable: false,
  303. configurable: true
  304. });
  305. Object.defineProperty(ViewManager.prototype, "layerViewList", {
  306. get: function () {
  307. return this._layerViewList;
  308. },
  309. enumerable: false,
  310. configurable: true
  311. });
  312. Object.defineProperty(ViewManager.prototype, "curScene", {
  313. get: function () {
  314. return this._curScene;
  315. },
  316. enumerable: false,
  317. configurable: true
  318. });
  319. ViewManager.prototype.pushLayerList = function (mediator) {
  320. this._layerViewList.push(mediator);
  321. };
  322. // 实例
  323. ViewManager._instance = new ViewManager();
  324. return ViewManager;
  325. }());
  326. exports.ViewManager = ViewManager;
  327. cc._RF.pop();