UIDialog.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { _decorator, Component, Label, Node } from 'cc';
  6. const { ccclass, property } = _decorator;
  7. export interface IDialogConfig {
  8. message: string;
  9. /** 标题 */
  10. title?: string;
  11. /** 确定按钮文字 */
  12. lblConfirm?: string;
  13. /** 取消按钮文字 */
  14. lblCancel?: string;
  15. /** 隐藏确定按钮 */
  16. hideConfirm?: boolean;
  17. /** 隐藏取消按钮 */
  18. showCancel?: boolean;
  19. /** 显示右上角关闭按钮 */
  20. showClose?: boolean;
  21. /** 对话框回调 返回true则关闭dialog*/
  22. callBack?: (res: boolean) => Promise<boolean> | boolean;
  23. }
  24. @ccclass('UIDialog')
  25. export class UIDialog extends UIPopup<IDialogConfig> {
  26. readonly viewId: UIViewID = UIViewID.UIDialog;
  27. @property(Node)
  28. btnConfirm: Node;
  29. @property(Node)
  30. btnCancel: Node;
  31. @property(Node)
  32. btnClose: Node;
  33. @property(Label)
  34. lblMsg: Label;
  35. @property(Label)
  36. lblTitle: Label;
  37. @property(Label)
  38. lblConfirm: Label;
  39. @property(Label)
  40. lblCancenl: Label;
  41. private _config: IDialogConfig;
  42. protected onCreate(): void | Promise<void> {
  43. addBtnClick(this.btnConfirm, this.onBtnConfirmClick, this);
  44. addBtnClick(this.btnCancel, this.onBtnCancelClick, this);
  45. addBtnClick(this.btnClose, this.onBtnCancelClick, this);
  46. }
  47. protected onOpen(config: IDialogConfig): void | Promise<void> {
  48. this._config = config;
  49. this.lblMsg.string = config.message;
  50. this.lblTitle.string = config.title ?? AppKernel.i18n.t("dialog.tips");
  51. this.lblConfirm.string = config.lblConfirm ?? AppKernel.i18n.t("dialog.confirm");
  52. this.lblCancenl.string = config.lblCancel ?? AppKernel.i18n.t("dialog.cancel");
  53. this.btnConfirm.active = !config.hideConfirm;
  54. this.btnCancel.active = config.showCancel && !config.showClose;
  55. this.btnClose.active = config.showClose;
  56. const allBtn = this.btnConfirm.active && this.btnCancel.active;
  57. this.btnConfirm.x = allBtn ? 168 : 0;
  58. }
  59. private onBtnConfirmClick() {
  60. this.runCallback(true);
  61. }
  62. private onBtnCancelClick() {
  63. this.runCallback(false);
  64. }
  65. private async runCallback(res: boolean) {
  66. const callBack = this._config.callBack;
  67. if (!!callBack) {
  68. if (await callBack(res)) {
  69. this.closeSelf();
  70. }
  71. return;
  72. }
  73. this.closeSelf();
  74. }
  75. }