"use strict"; cc._RF.push(module, '8d5d8F7SBJOrbKNJeVVdc39', 'ViewManager'); // lightMVC/core/manager/ViewManager.ts "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewManager = void 0; var Constants_1 = require("../Constants"); var FrameworkCfg_1 = require("../FrameworkCfg"); /** * mvc框架控制类 * @author ituuz * @description 负责控制和维护框架各个节点和结构间的跳转和关联。 */ var ViewManager = /** @class */ (function () { /** * @constructor * @private */ function ViewManager() { /** 上一场景类 */ this._preSceneMediatorCls = null; this._preSceneViewCls = null; /** 当前场景类 */ this._curSceneMediatorCls = null; this._curSceneViewCls = null; /***是否有黑色背景 */ this._hasblackBg = false; this._popViewList = []; this._layerViewList = []; } /** * 单例获取类 */ ViewManager.getInstance = function () { return this._instance; }; /** * 运行场景 * @param {{new(): BaseMediator}} mediator 场景mediator类型,类类型。 * @param {{new(): BaseScene}} view 场景mediator类型,类类型。 * @param {Object} data 自定义的任意类型透传数据。(可选) * @param {()=>void} cb 加载完成回调. * @private */ ViewManager.prototype.__runScene__ = function (mediator, view, data, cb) { var _this = this; // 创建并绑定场景 var sceneMediator = new mediator(); sceneMediator["__init__"](); sceneMediator.init(data); // 如果前一场景不为空则进行清理 if (this._curScene) { this._curScene.destroy(); } // 保存当前场景 this._curScene = sceneMediator; if (this._curSceneMediatorCls != null && this._curSceneViewCls != null) { this._preSceneMediatorCls = this._curSceneMediatorCls; this._preSceneViewCls = this._curSceneViewCls; } this._curSceneMediatorCls = mediator; this._curSceneViewCls = view; // 处理场景显示逻辑 var scenePath = (view).path(); if (scenePath === "") { var ccs_1 = new cc.Scene(); ccs_1.name = "Scene"; var canvasNode = new cc.Node(); canvasNode.name = "Canvas"; var canvas = canvasNode.addComponent(cc.Canvas); canvas.designResolution = FrameworkCfg_1.default.DESIGN_RESOLUTION; canvas.fitHeight = FrameworkCfg_1.default.FIT_HEIGHT; canvas.fitWidth = FrameworkCfg_1.default.FIT_WIDTH; sceneMediator.view = canvasNode.addComponent(view); sceneMediator.view.__init__(); ccs_1.addChild(canvasNode); // 这里延迟1ms是为了让场景在下一帧加载 setTimeout(function () { _this.__closeAllView__(); cc.director.runSceneImmediate(ccs_1); sceneMediator.viewDidAppear(); cb && cb(); }, 1); } else { cc.director.loadScene(scenePath, function () { _this.__closeAllView__(); var canvas = cc.director.getScene().getChildByName('Canvas'); if (canvas) { sceneMediator.view = canvas.addComponent(view); sceneMediator.view.__init__(); sceneMediator.viewDidAppear(); cb && cb(); } else { console.log("场景中必须包含默认的Canvas节点!"); } }); } }; /** * 返回上一场景 * @returns {boolean}是否存在上一个场景 */ ViewManager.prototype.__backScene__ = function () { if (this._preSceneMediatorCls && this._preSceneViewCls) { this.__runScene__(this._preSceneMediatorCls, this._preSceneViewCls); return true; } return false; }; /** * 打开view界面 * @param {{new(): BaseMediator}} mediator 界面mediator类型,类类型。 * @param {{new(): BaseView}} view view 场景mediator类型,类类型。 * @param {boolean} hasShowBlackBg 是否有黑色背景图 * @param {Object} data 自定义的任意类型透传数据。(可选) * @param {OPEN_VIEW_OPTION} option 打开ui的操作选项,枚举类型。 * @param {number} zOrder 层级。 * @param {()=>void} cb 加载完成回调. */ ViewManager.prototype.__showView__ = function (mediator, view, data, option, zOrder, cb, hasShowBlackBg) { var _this = this; if (hasShowBlackBg === void 0) { hasShowBlackBg = false; } // 处理打开UI的其他操作 this.openViewOptionHandler(option); // 创建并绑定view var viewMediator = new mediator(); viewMediator["__init__"](); //设置是否有黑色背景 this._hasblackBg = hasShowBlackBg; // 处理场景显示逻辑 var viewPath = (view).path(); var viewPathList = viewPath.split('/'); var bundle = viewPathList[0]; viewPath = ""; for (var i = 1; i < viewPathList.length; i++) { if (i < viewPathList.length - 1) { viewPath += viewPathList[i] + "/"; } else { viewPath += viewPathList[i]; } } // console.log("bundle: ",bundle," viewPath: ",viewPath); if (viewPath === "") { var viewNode = new cc.Node(); this.initViewMediator(viewMediator, viewNode, view, option); viewMediator.init(data); viewMediator.viewDidAppear(); cb && cb(); } else { if (bundle === "" || bundle === null) { cc.loader.loadRes(viewPath, cc.Prefab, function (err, prefab) { if (err) { console.error(err); return; } var viewNode = cc.instantiate(prefab); _this.initViewMediator(viewMediator, viewNode, view, option); viewMediator.init(data); viewMediator.viewDidAppear(); cb && cb(); }); } else { cc.assetManager.loadBundle(bundle, function (err, bundle) { bundle.load(viewPath, cc.Prefab, function (err, prefab) { if (err) { console.error(err); return; } var viewNode = cc.instantiate(prefab); _this.initViewMediator(viewMediator, viewNode, view, option); viewMediator.init(data); viewMediator.viewDidAppear(); cb && cb(); }); }); } } }; /** * 初始化ViewMediator * @param {BaseMediator} mediator ViewMediator * @param {cc.Node} viewNode view显示节点 * @param {{new(): BaseView}} view view显示组件类 * @param {OPEN_VIEW_OPTION} option 打开选项 * @param {number} zOrder 层级排序 */ ViewManager.prototype.initViewMediator = function (mediator, viewNode, view, option, zOrder) { viewNode.zIndex = zOrder; mediator.view = viewNode.addComponent(view); //出现黑色背景图 this.blackBg(viewNode); mediator.view.__init__(); // 根据不同打开类型,存储到不同队列中。 if (option === Constants_1.OPEN_VIEW_OPTION.OVERLAY || option === Constants_1.OPEN_VIEW_OPTION.SINGLE) { this._popViewList.push(mediator); } else if (option === Constants_1.OPEN_VIEW_OPTION.LAYER) { this._layerViewList.push(mediator); } }; /** * * 是否显示黑色背景 */ ViewManager.prototype.blackBg = function (viewNode) { if (this._hasblackBg) { this._hasblackBg = false; var node = new cc.Node('myNode'); node.name = "blackBg" + viewNode.name; var sprite_1 = node.addComponent(cc.Sprite); node.addComponent(cc.BlockInputEvents); cc.loader.loadRes("blackBg", cc.SpriteFrame, function (err, spriteFrame) { if (err) { console.error(err); return; } sprite_1.spriteFrame = spriteFrame; cc.director.getScene().getChildByName('Canvas').addChild(node); node.width = FrameworkCfg_1.default.DESIGN_RESOLUTION.width; node.height = FrameworkCfg_1.default.DESIGN_RESOLUTION.height; node.x = 0; node.y = 0; cc.director.getScene().getChildByName('Canvas').addChild(viewNode); }); } else { cc.director.getScene().getChildByName('Canvas').addChild(viewNode); } }; /** * 关闭指定View * @param view * @private */ ViewManager.prototype.__closeView__ = function (view) { for (var i = 0; i < this._popViewList.length; i++) { if (this._popViewList[i].view === view) { this._popViewList.splice(i, 1); return; } } for (var i = 0; i < this._layerViewList.length; i++) { if (this._layerViewList[i].view === view) { this._layerViewList.splice(i, 1); return; } } }; /** * 关闭所有弹出窗口 * @private */ ViewManager.prototype.__closeAllPopView__ = function () { for (var i = 0; i < this._popViewList.length; i++) { this._popViewList[i].view["__onClose__"](); } this._popViewList = []; }; /** * 关闭所有添加层级 * @private */ ViewManager.prototype.__closeAllAddLayer__ = function () { for (var i = 0; i < this._layerViewList.length; i++) { this._layerViewList[i].view["__onClose__"](); } this._layerViewList = []; }; /** * 关闭所有view * @private */ ViewManager.prototype.__closeAllView__ = function () { this.__closeAllPopView__(); this.__closeAllAddLayer__(); }; /** * 根据参数处理ui的打开方式 * @param option * @private */ ViewManager.prototype.openViewOptionHandler = function (option) { // 设置默认值 if (!option) { option = Constants_1.OPEN_VIEW_OPTION.OVERLAY; } // 根据不同操作做不同处理 if (option === Constants_1.OPEN_VIEW_OPTION.SINGLE) { // TODO:暂时不提供这种关闭其他view的打开方式,可以通过BaseView.closeAllPopView()来实现。 } }; Object.defineProperty(ViewManager.prototype, "popViewList", { /**************************** getter and setter ******************************/ get: function () { return this._popViewList; }, enumerable: false, configurable: true }); Object.defineProperty(ViewManager.prototype, "layerViewList", { get: function () { return this._layerViewList; }, enumerable: false, configurable: true }); Object.defineProperty(ViewManager.prototype, "curScene", { get: function () { return this._curScene; }, enumerable: false, configurable: true }); ViewManager.prototype.pushLayerList = function (mediator) { this._layerViewList.push(mediator); }; // 实例 ViewManager._instance = new ViewManager(); return ViewManager; }()); exports.ViewManager = ViewManager; cc._RF.pop();