248 lines
6.6 KiB
TypeScript
248 lines
6.6 KiB
TypeScript
import {
|
|||
|
|
_decorator,
|
||
|
|
Component,
|
||
|
|
Label,
|
||
|
|
Node,
|
||
|
|
Size,
|
||
|
|
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("RankItem")
|
||
|
|
export class RankItem extends Component {
|
||
|
|
private _nodeTab: any = {};
|
||
|
|
|
||
|
|
private rankIndex: Label;
|
||
|
|
private lv1: Node;
|
||
|
|
private lv2: Node;
|
||
|
|
private lv3: Node;
|
||
|
|
|
||
|
|
private rankPic: Sprite;
|
||
|
|
private girlName: Label;
|
||
|
|
private tag: Label;
|
||
|
|
|
||
|
|
private heartParent: Node;
|
||
|
|
private hearts: Sprite[] = [];
|
||
|
|
|
||
|
|
private chatBtn: Node;
|
||
|
|
private detailBtn: Node;
|
||
|
|
|
||
|
|
private id: number;
|
||
|
|
|
||
|
|
// 标记是否已初始化(用于虚拟列表复用)
|
||
|
|
public _initialized: boolean = false;
|
||
|
|
|
||
|
|
// 兼容原有调用方式的 init 方法
|
||
|
|
init() {
|
||
|
|
if (this._initialized) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Utils.parseNode(this.node, this._nodeTab);
|
||
|
|
|
||
|
|
this.rankIndex = this._nodeTab.rankLabel?.getComponent(Label);
|
||
|
|
this.lv1 = this._nodeTab.lv1;
|
||
|
|
this.lv2 = this._nodeTab.lv2;
|
||
|
|
this.lv3 = this._nodeTab.lv3;
|
||
|
|
|
||
|
|
this.rankPic = this._nodeTab.avatar.getComponent(Sprite);
|
||
|
|
this.girlName = this._nodeTab.name?.getComponent(Label);
|
||
|
|
this.tag = this._nodeTab.tag?.getComponent(Label);
|
||
|
|
|
||
|
|
this.heartParent = this._nodeTab.heartBg;
|
||
|
|
|
||
|
|
if (this.heartParent) {
|
||
|
|
this.heartParent.children.forEach((a) =>
|
||
|
|
this.hearts.push(a.getComponent(Sprite))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
this.chatBtn = this._nodeTab.chatBtn;
|
||
|
|
this.detailBtn = this._nodeTab.detailBtn;
|
||
|
|
|
||
|
|
if (this.chatBtn) {
|
||
|
|
GButton.BandClick(this.chatBtn, this.chatToHer, this);
|
||
|
|
}
|
||
|
|
if (this.detailBtn) {
|
||
|
|
GButton.BandClick(this.detailBtn, this.gotoDetail, this);
|
||
|
|
}
|
||
|
|
|
||
|
|
this._initialized = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 节点回收到对象池时调用,重置状态
|
||
|
|
reset(): void {
|
||
|
|
this.id = -1;
|
||
|
|
this.node.active = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
picSize: Size = new Size(100, 100);
|
||
|
|
/**
|
||
|
|
* 刷新排行榜项显示
|
||
|
|
* @param girlId 技师ID
|
||
|
|
* @param rankIndex 排名序号(从1开始)
|
||
|
|
*/
|
||
|
|
refresh(girlId: number, rankIndex: number) {
|
||
|
|
this.id = girlId;
|
||
|
|
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||
|
|
const category = girlData.getGrilCategoryById(girlId);
|
||
|
|
|
||
|
|
// 显示排名
|
||
|
|
if (this.rankIndex) {
|
||
|
|
this.lv1.active = rankIndex === 1;
|
||
|
|
this.lv2.active = rankIndex === 2;
|
||
|
|
this.lv3.active = rankIndex === 3;
|
||
|
|
|
||
|
|
this.rankIndex.node.active = rankIndex > 3;
|
||
|
|
this.rankIndex.string = rankIndex.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 数据
|
||
|
|
let nameKey = girlData.getGrilName(category.toString(), this.id);
|
||
|
|
if (this.girlName) {
|
||
|
|
this.girlName.string = LanguageUtils.getText(nameKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
let tagKey = girlData.getGrilTagKey(category.toString(), this.id);
|
||
|
|
if (this.tag) {
|
||
|
|
this.tag.string = LanguageUtils.getText(tagKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (this.heartParent) {
|
||
|
|
this.heartParent.active = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 加载头像
|
||
|
|
let avatarPath = girlData.getGrilAvatar(category.toString(), this.id);
|
||
|
|
|
||
|
|
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
|
||
|
|
let newPath = envData.cdn + "/" + "Girls/" + avatarPath + ".png";
|
||
|
|
|
||
|
|
if (this.rankPic) {
|
||
|
|
ResManager.I.changeRemoteSpriteFrame(this.rankPic, newPath, () => {
|
||
|
|
this.doScaleToFill(this.rankPic, this.picSize);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 显示好感度
|
||
|
|
const star = girlData.getStar(category.toString(), this.id);
|
||
|
|
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"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
doScaleToFill(img: Sprite, size: Size) {
|
||
|
|
if (!img || !img.spriteFrame) return;
|
||
|
|
|
||
|
|
const itemSize = size;
|
||
|
|
const imageSize = img.spriteFrame.originalSize;
|
||
|
|
|
||
|
|
if (
|
||
|
|
itemSize.width === 0 ||
|
||
|
|
itemSize.height === 0 ||
|
||
|
|
imageSize.width === 0 ||
|
||
|
|
imageSize.height === 0
|
||
|
|
) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const scaleX = itemSize.width / imageSize.width;
|
||
|
|
const scaleY = itemSize.height / imageSize.height;
|
||
|
|
const scale = Math.max(scaleX, scaleY);
|
||
|
|
|
||
|
|
// 设置Sprite的sizeMode为CUSTOM,允许自定义大小
|
||
|
|
img.sizeMode = Sprite.SizeMode.CUSTOM;
|
||
|
|
|
||
|
|
// 获取图片节点的UITransform并设置大小
|
||
|
|
const imgTransform = img.node.getComponent(UITransform);
|
||
|
|
if (imgTransform) {
|
||
|
|
imgTransform.setContentSize(
|
||
|
|
imageSize.width * scale,
|
||
|
|
imageSize.height * scale
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* 聊天按钮点击事件
|
||
|
|
*/
|
||
|
|
chatToHer() {
|
||
|
|
const girlData = DataManager.I.getDataById<GirlData>(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("[RankItem] 无法获取当前激活的主页面");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 详情按钮点击事件
|
||
|
|
*/
|
||
|
|
gotoDetail() {
|
||
|
|
const girlData = DataManager.I.getDataById<GirlData>(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("[RankItem] 无法获取当前激活的主页面");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|