123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- import { AudioConst } from "../../data/const/TypeConst";
- import GameDataCenter from "../../data/GameDataCenter";
- import EventMng from "../../manager/EventMng";
- import { uiCommon } from "../../utils/UICommon";
- export interface UIClass<T extends UIBase> {
- new(): T;
- getUrl(): string;
- getName(): string;
- getPop(): BASE_POP;
- getZindex(): number;
- /** 预加载页面所需资源 */
- onCollectRes(): void;
- }
- // 弹出方式
- export enum BASE_POP {
- MAIN = "MAIN",//主场景
- UI = "UI", //除了MAIN以外的其他非弹窗UI
- UI_HALF = "UI_HALF",// 与UI类似,但不展示底部按钮,只显示顶部资源栏
- FULL = "FULL",// 盖住菜单界面的窗口
- POP = "POP", // 弹窗
- MemuBottom = "MemuBottom", // 主界面的底部菜单栏ui
- Battle = "Battle" // 战斗弹窗
- }
- interface RegisterEvent {
- callback: Function,
- target?: any,
- playAudio?: boolean,
- }
- const PREFAB_UI_DIR = 'prefab/';
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default abstract class UIBase extends cc.Component {
- protected static prefabUrl;
- protected static className;
- static onCollectRes() {
- }
- protected mTag: any;
- public get tag(): any {
- return this.mTag;
- }
- public set tag(value: any) {
- this.mTag = value;
- }
- /**
- * 得到prefab的路径,相对于resources目录
- */
- public static getUrl(): string {
- return this.prefabUrl;
- //return PREFAB_UI_DIR + this.prefabUrl;
- }
- /**
- * 类名,用于给UI命名
- */
- public static getName(): string {
- return this.className;
- }
- protected static pop: BASE_POP = BASE_POP.UI
- public static getPop(): BASE_POP {
- return this.pop
- }
- protected static _zindex: number
- public static getZindex(): number {
- return this._zindex
- }
- public belong: any = null
- public showAnim: boolean = true
- public bgm: AudioConst = AudioConst.empty
- /** 是否注册防穿透事件 */
- protected isRegisterStopEvent: boolean = true;
- /**通知事件列表 */
- private _notifyEventList: Map<string, Function>;
- /**点击事件列表 */
- private _registerEventList: Map<string, RegisterEvent>;
- /* ----------------------------- 以下方法不能在子类重写 ----------------------------- */
- /**初始化函数,在onLoad之前被调用,params为打开ui是传入的不定参数数组 */
- init(params) {
- this.onInit(params);
- }
- /**onLoad 会在组件被首次加载的时候被回调。且优先于任何start */
- onLoad() {
- this._notifyEventList = new Map<string, Function>();
- // this._registerEventList = new Map<string, RegisterEvent>();
- this.onUILoad();
- }
- onDestroy() {
- this.onUIDestroy();
- }
- onEnable() {
- // if (this.isRegisterStopEvent) {
- // this.onRegisterEvent(this.node, this.touchEvent, this);
- // }
- if (this.bgm != AudioConst.empty) {
- GameDataCenter.audio.playMusic(this.bgm)
- }
- this.onShow();
- }
- onDisable() {
- if (this.isRegisterStopEvent) {
- this.unRegisterEvent(this.node);
- }
- this.onHide();
- try {
- let self = this;
- this._notifyEventList.forEach((f, key) => {
- // console.log("_notifyEventList key = "+key)
- EventMng.off(key, f, self);
- }, this);
- this._notifyEventList.clear();
- } catch (error) {
- console.error("onDisable->" + error)
- }
- if (this.registerCount > 0) {
- console.error(this.name + " 按钮注册后没off掉啊喂 \n(╯-_-)╯╧╧ ")
- }
- }
- /**注册notice事件,disable的时候会自动移除 */
- initEvent(eventName: string, cb: Function) {
- EventMng.on(eventName, cb, this);
- this._notifyEventList.set(eventName, cb);
- }
- private touchEvent(event) {
- event.stopPropagation();
- }
- start() {
- // this.onStart(); // 改在UiMng中,在init和添加到父节点之后触发
- }
- update(dt) {
- this.onUpdate(dt);
- }
- /* ---------------------------------------------------------------------------------- */
- onInit(params) {
- }
- onUILoad() {
- }
- onUIDestroy() {
- }
- onShow() {
- }
- onHide() {
- }
- onStart(isFirstTime: boolean) {
- }
- onUpdate(dt: number) {
- }
- onClose() {
- }
- registerCount: number = 0
- /**
- * 注册touch事件
- * @param node
- * @param callback
- * @param target
- * @param playAudio 是否播放音效,默认播放
- */
- onRegisterEvent(node: cc.Node, callback, target = null, params: any = [], audio: AudioConst = AudioConst.effect_click) {
- this.registerCount++
- uiCommon.onRegisterEvent(node, callback, target, params, audio)
- }
- unRegisterEvent(node: cc.Node) {
- this.registerCount--
- uiCommon.unRegisterEvent(node)
- }
- }
|