Files
oops-plugin-framework/assets/core/gui/layer/LayerNotify.ts
dgflash 08f5ee4e20 优化
2026-06-29 15:16:26 +08:00

134 lines
3.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { BlockInputEvents, Node, instantiate } from 'cc';
import { ViewUtil } from '../../utils/ViewUtil';
import { GuiPromptConfig } from '../GuiPromptConfig';
import { Notify } from '../prompt/Notify';
import { LayerHelper } from './LayerHelper';
/* 滚动消息提示层 */
export class LayerNotify extends Node {
private black!: BlockInputEvents;
/** 等待提示资源 */
private wait: Node = null!;
/** 自定义弹出提示资源 */
private notify: Node = null!;
/** 自定义弹出提示内容资源 */
private notifyItem: Node = null!;
/** 标记是否正在打开等待提示中 */
private waitOpening: boolean = false;
/** 标记是否在打开过程中被请求关闭 */
private waitShouldClose: boolean = false;
constructor(name: string) {
super(name);
LayerHelper.setFullScreen(this);
this.black = this.addComponent(BlockInputEvents);
this.black.enabled = false;
}
/** 打开等待提示 */
async waitOpen() {
this.waitOpening = true;
try {
if (this.wait == null) {
this.wait = await ViewUtil.createPrefabNodeAsync(
GuiPromptConfig.Wait.path
);
}
// 异步操作完成后,检查是否已被请求关闭
if (this.waitShouldClose) {
this.waitShouldClose = false;
this.doWaitClose();
return;
}
if (this.wait.parent == null) {
this.wait.parent = this;
this.black.enabled = true;
}
}
finally {
this.waitOpening = false;
}
}
/** 关闭等待提示 */
waitClose() {
// 如果正在打开中,标记应该关闭,等打开完成后再关闭
if (this.waitOpening) {
this.waitShouldClose = true;
return;
}
this.doWaitClose();
}
/** 实际执行关闭等待提示 */
private doWaitClose() {
if (this.wait && this.wait.parent) {
this.wait.parent = null;
this.black.enabled = false;
}
this.waitShouldClose = false;
}
/**
* 渐隐飘过提示
* @param content 文本表示
* @param useI18n 是否使用多语言
*/
async toast(content: string, useI18n: boolean) {
if (this.notify == null) {
this.notify = await ViewUtil.createPrefabNodeAsync(
GuiPromptConfig.Notify.path
);
this.notifyItem = this.notify.children[0];
this.notifyItem.parent = null;
}
this.notify.parent = this;
const item = instantiate(this.notifyItem);
const prompt = item.getChildByName('prompt')!;
const notify = prompt.getComponent(Notify)!;
item.parent = this.notify;
notify.onComplete = () => {
if (this.notify.children.length <= 1) {
this.notify.parent = null;
}
};
notify.toast(content, useI18n);
// 超过3个提示就释放第一个提示
if (this.notify.children.length > 3) {
this.notify.children[0].destroy();
}
}
/** 销毁时释放资源 */
onDestroy() {
// 清理等待提示节点
if (this.wait) {
this.wait.destroy();
this.wait = null!;
}
// 清理通知提示节点
if (this.notify) {
this.notify.destroy();
this.notify = null!;
}
// 清理通知项模板节点
if (this.notifyItem) {
this.notifyItem.destroy();
this.notifyItem = null!;
}
// 清理事件阻挡组件
this.black = null!;
}
}