Files
18xchat/assets/Scripts/chat18x/ui/panels/ChatPanel.ts
T

165 lines
4.2 KiB
TypeScript
Raw Normal View History

import {
_decorator,
Component,
EditBox,
Node,
Label,
Sprite,
UITransform,
} from "cc";
2025-08-11 16:08:59 +08:00
// 首先加载 polyfills 以确保兼容性
import "../../utils/polyfills";
import { DialogManager } from "../../manager/DialogManager";
import { ChatAIService } from "../../core/ChatAIService";
2025-07-25 18:27:30 +08:00
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { InnerMsgCode } from "db://assets/Scripts/Main/Config/InnerMsgCode";
import { ChatContentsLayout } from "../components/ChatContentsLayout";
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
2025-07-25 18:27:30 +08:00
import GameRootUI from "db://assets/Scripts/Main/Common/GameRootUI";
import { ImagePopup } from "../components/ImagePopup";
2025-08-12 16:58:02 +08:00
2025-08-01 18:28:36 +08:00
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import { VideoRoleType } from "db://assets/Scripts/Main/Common/GlobalValue";
2025-08-12 16:58:02 +08:00
import ConfigManager from "../../manager/ConfigManager";
2025-08-12 19:49:50 +08:00
import LanguageUtils from "../../../Main/Common/LanguageUtils";
2025-07-22 16:43:10 +08:00
const { ccclass, property } = _decorator;
@ccclass("ChatPanel")
2025-07-25 18:27:30 +08:00
export class ChatPanel extends li_BaseView {
2025-08-11 16:08:59 +08:00
manager: DialogManager = null;
2025-07-22 16:43:10 +08:00
@property(EditBox)
editBox: EditBox = null;
2025-07-25 15:20:02 +08:00
@property(Label)
girlName: Label = null;
2025-08-01 18:28:36 +08:00
@property(Sprite)
girlImg: Sprite = null;
2025-07-25 18:27:30 +08:00
@property(ChatContentsLayout)
layout: ChatContentsLayout = null;
@property(ImagePopup)
popUpImage: ImagePopup = null;
id: number;
2025-08-01 18:28:36 +08:00
private _nodeTab: any = {};
2025-08-12 19:49:50 +08:00
nameKey: string;
2025-08-13 16:53:27 +08:00
private onLanguageChangeCallback = () => {
if (!this.girlName) return;
this.girlName.string = LanguageUtils.getText(this.nameKey);
};
openUIDataCT(data) {
2025-08-01 18:28:36 +08:00
this.id = data;
2025-08-11 14:29:39 +08:00
// 设置当前聊天的角色ID
ChatAIService.Instance.setCurrentRole(this.id);
2025-08-01 18:28:36 +08:00
}
2025-07-25 18:27:30 +08:00
onLoadCT() {
Utils.parseNode(this.node, this._nodeTab);
2025-08-01 18:28:36 +08:00
2025-07-25 18:27:30 +08:00
this.register();
2025-08-01 18:28:36 +08:00
this.refresh(this.id);
2025-07-25 18:27:30 +08:00
}
register() {
Utils.addInnerEL(
InnerMsgCode.Chat_DialogRefresh,
this,
this.onDialogUpdate
);
2025-08-12 19:49:50 +08:00
2025-08-13 16:53:27 +08:00
Utils.addInnerEL(
InnerMsgCode.LanguageChange,
this,
this.onLanguageChangeCallback
);
2025-08-12 19:49:50 +08:00
}
onDestroy(): void {
2025-08-13 16:53:27 +08:00
Utils.removeInnerEL(
InnerMsgCode.Chat_DialogRefresh,
this,
this.onDialogUpdate
);
Utils.removeInnerEL(
InnerMsgCode.LanguageChange,
this,
this.onLanguageChangeCallback
);
2025-07-25 18:27:30 +08:00
}
refresh(id: number) {
2025-07-25 15:20:02 +08:00
this.id = id;
2025-08-12 16:58:02 +08:00
const data = ConfigManager.tables.TbGirls.get(this.id);
const dataDetail = ConfigManager.tables.TbGirlsDetail.get(this.id);
if (!data) return;
2025-08-12 19:49:50 +08:00
this.nameKey = data.nameKey;
this.girlName.string = LanguageUtils.getText(data.nameKey);
ResManager.I.changeBundleSpriteFrame(
this.girlImg,
2025-08-12 16:58:02 +08:00
data.avatarPath,
"Chat18x",
() => {
let sizeTran = this.girlImg.node.parent.getComponent(UITransform);
Utils.adjustBgPixelRatioToSize(
sizeTran.contentSize,
this.girlImg.node,
2
);
}
);
let uiTransform: UITransform =
this._nodeTab.BgFrame.getComponent(UITransform);
Utils.sendInnerMsg(InnerMsgCode.SimplePlayVide, {
2025-08-12 16:58:02 +08:00
path: dataDetail.pics[0],
size: uiTransform.contentSize,
2025-08-13 16:17:44 +08:00
bgFrameNode: this._nodeTab.BgFrame,
2025-08-01 18:28:36 +08:00
});
2025-07-25 15:20:02 +08:00
}
onDialogUpdate() {
2025-08-13 12:55:27 +08:00
if (this.layout) {
this.layout.UpdateDialog(DialogManager.getInstance().getDialogs());
}
2025-07-25 18:27:30 +08:00
}
2025-07-22 16:43:10 +08:00
protected onEnable(): void {
2025-08-11 16:08:59 +08:00
if (!this.manager) this.manager = DialogManager.getInstance();
2025-07-25 18:27:30 +08:00
GameRootUI.I.hideDefaultView();
2025-07-22 16:43:10 +08:00
}
public async OnClickSend() {
const str = this.editBox.string;
if (!str || str == "") return;
console.log("post:" + str);
this.editBox.string = "";
2025-07-25 18:27:30 +08:00
DialogManager.getInstance().updateDialog(true, str, true);
2025-07-22 16:43:10 +08:00
2025-08-11 14:29:39 +08:00
// 使用新的sendMessage方法,传入角色ID
const ret = await ChatAIService.Instance.sendMessage(this.id, str);
2025-07-22 16:43:10 +08:00
if (this.manager) {
2025-08-11 14:29:39 +08:00
this.manager.updateDialog(false, ret);
2025-07-22 16:43:10 +08:00
}
2025-08-11 14:29:39 +08:00
console.log("ret:" + ret);
2025-07-25 18:27:30 +08:00
//测试
2025-08-01 18:28:36 +08:00
//this.popUpImage.refresh("Image/1/blur_naked_1");
2025-07-25 18:27:30 +08:00
}
returnBtn() {
2025-07-25 18:27:30 +08:00
this.onClose();
GameRootUI.I.showDefaultView();
//ViewManager.I.openBundlesView("GirlListPanel");
2025-07-22 16:43:10 +08:00
}
2025-08-13 16:53:27 +08:00
onClickRecord() {
ViewManager.I.openBundlesView("RecordPanel", this.id);
}
2025-07-22 16:43:10 +08:00
}