Files
18xchat/assets/Scripts/chat18x/uiitems/DetailImageItem.ts
T
2025-09-09 16:09:10 +08:00

180 lines
4.4 KiB
TypeScript

import {
_decorator,
Component,
Label,
math,
Node,
Sprite,
UITransform,
View,
} 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 "../ui/panels/GirlDetailPanel";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import { DataManager, DataId } from "../data/DataManager";
import { GirlData } from "../data/GirlData";
const { ccclass, property } = _decorator;
@ccclass("DetailImageItem")
export class DetailImageItem extends Component {
@property(Sprite)
img: Sprite;
@property(Node)
question: Node;
@property(Sprite)
video: Sprite;
@property(Node)
priceFrame: Node;
@property(Label)
videoPrice: Label;
base: GirlDetailPanel;
url: string;
isImage: boolean;
uitransform: UITransform;
category: number;
girlId: number;
resId: number;
type: number;
onLoad() {
GButton.BandClick(this.node, this.onClickThis, this);
this.uitransform = this.node.getComponent(UITransform);
}
onClickThis() {
let data = {
url: this.url,
isImg: this.isImage,
closeFunc: () => {
this.base.setVideEnable(true);
},
};
//判断是否已经解锁此资源
const isRelease = false;
if (isRelease) {
if (this.url) {
ViewManager.I.openBundlesView("ShowPanel", data);
this.base.setVideEnable(false);
}
} else {
ViewManager.I.openBundlesView("PopupGirlDetailPanel", {
resId: this.resId,
girlId: this.girlId,
});
}
}
scaleToFillItem() {
if (!this.img || !this.img.spriteFrame) return;
// 延迟一帧确保UI布局完成
this.doScaleToFill();
}
doScaleToFill() {
if (!this.img || !this.img.spriteFrame) return;
if (!this.uitransform)
this.uitransform = this.node.getComponent(UITransform);
const itemSize = this.uitransform.contentSize;
const imageSize = this.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,允许自定义大小
this.img.sizeMode = Sprite.SizeMode.CUSTOM;
// 获取图片节点的UITransform并设置大小
const imgTransform = this.img.node.getComponent(UITransform);
if (imgTransform) {
imgTransform.setContentSize(
imageSize.width * scale,
imageSize.height * scale
);
}
}
refresh(
base: GirlDetailPanel,
type: number,
category: number,
id: number,
resId: number
) {
this.base = base;
this.type = type;
this.category = category;
this.girlId = id;
this.resId = resId;
this.question.active = true; // 显示问号,表示加载中
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
let imgPath = "";
this.video.enabled = type === 2;
this.priceFrame.active = type === 2;
if (type === 1) {
// 图片
this.url = girlData.getGrilPhotoPic(
this.category.toString(),
this.girlId,
this.resId
);
imgPath = this.url;
this.isImage = true;
} else if (type === 2) {
// 视频
this.isImage = false;
this.url = girlData.getGrilVideoPath(
this.category.toString(),
this.girlId,
this.resId
);
let videoPrice = girlData.getGrilVideoPrice(
this.category.toString(),
this.girlId,
this.resId
);
this.url = girlData.getGrilVideoPath(
this.category.toString(),
this.girlId,
this.resId
);
if (videoPrice > 0) {
imgPath = this.url + "_thumbnail_blur";
} else {
imgPath = this.url + "_thumbnail";
}
this.priceFrame.active = videoPrice > 0;
this.videoPrice.string = videoPrice.toString();
}
ResManager.I.changeBundleSpriteFrame(this.img, imgPath, "Chat18x", () => {
this.question.active = false; // 图片加载成功后隐藏问号
this.scaleToFillItem();
});
}
}