import { _decorator, Component, CCString, find, Node, Enum, Prefab, instantiate, EventHandler, SystemEventType } from "cc"; import { ResourcesUtils } from "../../resourceManager/ResourcesUtils"; import { Window } from "./Window"; import { WindowOpenMode } from "./WindowOpenMode"; import { WindowSystem } from "./WindowSystem"; const { ccclass, property } = _decorator; /** * 打开窗口组件,定义窗口的参数、窗口的打开等 * @author 袁浩 */ @ccclass('OpenWindow') export class OpenWindow extends Component { @property({ tooltip: "要加载的预制体对象相对于resources的路径" }) public prefabPath: string = ''; @property({ tooltip: "窗口打开的模式", type: Enum(WindowOpenMode) }) public openMode: WindowOpenMode = WindowOpenMode.NotCloseAndAdd; @property({ tooltip: "自定义参数" }) public params: string[] = []; @property({ tooltip: "窗口打开触发的节点,不填则无触发", type: [Node] }) public openFroms: Node[] = []; @property({ tooltip: "窗口打开中的回调", type: EventHandler }) public onOpening: EventHandler; @property({ tooltip: "窗口关闭中的回调", type: EventHandler }) public onClosing: EventHandler; protected start() { this.checkOpenFroms(); } public checkOpenFroms() { for (let i = 0; i < this.openFroms.length; i++) { const node = this.openFroms[i]; node.on(SystemEventType.TOUCH_END, () => { this.open() }, this); } } private get windowSystem(): WindowSystem { var windowSystemNode = find("Canvas/WindowSystem"); if (!windowSystemNode) {//如果没有窗口系统节点则创建一个 var windowSystemNode = new Node("WindowSystem"); windowSystem = windowSystemNode.addComponent(WindowSystem); } var windowSystem = windowSystemNode.getComponent(WindowSystem) || windowSystemNode.addComponent(WindowSystem); return windowSystem; } /** * 打开窗口 * @param params 给窗口传递的参数,如果有值,那么此参数始终跟随在自定义参数后面 */ public async open(...params) { var _window = await this.windowSystem.open(this.prefabPath, this.openMode, params, this.onOpening, this.onClosing); return _window; } }