Files
18xchat/assets/Scripts/chat18x/uiitems/DetailImageItem.ts
T
2025-10-14 15:19:12 +08:00

276 lines
7.3 KiB
TypeScript

import {
_decorator,
Component,
Label,
math,
Node,
randomRange,
Sprite,
UITransform,
View,
} from "cc";
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
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";
import { EnvData } from "../data/EnvData";
import { NavigationManager } from "../manager/NavigationManager";
import { GirlService } from "db://assets/Scripts/chat18x/network/services/GirlService";
const { ccclass, property } = _decorator;
@ccclass("DetailImageItem")
export class DetailImageItem extends Component {
@property(Sprite)
img: Sprite;
@property(Sprite)
video: Sprite;
@property(Sprite)
videoReady: Sprite;
@property(Node)
priceFrame: Node;
@property(Label)
videoPrice: Label;
base: GirlDetailPanel;
url: string;
isImage: boolean;
uitransform: UITransform;
category: number;
girlId: number;
resId: number;
type: proto.cs.EnmResType;
onLoad() {
GButton.BandClick(this.node, this.onClickThis, this);
this.uitransform = this.node.getComponent(UITransform);
}
isVisible: boolean;
onClickThis() {
// 数据
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
// 是否可见,不可见代表需要解锁
if (this.isVisible) {
let data = {
url: this.url,
isImg: this.isImage,
closeFunc: () => {
this.base.setVideEnable(true);
},
};
if (this.url) {
NavigationManager.Instance.openPopupPanel("ShowPanel", data);
this.base.setVideEnable(false);
// 查看图片或者视频上报,用于排行榜
this.reqViewGirl(this.girlId);
}
} else {
NavigationManager.Instance.openPopupPanel("PopupGirlDetailPanel", {
category: this.category,
resId: this.resId,
girlId: this.girlId,
base: this,
type: this.type,
});
// ViewManager.I.openBundlesView("PopupGirlDetailPanel", {
// category: this.category,
// resId: this.resId,
// girlId: this.girlId,
// base: this,
// type: this.type,
// });
}
// if (!this.isImage) {
// //判断是否已经解锁此资源
// } else {
// if (this.url) {
// ViewManager.I.openBundlesView("ShowPanel", data);
// this.base.setVideEnable(false);
// }
// }
}
/**
* 查看图片或者视频上报,用于排行榜
* @param girlId 角色Id
*/
private async reqViewGirl(girlId: number) {
const reqData = {
girlId,
};
let res = await GirlService.I.reqViewGirl(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const resData = res.data;
}
}
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: proto.cs.EnmResType,
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 === proto.cs.EnmResType.ERT_Video;
this.videoReady.enabled = false;
this.priceFrame.active = type === proto.cs.EnmResType.ERT_Video;
let price = 0;
// 是否可见,不可见代表需要解锁
if (this.type === proto.cs.EnmResType.ERT_Image) {
// 图片
this.isVisible = girlData.isGirlPhotoVisible(
this.category.toString(),
this.girlId,
this.resId
);
} else if (this.type === proto.cs.EnmResType.ERT_Video) {
// 视频
this.isVisible = girlData.isGirlVideoVisible(
this.category.toString(),
this.girlId,
this.resId
);
if (this.isVisible) {
this.video.enabled = false;
this.videoReady.enabled = true;
} else {
this.video.enabled = true;
this.videoReady.enabled = false;
}
}
if (type === proto.cs.EnmResType.ERT_Image) {
// 图片
price = girlData.getGrilPhotoOriginalPrice(
this.category.toString(),
this.girlId,
this.resId
);
this.url = girlData.getGrilPhotoPic(
this.category.toString(),
this.girlId,
this.resId
);
this.isImage = true;
} else if (type === proto.cs.EnmResType.ERT_Video) {
// 视频
price = girlData.getGrilVideoOriginalPrice(
this.category.toString(),
this.girlId,
this.resId
);
this.isImage = false;
this.url = girlData.getGrilVideoPath(
this.category.toString(),
this.girlId,
this.resId
);
}
if (this.isVisible) {
imgPath = this.url + "_thumbnail";
this.priceFrame.active = false;
} else {
this.priceFrame.active = true;
this.videoPrice.string = price.toString();
imgPath = this.url + "_thumbnail_blur";
}
// 加载远程资源
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
let newPath = envData.cdn + "/" + "Girls/" + imgPath + ".jpg";
ResManager.I.changeRemoteSpriteFrame(this.img, newPath, () => {
this.scaleToFillItem();
});
}
refreshVideoBuy(success: boolean) {
if (success) {
this.priceFrame.active = false;
this.isVisible = true;
const imgPath = this.url + "_thumbnail";
// 加载远程资源
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
let newPath = envData.cdn + "/" + "Girls/" + imgPath + ".jpg";
ResManager.I.changeRemoteSpriteFrame(this.img, newPath, () => {
this.scaleToFillItem();
});
}
}
reset() {
this.base = null;
this.url = "";
this.isImage = false;
this.category = 0;
this.girlId = 0;
this.resId = 0;
this.type = null;
this.isVisible = false;
this.video.enabled = false;
this.priceFrame.active = false;
this.videoPrice.string = "";
if (this.img && this.img.spriteFrame) {
this.img.spriteFrame = null;
}
}
}