代码整理,ai相关配置转luban,聊天气泡缓存

This commit is contained in:
2025-08-13 11:31:21 +08:00
parent b511412833
commit db0b040fd8
78 changed files with 913 additions and 5189 deletions
@@ -1,7 +1,7 @@
import { _decorator, Component, instantiate, Node, Vec3 } from "cc";
import { DialogManager } from "../../manager/DialogManager";
import { DialogBubble } from "./DialogBubble";
import { Dialog } from "../../utils/DemoData";
import { Dialog } from "../../data/DialogData";
const { ccclass, property } = _decorator;
const BUTTOM_Y = -955.571;
@@ -16,6 +16,7 @@ export class ChatContentsLayout extends Component {
rBubble: DialogBubble = null;
bubbles: DialogBubble[] = [];
cachedBubbles: DialogBubble[] = [];
protected start(): void {
this.lBubble.node.active = false;
@@ -29,25 +30,45 @@ export class ChatContentsLayout extends Component {
}
UpdateDialog(dialogs: Dialog[]) {
//if (!this.bubbles) this.bubbles = [];
for (let i = this.bubbles.length - 1; i >= 0; i--) {
// 隐藏当前使用的气泡并放入缓存
for (let i = 0; i < this.bubbles.length; i++) {
if (this.bubbles[i].node) {
this.bubbles[i].node.destroy();
this.bubbles[i].node.active = false;
this.cachedBubbles.push(this.bubbles[i]);
}
}
this.bubbles = [];
let initPosY: number = BUTTOM_Y;
for (let i = dialogs.length - 1; i >= 0; i--) {
const dialog = dialogs[i]; //倒序
let newBubble: DialogBubble = null;
let newBubbleNode = dialog.isPlayer
? instantiate(this.rBubble.node)
: instantiate(this.lBubble.node);
let newBubble = newBubbleNode.getComponent(DialogBubble);
// 尝试从缓存中获取合适的气泡
const isPlayerBubble = dialog.isPlayer;
for (let j = 0; j < this.cachedBubbles.length; j++) {
const cached = this.cachedBubbles[j];
if (cached && cached.node) {
// 检查气泡类型是否匹配(通过位置判断左右气泡)
const isRightBubble = cached.node.position.x > 0;
if ((isPlayerBubble && isRightBubble) || (!isPlayerBubble && !isRightBubble)) {
newBubble = cached;
this.cachedBubbles.splice(j, 1);
break;
}
}
}
// 如果缓存中没有合适的气泡,创建新的
if (!newBubble) {
let newBubbleNode = isPlayerBubble
? instantiate(this.rBubble.node)
: instantiate(this.lBubble.node);
newBubble = newBubbleNode.getComponent(DialogBubble);
newBubbleNode.setParent(this.node);
}
newBubbleNode.setParent(this.node);
newBubble.node.active = true;
let offset = newBubble.updateBubbleContent(dialog.content);
let pos = newBubble.node.position;
@@ -56,5 +77,21 @@ export class ChatContentsLayout extends Component {
initPosY += offset + 20;
this.bubbles.push(newBubble);
}
// 清理多余的缓存气泡,避免内存泄漏
this.cleanupExcessCachedBubbles();
}
private cleanupExcessCachedBubbles() {
const maxCachedBubbles = 20; // 最大缓存数量
if (this.cachedBubbles.length > maxCachedBubbles) {
const excessCount = this.cachedBubbles.length - maxCachedBubbles;
for (let i = 0; i < excessCount; i++) {
const bubble = this.cachedBubbles.shift();
if (bubble && bubble.node) {
bubble.node.destroy();
}
}
}
}
}
@@ -48,6 +48,12 @@ export class ChatPanel extends li_BaseView {
private _nodeTab: any = {};
nameKey: string;
private onLanguageChangeCallback = () => {
if (!this.girlName) return;
this.girlName.string = LanguageUtils.getText(this.nameKey);
};
openUIDataCT(data) {
this.id = data;
// 设置当前聊天的角色ID
@@ -67,14 +73,10 @@ export class ChatPanel extends li_BaseView {
this.onDialogUpdate
);
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.girlName.string = LanguageUtils.getText(this.nameKey);
});
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, this.onLanguageChangeCallback);
}
onDestroy(): void {
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.girlName.string = LanguageUtils.getText(this.nameKey);
});
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, this.onLanguageChangeCallback);
}
refresh(id: number) {
this.id = id;
@@ -1,60 +0,0 @@
import { _decorator, Component, math, Node, Sprite } from "cc";
import { Config18x } from "db://assets/Scripts/Main/Config/Config18x";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import { GirlDetailPanel } from "./GirlDetailPanel";
const { ccclass, property } = _decorator;
@ccclass("DetailImageItem")
export class DetailImageItem extends Component {
@property(Sprite)
img: Sprite;
@property(Node)
lock: Node;
@property(Node)
question: Node;
base: GirlDetailPanel;
url: string;
onLoad() {
GButton.BandClick(this.node, this.onClickThis, this);
}
onClickThis() {
let data = {
url: this.url,
closeFunc: () => {
this.base.setVideEnable(true);
},
};
if (this.url) {
ViewManager.I.openBundlesView("ShowPanel", data);
this.base.setVideEnable(false);
}
}
refresh(path: string, base: GirlDetailPanel) {
this.base = base;
this.lock.active = false;
this.question.active = false;
if (true) {
this.url = path;
// if (info.imageState !== ImageState.Release) {
// this.url += '_blured';
// if (info.imageState === ImageState.Lock)
// this.img.color = new math.Color(127, 127, 127, 255);
// else
// this.img.color = new math.Color(50, 50, 50, 255);
// } else {
// this.img.color = new math.Color(255, 255, 255, 255);
// }
ResManager.I.changeBundleSpriteFrame(this.img, this.url, "Chat18x");
}
}
}
@@ -1,9 +0,0 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "da3f4fde-7c2a-41b5-b59f-2b539cc41eec",
"files": [],
"subMetas": {},
"userData": {}
}
@@ -1,7 +1,7 @@
import { _decorator, Label, Node, Sprite, VideoPlayer, instantiate } from "cc";
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import { DetailImageItem } from "./DetailImageItem";
import { DetailImageItem } from "../../uiitems/DetailImageItem";
import { NavigationManager } from "../../manager/NavigationManager";
import ConfigManager from "../../manager/ConfigManager";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
@@ -1,108 +0,0 @@
import { _decorator, Component, Label, Node, Sprite, UITransform } from "cc";
import { Config18x } from "db://assets/Scripts/Main/Config/Config18x";
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { NavigationManager } from "../../manager/NavigationManager";
import { Girl, PriceType } from "../../../schema/schema";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
import { InnerMsgCode } from "../../../Main/Config/InnerMsgCode";
const { ccclass, property } = _decorator;
@ccclass("GirlListItem")
export class GirlListItem extends Component {
@property(Sprite)
avatar: Sprite;
@property(Label)
girlName: Label;
@property(Label)
tags: Label;
@property(Label)
price: Label;
@property(Node)
freeNode: Node;
@property(Node)
tryNode: Node;
@property(Node)
chatBtn: Node;
@property(Node)
starParent: Node;
id: number = -1;
nameKey: string;
tagKey: string;
protected onLoad(): void {
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.girlName.string = this.girlName.string = LanguageUtils.getText(
this.nameKey
);
let desc = "";
const tags = LanguageUtils.getText(this.tagKey).split("|");
for (let i = 0; i < tags.length; i++) {
if (i != 0) desc += "\n";
desc += tags[i];
}
this.tags.string = desc;
});
}
protected onDestroy(): void {
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.girlName.string = this.girlName.string = LanguageUtils.getText(
this.nameKey
);
let desc = "";
const tags = LanguageUtils.getText(this.tagKey).split("|");
for (let i = 0; i < tags.length; i++) {
if (i != 0) desc += "\n";
desc += tags[i];
}
this.tags.string = desc;
});
}
baseNode: Node;
refreshData(data: Girl, baseNode: Node) {
this.baseNode = baseNode;
this.id = data.id;
this.nameKey = data.nameKey;
this.girlName.string = LanguageUtils.getText(data.nameKey);
this.tagKey = data.tagKey;
let desc = "";
const tags = LanguageUtils.getText(data.tagKey).split("|");
for (let i = 0; i < tags.length; i++) {
if (i != 0) desc += "|";
desc += tags[i];
}
this.tags.string = desc;
this.price.node.active = data.priceType == PriceType.pay;
this.freeNode.active = data.priceType == PriceType.free;
this.tryNode.active = data.priceType == PriceType.first_time_free;
ResManager.I.changeBundleSpriteFrame(
this.avatar,
data.avatarPath,
"Chat18x",
() => {
let sizeTran = this.avatar.node.parent.getComponent(UITransform);
Utils.adjustBgPixelRatioToSize(
sizeTran.contentSize,
this.avatar.node,
2
);
}
);
for (let i = 0; i < 5; i++) {
this.starParent.children[i].active = i < data.starCount;
}
}
onClickDetail() {
NavigationManager.Instance.navigateToGirlDetail(this.id);
ViewManager.I.closeView(this.baseNode);
}
update(deltaTime: number) {}
}
@@ -1,9 +0,0 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "c9c66d7b-1a74-4d16-8410-324541cfc05e",
"files": [],
"subMetas": {},
"userData": {}
}
@@ -2,7 +2,7 @@ import { _decorator, Component, Node, instantiate } from "cc";
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import { GirlListItem } from "./GirlListItem";
import { GirlListItem } from "../../uiitems/GirlListItem";
import ConfigManager from "../../manager/ConfigManager";
const { ccclass, property } = _decorator;
@@ -1,61 +0,0 @@
import { _decorator, Component, Label, Node, Sprite, UITransform } from "cc";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { NavigationManager } from "../../manager/NavigationManager";
import { TbThemes, Theme } from "../../../schema/schema";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
import { InnerMsgCode } from "../../../Main/Config/InnerMsgCode";
const { ccclass, property } = _decorator;
@ccclass("ThemeItem")
export class ThemeItem extends Component {
@property(Sprite)
lockImg: Sprite;
@property(Label)
titleName: Label;
@property(Sprite)
img: Sprite;
private category: number;
private isLocked: boolean;
key: string;
protected onLoad(): void {
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.titleName.string = LanguageUtils.getText(this.key);
});
}
protected onDestroy(): void {
Utils.removeInnerEL(InnerMsgCode.LanguageChange, this, () => {
this.titleName.string = LanguageUtils.getText(this.key);
});
}
refresh(themeData: Theme) {
this.lockImg.node.active = !themeData.isRelease;
this.isLocked = !themeData.isRelease;
this.key = themeData.key;
this.titleName.string = LanguageUtils.getText(themeData.key);
this.category = themeData.category;
ResManager.I.changeBundleSpriteFrame(
this.img,
themeData.path,
"Chat18x",
() => {
let sizeTran = this.img.node.parent.getComponent(UITransform);
Utils.adjustBgPixelRatioToSize(sizeTran.contentSize, this.img.node, 2);
}
);
GButton.RemoveAndBandClick(this.node, this.OnClickThis, this);
}
OnClickThis() {
if (this.isLocked) {
return;
}
NavigationManager.Instance.navigateToGirlList(this.category);
}
}
@@ -1,9 +0,0 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "c715a0ca-612c-4b4b-9cb3-ba48081a2f55",
"files": [],
"subMetas": {},
"userData": {}
}
@@ -11,9 +11,9 @@ import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import Utils from "db://assets/Scripts/Main/Common/Utils";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
import { ThemeItem } from "./ThemeItem";
import { ThemeItem } from "../../uiitems/ThemeItem";
import { NavigationManager } from "../../manager/NavigationManager";
import { GirlListItem } from "./GirlListItem";
import { GirlListItem } from "../../uiitems/GirlListItem";
import ConfigManager from "../../manager/ConfigManager";
import LanguageUtils from "../../../Main/Common/LanguageUtils";
import { ViewManager } from "../../../Main/Manager/ViewManager";