import { AppKernel } from '@scripts/app/AppKernel'; import { UIViewID } from '@scripts/define/UIDefine'; import { UIPopup } from '@scripts/framework/ui/UIPopup'; import { addBtnClick } from '@scripts/utils/UIHelper'; import { _decorator, Component, Label, Node } from 'cc'; const { ccclass, property } = _decorator; export interface IDialogConfig { message: string; /** 标题 */ title?: string; /** 确定按钮文字 */ lblConfirm?: string; /** 取消按钮文字 */ lblCancel?: string; /** 隐藏确定按钮 */ hideConfirm?: boolean; /** 隐藏取消按钮 */ showCancel?: boolean; /** 显示右上角关闭按钮 */ showClose?: boolean; /** 对话框回调 返回true则关闭dialog*/ callBack?: (res: boolean) => Promise | boolean; } @ccclass('UIDialog') export class UIDialog extends UIPopup { readonly viewId: UIViewID = UIViewID.UIDialog; @property(Node) btnConfirm: Node; @property(Node) btnCancel: Node; @property(Node) btnClose: Node; @property(Label) lblMsg: Label; @property(Label) lblTitle: Label; @property(Label) lblConfirm: Label; @property(Label) lblCancenl: Label; private _config: IDialogConfig; protected onCreate(): void | Promise { addBtnClick(this.btnConfirm, this.onBtnConfirmClick, this); addBtnClick(this.btnCancel, this.onBtnCancelClick, this); addBtnClick(this.btnClose, this.onBtnCancelClick, this); } protected onOpen(config: IDialogConfig): void | Promise { this._config = config; this.lblMsg.string = config.message; this.lblTitle.string = config.title ?? AppKernel.i18n.t("dialog.tips"); this.lblConfirm.string = config.lblConfirm ?? AppKernel.i18n.t("dialog.confirm"); this.lblCancenl.string = config.lblCancel ?? AppKernel.i18n.t("dialog.cancel"); this.btnConfirm.active = !config.hideConfirm; this.btnCancel.active = config.showCancel && !config.showClose; this.btnClose.active = config.showClose; const allBtn = this.btnConfirm.active && this.btnCancel.active; this.btnConfirm.x = allBtn ? 168 : 0; } private onBtnConfirmClick() { this.runCallback(true); } private onBtnCancelClick() { this.runCallback(false); } private async runCallback(res: boolean) { const callBack = this._config.callBack; if (!!callBack) { if (await callBack(res)) { this.closeSelf(); } return; } this.closeSelf(); } }