HuaweiDemo/assets/script/framework/uiManager.ts

288 lines
8.8 KiB
TypeScript
Raw Permalink Normal View History

2023-11-07 01:17:57 +00:00
import { _decorator, Node, find, isValid} from "cc";
import { ResourceUtil } from "./resourceUtil";
import { PoolManager } from "./poolManager";
import { Tips } from "../ui/common/tips";
import { Constant } from "./constant";
import { TransitionPanel } from "../ui/fight/transition_panel";
import { TransitionBgPanel } from "../ui/fight/transition_bg_panel";
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, isTip?:boolean) {
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)) {
var parent = null;
if(isTip){
parent = find("Canvas/ui/tip");
}else{
parent = find("Canvas/ui/dislog");
}
panel.parent = parent;
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;
}
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);
}
}, isTip);
}
/**
*
* @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);
}
/**
*
* @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);
});
}
public showTransition(sceneName:string) {
ResourceUtil.getUIPrefabRes(Constant.PANEL_NAME.TRANSITION, function (err: any, prefab: any) {
if (err) {
return;
}
let transitionNode = PoolManager.instance.getNode(prefab, find('') as Node);
let transitionScript = transitionNode.getComponent(TransitionPanel) as TransitionPanel;
transitionScript.show(sceneName);
});
}
public showTransitionBg(callback:Function) {
ResourceUtil.getUIPrefabRes(Constant.PANEL_NAME.TRANSITION_BG, function (err: any, prefab: any) {
if (err) {
return;
}
let transitionNode = PoolManager.instance.getNode(prefab, find('Canvas/ui/tip') as Node);
let transitionScript = transitionNode.getComponent(TransitionBgPanel) as TransitionBgPanel;
transitionScript.show(callback);
});
}
}