123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import { _decorator, Component, Node, SpriteComponent, Color, RichTextComponent, find, isValid, Vec3, UITransformComponent } from "cc";
- import { resourceUtil } from "./resourceUtil";
- import { poolManager } from "./poolManager";
- import { constant } from "./constant";
- import { tips } from "../ui/common/tips";
- const { ccclass, property } = _decorator;
- const SHOW_STR_INTERVAL_TIME = 800;
- @ccclass("uiManager")
- export class uiManager {
- private _dictSharedPanel: any = {}
- private _dictLoading: any = {}
- private _arrPopupDialog: any = []
- private _showTipsTime: number = 0
- private static _instance: uiManager;
- public static get instance() {
- if (this._instance) {
- return this._instance;
- }
- this._instance = new uiManager();
- return this._instance;
- }
- /**
- * 检查当前界面是否正在展示
- * @param panelPath
- */
- public isDialogVisible(panelPath: string) {
- if (!this._dictSharedPanel.hasOwnProperty(panelPath)) {
- return false;
- }
- let panel = this._dictSharedPanel[panelPath];
- return isValid(panel) && panel.active && panel.parent;
- }
- /**
- * 显示单例界面
- * @param {String} panelPath
- * @param {Array} args
- * @param {Function} cb 回调函数,创建完毕后回调
- */
- public showDialog(panelPath: string, args?: any, cb?: Function) {
- if (this._dictLoading[panelPath]) {
- return;
- }
- let idxSplit = panelPath.lastIndexOf('/');
- let scriptName = panelPath.slice(idxSplit + 1);
- if (!args) {
- args = [];
- }
- if (this._dictSharedPanel.hasOwnProperty(panelPath)) {
- let panel = this._dictSharedPanel[panelPath];
- if (isValid(panel)) {
- panel.parent = find("Canvas");
- panel.active = true;
- let script = panel.getComponent(scriptName);
- let script2 = panel.getComponent(scriptName.charAt(0).toUpperCase() + scriptName.slice(1));
- if (script && script.show) {
- script.show.apply(script, args);
- cb && cb(script);
- } else if (script2 && script2.show) {
- script2.show.apply(script2, args);
- cb && cb(script2);
- } else {
- throw `查找不到脚本文件${scriptName}`;
- }
- return;
- }
- }
- this._dictLoading[panelPath] = true;
- resourceUtil.createUI(panelPath, (err: any, node: any) => {
- //判断是否有可能在显示前已经被关掉了?
- let isCloseBeforeShow = false;
- if (!this._dictLoading[panelPath]) {
- //已经被关掉
- isCloseBeforeShow = true;
- }
- this._dictLoading[panelPath] = false;
- if (err) {
- console.error(err);
- return;
- }
- // node.getComponent(UITransformComponent).priority = constant.ZORDER.DIALOG;
- this._dictSharedPanel[panelPath] = node;
- let script: any = node.getComponent(scriptName);
- let script2: any = node.getComponent(scriptName.charAt(0).toUpperCase() + scriptName.slice(1));
- if (script && script.show) {
- script.show.apply(script, args);
- cb && cb(script);
- } else if (script2 && script2.show) {
- script2.show.apply(script2, args);
- cb && cb(script2);
- } else {
- throw `查找不到脚本文件${scriptName}`;
- }
- if (isCloseBeforeShow) {
- //如果在显示前又被关闭,则直接触发关闭掉
- this.hideDialog(panelPath);
- }
- });
- }
- /**
- * 隐藏单例界面
- * @param {String} panelPath
- * @param {fn} callback
- */
- public hideDialog(panelPath: string, callback?: Function) {
- if (this._dictSharedPanel.hasOwnProperty(panelPath)) {
- let panel = this._dictSharedPanel[panelPath];
- if (panel && isValid(panel)) {
- let ani = panel.getComponent('animationUI');
- if (ani) {
- ani.close(() => {
- panel.parent = null;
- if (callback && typeof callback === 'function') {
- callback();
- }
- });
- } else {
- panel.parent = null;
- if (callback && typeof callback === 'function') {
- callback();
- }
- }
- } else if (callback && typeof callback === 'function') {
- callback();
- }
- }
- this._dictLoading[panelPath] = false;
- }
- /**
- * 将弹窗加入弹出窗队列
- * @param {string} panelPath
- * @param {string} scriptName
- * @param {*} param
- */
- public pushToPopupSeq(panelPath: string, scriptName: string, param: any) {
- let popupDialog = {
- panelPath: panelPath,
- scriptName: scriptName,
- param: param,
- isShow: false
- };
- this._arrPopupDialog.push(popupDialog);
- this._checkPopupSeq();
- }
- /**
- * 将弹窗加入弹出窗队列
- * @param {number} index
- * @param {string} panelPath
- * @param {string} scriptName
- * @param {*} param
- */
- public insertToPopupSeq(index: number, panelPath: string, param: any) {
- let popupDialog = {
- panelPath: panelPath,
- param: param,
- isShow: false
- };
- this._arrPopupDialog.splice(index, 0, popupDialog);
- //this._checkPopupSeq();
- }
- /**
- * 将弹窗从弹出窗队列中移除
- * @param {string} panelPath
- */
- public shiftFromPopupSeq(panelPath: string) {
- this.hideDialog(panelPath, () => {
- if (this._arrPopupDialog[0] && this._arrPopupDialog[0].panelPath === panelPath) {
- this._arrPopupDialog.shift();
- this._checkPopupSeq();
- }
- })
- }
- /**
- * 检查当前是否需要弹窗
- */
- private _checkPopupSeq() {
- if (this._arrPopupDialog.length > 0) {
- let first = this._arrPopupDialog[0];
- if (!first.isShow) {
- this.showDialog(first.panelPath, first.param);
- this._arrPopupDialog[0].isShow = true;
- }
- }
- }
- /**
- * 显示提示
- * @param {String} content
- * @param {Function} cb
- */
- public showTips(content: string | number, callback?: Function) {
- let str = String(content);
- let next = () => {
- this._showTipsAni(str, callback);
- }
- var now = Date.now();
- if (now - this._showTipsTime < SHOW_STR_INTERVAL_TIME) {
- var spareTime = SHOW_STR_INTERVAL_TIME - (now - this._showTipsTime);
- setTimeout(() => {
- next();
- }, spareTime);
- this._showTipsTime = now + spareTime;
- } else {
- next();
- this._showTipsTime = now;
- }
- }
- /**
- * 内部函数
- * @param {String} content
- * @param {Function} cb
- */
- private _showTipsAni(content: string, callback?: Function) {
- resourceUtil.getUIPrefabRes('common/tips', function (err: any, prefab: any) {
- if (err) {
- return;
- }
- let tipsNode = poolManager.instance.getNode(prefab, find("Canvas") as Node);
- let tipScript = tipsNode.getComponent(tips) as tips;
- tipScript.show(content, callback);
- });
- }
- }
|