找物源码导读Cocos Creator 3.8 · assets/scripts
0%
已读 0 / 424
目录— 个文件
目录加载中…
…/ui/common/UICommonPopupPanel.ts
1import { UIPanelFramePath } from "../UIPanelFramePath";
2/**
3 * 通用弹窗面板
4 * 支持:标题、内容、关闭按钮、双按钮(可独立显隐+回调)
5 */
6
7import { _decorator, Label, Layout, Node, Widget } from 'cc';
8import { UIBasePanel } from "../UIBasePanel";
9import { UtilityNodeHelper } from "../../utility/UtilityNodeHelper";
10import { Framework } from "../../../Framework";
11
12const { ccclass, property } = _decorator;
13
14export interface UICommonPopupParams {
15 /** 标题文本 */
16 title?: string;
17 /** 内容/提示文本 */
18 content?: string;
19 /** 按钮1文案,不传则隐藏 */
20 btn1Text?: string;
21 /** 按钮1回调 */
22 btn1Callback?: () => void;
23 /** 按钮2文案,不传则隐藏 */
24 btn2Text?: string;
25 /** 按钮2回调 */
26 btn2Callback?: () => void;
27 /** 是否显示关闭按钮,默认true */
28 showClose?: boolean;
29 /** 关闭回调 */
30 closeCallback?: () => void;
31}
32
33@ccclass('UICommonPopupPanel')
34export class UICommonPopupPanel extends UIBasePanel {
35
36 @property({ type: Label, tooltip: '标题' })
37 private titleLabel: Label = null!;
38
39 @property({ type: Label, tooltip: '内容/提示文本' })
40 private contentLabel: Label = null!;
41
42 @property({ type: Node, tooltip: '按钮1节点' })
43 private btn1Node: Node = null!;
44
45 @property({ type: Label, tooltip: '按钮1文本' })
46 private btn1Label: Label = null!;
47
48 @property({ type: Node, tooltip: '按钮2节点' })
49 private btn2Node: Node = null!;
50
51 @property({ type: Label, tooltip: '按钮2文本' })
52 private btn2Label: Label = null!;
53
54 private _params: UICommonPopupParams = {};
55 private _closeCallback?: () => void;
56
57 public OnInit(param: UICommonPopupParams): void {
58 super.OnInit(param);
59 }
60
61 public OnOpen(param: UICommonPopupParams): void {
62 super.OnOpen(param);
63 this._params = param || {};
64 this._render();
65 }
66
67 public OnClose(param: any): void {
68 super.OnClose(param);
69 this.unschedule(this._refreshDynamicLayout);
70 this._params = {};
71 this._closeCallback = undefined;
72 }
73
74 protected RegisterBtn(): void {
75 super.RegisterBtn();
76
77 UtilityNodeHelper.AddClick(this.btn1Node, this._onBtn1Click, this);
78 UtilityNodeHelper.AddClick(this.btn2Node, this._onBtn2Click, this);
79 }
80
81 private _render(): void {
82 const p = this._params;
83
84 this.titleLabel.string = p.title || '';
85 this.contentLabel.string = p.content || '';
86
87 this._setupBtn(this.btn1Node, this.btn1Label, p.btn1Text, p.btn1Callback);
88 this._setupBtn(this.btn2Node, this.btn2Label, p.btn2Text, p.btn2Callback);
89
90 this.closeBtn.active = p.showClose !== false;
91 this._closeCallback = p.closeCallback;
92
93 // 文本和按钮显隐会改变两层 Layout 的尺寸,下一帧按依赖顺序强制重算,
94 // 避免 Close 的 Widget 仍使用预制体默认尺寸而偏出弹窗。
95 this.unschedule(this._refreshDynamicLayout);
96 this.scheduleOnce(this._refreshDynamicLayout, 0);
97 }
98
99 private _refreshDynamicLayout(): void {
100 if (!this.node?.isValid) return;
101
102 // 动态文案和按钮显隐会改变节点尺寸,需按“按钮组 → 背景 → Widget 对齐”的依赖顺序刷新。
103 const buttonGroup = this.btn1Node.parent;
104 buttonGroup?.getComponent(Layout)?.updateLayout();
105
106 const background = this.contentLabel.node.parent;
107 background?.getComponent(Layout)?.updateLayout();
108
109 const alignRoot = this.closeBtn.parent;
110 alignRoot?.getComponent(Widget)?.updateAlignment();
111 this.titleLabel.node.getComponent(Widget)?.updateAlignment();
112 this.closeBtn.getComponent(Widget)?.updateAlignment();
113 }
114
115 private _setupBtn(node: Node, label: Label, text?: string, callback?: () => void): void {
116 if (!text) {
117 node.active = false;
118 return;
119 }
120 node.active = true;
121 label.string = text;
122 (node as any).__popupCallback = callback;
123 }
124
125 private _onBtn1Click(): void {
126 const cb = (this.btn1Node as any).__popupCallback as (() => void) | undefined;
127 if (cb) {
128 try { cb(); } catch (e) { console.error('[UICommonPopupPanel] btn1 callback error:', e); }
129 }
130 this._closeSelf();
131 }
132
133 private _onBtn2Click(): void {
134 const cb = (this.btn2Node as any).__popupCallback as (() => void) | undefined;
135 if (cb) {
136 try { cb(); } catch (e) { console.error('[UICommonPopupPanel] btn2 callback error:', e); }
137 }
138 this._closeSelf();
139 }
140
141 OnCloseClick(): void {
142 if (this._closeCallback) {
143 try { this._closeCallback(); } catch (e) { console.error('[UICommonPopupPanel] close callback error:', e); }
144 }
145 this._closeSelf();
146 }
147
148 private _closeSelf(): void {
149 Framework.UI.ClosePanel(UIPanelFramePath.UICommonPopupPanel);
150 }
151}
讲解152 行
通用双按钮弹窗面板人工通读

