GUIManager.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import GUIProxy from './GUIProxy';
  2. export class GUIManager{
  3. //UI 注册信息
  4. private m_guiMap:Map<number,GUIProxy>=new Map<number,GUIProxy>();
  5. //正在显示的UI
  6. private m_showing:Map<number,GUIProxy>=new Map<number,GUIProxy>();
  7. private m_showingList:GUIProxy[]=[];
  8. /**
  9. * 注册UI
  10. * @param key 全局唯一KEY
  11. * @param assetBundle 资源所在AssetBundle
  12. * @param packageName 包名称
  13. * @param resName UI界面
  14. * @param mediatorClass UI界面类
  15. */
  16. RegisterGUI(key:number,assetBundle:string,packageName:string,resName:string,mediatorClass:any):void{
  17. if(this.m_guiMap.has(key)){
  18. throw new Error("UI重复注册:"+key);
  19. }
  20. let guiProxy:GUIProxy=new GUIProxy(key,assetBundle,packageName,resName,mediatorClass);
  21. this.m_guiMap.set(key,guiProxy);
  22. }
  23. /**
  24. * 取消注册
  25. * @param key UI 全局唯一KEY
  26. */
  27. Unregister(key:number):void{
  28. if(this.m_guiMap.has(key)==false){
  29. throw new Error("无法取消未注册的UI:"+key);
  30. }
  31. }
  32. /**
  33. * 显示UI
  34. * @param key
  35. * @param data
  36. */
  37. Show(key:number,data?:any):void{
  38. if(this.m_guiMap.has(key)==false){
  39. throw new Error("未注册的UI:"+key);
  40. }
  41. let guiProxy:GUIProxy;
  42. //已经显示
  43. if(this.m_showing.has(key)){
  44. return;
  45. }else{
  46. guiProxy=this.m_guiMap.get(key);
  47. guiProxy.Show(data);
  48. this.m_showing.set(key,guiProxy);
  49. this.m_showingList.push(guiProxy);
  50. }
  51. }
  52. /**
  53. * 隐藏UI
  54. * @param key
  55. */
  56. Hide(key:number):void{
  57. if(this.m_guiMap.has(key)==false){
  58. throw new Error("未注册的UI:"+key);
  59. }
  60. let guiProxy:GUIProxy;
  61. //已经显示
  62. if(this.m_showing.has(key)==false){
  63. return;
  64. }else{
  65. this.m_showing.delete(key);
  66. guiProxy=this.m_guiMap.get(key);
  67. guiProxy.Hide();
  68. let index:number=this.m_showingList.indexOf(guiProxy);
  69. this.m_showingList.splice(index,1);
  70. }
  71. }
  72. /**
  73. * 心跳
  74. * @param dt
  75. */
  76. Tick(dt:number):void{
  77. let count:number=this.m_showingList.length;
  78. if(count==0){
  79. return;
  80. }
  81. for (let index = 0; index < count; index++) {
  82. const element = this.m_showingList[index];
  83. element.Tick(dt);
  84. }
  85. }
  86. /**
  87. * 单例
  88. */
  89. private static instance:GUIManager;
  90. public static get single():GUIManager{
  91. if(this.instance==null){
  92. this.instance=new GUIManager();
  93. }
  94. return this.instance;
  95. }
  96. }