GUIProxy.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { GUIManager } from "./GUIManager";
  2. import GUIMediator from "./GUIMediator";
  3. import LayerManager from "./layers/LayerManager";
  4. /**
  5. * UI代理
  6. */
  7. export default class GUIProxy
  8. {
  9. key:number;
  10. assetBundleName:string;
  11. assetBundle:cc.AssetManager.Bundle;
  12. packageName:string;
  13. resName:string;
  14. mediatorClass:any;
  15. uiNode:fgui.GComponent;
  16. mediator:GUIMediator;
  17. /**
  18. * 状态 0 未加载 1加载中 2加载完成
  19. */
  20. state:number=0;
  21. /**
  22. * 是否显示
  23. */
  24. isShowing:boolean;
  25. data:any;
  26. constructor(key:number,assetBundle:string,packageName:string,resName:string,mediatorClass:any){
  27. this.key=key;
  28. this.assetBundleName=assetBundle;
  29. this.packageName=packageName;
  30. this.resName=resName;
  31. this.mediatorClass=mediatorClass;
  32. }
  33. /**
  34. * 显示(内部接口)
  35. * @param data
  36. */
  37. Show(data?:any):void{
  38. this.data=data;
  39. this.isShowing=true;
  40. if(this.state==0){
  41. this.LoadUIAssets();
  42. }else if(this.state==1){
  43. //加载中啥也不干
  44. }else{
  45. this.AddToLayer();
  46. }
  47. }
  48. /**
  49. * 内部接口
  50. */
  51. Hide():void{
  52. if(this.mediator){
  53. LayerManager.single.RemoveFormeLayer(this.mediator.LayerIndex,this.uiNode);
  54. this.mediator.Hide();
  55. }
  56. this.isShowing=false;
  57. }
  58. /**
  59. * 心跳
  60. * @param dt
  61. */
  62. Tick(dt:number):void{
  63. if(this.mediator!=null){
  64. this.mediator.Tick(dt);
  65. }
  66. }
  67. /**
  68. * 添加到层上并显示
  69. */
  70. private AddToLayer():void{
  71. this.uiNode.setSize(fgui.GRoot.inst.width,fgui.GRoot.inst.height);
  72. LayerManager.single.AddToLayer(this.mediator.LayerIndex,this.uiNode);
  73. this.mediator.Show(this.data);
  74. }
  75. private LoadUIAssets(){
  76. this.state=1;
  77. this.assetBundle=cc.assetManager.getBundle(this.assetBundleName);
  78. if(this.assetBundle==null){
  79. cc.assetManager.loadBundle(this.assetBundleName,this.OnLoadAssetBundleComplete.bind(this));
  80. }else{
  81. this.StartLoadUIPackage();
  82. }
  83. }
  84. private OnLoadAssetBundleComplete(err:Error,bundle:cc.AssetManager.Bundle):void{
  85. if(err){
  86. throw new Error("UI所在AssetBundle"+this.assetBundleName+"加载出错!");
  87. }
  88. this.assetBundle=bundle;
  89. this.StartLoadUIPackage();
  90. }
  91. private StartLoadUIPackage():void{
  92. fgui.UIPackage.loadPackage(this.assetBundle,this.packageName,this.OnLoadUIAssetComplete.bind(this));
  93. }
  94. private OnLoadUIAssetComplete(error: Error):void{
  95. if(error!=null){
  96. throw new Error("加载UI资源包出错:"+this.packageName);
  97. }
  98. this.state=2;
  99. this.uiNode=fgui.UIPackage.createObject(this.packageName,this.resName).asCom;
  100. this.mediator=new this.mediatorClass();
  101. this.mediator.uiKey=this.key;
  102. this.mediator._view=this.uiNode;
  103. //添加到层
  104. if(this.isShowing){
  105. this.AddToLayer();
  106. }
  107. }
  108. /**
  109. * 获取UI所属层
  110. */
  111. get LayerIndex(){
  112. return 3;
  113. }
  114. }