123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import { ViewZorder } from "../constant/ViewZOrder";
- import UIBase, { UIClass } from "./UIBase";
- import gameEventManager from "../../gameLogic/utrl/gameEventManager";
- import {EVENT_TYPE} from "../../gameLogic/utrl/gameEnum";
- import EventManager from "../event/EventManager";
- import UIAirdropView from "../../ui/uiview/Interface/UIAirdropView";
- import {Log, LOG_TAG} from "../log/Log";
- const needCheckLockUI = ['UISignView','UIShopView','UIAirdropView','UICrazyAwardView','UIWelfareView']
- export default class UIMng {
- private static instance: UIMng;
- private uiList: UIBase[] = [];
- public static getInstance(): UIMng {
- if (this.instance == null) {
- this.instance = new UIMng();
- }
- return this.instance;
- }
- UILock = false;
- private constructor() {
- }
- setLock(flag :boolean){
- if (!flag){
- EventManager.emit(EVENT_TYPE.unLockUI)
- }
- this.UILock = flag;
- }
- /**
- * 打开UI
- * @param uiClass
- * @param zOrder
- * @param callback 打开完毕回调函数
- * @param onProgress 打开过程进度函数
- * @param args 传入到UI的参数
- */
- public openUI<T extends UIBase>(uiClass: UIClass<T>, zOrder: number = ViewZorder.UI, callback?: Function, onProgress?: Function, ...args: any[]) {
- if (needCheckLockUI.includes(uiClass.getName())) {
- //特殊处理,非强制弹窗ui检查lock ,未来可优化写法
- if (this.UILock) {
- Log.log(LOG_TAG.DEBUG,`锁未被释放:${uiClass.getName()}`)
- gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
- return;
- }
- }
- if (this.getUI(uiClass)) {
- console.error(`UIMng OpenUI 1: ui ${uiClass.getName()} is already exist, please check`);
- return;
- }
- cc.resources.load(uiClass.getUrl(), (completedCount: number, totalCount: number, item: any) => {
- onProgress && onProgress(completedCount, totalCount, item);
- }, (error: Error, prefab: cc.Prefab) => {
- if (error) {
- console.error(`UIMng OpenUI: load ui error: ${error}`);
- return;
- }
- if (this.getUI(uiClass)) {
- console.error(`UIMng OpenUI 2: ui ${uiClass.getName()} is already exist, please check`);
- return;
- }
- let uiNode: cc.Node = cc.instantiate(prefab);
- let ui = uiNode.getComponent(uiClass) as UIBase;
- if (!ui) {
- console.error(`${uiClass.getUrl()}没有绑定UI脚本!!!`);
- return;
- }
- ui.init(args);
- // let uiRoot = cc.director.getScene().getChildByName('UIRoot');
- let uiRoot = cc.director.getScene().getChildByName('Canvas');;
- if (!uiRoot) {
- console.error(`当前场景${cc.director.getScene().name}Canvas!!!`);
- return;
- }
- uiNode.parent = uiRoot;
- uiNode.zIndex = zOrder;
- this.uiList.push(ui);
- ui.tag = uiClass;
- callback && callback(ui);
- });
- }
- /**
- * 清除依赖资源
- * @param prefabUrl
- */
- private clearDependsRes(prefabUrl) {
- let deps = cc.loader.getDependsRecursively(prefabUrl);
- // console.log(`UIMng clearDependsRes: release ${prefabUrl} depends resources `, deps);
- deps.forEach((item) => {
- // todo:排除公共资源,然后清理
- // if (item.indexOf('common') === -1) {
- // cc.loader.release(item);
- // }
- });
- }
- public closeUI<T extends UIBase>(uiClass: UIClass<T>) {
- for (let i = 0; i < this.uiList.length; ++i) {
- if (this.uiList[i].tag === uiClass) {
- if (cc.isValid(this.uiList[i].node)) {
- this.uiList[i].node.destroy();
- this.clearDependsRes(uiClass.getUrl());
- }
- this.uiList.splice(i, 1);
- return;
- }
- }
- }
- public closeAllUI() {
- if (this.uiList.length == 0) {
- return;
- }
- this.closeUI(this.uiList[0].tag);
- while (this.uiList.length > 0) {
- this.closeUI(this.uiList[0].tag);
- }
- }
- public showUI<T extends UIBase>(uiClass: UIClass<T>, callback?: Function) {
- let ui = this.getUI(uiClass);
- if (!ui) {
- console.error(`UIMng showUI: ui ${uiClass.getName()} not exist`);
- return;
- }
- ui.node.active = true;
- }
- public hideUI<T extends UIBase>(uiClass: UIClass<T>) {
- let ui = this.getUI(uiClass);
- if (ui) {
- ui.node.active = false;
- }
- }
- public getUI<T extends UIBase>(uiClass: UIClass<T>): UIBase {
- for (let i = 0; i < this.uiList.length; ++i) {
- if (this.uiList[i].tag === uiClass) {
- return this.uiList[i];
- }
- }
- return null;
- }
- public isShowing<T extends UIBase>(uiClass: UIClass<T>) {
- let ui = this.getUI(uiClass);
- if (!ui) {
- return false;
- }
- return ui.node.active;
- }
- }
|