112 lines
2.5 KiB
TypeScript
112 lines
2.5 KiB
TypeScript
import {
|
|
_decorator,
|
|
Component,
|
|
Node,
|
|
Sprite,
|
|
UITransform,
|
|
VideoPlayer,
|
|
view,
|
|
} 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";
|
|
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(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);
|
|
|
|
// 添加视频加载完成回调
|
|
this.videoPlayer.node.on(
|
|
VideoPlayer.EventType.READY_TO_PLAY,
|
|
() => {
|
|
this.adjustVideoScale();
|
|
},
|
|
this
|
|
);
|
|
}
|
|
|
|
start() {
|
|
//this.splash.node.on(Node.EventType.TOUCH_START,this.onclickback);
|
|
GButton.BandClick(this.splash.node, this.onClose, this);
|
|
}
|
|
|
|
protected onClose() {
|
|
if (this.func) {
|
|
this.func();
|
|
}
|
|
super.onClose();
|
|
}
|
|
|
|
show(path: string) {
|
|
if (this.isImg) {
|
|
ResManager.I.changeBundleSpriteFrame(this.image, path, "Chat18x", () => {
|
|
Utils.adjustBgPixelRatio(this.image.node, 3);
|
|
});
|
|
} else {
|
|
ResManager.I.changeBundleVideo(this.videoPlayer, path, "Chat18x", () => {
|
|
this.adjustVideoScale();
|
|
});
|
|
}
|
|
}
|
|
|
|
adjustVideoScale() {
|
|
if (!this.videoPlayer) {
|
|
return;
|
|
}
|
|
|
|
const tryAdjustScale = () => {
|
|
const screenSize = view.getVisibleSize();
|
|
|
|
const videoTransform = this.videoPlayer.node.getComponent(UITransform);
|
|
|
|
if (videoTransform && videoTransform.height > 0) {
|
|
let scale = screenSize.width / videoTransform.width;
|
|
this.videoPlayer.node.setScale(scale, scale, 1);
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
// 立即尝试一次
|
|
if (!tryAdjustScale()) {
|
|
// 如果失败,延迟尝试
|
|
this.scheduleOnce(() => {
|
|
if (!tryAdjustScale()) {
|
|
// 再次延迟尝试
|
|
this.scheduleOnce(() => {
|
|
tryAdjustScale();
|
|
}, 0.5);
|
|
}
|
|
}, 0.1);
|
|
}
|
|
}
|
|
}
|