UIMgr.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * 界面相关的工具
  3. */
  4. require("UIInfo")
  5. const Proxy = require('Proxy');
  6. /**
  7. * 登录数据管理
  8. */
  9. let UIMgr = {
  10. //* ************* 初始化 ************* *//
  11. /**
  12. * 初始化
  13. *
  14. */
  15. init () {
  16. if (CC_EDITOR) {
  17. return;
  18. }
  19. this.UIRoots = {
  20. [window.JMC.Proxy.Main]:"Main",
  21. [window.JMC.Proxy.Navigation]:"Navigation",
  22. [window.JMC.Proxy.Toast]:"Toast",
  23. [window.JMC.Proxy.UI]:"UI",
  24. };
  25. },
  26. getUIRoot () {
  27. let curScene = cc.director.getScene();
  28. let root = curScene.getChildByName('Canvas').getChildByName("Root").getChildByName("UIRoot");
  29. return root;
  30. },
  31. open(type, param) {
  32. let prefabPath = 'edt_prefab/' + window.JMC.UIPath[type];
  33. this.showUI(prefabPath, param)
  34. },
  35. showUI(prefabPath, alertData, cb, cache = false) {
  36. G.LogUtils.warn('showAlert', prefabPath);
  37. let self = this;
  38. let temp = undefined;
  39. cc.loader.loadRes(prefabPath, cc.Prefab, (completedCount, totalCount, item)=>{
  40. temp = G.AppUtils.getLoadProgressInfo(temp, completedCount, totalCount);
  41. G.AppUtils.getSceneCtrl().updateLoadingProgress(temp.precent * 100, 100, item);
  42. }, (err, prefab) => {
  43. G.AppUtils.getSceneCtrl().updateLoadingProgress(100, 100, undefined);
  44. if (err) {
  45. G.AppUtils.getSceneCtrl().addToast('网络异常!');
  46. G.LogUtils.error('[showAlert]', err);
  47. if (cb) {
  48. cb(undefined, 'error', err);
  49. }
  50. return;
  51. }
  52. let node = cc.instantiate(prefab);
  53. if (cb) {
  54. cb(alert, 'willShow', undefined);
  55. }
  56. let uiRoot = self.getUIRoot();
  57. let proxy = node.getComponent(Proxy);
  58. if (proxy){
  59. uiRoot = uiRoot.getChildByName(self.UIRoots[proxy.type])
  60. self.clearUIType(proxy.type, proxy.type == window.JMC.Proxy.Main)
  61. }
  62. // let uiRoot = this.getUIRoot();
  63. // .getChildByName(this.UIRoots[type])
  64. let ret = self.pushUI(uiRoot, node);
  65. if (!ret) {
  66. return;
  67. }
  68. if (!cache) {
  69. cc.loader.releaseRes(prefabPath);
  70. }
  71. });
  72. },
  73. /**
  74. * 弹出指定弹框
  75. *
  76. * @author Pyden
  77. * @date 2019-03-22
  78. * @param {JMAlertBase} alert 弹框
  79. */
  80. pushUI (uiRoot, node) {
  81. if (!cc.isValid(node) || !cc.isValid(uiRoot)) {
  82. return false;
  83. }
  84. uiRoot.addChild(node);
  85. cc.game.emit('e_ui_push_alert', {node});
  86. return true;
  87. },
  88. clearUIType(type, isClear) {
  89. if (!isClear) {
  90. return
  91. }
  92. let uiRoot = this.getUIRoot();
  93. let typeRoot = uiRoot.getChildByName(this.UIRoots[type]);
  94. typeRoot.removeAllChildren();
  95. }
  96. }
  97. module.exports = UIMgr;