| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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;
- /** 隐藏取消按钮 */
- hideCancel?: boolean;
- /** 显示右上角关闭按钮 */
- showClose?: boolean;
- /** 对话框回调 */
- callBack?: (res: boolean) => Promise<boolean>;
- }
- @ccclass('UIDialog')
- export class UIDialog extends UIPopup<IDialogConfig> {
- 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<void> {
- addBtnClick(this.btnConfirm, this.onBtnConfirmClick, this);
- addBtnClick(this.btnCancel, this.onBtnCancelClick, this);
- addBtnClick(this.btnClose, this.onBtnCancelClick, this);
- }
- protected onOpen(config: IDialogConfig): void | Promise<void> {
- 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.hideCancel && !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();
- }
- }
|