import { _decorator, Component, Constructor, find, Node, Prefab, instantiate, EventHandler } from "cc"; import { ResourcesUtils } from "../../resourceManager/ResourcesUtils"; import { Log } from "../../utils/Log"; import { Utils } from "../../utils/Utils"; import { Tips } from "./Tips"; import { Window } from "./Window"; import { WindowOpenMode } from "./WindowOpenMode"; const { ccclass, property } = _decorator; /** * 窗口系统,负责管理窗口,底层运行时自动创建,无需在编辑器中绑定 * @author 袁浩 */ @ccclass('WindowSystem') export class WindowSystem extends Component { public createBySystem: boolean = false; public windowList: Window[] = []; start() { if (!this.createBySystem) { Log.warn("WindowSystem组件由底层创建,无需编辑器创建"); } } public onWindowOpen(_window: Window) { this.windowList.push(_window); } public onWindowClose(_window: Window) { Utils.removeArrayItem(this.windowList, _window); } /** * 最近打开的窗口 */ public get currentWindow(): Window { return this.windowList[this.windowList.length - 1]; } public destroyWindow(_window: Window) { _window.openWindowOnClosing && _window.openWindowOnClosing.emit([_window]); if (_window.onClosing) { _window.onClosing.emit([() => { _window.node.destroy(); Utils.removeArrayItem(this.windowList, _window); }]); } else { _window.node.destroy(); Utils.removeArrayItem(this.windowList, _window); } } private isLoadding: boolean = false; /** * 打开窗口 * @param prefabPath 预制体路径 * @param openMode 窗口打开模式 * @param params 给窗口传递的参数 * @returns */ public async open(prefabPath: string, openMode: WindowOpenMode, params: any[], onOpening: EventHandler = null, onClosing: EventHandler = null) { var _window: Window; switch (openMode) { case WindowOpenMode.NotCloseAndAdd: _window = await this.doOpen(prefabPath, params, onOpening, onClosing); break; case WindowOpenMode.NotCloseAndCover: if (this.currentWindow && prefabPath == this.currentWindow.prefabPath) { this.currentWindow.setParams(params); _window = this.currentWindow; } else { _window = await this.doOpen(prefabPath, params, onOpening, onClosing); } break; case WindowOpenMode.CloseAndAdd: this.close(); _window = await this.doOpen(prefabPath, params, onOpening, onClosing); break; case WindowOpenMode.CloseAndCover: if (this.currentWindow && prefabPath == this.currentWindow.prefabPath) { this.currentWindow.setParams(params); _window = this.currentWindow; } else { this.close(); _window = await this.doOpen(prefabPath, params, onOpening, onClosing); } break; default: break; } return _window; } private async doOpen(prefabPath: string, params: any[], onOpening: EventHandler = null, onClosing: EventHandler = null) { if (this.isLoadding) { return null; } this.isLoadding = true; var prefab = await ResourcesUtils.load(prefabPath, null, this.node); var windowNode = instantiate(prefab); var _window = windowNode.getComponent(Window) || windowNode.addComponent(Window); _window.node.parent = find("Canvas"); _window.setParams(params); _window.prefabPath = prefabPath; onClosing && (_window.openWindowOnClosing = onClosing); onOpening && onOpening.emit([_window]); this.isLoadding = false; return _window; } public async showTips(message: string, prefabPath: string = "Prefabs/Tips") { var _window = await this.open(prefabPath, WindowOpenMode.NotCloseAndAdd, [message]); if (_window) { _window.getComponent(Tips) || _window.addComponent(Tips); } } /** * 关闭窗口 * @param type 指定窗口类型,如果不传则关闭最近打开的窗口 */ public close(type: ui_window | Window | Node = null): void { if (!this.currentWindow) { return; } if (type) { var windowList = this.windowList.concat(); for (let i = 0; i < windowList.length; i++) { const _window = windowList[i]; if (type instanceof Window) { if (type == _window) { this.destroyWindow(_window); } } else if (type instanceof Node) { if (type == _window.node) { this.destroyWindow(_window); } } else if (_window instanceof type) { this.destroyWindow(_window); } } } else { var _window = this.windowList[this.windowList.length - 1]; this.destroyWindow(_window); } } /** * 关闭指定路径的窗口 * @param path 路径,例:Canvas/XXWindow */ public closeByPath(path: string): void { if (!this.currentWindow) { return; } var _windowNode = find(path); _windowNode && this.destroyWindow(_windowNode.getComponent(Window)); } /** * 关闭所有窗口 */ public closeAll() { while (this.windowList.length) { var _window = this.windowList.shift(); this.destroyWindow(_window); } } } export type ui_window = Constructor;