Files
Max-Cocos-Demo/assets/games/scripts/uis/CommonDialogBox.ts
2025-10-28 21:55:41 +08:00

154 lines
3.9 KiB
TypeScript

import BasePopup from "@max-studio/core/ui/BasePopup";
import { uiConfig, UIType } from "@max-studio/core/ui/UIDecorator";
import UIManager from "@max-studio/core/ui/UIManager";
import { IDialogBoxOptions } from "assets/scripts/DialogBox";
import { Vec3 } from "cc";
import { Button } from "cc";
import { tween } from "cc";
import { Label } from "cc";
import { _decorator, Node } from "cc";
const { ccclass, property } = _decorator;
/**
* 基础对话框组件
*/
@uiConfig({
bundle: "games",
prefab: "prefabs/uis/CommonDialogBox",
isMulti: true,
isCache: true,
type: UIType.POPUP,
})
@ccclass("CommonDialogBox")
export class CommonDialogBox extends BasePopup {
@property(Label)
titleLabel: Label = null!;
@property(Node)
contentRoot: Node = null!;
@property(Label)
contentLabel: Label = null!;
@property(Button)
confirmBtn: Button = null!;
@property(Label)
confirmBtnLabel: Label = null!;
@property(Button)
cancelBtn: Button = null!;
@property(Label)
cancelBtnLabel: Label = null!;
@property(Button)
closeBtn: Button = null!;
// 回调函数
private onConfirmCallback: (() => void) | null = null;
private onCancelCallback: (() => void) | null = null;
protected onLoad(): void {
// 绑定按钮事件
if (this.confirmBtn) {
this.confirmBtn.node.on(Button.EventType.CLICK, this.onConfirmClick, this);
}
if (this.cancelBtn) {
this.cancelBtn.node.on(Button.EventType.CLICK, this.onCancelClick, this);
}
if (this.closeBtn) {
this.closeBtn.node.on(Button.EventType.CLICK, this.onCloseClick, this);
}
}
/**
* 确定按钮点击事件
*/
private onConfirmClick(): void {
// 执行回调
if (this.onConfirmCallback) {
this.onConfirmCallback();
}
// 隐藏对话框
void UIManager.getInstance().closeUI(this);
}
async onShow(options: IDialogBoxOptions = {}) {
this.contentRoot.scale = Vec3.ZERO;
tween(this.contentRoot).to(0.3, { scale: Vec3.ONE }, { easing: "backOut" }).start();
const {
title = "提示",
content = "",
onConfirm,
onCancel,
confirmText = "确定",
cancelText = "取消",
showCancel = true,
hideClose = false,
} = options;
// 设置标题和内容
if (this.titleLabel) {
this.titleLabel.string = title;
}
if (this.contentLabel) {
this.contentLabel.string = content;
}
// 设置按钮文本
if (this.confirmBtnLabel) {
this.confirmBtnLabel.string = confirmText;
}
if (this.cancelBtnLabel) {
this.cancelBtnLabel.string = cancelText;
}
// 控制取消按钮显示状态
if (this.cancelBtn) {
this.cancelBtn.node.active = showCancel;
}
if (this.closeBtn) {
this.closeBtn.node.active = !hideClose;
}
// 保存回调函数
this.onConfirmCallback = onConfirm || null;
this.onCancelCallback = onCancel || null;
}
/**
* 取消按钮点击事件
*/
private onCancelClick(): void {
// 执行回调
if (this.onCancelCallback) {
this.onCancelCallback();
}
// 隐藏对话框
void UIManager.getInstance().closeUI(this);
}
private onCloseClick(): void {
// 隐藏对话框
void UIManager.getInstance().closeUI(this);
}
async onHide(): Promise<void> {
return new Promise((resolve) => {
tween(this.contentRoot)
.to(0.2, { scale: new Vec3(0.8, 0.8, 0.8) }, { easing: "quadIn" })
.call(() => {
resolve();
})
.start();
});
}
}