marineparkclient/assets/common/ui/base/UIBase.ts
2023-08-15 11:09:12 +08:00

157 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIBinder from "./UIBinder";
import uiHelper from "./UIHelper";
import UIManager from "../UIManager";
import { PanelType, SysDefine } from "../config/SysDefine";
import Binder from "./Binder";
import AdapterManager from "../AdapterManager";
import TipsManager from "../TipsManager";
import { MaskType } from "../MaskType";
import ResManager from "../../res/ResManager";
const {ccclass, property} = cc._decorator;
@ccclass
export default class UIBase extends UIBinder {
/** 窗体id,该窗体的唯一标示(请不要对这个值进行赋值操作, 内部已经实现了对应的赋值) */
public uid: string;
/** 窗体类型 */
public panelType: PanelType = PanelType.Screen;
/** 阴影类型, 只对PopUp类型窗体启用 */
public maskType = new MaskType();
/** 关闭窗口后销毁, 会将其依赖的资源一并销毁, 采用了引用计数的管理, 不用担心会影响其他窗体 */
public canDestory = false;
/** 自动绑定结点 */
public autoBind = true;
/** 回调 */
protected _cb: (confirm: any) => void;
/** 是否已经调用过preinit方法 */
private _inited = false;
private _vecChildPerfab:Array<cc.Node> = []; //动态加载的子节点
onLoad(){
}
/** 资源路径,如果没写的话就是类名 */
public static _prefabPath = "";
public static set prefabPath(path: string) {
this._prefabPath = path;
}
public static get prefabPath() {
if(!this._prefabPath || this._prefabPath.length <= 0) {
this._prefabPath = SysDefine.UI_PATH_ROOT + uiHelper.getComponentName(this);
console.log("component name:", uiHelper.getComponentName(this))
}
return this._prefabPath;
}
/** 打开关闭UIBase */
public static async openView(...parmas: any): Promise<UIBase> {
return await UIManager.getInstance().openUIPanel(this.prefabPath, ...parmas);
}
public static async openViewWithLoading(...parmas: any): Promise<UIBase> {
await TipsManager.inst.showLoadingPanel(this.prefabPath);
let uiBase = await this.openView(...parmas);
await TipsManager.inst.hideLoadingPanel();
return uiBase;
}
public static async closeView(): Promise<boolean> {
return await UIManager.getInstance().closeUIPanel(this.prefabPath);
}
/** 预先初始化 */
public async _preInit() {
if(this._inited) return ;
this._inited = true;
if(this.autoBind) {
Binder.bindComponent(this);
}
// 加载这个UI依赖的其他资源其他资源可以也是UI
await this.load();
}
/** 可以在这里进行一些资源的加载, 具体实现可以看test下的代码 */
public async load() {}
public onShow(...obj: any) {}
public onHide() {}
//清空对象
public onRemove(){
for(let idx=0;idx<this._vecChildPerfab.length;++idx){
let com = this._vecChildPerfab[idx].getComponent(UIBase)
ResManager.inst.destoryPrefab(com)
}
}
/** 通过闭包保留resolve.在合适的时间调用cb方法 */
public waitPromise(): Promise<any> {
return new Promise((resolve, reject) => {
this._cb = (confirm: any) => {
resolve(confirm);
}
});
}
//关闭自己
public async closeUIPanel(): Promise<boolean> {
return await UIManager.getInstance().closeUIPanel(this.uid);
}
/**
*
* @param perfabName 预制体名字
* @param parent 当前窗体的节点
*/
public async addChildPerfab(perfabName: string,parent:cc.Node) {
let nodePb = await ResManager.inst.loadPrefab(perfabName)
if(nodePb)
{
parent.addChild(nodePb);
this._vecChildPerfab.push(nodePb)
}
return nodePb
}
//需要手动删除的时候,可以调用,非必须
public async delChildPerfab(child:cc.Node) {
let index = this._vecChildPerfab.indexOf(child);
if(index > -1) {
let com = child.getComponent(UIBase)
ResManager.inst.destoryPrefab(com)
this._vecChildPerfab.splice(index,1);
}
}
/**
* 弹窗动画ui
*/
public async showAnimation() {
if(this.panelType === PanelType.PopUp) {
this.node.scale = 0;
await uiHelper.runTweenAsync(this.node, cc.tween().to(0.3, {scale: 1}, cc.easeBackOut()));
}
}
public async hideAnimation() {
if(this.panelType === PanelType.PopUp) {
this.node.scale = 1;
await uiHelper.runTweenAsync(this.node, cc.tween().to(0.3, {scale: 0}, cc.easeBackOut()));
}
}
/** 设置是否挡住触摸事件 */
private _blocker: cc.BlockInputEvents = null;
public setBlockInput(block: boolean) {
if(block && !this._blocker) {
let node = new cc.Node('block_input_events');
this._blocker = node.addComponent(cc.BlockInputEvents);
this._blocker.node.setContentSize(AdapterManager.inst.visibleSize);
this.node.addChild(this._blocker.node, cc.macro.MAX_ZINDEX);
}else if(!block && this._blocker) {
this._blocker.node.destroy();
this._blocker.node.removeFromParent();
this._blocker = null;
}
}
}