UIWindow.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const FairyGUI = CS.FairyGUI
  2. const FairyEditor = CS.FairyEditor
  3. const App = FairyEditor.App;
  4. export class WindData {
  5. packName: string
  6. resName: string
  7. url: string;
  8. constructor(pkg, res) {
  9. this.packName = pkg;
  10. this.resName = res;
  11. this.url = "ui://" + pkg + "/" + res;
  12. }
  13. }
  14. export default class UIWindow extends CS.FairyGUI.Window {
  15. private static readonly mWinds = new Map<string, UIWindow>();
  16. private _windData: WindData;
  17. constructor(data: WindData) {
  18. super();
  19. if (data == null) return
  20. this._windData = data;
  21. this.__onInit = () => { this.onInit(); }
  22. this.__onShown = () => { this.onShown(); }
  23. this.__onHide = (() => { this.onHide(); })
  24. }
  25. get windData() {
  26. return this._windData
  27. }
  28. private get url() {
  29. return this._windData.url
  30. }
  31. public static add(wind: UIWindow) {
  32. this.mWinds.set(wind.url, wind);
  33. }
  34. /**查看路径资源是否创建 */
  35. public static FindUrl(url: string): UIWindow | undefined {
  36. if (url == null) url = "";
  37. return this.mWinds.has(url) ? this.mWinds.get(url) : undefined;
  38. }
  39. /**界面是否正在被展示 */
  40. public static isShow(url: string) {
  41. let wind = this.FindUrl(url);
  42. if (wind == undefined) return false;
  43. return wind ? wind.isShowing && wind["_inited"] == true : false;
  44. }
  45. /**
  46. * 实例化窗口资源 第一次创建时才会调用
  47. * @returns
  48. */
  49. protected onInit() {
  50. let self = this;
  51. App.consoleView.Log("创建组件:" + self.url);
  52. let windObj = FairyGUI.UIPackage.CreateObject(self.windData.packName, self.windData.resName) as CS.FairyGUI.GComponent;
  53. if (windObj == null) {
  54. App.consoleView.Log("创建窗口失败 url:" + self.url);
  55. return;
  56. }
  57. self.contentPane = windObj;
  58. this.Center();
  59. }
  60. protected onShown() {
  61. }
  62. protected onHide() {
  63. }
  64. //#region 界面显示
  65. /**
  66. * 打开面板
  67. * @param id 面板路径
  68. * @param param 需求参数
  69. */
  70. static show(id: any, param?: any) {
  71. if (this.mWinds.has(id)) {
  72. let wind: UIWindow = this.mWinds.get(id) as UIWindow;
  73. wind.data = param;
  74. if (wind.isShowing && wind.contentPane) {
  75. wind.onShown();
  76. } else {
  77. wind.Show();
  78. }
  79. } else {
  80. App.consoleView.Log("显示窗口失败没有注册窗口 id:" + id);
  81. }
  82. }
  83. //#endregion
  84. public static hide(url: string, param?: any) {
  85. if (this.mWinds.has(url)) {
  86. let wind = this.mWinds.get(url) as UIWindow;
  87. if (wind.isShowing) {
  88. wind.data = param;
  89. wind.Hide();
  90. }
  91. } else {
  92. App.consoleView.Log("隐藏窗口失败没有注册窗口 id:" + url);
  93. }
  94. }
  95. public static delAll(filter: Array<string>) {
  96. let needDel = new Array<string>();
  97. this.mWinds.forEach((v, k) => {
  98. if (filter == null || filter.findIndex(a => a == v.url) == -1) {
  99. needDel.push(v.url);
  100. }
  101. });
  102. for (let i = 0; i < needDel.length; i++) {
  103. this.remove(this.mWinds.get(needDel[i]) as UIWindow);
  104. }
  105. }
  106. public static remove(wind: UIWindow) {
  107. if (this.mWinds.has(wind.url)) {
  108. this.mWinds.delete(wind.url);
  109. App.consoleView.Log("卸载窗口:" + wind.url);
  110. wind._windData = null;
  111. wind.RemoveFromParent();
  112. wind.contentPane.Dispose();
  113. wind.contentPane = null;
  114. wind.Dispose();
  115. wind = null;
  116. }
  117. }
  118. }