Files
18xchat/assets/Scripts/chat18x/uiitems/DetailImageItem.ts
T
2025-08-26 14:23:02 +08:00

101 lines
2.6 KiB
TypeScript

import { _decorator, Component, math, Node, Sprite, UITransform } 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";
const { ccclass, property } = _decorator;
@ccclass("DetailImageItem")
export class DetailImageItem extends Component {
@property(Sprite)
img: Sprite;
@property(Node)
lock: Node;
@property(Node)
question: Node;
base: GirlDetailPanel;
url: string;
isImage: boolean;
uitransform: UITransform;
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);
},
};
if (this.url) {
ViewManager.I.openBundlesView("ShowPanel", data);
this.base.setVideEnable(false);
}
}
scaleToFillItem() {
if (!this.img || !this.img.spriteFrame) return;
// 延迟一帧确保UI布局完成
this.scheduleOnce(() => {
this.doScaleToFill();
}, 0);
}
doScaleToFill() {
if (!this.img || !this.img.spriteFrame) return;
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(path: string, base: GirlDetailPanel, isImage: boolean = true) {
this.base = base;
this.lock.active = false;
this.question.active = false;
this.url = path;
let imgPath = this.url;
this.isImage = isImage;
if (!isImage) {
imgPath = this.url + "_thumbnail";
}
ResManager.I.changeBundleSpriteFrame(this.img, imgPath, "Chat18x", () => {
this.scaleToFillItem();
});
}
}