UIMgr.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // UI
  2. class UIMgr{
  3. constructor() {
  4. console.log('UIMgr初始化');
  5. this.recordList = {};
  6. this.popupList = [];
  7. }
  8. openUI(path, callBack){
  9. let arr = path.split('/');
  10. let key = arr[arr.length - 1];
  11. if(key != 'TipsLayer')
  12. this.showWait();
  13. cc.resources.load(path, function(err, prefab){
  14. if(err){
  15. console.log('加载界面出错: ', path);
  16. return;
  17. }
  18. this.hideWait();
  19. if(cc.isValid(this.recordList[key])){
  20. console.log("弹框已加载: ", key);
  21. if (callBack)
  22. callBack(this.recordList[key]);
  23. return;
  24. }
  25. let pf = cc.instantiate(prefab);
  26. let layer = cc.director.getScene();
  27. pf.parent = layer;
  28. pf.name = key;
  29. this.setLayerIndex(pf);
  30. this.recordList[key] = pf;
  31. this.popupList.push({name: key, node: pf});
  32. if(callBack)
  33. callBack(pf);
  34. }.bind(this));
  35. }
  36. setLayerIndex(target){
  37. let tName = target.name;
  38. if(tName == 'TipsLayer'){
  39. target.zIndex = 100 + this.popupList.length;
  40. }else{
  41. // target.zIndex = 200 + this.popupList.length;
  42. }
  43. }
  44. clearPopupList(){
  45. this.popupList = [];
  46. }
  47. getPopupNums() {
  48. console.log('getPopupNums: ', this.popupList);
  49. return this.popupList.length;
  50. }
  51. closeUI(key, bDestroy=true){
  52. for(let i=0; i < this.popupList.length; i++){
  53. let item = this.popupList[i];
  54. if(item.name == key){
  55. if(bDestroy){
  56. if(cc.isValid(item.node)){
  57. item.node.destroy();
  58. }
  59. }
  60. this.popupList.splice(i, 1);
  61. break;
  62. }
  63. }
  64. delete this.recordList[key];
  65. }
  66. closeAllUI(){
  67. for(let i=0; i < this.popupList.length; i++){
  68. let item = this.popupList[i];
  69. if (cc.isValid(item.node)) {
  70. item.node.destroy();
  71. }
  72. }
  73. this.popupList = [];
  74. this.recordList = {};
  75. }
  76. // -------- wait --------
  77. showWait(callBack = null){
  78. let waitNode = cc.find('WaitNodeLayer');
  79. if(waitNode)
  80. waitNode.getComponent('WaitNodeLayer').showWait(callBack);
  81. }
  82. hideWait(){
  83. let waitNode = cc.find('WaitNodeLayer');
  84. if(waitNode)
  85. waitNode.getComponent('WaitNodeLayer').hideWait();
  86. }
  87. }
  88. let instance = null;
  89. UIMgr.getInstance = function(){
  90. if(!instance){
  91. instance = new UIMgr();
  92. }
  93. return instance;
  94. }
  95. export default UIMgr.getInstance();