Files
18xchat/assets/Scripts/chat18x/uiitems/DetailImageItem.ts
T

100 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-08-13 16:17:44 +08:00
import { _decorator, Component, math, Node, Sprite, UITransform } from "cc";
2025-08-12 16:58:02 +08:00
import { Config18x } from "db://assets/Scripts/Main/Config/Config18x";
2025-08-01 18:28:36 +08:00
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
2025-08-12 16:58:02 +08:00
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
import { GirlDetailPanel } from "../ui/panels/GirlDetailPanel";
2025-08-01 18:28:36 +08:00
2025-08-12 16:58:02 +08:00
const { ccclass, property } = _decorator;
2025-08-01 18:28:36 +08:00
2025-08-12 16:58:02 +08:00
@ccclass("DetailImageItem")
2025-08-01 18:28:36 +08:00
export class DetailImageItem extends Component {
2025-08-12 16:58:02 +08:00
@property(Sprite)
img: Sprite;
2025-08-01 18:28:36 +08:00
2025-08-12 16:58:02 +08:00
@property(Node)
lock: Node;
@property(Node)
question: Node;
2025-08-01 18:28:36 +08:00
2025-08-12 16:58:02 +08:00
base: GirlDetailPanel;
url: string;
2025-08-26 14:23:02 +08:00
isImage: boolean;
2025-08-13 16:17:44 +08:00
uitransform: UITransform;
2025-08-12 16:58:02 +08:00
onLoad() {
GButton.BandClick(this.node, this.onClickThis, this);
2025-08-13 16:17:44 +08:00
this.uitransform = this.node.getComponent(UITransform);
2025-08-12 16:58:02 +08:00
}
onClickThis() {
let data = {
url: this.url,
2025-08-26 14:23:02 +08:00
isImg: this.isImage,
2025-08-12 16:58:02 +08:00
closeFunc: () => {
this.base.setVideEnable(true);
},
};
if (this.url) {
ViewManager.I.openBundlesView("ShowPanel", data);
this.base.setVideEnable(false);
2025-08-01 18:28:36 +08:00
}
2025-08-12 16:58:02 +08:00
}
2025-08-13 16:17:44 +08:00
scaleToFillItem() {
if (!this.img || !this.img.spriteFrame) return;
// 延迟一帧确保UI布局完成
2025-08-27 16:35:26 +08:00
this.doScaleToFill();
2025-08-13 16:17:44 +08:00
}
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;
2025-08-25 20:15:42 +08:00
2025-08-13 16:17:44 +08:00
// 获取图片节点的UITransform并设置大小
const imgTransform = this.img.node.getComponent(UITransform);
if (imgTransform) {
2025-08-25 20:15:42 +08:00
imgTransform.setContentSize(
imageSize.width * scale,
imageSize.height * scale
2025-08-13 16:17:44 +08:00
);
2025-08-01 18:28:36 +08:00
}
2025-08-12 16:58:02 +08:00
}
2025-08-25 20:15:42 +08:00
refresh(path: string, base: GirlDetailPanel, isImage: boolean = true) {
this.base = base;
this.lock.active = false;
2025-08-27 16:35:26 +08:00
this.question.active = true; // 显示问号,表示加载中
2025-08-25 20:15:42 +08:00
this.url = path;
let imgPath = this.url;
2025-08-26 14:23:02 +08:00
this.isImage = isImage;
2025-08-25 20:15:42 +08:00
if (!isImage) {
imgPath = this.url + "_thumbnail";
}
ResManager.I.changeBundleSpriteFrame(this.img, imgPath, "Chat18x", () => {
2025-08-27 16:35:26 +08:00
this.question.active = false; // 图片加载成功后隐藏问号
2025-08-25 20:15:42 +08:00
this.scaleToFillItem();
});
}
2025-08-01 18:28:36 +08:00
}