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"; import { NavigationManager, PanelType, PageTransitionType, } from "../manager/NavigationManager"; import { logger } from "../../Main/Common/Logger"; 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; // 标记是否已初始化(用于虚拟列表复用) public _initialized: boolean = false; // 兼容原有调用方式的 init 方法 init() { if (this._initialized) { return; } 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); GButton.BandClick(this._nodeTab.detailBtn, this.gotoDetail, this); this._initialized = true; } // 节点回收到对象池时调用,重置状态 reset(): void { this.id = -1; this.node.active = false; } refresh(girlId: number) { this.id = girlId; const girlData = DataManager.I.getDataById(DataId.Girl); const category = girlData.getGrilCategoryById(girlId); // 数据 let nameKey = girlData.getGrilName(category.toString(), this.id); this.girlName.string = LanguageUtils.getText(nameKey); let tagKey = girlData.getGrilTagKey(category.toString(), this.id); this.tag.string = LanguageUtils.getText(tagKey); this.heartParent.active = true; let avatarPath = girlData.getGrilAvatar(category.toString(), this.id); const envData = DataManager.I.getDataById(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); //logger.log("好感度:" + star); for (let i = 0; i < this.hearts.length; i++) { const element = this.hearts[i]; if (star > i) { ResManager.I.changeResourceSpriteFrame( element, "ui/common/heart/heart" ); } else { ResManager.I.changeResourceSpriteFrame( element, "ui/common/heart/heart_empty" ); } } } chatToHer() { const girlData = DataManager.I.getDataById(DataId.Girl); NavigationManager.Instance.setSelectedGirlId(this.id); NavigationManager.Instance.setSelectedCategoryId( girlData.getGrilCategoryById(this.id) ); // 直接在当前页面打开ChatPanel作为子页面 const currentPanel = NavigationManager.Instance.getCurrentActivePanelNode(); if (currentPanel) { const chatData = { girlId: this.id, base: currentPanel }; NavigationManager.Instance.openSubPanel( "ChatPanel", currentPanel, chatData, { openAnimation: PageTransitionType.NONE, closeAnimation: PageTransitionType.NONE, hideBasePanel: true, } ); } else { logger.warn("[RecomandItem] 无法获取当前激活的主页面"); } } gotoDetail() { const girlData = DataManager.I.getDataById(DataId.Girl); NavigationManager.Instance.setSelectedGirlId(this.id); NavigationManager.Instance.setSelectedCategoryId( girlData.getGrilCategoryById(this.id) ); // 以subpanel方式打开GirlDetailPanel const currentPanel = NavigationManager.Instance.getCurrentActivePanelNode(); if (currentPanel) { NavigationManager.Instance.openSubPanel( "GirlDetailPanel", currentPanel, this.id, { openAnimation: PageTransitionType.NONE, closeAnimation: PageTransitionType.NONE, hideBasePanel: true, // 隐藏底层panel,实现全屏显示 } ); } else { logger.warn("[RecomandItem] 无法获取当前激活的主页面"); } } }