OpenWindow.ts 1.7 KB

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