149 lines
4.4 KiB
TypeScript
149 lines
4.4 KiB
TypeScript
import {
|
|
_decorator,
|
|
Component,
|
|
Node,
|
|
Sprite,
|
|
UITransform,
|
|
VideoPlayer,
|
|
VideoClip,
|
|
view,
|
|
Size,
|
|
Vec3,
|
|
} from "cc";
|
|
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
|
|
import ResManager from "db://assets/Scripts/Main/Manager/ResManager";
|
|
import Utils from "db://assets/Scripts/Main/Common/Utils";
|
|
import { ViewManager } from "db://assets/Scripts/Main/Manager/ViewManager";
|
|
import { GButton } from "db://assets/Scripts/Main/Common/GButton";
|
|
import { SceneBgVideoLayer } from "../../../Sub/UI/SceneBgVideoLayer";
|
|
import { DataManager, DataId } from "../../data/DataManager";
|
|
import { EnvData } from "../../data/EnvData";
|
|
const { ccclass, property } = _decorator;
|
|
|
|
interface ShowPanelData {
|
|
url: string;
|
|
isImg: boolean;
|
|
closeFunc: Function;
|
|
}
|
|
|
|
@ccclass("ShowPanel")
|
|
export class ShowPanel extends li_BaseView {
|
|
@property(Sprite)
|
|
image: Sprite;
|
|
@property(Sprite)
|
|
splash: Sprite;
|
|
@property(UITransform)
|
|
videoArea: UITransform;
|
|
@property(VideoPlayer)
|
|
videoPlayer: VideoPlayer;
|
|
|
|
url: string;
|
|
func: Function;
|
|
isImg: boolean;
|
|
openUIDataCT(data: ShowPanelData) {
|
|
super.openUIDataCT(data);
|
|
this.url = data.url;
|
|
this.isImg = data.isImg;
|
|
this.func = data.closeFunc;
|
|
}
|
|
|
|
onLoadCT() {
|
|
this.show(this.url);
|
|
}
|
|
|
|
start() {
|
|
//this.splash.node.on(Node.EventType.TOUCH_START,this.onclickback);
|
|
GButton.BandClick(this.splash.node, this.onClose, this);
|
|
}
|
|
|
|
protected onClose() {
|
|
// 停止视频播放
|
|
if (this.videoPlayer) {
|
|
this.videoPlayer.stop();
|
|
}
|
|
|
|
if (this.func) {
|
|
this.func();
|
|
}
|
|
super.onClose();
|
|
}
|
|
|
|
show(path: string) {
|
|
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
|
|
if (this.isImg) {
|
|
// ResManager.I.changeBundleSpriteFrame(this.image, path, "Girls", () => {
|
|
// Utils.adjustBgPixelRatio(this.image.node, 3);
|
|
// });
|
|
// 加载远程资源
|
|
let newPath = envData.cdn + "/" + "Girls/" + path + ".png";
|
|
ResManager.I.changeRemoteSpriteFrame(this.image, newPath, () => {
|
|
Utils.adjustBgPixelRatio(this.image.node, 3);
|
|
});
|
|
} else {
|
|
// 使用自己的 videoPlayer 进行置顶显示
|
|
if (this.videoPlayer) {
|
|
const targetSize = this.videoArea
|
|
? new Size(this.videoArea.width, this.videoArea.height)
|
|
: new Size(600, 800);
|
|
// 加载远程资源
|
|
let newPath = envData.cdn + "/" + "Girls/" + path + ".mp4";
|
|
ResManager.I.loadRemoteVideo(
|
|
this.videoPlayer,
|
|
newPath,
|
|
(res: VideoClip) => {
|
|
// 设置视频大小和位置
|
|
const uiTransform = this.videoPlayer.node.getComponent(UITransform);
|
|
if (uiTransform && this.videoArea) {
|
|
uiTransform.setContentSize(targetSize.width, targetSize.height);
|
|
// 设置位置为 videoArea 的位置
|
|
const pos = this.videoArea.node.getWorldPosition();
|
|
this.videoPlayer.node.setWorldPosition(pos);
|
|
}
|
|
|
|
this.videoPlayer.loop = true;
|
|
this.videoPlayer.play();
|
|
|
|
// 延迟调整缩放以填充区域
|
|
this.scheduleOnce(() => {
|
|
this.adjustVideoScale();
|
|
}, 0.1);
|
|
}
|
|
);
|
|
} else {
|
|
// 如果没有配置 videoPlayer,使用 SceneBgVideoLayer 作为后备
|
|
const targetSize = this.videoArea
|
|
? new Size(this.videoArea.width, this.videoArea.height)
|
|
: new Size(600, 800);
|
|
const videoAreaPos = this.videoArea
|
|
? this.videoArea.node.getWorldPosition()
|
|
: undefined;
|
|
|
|
if (SceneBgVideoLayer.handle) {
|
|
SceneBgVideoLayer.handle.playVideo(path, {
|
|
loop: true,
|
|
size: targetSize,
|
|
position: videoAreaPos,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 调整视频缩放以填满区域
|
|
*/
|
|
private adjustVideoScale() {
|
|
if (!this.videoPlayer || !this.videoArea) return;
|
|
|
|
const videoTransform = this.videoPlayer.node.getComponent(UITransform);
|
|
if (videoTransform && videoTransform.height > 0) {
|
|
const scaleX = this.videoArea.width / videoTransform.width;
|
|
const scaleY = this.videoArea.height / videoTransform.height;
|
|
const scale = Math.max(scaleX, scaleY); // 填满整个区域
|
|
|
|
this.videoPlayer.node.setScale(new Vec3(scale, scale, 1));
|
|
console.log(`ShowPanel: 调整视频缩放到 ${scale}`);
|
|
}
|
|
}
|
|
}
|