UICommonPopupPanel.ts

继承 UIBasePanel 的通用弹窗,按参数渲染标题、正文、最多两个按钮和关闭按钮,文案为空时隐藏对应按钮。点击按钮先执行回调再关闭自身,并在下一帧按依赖顺序重算 Layout 与 Widget,避免动态文案撑破布局。

先看 UICommonPopupParams 了解入参,再按 OnOpen、_render、_refreshDynamicLayout、两个点击处理的顺序阅读。

特殊处理与易错点

7注释通用弹窗面板 支持:标题、内容、关闭按钮、双按钮(可独立显隐+回调)
16注释标题文本
18注释内容/提示文本
20注释按钮1文案,不传则隐藏
22注释按钮1回调
24注释按钮2文案,不传则隐藏
26注释按钮2回调
28注释是否显示关闭按钮,默认true
30注释关闭回调
34注释UICommonPopupPanel:UICommonPopupPanel 是「通用 UI 组件」目录下的UI 面板/组件。负责界
37属性预制体绑定的标题 Label,_render 时写入 title,缺省写空串。
40属性正文 Label,同时通过它的父节点找到背景 Layout 参与重算。
43属性第一个按钮的节点,无文案时被置为 inactive。
46属性第一个按钮上的文本组件。
49属性第二个按钮的节点,无文案时被置为 inactive。
52属性第二个按钮上的文本组件。
54属性本次打开传入的弹窗参数引用,OnClose 时重置为空对象。
57方法接收弹窗参数后只转调基类 OnInit 触发按钮注册,本身不读取参数内容。
61方法先调用基类 OnOpen 完成打开流程,再保存参数引用并执行 _render 渲染界面。
67方法先走基类关闭流程,然后取消待执行的布局刷新并清空参数与关闭回调。
74方法在基类注册关闭按钮之后,给按钮一和按钮二节点各绑定一次点击处理。
81方法把参数写入标题和正文,按文案有无显示或隐藏两个按钮,并安排下一帧刷新布局。
95注释文本和按钮显隐会改变两层 Layout 的尺寸,下一帧按依赖顺序强制重算, 避免 Close 的 Widget 仍使用预制体默认尺寸而偏出弹窗。
99方法节点有效时依次刷新按钮组 Layout、背景 Layout、外层 Widget 以及标题和关闭按钮的 Wid…
103注释动态文案和按钮显隐会改变节点尺寸,需按“按钮组 → 背景 → Widget 对齐”的依赖顺序刷新。
115方法文案为空时隐藏按钮节点,否则显示、写入文本并把回调挂到节点的 __popupCallback 上。
125方法从按钮一节点取出挂载的回调并用 try-catch 执行,随后关闭弹窗。
133方法从按钮二节点取出挂载的回调并用 try-catch 执行,随后关闭弹窗。
141方法执行打开时传入的关闭回调,捕获异常后关闭面板。
148方法用 UIPanelFramePath 常量调用 Framework.UI.ClosePanel 关闭本弹窗。