220 lines
6.1 KiB
TypeScript
220 lines
6.1 KiB
TypeScript
import { _decorator, Node, view, UITransform, Size } from 'cc';
|
||
import { BaseModal } from 'db://assets/scripts/core/BaseModal';
|
||
import { WxSDK } from 'db://assets/scripts/utils/WxSDK';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/**
|
||
* TimeoutModal 回调接口
|
||
*/
|
||
export interface TimeoutModalCallbacks {
|
||
/** 点击求助好友回调 */
|
||
onShare?: () => void;
|
||
/** 点击再次挑战回调 */
|
||
onRestart?: () => void;
|
||
/** 点击下一题回调 */
|
||
onNext?: () => void;
|
||
/** 点击返回主页 / 关闭按钮回调 */
|
||
onHome?: () => void;
|
||
}
|
||
|
||
interface TimeoutModalParams {
|
||
levelIndex?: number;
|
||
shareMode?: boolean;
|
||
}
|
||
|
||
/**
|
||
* 时间耗尽弹窗组件
|
||
* 继承 BaseModal,显示倒计时结束提示,提供"求助好友"、"再次挑战"和"返回主页"三个按钮
|
||
*/
|
||
@ccclass('TimeoutModal')
|
||
export class TimeoutModal extends BaseModal {
|
||
/** 静态常量:弹窗层级 */
|
||
public static readonly MODAL_Z_INDEX = 999;
|
||
|
||
/** 关闭按钮 */
|
||
@property(Node)
|
||
closeBtn: Node | null = null;
|
||
|
||
/** 求助好友按钮 */
|
||
@property(Node)
|
||
buttonShare: Node | null = null;
|
||
|
||
/** 再次挑战按钮 */
|
||
@property(Node)
|
||
buttonRestart: Node | null = null;
|
||
|
||
/** 下一题按钮(分享挑战超时时显示) */
|
||
@property(Node)
|
||
buttonNext: Node | null = null;
|
||
|
||
/** 返回主页按钮 */
|
||
@property(Node)
|
||
buttonHome: Node | null = null;
|
||
|
||
/** 回调函数 */
|
||
private _callbacks: TimeoutModalCallbacks = {};
|
||
|
||
/** 缓存的屏幕尺寸 */
|
||
private _screenSize: Size | null = null;
|
||
|
||
/**
|
||
* 设置回调函数
|
||
*/
|
||
setCallbacks(callbacks: TimeoutModalCallbacks): void {
|
||
this._callbacks = callbacks;
|
||
}
|
||
|
||
/**
|
||
* 页面首次加载时调用
|
||
*/
|
||
onViewLoad(): void {
|
||
console.log('[TimeoutModal] onViewLoad');
|
||
this._resolveNodes();
|
||
this._bindButtonEvents();
|
||
this._refreshModeButtons();
|
||
}
|
||
|
||
/**
|
||
* 页面每次显示时调用
|
||
*/
|
||
onViewShow(): void {
|
||
super.onViewShow();
|
||
this._updateWidget();
|
||
this._refreshModeButtons();
|
||
}
|
||
|
||
/**
|
||
* 页面销毁时调用
|
||
*/
|
||
onViewDestroy(): void {
|
||
this._unbindButtonEvents();
|
||
}
|
||
|
||
/**
|
||
* 设置弹窗尺寸为全屏
|
||
*/
|
||
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 {
|
||
if (this.closeBtn) {
|
||
this.closeBtn.on(Node.EventType.TOUCH_END, this._onHomeClick, this);
|
||
}
|
||
if (this.buttonShare) {
|
||
this.buttonShare.on(Node.EventType.TOUCH_END, this._onShareClick, this);
|
||
}
|
||
if (this.buttonRestart) {
|
||
this.buttonRestart.on(Node.EventType.TOUCH_END, this._onRestartClick, this);
|
||
}
|
||
if (this.buttonNext) {
|
||
this.buttonNext.on(Node.EventType.TOUCH_END, this._onNextClick, this);
|
||
}
|
||
if (this.buttonHome) {
|
||
this.buttonHome.on(Node.EventType.TOUCH_END, this._onHomeClick, this);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解除按钮事件绑定
|
||
*/
|
||
private _unbindButtonEvents(): void {
|
||
if (this.closeBtn && this.closeBtn.isValid) {
|
||
this.closeBtn.off(Node.EventType.TOUCH_END, this._onHomeClick, this);
|
||
}
|
||
if (this.buttonShare && this.buttonShare.isValid) {
|
||
this.buttonShare.off(Node.EventType.TOUCH_END, this._onShareClick, this);
|
||
}
|
||
if (this.buttonRestart && this.buttonRestart.isValid) {
|
||
this.buttonRestart.off(Node.EventType.TOUCH_END, this._onRestartClick, this);
|
||
}
|
||
if (this.buttonNext && this.buttonNext.isValid) {
|
||
this.buttonNext.off(Node.EventType.TOUCH_END, this._onNextClick, this);
|
||
}
|
||
if (this.buttonHome && this.buttonHome.isValid) {
|
||
this.buttonHome.off(Node.EventType.TOUCH_END, this._onHomeClick, this);
|
||
}
|
||
}
|
||
|
||
private _resolveNodes(): void {
|
||
this.closeBtn = this.closeBtn ?? this._findChild(this.node, 'closeBtn');
|
||
this.buttonShare = this.buttonShare ?? this._findChild(this.node, 'ButtonShare');
|
||
this.buttonRestart = this.buttonRestart ?? this._findChild(this.node, 'ButtonRestart');
|
||
this.buttonNext = this.buttonNext ?? this._findChild(this.node, 'ButtonNext');
|
||
this.buttonHome = this.buttonHome ?? this._findChild(this.node, 'ButtonHome');
|
||
}
|
||
|
||
private _refreshModeButtons(): void {
|
||
const isShareMode = this._params?.shareMode === true;
|
||
if (this.buttonRestart) {
|
||
this.buttonRestart.active = !isShareMode;
|
||
}
|
||
if (this.buttonNext) {
|
||
this.buttonNext.active = isShareMode;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 求助好友按钮点击
|
||
*/
|
||
private _onShareClick(): void {
|
||
console.log('[TimeoutModal] 点击求助好友');
|
||
|
||
WxSDK.shareAppMessage({
|
||
title: '这道题太难了,快来帮帮我!',
|
||
query: `level=${this._params?.levelIndex ?? 1}`
|
||
});
|
||
|
||
this._callbacks.onShare?.();
|
||
}
|
||
|
||
/**
|
||
* 再次挑战按钮点击
|
||
*/
|
||
private _onRestartClick(): void {
|
||
console.log('[TimeoutModal] 点击再次挑战');
|
||
this._callbacks.onRestart?.();
|
||
}
|
||
|
||
/**
|
||
* 下一题按钮点击
|
||
*/
|
||
private _onNextClick(): void {
|
||
console.log('[TimeoutModal] 点击下一题');
|
||
this._callbacks.onNext?.();
|
||
}
|
||
|
||
/**
|
||
* 返回主页 / 关闭按钮点击
|
||
*/
|
||
private _onHomeClick(): void {
|
||
console.log('[TimeoutModal] 点击返回主页');
|
||
this._callbacks.onHome?.();
|
||
}
|
||
|
||
private _findChild(root: Node, nodeName: string): Node | null {
|
||
if (root.name === nodeName) {
|
||
return root;
|
||
}
|
||
|
||
for (const child of root.children) {
|
||
const found = this._findChild(child, nodeName);
|
||
if (found) {
|
||
return found;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|