2025-04-24 17:03:28 +08:00

99 lines
2.9 KiB
TypeScript

import FGUtil from "../../gear_2.3.4/fgui/FGUtil";
import SKDataUtil from "../../gear_2.3.4/util/SKDataUtil";
import SKUIUtil from "../../gear_2.3.4/util/SKUIUtil";
import GameUtil from "../../core/GameUtil";
export default class MsgAlert {
static shared: MsgAlert = new MsgAlert();
static hgap: number = 50;
static vgap: number = 45;
static max: number = 5;
static top: number = 150;
list: string[];
showList: cc.Node[];
// MsgAlert.addMsg("您獲得了[img]ui://main_ui/10406[/img][八荒遺風][img]ui://main_ui/9023[/img][夥伴修煉手冊]X20");
constructor() {
this.list = [];
this.showList = [];
}
// 根據代碼提示信息
static addMsg(value: string) {
if (value == null) {
return;
}
if (!SKDataUtil.isString(value)) {
value = String(value);
}
if (GameUtil.msgText[value]){
value = GameUtil.msgText[value];
}
this.shared.addMsg(value);
}
static removeAll() {
for (let node of this.shared.showList) {
if (SKUIUtil.isValid(node)) {
node.stopAllActions();
node.removeFromParent();
}
}
}
private addMsg(value: string) {
if (value == null) {
cc.warn("消息不能為空!");
return;
}
// 如果超過顯示數量上限
if (this.showList.length >= MsgAlert.max) {
let first = this.showList.shift();
first.stopAllActions();
first.removeFromParent();
for (let node of this.showList) {
if (SKUIUtil.isValid(node)) {
node.y += MsgAlert.vgap;
}
}
}
this.list.push(value);
this.check();
}
private check() {
if (this.list.length < 1) {
return;
}
let msg = this.list.shift();
let box = fgui.UIPackage.createObject("main_ui", "msg_bar").asCom;
let richRTF = box.getChild("title").asRichTextField;
richRTF.singleLine = true;
richRTF.maxWidth = 0;
richRTF.text = msg;
let width = FGUtil.richTextWidthOf(richRTF) + MsgAlert.hgap * 2;
box.width = width;
let parentW = FGUtil.root().width;
box.x = (parentW - richRTF.width) / 2;
box.y = MsgAlert.top + this.showList.length * MsgAlert.vgap;
FGUtil.root().addChild(box);
this.showList.push(box.node);
cc.tween(box.node).delay(1.8).to(0.2, { opacity: 0 }).then(cc.callFunc(this.hideBox, this, box.node)).start();
}
hideBox(node: cc.Node) {
node.stopAllActions();
node.removeFromParent();
let index = this.showList.indexOf(node);
if (index < 0) {
return;
}
this.showList.splice(index, 1);
for (let node of this.showList) {
if (SKUIUtil.isValid(node)) {
node.y += MsgAlert.vgap;
}
}
}
}