Files
18xchat/assets/Scripts/chat18x/uiitems/RecomandItem.ts
T

159 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-10-10 16:09:14 +08:00
import { _decorator, Component, Label, Node, Sprite, UITransform } from "cc";
import Utils from "../../Main/Common/Utils";
import { GButton } from "../../Main/Common/GButton";
import { DataManager, DataId } from "../data/DataManager";
import { GirlData } from "../data/GirlData";
import LanguageUtils from "../../Main/Common/LanguageUtils";
import ResManager from "../../Main/Manager/ResManager";
import { EnvData } from "../data/EnvData";
2025-10-10 18:21:33 +08:00
import {
NavigationManager,
PanelType,
PageTransitionType,
} from "../manager/NavigationManager";
import { logger } from "../../Main/Common/Logger";
2025-10-10 16:09:14 +08:00
const { ccclass, property } = _decorator;
@ccclass("RecomandItem")
export class RecomandItem extends Component {
private _nodeTab: any = {};
private recPic: Sprite;
private girlName: Label;
private tag: Label;
private heartParent: Node;
private hearts: Sprite[] = [];
private chatBtn: Node;
private id: number;
2025-10-14 16:59:02 +08:00
// 标记是否已初始化(用于虚拟列表复用)
public _initialized: boolean = false;
// 兼容原有调用方式的 init 方法
2025-10-10 16:09:14 +08:00
init() {
2025-10-14 16:59:02 +08:00
if (this._initialized) {
return;
}
2025-10-10 16:09:14 +08:00
Utils.parseNode(this.node, this._nodeTab);
this.recPic = this._nodeTab.recPic.getComponent(Sprite);
this.girlName = this._nodeTab.name.getComponent(Label);
this.tag = this._nodeTab.tag.getComponent(Label);
this.heartParent = this._nodeTab.heartBg;
this.heartParent.children.forEach((a) =>
this.hearts.push(a.getComponent(Sprite))
);
this.chatBtn = this._nodeTab.chatBtn;
GButton.BandClick(this.chatBtn, this.chatToHer, this);
2025-10-10 18:21:33 +08:00
GButton.BandClick(this._nodeTab.detailBtn, this.gotoDetail, this);
2025-10-14 16:59:02 +08:00
this._initialized = true;
}
// 节点回收到对象池时调用,重置状态
reset(): void {
this.id = -1;
this.node.active = false;
2025-10-10 16:09:14 +08:00
}
refresh(girlId: number) {
this.id = girlId;
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
const category = girlData.getGrilCategoryById(girlId);
2025-10-11 16:19:57 +08:00
// 数据
let nameKey = girlData.getGrilName(category.toString(), this.id);
this.girlName.string = LanguageUtils.getText(nameKey);
let tagKey = girlData.getGrilTagKey(category.toString(), this.id);
2025-10-10 16:09:14 +08:00
2025-10-11 16:19:57 +08:00
this.tag.string = LanguageUtils.getText(tagKey);
2025-10-10 16:09:14 +08:00
2025-10-11 16:19:57 +08:00
this.heartParent.active = true;
2025-10-10 16:09:14 +08:00
2025-10-11 16:19:57 +08:00
let avatarPath = girlData.getGrilAvatar(category.toString(), this.id);
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
let newPath = envData.cdn + "/" + "Girls/" + avatarPath + ".png";
//listAvatarPath = listAvatarPath.replace("Avatar", "avatar"); //临时
ResManager.I.changeRemoteSpriteFrame(this.recPic, newPath, () => {
let sizeTran = this.recPic.node.parent.getComponent(UITransform);
Utils.adjustBgPixelRatioToSize(sizeTran.contentSize, this.recPic.node, 1);
});
const star = girlData.getStar(category.toString(), this.id);
2025-10-14 16:59:02 +08:00
//logger.log("好感度:" + star);
2025-10-10 16:09:14 +08:00
for (let i = 0; i < this.hearts.length; i++) {
const element = this.hearts[i];
2025-10-11 16:19:57 +08:00
if (star > i) {
2025-10-11 14:59:05 +08:00
ResManager.I.changeResourceSpriteFrame(
element,
"ui/common/heart/heart"
);
2025-10-10 16:09:14 +08:00
} else {
2025-10-11 14:59:05 +08:00
ResManager.I.changeResourceSpriteFrame(
element,
"ui/common/heart/heart_empty"
);
2025-10-10 16:09:14 +08:00
}
}
}
chatToHer() {
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
NavigationManager.Instance.setSelectedGirlId(this.id);
NavigationManager.Instance.setSelectedCategoryId(
girlData.getGrilCategoryById(this.id)
);
2025-10-10 18:21:33 +08:00
2025-10-13 17:09:39 +08:00
// 直接在当前页面打开ChatPanel作为子页面
const currentPanel = NavigationManager.Instance.getCurrentActivePanelNode();
if (currentPanel) {
const chatData = { girlId: this.id, base: currentPanel };
2025-10-14 16:59:02 +08:00
NavigationManager.Instance.openSubPanel(
"ChatPanel",
currentPanel,
chatData,
{
2025-10-14 18:54:02 +08:00
openAnimation: PageTransitionType.NONE,
closeAnimation: PageTransitionType.NONE,
2025-10-14 16:59:02 +08:00
hideBasePanel: true,
}
);
2025-10-13 17:09:39 +08:00
} else {
logger.warn("[RecomandItem] 无法获取当前激活的主页面");
}
2025-10-10 16:09:14 +08:00
}
2025-10-10 18:21:33 +08:00
gotoDetail() {
2025-10-10 16:09:14 +08:00
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
NavigationManager.Instance.setSelectedGirlId(this.id);
NavigationManager.Instance.setSelectedCategoryId(
girlData.getGrilCategoryById(this.id)
);
2025-10-10 18:21:33 +08:00
// 以subpanel方式打开GirlDetailPanel
const currentPanel = NavigationManager.Instance.getCurrentActivePanelNode();
if (currentPanel) {
NavigationManager.Instance.openSubPanel(
"GirlDetailPanel",
currentPanel,
this.id,
{
2025-10-13 17:09:39 +08:00
openAnimation: PageTransitionType.NONE,
closeAnimation: PageTransitionType.NONE,
2025-10-10 18:21:33 +08:00
hideBasePanel: true, // 隐藏底层panel,实现全屏显示
}
);
} else {
logger.warn("[RecomandItem] 无法获取当前激活的主页面");
}
2025-10-10 16:09:14 +08:00
}
}