OpenWindow.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { _decorator, Component, CCString, find, Node, Enum, Prefab, instantiate, EventHandler, SystemEventType } from "cc";
  2. import { ResourcesUtils } from "../../resourceManager/ResourcesUtils";
  3. import { Window } from "./Window";
  4. import { WindowOpenMode } from "./WindowOpenMode";
  5. import { WindowSystem } from "./WindowSystem";
  6. const { ccclass, property } = _decorator;
  7. /**
  8. * 打开窗口组件,定义窗口的参数、窗口的打开等
  9. * @author 袁浩
  10. */
  11. @ccclass('OpenWindow')
  12. export class OpenWindow extends Component {
  13. @property({ tooltip: "要加载的预制体对象相对于resources的路径" })
  14. public prefabPath: string = '';
  15. @property({ tooltip: "窗口打开的模式", type: Enum(WindowOpenMode) })
  16. public openMode: WindowOpenMode = WindowOpenMode.NotCloseAndAdd;
  17. @property({ tooltip: "自定义参数" })
  18. public params: string[] = [];
  19. @property({ tooltip: "窗口打开触发的节点,不填则无触发", type: [Node] })
  20. public openFroms: Node[] = [];
  21. @property({ tooltip: "窗口打开中的回调", type: EventHandler })
  22. public onOpening: EventHandler;
  23. @property({ tooltip: "窗口关闭中的回调", type: EventHandler })
  24. public onClosing: EventHandler;
  25. protected start() {
  26. this.checkOpenFroms();
  27. }
  28. public checkOpenFroms() {
  29. for (let i = 0; i < this.openFroms.length; i++) {
  30. const node = this.openFroms[i];
  31. node.on(SystemEventType.TOUCH_END, () => { this.open() }, this);
  32. }
  33. }
  34. private get windowSystem(): WindowSystem {
  35. var windowSystemNode = find("Canvas/WindowSystem");
  36. if (!windowSystemNode) {//如果没有窗口系统节点则创建一个
  37. var windowSystemNode = new Node("WindowSystem");
  38. windowSystem = windowSystemNode.addComponent(WindowSystem);
  39. }
  40. var windowSystem = windowSystemNode.getComponent(WindowSystem) || windowSystemNode.addComponent(WindowSystem);
  41. return windowSystem;
  42. }
  43. /**
  44. * 打开窗口
  45. * @param params 给窗口传递的参数,如果有值,那么此参数始终跟随在自定义参数后面
  46. */
  47. public async open(...params) {
  48. var _window = await this.windowSystem.open(this.prefabPath, this.openMode, params, this.onOpening, this.onClosing);
  49. return _window;
  50. }
  51. }