UIMgr.js 2.7 KB

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