feat: 支持通用弹窗以及下一题二次确认
This commit is contained in:
@@ -40,7 +40,7 @@ Git 历史采用 Conventional Commits,且摘要多为中文,例如 `feat:
|
||||
<claude-mem-context>
|
||||
# Memory Context
|
||||
|
||||
# $CMEM mp-xieyingeng 2026-05-10 10:19pm GMT+8
|
||||
# $CMEM mp-xieyingeng 2026-05-11 9:03am GMT+8
|
||||
|
||||
Legend: 🎯session 🔴bugfix 🟣feature 🔄refactor ✅change 🔵discovery ⚖️decision
|
||||
Format: ID TIME TYPE TITLE
|
||||
|
||||
1645
assets/prefabs/CommonModal.prefab
Normal file
1645
assets/prefabs/CommonModal.prefab
Normal file
File diff suppressed because it is too large
Load Diff
13
assets/prefabs/CommonModal.prefab.meta
Normal file
13
assets/prefabs/CommonModal.prefab.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "prefab",
|
||||
"imported": true,
|
||||
"uuid": "5379669e-7cd7-45b6-9dd3-4a021730b23e",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"syncNodeName": "CommonModal"
|
||||
}
|
||||
}
|
||||
280
assets/prefabs/CommonModal.ts
Normal file
280
assets/prefabs/CommonModal.ts
Normal file
@@ -0,0 +1,280 @@
|
||||
import { _decorator, error, instantiate, Label, Node, Prefab, Size, UITransform, Vec3, view } from 'cc';
|
||||
import { BaseModal } from 'db://assets/scripts/core/BaseModal';
|
||||
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
export type CommonModalAction = () => void | boolean;
|
||||
|
||||
export interface CommonModalCallbacks {
|
||||
/** 点击关闭按钮回调,返回 false 可阻止默认关闭 */
|
||||
onClose?: CommonModalAction;
|
||||
/** 点击主按钮回调,返回 false 可阻止默认关闭 */
|
||||
onConfirm?: CommonModalAction;
|
||||
}
|
||||
|
||||
export interface CommonModalParams extends CommonModalCallbacks {
|
||||
/** 弹窗标题 */
|
||||
title?: string;
|
||||
/** 弹窗内容 */
|
||||
content?: string;
|
||||
/** 主按钮文案 */
|
||||
buttonHint?: string;
|
||||
/** 主按钮文案别名,方便业务侧按 buttonText 调用 */
|
||||
buttonText?: string;
|
||||
/** 点击关闭按钮后是否自动关闭弹窗,默认 true */
|
||||
closeOnClose?: boolean;
|
||||
/** 点击主按钮后是否自动关闭弹窗,默认 true */
|
||||
closeOnConfirm?: boolean;
|
||||
/** 关闭时是否销毁节点,默认 true */
|
||||
destroyOnClose?: boolean;
|
||||
/** 弹窗层级,默认 CommonModal.MODAL_Z_INDEX */
|
||||
zIndex?: number;
|
||||
}
|
||||
|
||||
@ccclass('CommonModal')
|
||||
export class CommonModal extends BaseModal {
|
||||
public static readonly MODAL_Z_INDEX = 999;
|
||||
|
||||
private static readonly DEFAULT_TITLE = '温馨提示';
|
||||
private static readonly DEFAULT_CONTENT = '';
|
||||
private static readonly DEFAULT_BUTTON_HINT = '确定';
|
||||
|
||||
@property({ type: Label, tooltip: '标题文本' })
|
||||
titleLabel: Label | null = null;
|
||||
|
||||
@property({ type: Label, tooltip: '内容文本' })
|
||||
contentLabel: Label | null = null;
|
||||
|
||||
@property({ type: Label, tooltip: '主按钮文本' })
|
||||
buttonHintLabel: Label | null = null;
|
||||
|
||||
@property({ type: Node, tooltip: '关闭按钮节点' })
|
||||
closeBtn: Node | null = null;
|
||||
|
||||
@property({ type: Node, tooltip: '主按钮节点' })
|
||||
buttonHint: Node | null = null;
|
||||
|
||||
private _title: string = CommonModal.DEFAULT_TITLE;
|
||||
private _content: string = CommonModal.DEFAULT_CONTENT;
|
||||
private _buttonHintText: string = CommonModal.DEFAULT_BUTTON_HINT;
|
||||
private _callbacks: CommonModalCallbacks = {};
|
||||
private _closeOnClose: boolean = true;
|
||||
private _closeOnConfirm: boolean = true;
|
||||
private _destroyOnClose: boolean = true;
|
||||
private _screenSize: Size | null = null;
|
||||
private _loaded: boolean = false;
|
||||
|
||||
/**
|
||||
* 直接弹出通用弹窗。
|
||||
*
|
||||
* 使用示例:
|
||||
* CommonModal.show(this.commonModalPrefab, {
|
||||
* title: '提示',
|
||||
* content: '是否继续?',
|
||||
* buttonHint: '继续',
|
||||
* onConfirm: () => this.startGame()
|
||||
* });
|
||||
*/
|
||||
static show(prefab: Prefab, params: CommonModalParams = {}, parent?: Node): CommonModal | null {
|
||||
const container = parent ?? ViewManager.instance.getContainer();
|
||||
if (!container) {
|
||||
error('[CommonModal] 缺少弹窗挂载节点');
|
||||
return null;
|
||||
}
|
||||
|
||||
const modalNode = instantiate(prefab);
|
||||
const modal = modalNode.getComponent(CommonModal);
|
||||
if (!modal) {
|
||||
error('[CommonModal] 预制体缺少 CommonModal 组件');
|
||||
modalNode.destroy();
|
||||
return null;
|
||||
}
|
||||
|
||||
modal.setParams(params);
|
||||
modalNode.setPosition(Vec3.ZERO);
|
||||
modalNode.setSiblingIndex(params.zIndex ?? CommonModal.MODAL_Z_INDEX);
|
||||
container.addChild(modalNode);
|
||||
modal.onViewLoad();
|
||||
modal._doShow();
|
||||
|
||||
return modal;
|
||||
}
|
||||
|
||||
protected start(): void {
|
||||
this.onViewLoad();
|
||||
|
||||
if (!this.isShowing) {
|
||||
this._doShow();
|
||||
}
|
||||
}
|
||||
|
||||
setParams(params: CommonModalParams = {}): void {
|
||||
super.setParams(params);
|
||||
this.setConfig(params);
|
||||
}
|
||||
|
||||
setConfig(params: CommonModalParams = {}): void {
|
||||
this._title = params.title ?? CommonModal.DEFAULT_TITLE;
|
||||
this._content = params.content ?? CommonModal.DEFAULT_CONTENT;
|
||||
this._buttonHintText = params.buttonHint ?? params.buttonText ?? CommonModal.DEFAULT_BUTTON_HINT;
|
||||
this._callbacks = {
|
||||
onClose: params.onClose,
|
||||
onConfirm: params.onConfirm
|
||||
};
|
||||
this._closeOnClose = params.closeOnClose ?? true;
|
||||
this._closeOnConfirm = params.closeOnConfirm ?? true;
|
||||
this._destroyOnClose = params.destroyOnClose ?? true;
|
||||
this._applyContent();
|
||||
}
|
||||
|
||||
setTitle(title: string): void {
|
||||
this._title = title;
|
||||
this._applyContent();
|
||||
}
|
||||
|
||||
setContent(content: string): void {
|
||||
this._content = content;
|
||||
this._applyContent();
|
||||
}
|
||||
|
||||
setButtonHint(buttonHint: string): void {
|
||||
this._buttonHintText = buttonHint;
|
||||
this._applyContent();
|
||||
}
|
||||
|
||||
setCallbacks(callbacks: CommonModalCallbacks): void {
|
||||
this._callbacks = callbacks;
|
||||
}
|
||||
|
||||
close(destroy: boolean = this._destroyOnClose): void {
|
||||
if (!this.node?.isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isShowing) {
|
||||
this._doHide();
|
||||
}
|
||||
|
||||
if (destroy) {
|
||||
this.node.destroy();
|
||||
} else {
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
||||
|
||||
onViewLoad(): void {
|
||||
if (this._loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._loaded = true;
|
||||
this._resolveNodes();
|
||||
this._bindButtonEvents();
|
||||
this._applyContent();
|
||||
this._updateWidget();
|
||||
}
|
||||
|
||||
onViewShow(): void {
|
||||
this._resolveNodes();
|
||||
this._applyContent();
|
||||
this._updateWidget();
|
||||
super.onViewShow();
|
||||
}
|
||||
|
||||
onViewDestroy(): void {
|
||||
this._unbindButtonEvents();
|
||||
this._callbacks = {};
|
||||
}
|
||||
|
||||
private _resolveNodes(): void {
|
||||
const panelNode = this._findNode('dialogPanel');
|
||||
|
||||
this.backdropNode = this.backdropNode ?? this._findNode('BgMask');
|
||||
this.animationNodes = this.animationNodes.length > 0 ? this.animationNodes : (panelNode ? [panelNode] : []);
|
||||
|
||||
this.closeBtn = this.closeBtn ?? this._findNode('dialogPanel/closeBtn');
|
||||
this.buttonHint = this.buttonHint ?? this._findNode('dialogPanel/ButtonHint');
|
||||
this.titleLabel = this.titleLabel ?? this._findNode('dialogPanel/Title')?.getComponent(Label) ?? null;
|
||||
this.contentLabel = this.contentLabel ?? this._findNode('dialogPanel/Content')?.getComponent(Label) ?? null;
|
||||
this.buttonHintLabel = this.buttonHintLabel
|
||||
?? this._findNode('dialogPanel/ButtonHint/Label')?.getComponent(Label)
|
||||
?? null;
|
||||
}
|
||||
|
||||
private _applyContent(): void {
|
||||
this._resolveNodes();
|
||||
|
||||
if (this.titleLabel) {
|
||||
this.titleLabel.string = this._title;
|
||||
}
|
||||
|
||||
if (this.contentLabel) {
|
||||
this.contentLabel.string = this._content;
|
||||
}
|
||||
|
||||
if (this.buttonHintLabel) {
|
||||
this.buttonHintLabel.string = this._buttonHintText;
|
||||
}
|
||||
}
|
||||
|
||||
private _updateWidget(): void {
|
||||
if (!this._screenSize) {
|
||||
this._screenSize = view.getVisibleSize();
|
||||
}
|
||||
|
||||
const uiTransform = this.node.getComponent(UITransform);
|
||||
if (uiTransform) {
|
||||
uiTransform.setContentSize(this._screenSize.width, this._screenSize.height);
|
||||
}
|
||||
}
|
||||
|
||||
private _bindButtonEvents(): void {
|
||||
this._unbindButtonEvents();
|
||||
|
||||
if (this.closeBtn) {
|
||||
this.closeBtn.on(Node.EventType.TOUCH_END, this._onCloseClick, this);
|
||||
}
|
||||
|
||||
if (this.buttonHint) {
|
||||
this.buttonHint.on(Node.EventType.TOUCH_END, this._onConfirmClick, this);
|
||||
}
|
||||
}
|
||||
|
||||
private _unbindButtonEvents(): void {
|
||||
if (this.closeBtn?.isValid) {
|
||||
this.closeBtn.off(Node.EventType.TOUCH_END, this._onCloseClick, this);
|
||||
}
|
||||
|
||||
if (this.buttonHint?.isValid) {
|
||||
this.buttonHint.off(Node.EventType.TOUCH_END, this._onConfirmClick, this);
|
||||
}
|
||||
}
|
||||
|
||||
private _onCloseClick(): void {
|
||||
const shouldContinue = this._callbacks.onClose?.();
|
||||
if (shouldContinue !== false && this._closeOnClose) {
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
|
||||
private _onConfirmClick(): void {
|
||||
const shouldContinue = this._callbacks.onConfirm?.();
|
||||
if (shouldContinue !== false && this._closeOnConfirm) {
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
|
||||
private _findNode(path: string): Node | null {
|
||||
const names = path.split('/').filter(Boolean);
|
||||
let current: Node | null = this.node;
|
||||
|
||||
for (const name of names) {
|
||||
current = current?.getChildByName(name) ?? null;
|
||||
if (!current) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
}
|
||||
9
assets/prefabs/CommonModal.ts.meta
Normal file
9
assets/prefabs/CommonModal.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ef212c1a-cfd5-495c-b2d1-95eeb754658a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -8813,7 +8813,7 @@
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": -777.017,
|
||||
"y": -800.517,
|
||||
"z": 0
|
||||
},
|
||||
"_lrot": {
|
||||
@@ -9525,6 +9525,10 @@
|
||||
"__uuid__": "e41c722f-f605-47f7-9ce4-abff0ed2020f",
|
||||
"__expectedType__": "cc.Prefab"
|
||||
},
|
||||
"commonModalPrefab": {
|
||||
"__uuid__": "5379669e-7cd7-45b6-9dd3-4a021730b23e",
|
||||
"__expectedType__": "cc.Prefab"
|
||||
},
|
||||
"roundedSpriteEffect": {
|
||||
"__uuid__": "f0080a34-1786-4547-8d81-d89cc517b63e",
|
||||
"__expectedType__": "cc.EffectAsset"
|
||||
@@ -9548,4 +9552,4 @@
|
||||
"instance": null,
|
||||
"targetOverrides": null
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
@@ -11,6 +11,7 @@ import { ShareManager } from 'db://assets/scripts/utils/ShareManager';
|
||||
import { PassModal } from 'db://assets/prefabs/PassModal';
|
||||
import { WrongModal } from 'db://assets/prefabs/WrongModal';
|
||||
import { TimeoutModal } from 'db://assets/prefabs/TimeoutModal';
|
||||
import { CommonModal } from 'db://assets/prefabs/CommonModal';
|
||||
import { StaminaInfo, NextLevelData, SubmitShareLevel } from 'db://assets/scripts/types/ApiTypes';
|
||||
import { AchievementTitleManager } from 'db://assets/scripts/utils/AchievementTitleManager';
|
||||
import { applyRoundedCorner } from 'db://assets/scripts/utils/roundedMaterial.utils';
|
||||
@@ -191,6 +192,9 @@ export class PageLevel extends BaseView {
|
||||
@property(Prefab)
|
||||
timeoutModalPrefab: Prefab | null = null;
|
||||
|
||||
@property(Prefab)
|
||||
commonModalPrefab: Prefab | null = null;
|
||||
|
||||
/** 主图圆角材质 EffectAsset */
|
||||
@property(EffectAsset)
|
||||
roundedSpriteEffect: EffectAsset | null = null;
|
||||
@@ -266,6 +270,9 @@ export class PageLevel extends BaseView {
|
||||
/** 超时弹窗实例 */
|
||||
private _timeoutModalNode: Node | null = null;
|
||||
|
||||
/** 通用确认弹窗实例 */
|
||||
private _commonModalNode: Node | null = null;
|
||||
|
||||
/** 是否处于分享挑战模式 */
|
||||
private _isShareMode: boolean = false;
|
||||
|
||||
@@ -365,6 +372,7 @@ export class PageLevel extends BaseView {
|
||||
this._closePassModal();
|
||||
this._closeWrongModal();
|
||||
this._closeTimeoutModal();
|
||||
this._closeCommonModal();
|
||||
this._stopStaminaRecoverTimer();
|
||||
|
||||
// 清理事件监听
|
||||
@@ -501,6 +509,7 @@ export class PageLevel extends BaseView {
|
||||
|
||||
// 显示解锁按钮(单个统一按钮)
|
||||
this.showUnlockButton();
|
||||
this._refreshTipsModeUI();
|
||||
|
||||
// 根据答案字数创建输入格
|
||||
if (config.answer) {
|
||||
@@ -929,8 +938,10 @@ export class PageLevel extends BaseView {
|
||||
}
|
||||
|
||||
this.playClickSound();
|
||||
this._recordCurrentShareSubmission();
|
||||
void this.goToNextLevel();
|
||||
this._showShareNextConfirmModal(() => {
|
||||
this._recordCurrentShareSubmission();
|
||||
void this.goToNextLevel();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1119,9 +1130,7 @@ export class PageLevel extends BaseView {
|
||||
if (this.liveNode) {
|
||||
this.liveNode.active = !isPkMode;
|
||||
}
|
||||
if (this.tipsLayout) {
|
||||
this.tipsLayout.active = !isPkMode;
|
||||
}
|
||||
this._refreshTipsModeUI();
|
||||
if (this.pkLevelProgressNode) {
|
||||
this.pkLevelProgressNode.active = isPkMode;
|
||||
}
|
||||
@@ -1136,6 +1145,29 @@ export class PageLevel extends BaseView {
|
||||
this.updatePkLevelProgressLabel();
|
||||
}
|
||||
|
||||
private _refreshTipsModeUI(): void {
|
||||
if (this.tipsLayout) {
|
||||
this.tipsLayout.active = true;
|
||||
}
|
||||
|
||||
if (this._isShareMode) {
|
||||
this.showClue(1);
|
||||
this.hideClue(2);
|
||||
this.hideClue(3);
|
||||
if (this.unLockTipsBtn) {
|
||||
this.unLockTipsBtn.active = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.showClue(1);
|
||||
this.showClue(2);
|
||||
this.showClue(3);
|
||||
if (this.unLockTipsBtn) {
|
||||
this.unLockTipsBtn.active = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端 level 使用 sortOrder,首关可能为 0;页面展示统一转成从 1 开始的关卡序号
|
||||
*/
|
||||
@@ -1833,8 +1865,10 @@ export class PageLevel extends BaseView {
|
||||
});
|
||||
passModal.setCallbacks({
|
||||
onNextLevel: () => {
|
||||
this._closePassModal();
|
||||
this.goToNextLevel();
|
||||
this._showShareNextConfirmModal(() => {
|
||||
this._closePassModal();
|
||||
void this.goToNextLevel();
|
||||
});
|
||||
},
|
||||
onShare: () => {
|
||||
// 分享后不关闭弹窗,用户可继续点击下一关
|
||||
@@ -2014,9 +2048,11 @@ export class PageLevel extends BaseView {
|
||||
});
|
||||
},
|
||||
onNext: () => {
|
||||
this._closeTimeoutModal();
|
||||
this._recordCurrentShareSubmission();
|
||||
void this.goToNextLevel();
|
||||
this._showShareNextConfirmModal(() => {
|
||||
this._closeTimeoutModal();
|
||||
this._recordCurrentShareSubmission();
|
||||
void this.goToNextLevel();
|
||||
});
|
||||
},
|
||||
onHome: () => {
|
||||
this._closeTimeoutModal();
|
||||
@@ -2046,6 +2082,47 @@ export class PageLevel extends BaseView {
|
||||
}
|
||||
}
|
||||
|
||||
private _closeCommonModal(): void {
|
||||
if (this._commonModalNode && this._commonModalNode.isValid) {
|
||||
this._commonModalNode.destroy();
|
||||
this._commonModalNode = null;
|
||||
console.log('[PageLevel] 关闭通用确认弹窗');
|
||||
}
|
||||
}
|
||||
|
||||
private _showShareNextConfirmModal(onConfirm: () => void): void {
|
||||
if (!this._isShareMode) {
|
||||
onConfirm();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._commonModalNode && this._commonModalNode.isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.commonModalPrefab) {
|
||||
console.warn('[PageLevel] commonModalPrefab 未设置,直接进入下一题');
|
||||
onConfirm();
|
||||
return;
|
||||
}
|
||||
|
||||
const modal = CommonModal.show(this.commonModalPrefab, {
|
||||
title: '切换下一题',
|
||||
content: '确认进入下一题吗?',
|
||||
buttonHint: '确认',
|
||||
zIndex: CommonModal.MODAL_Z_INDEX + 1,
|
||||
onClose: () => {
|
||||
this._commonModalNode = null;
|
||||
},
|
||||
onConfirm: () => {
|
||||
this._commonModalNode = null;
|
||||
onConfirm();
|
||||
},
|
||||
}, this.node.parent ?? this.node);
|
||||
|
||||
this._commonModalNode = modal?.node ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示错误提示
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user