import { _decorator, Node, Camera, Component, director, game, instantiate, Prefab, RenderTexture, view, resources, macro, } from "cc"; import Utils from "../Common/Utils"; import GameRootUI from "../Common/GameRootUI"; import ResManager from "./ResManager"; import { CommonConfig } from "../Config/CommonConfig"; const { ccclass, property } = _decorator; @ccclass("ViewManager") export class ViewManager extends Component { private static _I: ViewManager = null; public static get I(): ViewManager { if (!ViewManager._I) { ViewManager._I = new ViewManager(); } return ViewManager._I; } private m_viewList = []; private m_viewListData = {}; start() {} update(deltaTime: number) {} private openIdx = 0; private openViewByPrefab( err, i_prefab: Prefab | Node, i_openInfo, callBack = null, path, type: string = "" ) { if (err) { Utils.Log("打开界面时加载资源出错:" + err, path); return; } let t_node: any = instantiate(i_prefab); if (!t_node.isValid) return; //界面已打开 for (let i = this.m_viewList.length - 1; i >= 0; i--) { let t_view: Node = this.m_viewList[i]; if (t_view) { if (t_view.isValid && t_node.name == t_view.name) { t_node.setSiblingIndex(999); Utils.Log("已经打开的相同的界面:" + t_node.name); return; } } } //截屏纹理 if (i_openInfo && i_openInfo.blueBg === true) { i_openInfo.screenShotTex = this.getScreenShot(); } let viewSP = t_node.getComponent(t_node.name); if (!viewSP) { for (let index = 0; index < t_node["_components"].length; index++) { const element = t_node["_components"][index]; if (element.isScriptComponent) { viewSP = element; break; } } } this.m_viewList.push(t_node); this.m_viewListData[t_node.name] = { path: path, data: i_openInfo, sort: this.openIdx++, name: t_node.name, uuid: t_node["_prefab"]["asset"]["_uuid"], }; viewSP && viewSP.openUIData && viewSP.openUIData(i_openInfo); callBack && callBack(t_node, i_openInfo); if (type == "") { GameRootUI.I.AddUIToLayer( t_node, CommonConfig.UILayerGroup.Layer_ui1, this.m_viewList.length ); } else if (type == "top") { // t_node.sType = "top" GameRootUI.I.AddUIToLayer(t_node, CommonConfig.UILayerGroup.Layer_top); } else if (type == "tips") { GameRootUI.I.AddUIToLayer(t_node, CommonConfig.UILayerGroup.Layer_tips); } } //检查是否有相同的View private checkSameView(i_node: Node, path) { // let t_com = i_node.getComponent('li_BaseView'); // if (t_com) { // if (t_com.name == 'li_BaseView') return true; // for (let i = this.m_viewList.length - 1; i >= 0; i--) { // let t_view: Node = this.m_viewList[i]; // if (t_view) { // if (t_view.isValid && t_com.node.name == t_view.name) { // i_node.setSiblingIndex(999) // return false; // } // } // } // i_node['path'] = path; // this.m_viewList.push(i_node); // return true; // } return false; } //获取截屏纹理 private getScreenShot(): RenderTexture { // let texture = new RenderTexture(); // let winSize = view.getViewportRect() // let winSize1 = view.getVisibleSize() // texture.reset({ // width: winSize.width, // height: winSize.height, // }); // let camera = director.getScene().getComponentInChildren(Camera); // let oldTargetTexture = camera.targetTexture // camera.targetTexture = texture; // camera.render(); // camera.targetTexture = oldTargetTexture // return texture return null; } private getViewIndex(i_view: Node) { for (let i = this.m_viewList.length - 1; i >= 0; i--) { let t_view = this.m_viewList[i]; if (t_view == i_view) { return i; } } return -1; } /**打开resources窗体 */ openMainView( i_prefab: string | Prefab, i_openData?, callBack = null, type: string = "" ) { if (i_prefab instanceof Prefab) { this.openViewByPrefab( null, i_prefab, i_openData, callBack, i_prefab, type ); } else { resources.load(i_prefab, (err, $prefab: Prefab) => { this.openViewByPrefab( err, $prefab, i_openData, callBack, i_prefab, type ); }); } } /**打开bundle窗体 */ openBundlesView( i_prefab: string | Prefab, i_openData?, callBack = null, type: string = "" ) { if (i_prefab instanceof Prefab) { this.openViewByPrefab( null, i_prefab, i_openData, callBack, i_prefab, type ); } else { console.log("打开界面:", i_prefab); ResManager.I.getPrefab( i_prefab, (pb: Node) => { this.openViewByPrefab(null, pb, i_openData, callBack, i_prefab, type); }, null ); } } /**打开bundle弹窗 */ openBundlesPopupView( i_prefab: string | Prefab, i_openData?, callBack = null ) { if (i_prefab instanceof Prefab) { this.openViewByPrefab( null, i_prefab, i_openData, callBack, i_prefab, "tips" ); } else { console.log("打开界面:", i_prefab); ResManager.I.getPrefab( i_prefab, (pb: Node) => { this.openViewByPrefab( null, pb, i_openData, callBack, i_prefab, "tips" ); }, null ); } } /**关闭窗体 */ closeView(i_view: Node) { let closeUIName = null; let t_index = this.getViewIndex(i_view); if (t_index > -1) { let t_view = this.m_viewList[t_index]; if (t_view) { closeUIName = t_view.name; delete this.m_viewListData[t_view.name]; t_view.destroy(); t_view.removeFromParent(); } this.m_viewList.splice(t_index, 1); //删除列表元素 参数1是指定位置,参数2是删除元素个数 } else { if (!this.m_viewList.length) { i_view.destroy(); } Utils.Log("试图关闭一个队列中不存在的界面:", i_view.name); } } /**关闭所有窗体 */ closeAllView(isR = false) { for (let i = this.m_viewList.length - 1; i >= 0; i--) { let t_view = this.m_viewList[i]; if (t_view) { delete this.m_viewListData[t_view.name]; t_view.destroy(); t_view.removeFromParent(); } } this.m_viewList = []; this.m_viewListData = {}; this.openIdx = 0; } //是否在主界面,没有打开任何其他界面。主界面是在打开场景时直接创建的,不走这里的打开接口 isNotOpenAnyUI() { if (this.m_viewList && this.m_viewList.length > 0) { return false; } return true; } }