Window.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { _decorator, Component, find, Node, EventHandler, SystemEventType, tween, Vec3, Quat, quat } from "cc";
  2. import { UITween } from "../tween/UITween";
  3. import { WindowSystem } from "./WindowSystem";
  4. const { ccclass, property } = _decorator;
  5. /**
  6. * 窗口组件,必须赋予窗口顶级节点,定义关闭节点、窗口参数获取等
  7. * @author 袁浩
  8. */
  9. @ccclass('Window')
  10. export class Window extends Component {
  11. @property({ tooltip: "可关闭窗口的节点列表,如果无数据,那么窗口系统也会自动找子节点中名称closeNode的节点当作关闭节点", type: [Node] })
  12. public closeNodes: Node[] = [];
  13. @property({ tooltip: "窗口参数得到或者改变的回调,回调参数为彻底关闭方法", type: [EventHandler] })
  14. public onParams: EventHandler[] = [];
  15. @property({ tooltip: "窗口关闭中的回调,注意,当设置了onClosing,回调参数会关闭本脚本的方法来手动关闭,如果不调用会导致无法关闭", type: [EventHandler] })
  16. public onClosing: EventHandler[] = [];
  17. @property({ tooltip: "窗口关闭动画", type: UITween })
  18. public closeTween: UITween;
  19. /**
  20. * 窗口相对于resources的路径
  21. */
  22. public prefabPath: string;
  23. /**
  24. * OpenWindow脚本的关闭回调配置
  25. */
  26. public openWindowOnClosing: EventHandler[] = [];
  27. private _params: any[] = [];
  28. public setParams(value: any[]) {
  29. this._params = value;
  30. this.isStarted && this.callParams();
  31. }
  32. /**
  33. * openWindow.open传递的参数
  34. */
  35. public get params(): any {
  36. return this._params;
  37. }
  38. private isStarted: boolean = false;
  39. protected start() {
  40. this.checkCloseNode();
  41. this.callParams();
  42. this.isStarted = true;
  43. WindowSystem.onWindowOpen(this);
  44. }
  45. private callParams() {
  46. for (let i = 0; i < this.onParams.length; i++) {
  47. this.onParams[i].emit(this._params);
  48. }
  49. }
  50. protected checkCloseNode() {
  51. var closeNodes = this.closeNodes;
  52. var childCloseNode = this.node.getChildByName("closeNode");
  53. childCloseNode && closeNodes.indexOf(childCloseNode) == -1 && (closeNodes.push(childCloseNode));
  54. for (let i = 0; i < closeNodes.length; i++) {
  55. const closeNode = closeNodes[i];
  56. closeNode.on(SystemEventType.TOUCH_END, () => {
  57. this.close();
  58. }, this);
  59. }
  60. }
  61. protected onDestroy() {
  62. WindowSystem.onWindowClose(this);
  63. }
  64. /**
  65. * 关闭窗口
  66. */
  67. public close() {
  68. if (this.closeTween) {
  69. this.closeTween.target = this.node;
  70. let onOneRepeatComplete = new EventHandler();
  71. onOneRepeatComplete.target = this.node;
  72. onOneRepeatComplete.component = "Window";
  73. onOneRepeatComplete.handler = "forceClose";
  74. this.closeTween.onOneRepeatComplete.push(onOneRepeatComplete);
  75. this.closeTween.play();
  76. }
  77. else {
  78. WindowSystem.close(this);
  79. }
  80. }
  81. /**
  82. * 强制关闭窗口,无法获得onClosing时间
  83. */
  84. public forceClose() {
  85. this.node.destroy();
  86. }
  87. }