UIMgr.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * 界面相关的工具
  3. */
  4. require("UIInfo")
  5. /**
  6. * 登录数据管理
  7. */
  8. let UIMgr = {
  9. //* ************* 初始化 ************* *//
  10. /**
  11. * 初始化
  12. *
  13. */
  14. init () {
  15. if (CC_EDITOR) {
  16. return;
  17. }
  18. },
  19. getUIRoot () {
  20. return cc.director.getScene().getChildByName('Root');
  21. },
  22. open(type, param) {
  23. // edt_prefab/ctrl/prefab_normal_alert
  24. let prefabPath = 'edt_prefab/ctrl/' + window.JMC.UIPath[type];
  25. this.showUI(prefabPath, param)
  26. },
  27. showUI (prefabPath, alertData, cb, cache = false) {
  28. G.LogUtils.warn('showAlert', prefabPath);
  29. let temp = undefined;
  30. cc.loader.loadRes(prefabPath, cc.Prefab, (completedCount, totalCount, item)=>{
  31. temp = G.AppUtils.getLoadProgressInfo(temp, completedCount, totalCount);
  32. G.AppUtils.getSceneCtrl().updateLoadingProgress(temp.precent * 100, 100, item);
  33. }, (err, prefab) => {
  34. G.AppUtils.getSceneCtrl().updateLoadingProgress(100, 100, undefined);
  35. if (err) {
  36. G.AppUtils.getSceneCtrl().addToast('网络异常!');
  37. G.LogUtils.error('[showAlert]', err);
  38. if (cb) {
  39. cb(undefined, 'error', err);
  40. }
  41. return;
  42. }
  43. let node = cc.instantiate(prefab);
  44. let alert = node.getComponent('JMAlertBase');
  45. if (cb) {
  46. cb(alert, 'willShow', undefined);
  47. }
  48. let uiRoot = this.getUIRoot();
  49. let ret = this.pushUI(uiRoot, alert);
  50. if (!ret) {
  51. return;
  52. }
  53. // 因为reloadData如果出错会导致游戏闪屏,所以加上try catch
  54. try {
  55. alert.reloadData(alertData, cb);
  56. } catch (error) {
  57. G.LogUtils.error('AlertCtrl showAlert error:', error);
  58. }
  59. if (!cache) {
  60. cc.loader.releaseRes(prefabPath);
  61. }
  62. });
  63. },
  64. /**
  65. * 弹出指定弹框
  66. *
  67. * @author Pyden
  68. * @date 2019-03-22
  69. * @param {JMAlertBase} alert 弹框
  70. */
  71. pushUI (alert) {
  72. if (!cc.isValid(alert) || !cc.isValid(uiRoot)) {
  73. return false;
  74. }
  75. let zIndex = alert.alertZIndex;
  76. uiRoot.addChild(alert.node, zIndex);
  77. cc.game.emit('e_ui_push_alert', {alert: alert});
  78. return true;
  79. },
  80. }
  81. module.exports = UIMgr;