/** * 界面相关的工具 */ require("UIInfo") const Proxy = require('Proxy'); const ViewBase = require('ViewBase'); /** * 登录数据管理 */ let UIMgr = { //* ************* 初始化 ************* *// /** * 初始化 * */ init () { if (CC_EDITOR) { return; } // 常亮定义 this.defaultUIPath = 'edt_prefab/'; this.uiRoots = { [JMC.Proxy.Main]:"Main", [JMC.Proxy.Navigation]:"Navigation", [JMC.Proxy.Toast]:"Toast", [JMC.Proxy.UI]:"UI", }; }, getUIRoot () { let curScene = cc.director.getScene(); let root = curScene.getChildByName('Canvas').getChildByName("Root").getChildByName("UIRoot"); return root; }, open(type, param) { let prefabPath = this.defaultUIPath + JMC.UIPath[type]; this.showUI(prefabPath, param) }, showUI(prefabPath, param, cb, cache = false) { G.LogUtils.warn('showAlert', prefabPath); let self = this; cc.loader.loadRes(prefabPath, cc.Prefab, (completedCount, totalCount, item) => { }, (err, prefab) => { if (err) { G.AppUtils.getSceneCtrl().addToast('网络异常!'); G.LogUtils.error('[showAlert]', err); if (cb) { cb(undefined, 'error', err); } return; } let node = cc.instantiate(prefab); if (cb) { cb(alert, 'willShow', undefined); } let uiRoot = self.getUIRoot(); let proxy = node.getComponent(Proxy); if (proxy){ uiRoot = uiRoot.getChildByName(self.uiRoots[proxy.type]) self.clearUIType(proxy.type, proxy.type == JMC.Proxy.Main) } let ret = self.safeAddChild(uiRoot, node); if (!ret) { return; } let viewBase = node.getComponent(ViewBase); if (viewBase) { viewBase.init(param) } if (!cache) { cc.loader.releaseRes(prefabPath); } }); }, safeAddChild(uiRoot, node) { if (!cc.isValid(node) || !cc.isValid(uiRoot)) { return false; } uiRoot.addChild(node); cc.game.emit('e_ui_push_alert', {node}); return true; }, clearUIType(type, isClear) { if (!isClear) { return } let uiRoot = this.getUIRoot(); let typeRoot = uiRoot.getChildByName(this.uiRoots[type]); typeRoot.removeAllChildren(); } } module.exports = UIMgr;