Files
18xchat/assets/Scripts/Sub/UI/SceneBgVideoLayer.ts
T

383 lines
11 KiB
TypeScript
Raw Normal View History

2025-09-11 00:03:22 +08:00
import {
_decorator,
isValid,
Node,
VideoClip,
UITransform,
Vec3,
VideoPlayer,
Size,
} from "cc";
import Utils from "../../Main/Common/Utils";
import li_BaseView from "../../Main/Common/li_BaseView";
import ResManager from "../../Main/Manager/ResManager";
2025-09-16 14:39:14 +08:00
import { DataManager, DataId } from "../../chat18x/data/DataManager";
import { EnvData } from "../../chat18x/data/EnvData";
2025-09-21 20:05:36 +08:00
import { logger } from "db://assets/Scripts/Main/Common/Logger";
2025-07-17 17:18:21 +08:00
const { ccclass, property } = _decorator;
//场景背景层 - 视频模式
2025-09-11 00:03:22 +08:00
@ccclass("SceneBgVideoLayer")
2025-07-17 17:18:21 +08:00
export class SceneBgVideoLayer extends li_BaseView {
2025-09-11 00:03:22 +08:00
private _nodeTab: any = {};
private _data;
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
static handle: SceneBgVideoLayer | null = null;
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
private videoPlayer: VideoPlayer = null;
private isPlaying: boolean = false;
private currentVideoData: any = null;
2025-07-17 17:18:21 +08:00
2025-09-11 18:49:27 +08:00
// 待处理的视频配置(用于在视频准备好后应用)
private pendingVideoConfig: {
size?: { width: number; height: number } | Size;
position?: { x: number; y: number; z?: number } | Vec3;
scale?: number | Vec3;
} | null = null;
2025-09-11 00:03:22 +08:00
//----重写父类接口---------------------------------------
onLoadCT() {
Utils.parseNode(this.node, this._nodeTab);
this.videoPlayer = this._nodeTab.videoPlayer.getComponent(VideoPlayer);
if (!this.videoPlayer) {
2025-09-21 20:05:36 +08:00
logger.error("SceneBgVideoLayer: video节点未找到或没有VideoPlayer组件");
2025-09-11 00:03:22 +08:00
}
this.initVideoEvents();
SceneBgVideoLayer.handle = this;
}
openUIDataCT(data: any): void {
this._data = data;
}
2025-09-19 15:19:59 +08:00
getVideoClone() {
return this.videoPlayer;
}
2025-09-11 00:03:22 +08:00
/**初始化视频事件监听 */
private initVideoEvents() {
if (!this.videoPlayer) return;
// 移除之前的事件监听器,避免重复绑定
this.videoPlayer.node.off(
VideoPlayer.EventType.READY_TO_PLAY,
this.onVideoReadyToPlay,
this
);
//this.videoPlayer.node.off(VideoPlayer.EventType.VIDEO_PLAYER_EVENT, this.onVideoEvent, this);
// 添加新的事件监听
this.videoPlayer.node.on(
VideoPlayer.EventType.READY_TO_PLAY,
this.onVideoReadyToPlay,
this
);
// this.videoPlayer.node.on(VideoPlayer.EventType.VIDEO_PLAYER_EVENT, this.onVideoEvent, this);
}
//================== 公共 API 方法 ==================
/**
2025-09-11 10:54:06 +08:00
* 播放视频 - 统一的视频播放接口
2025-09-11 00:03:22 +08:00
* @param path 视频路径
2025-09-11 10:54:06 +08:00
* @param options 可选配置项
* @param options.loop 是否循环播放,默认true
* @param options.size 视频尺寸 {width, height}
* @param options.position 视频位置 {x, y, z}
* @param options.scale 视频缩放值
* @param options.autoFill 是否自动填充到指定尺寸
2025-09-11 00:03:22 +08:00
*/
2025-09-11 10:54:06 +08:00
playVideo(
path: string,
options?: {
loop?: boolean;
size?: { width: number; height: number } | Size;
position?: { x: number; y: number; z?: number } | Vec3;
scale?: number | Vec3;
}
) {
2025-09-11 00:03:22 +08:00
if (!this.videoPlayer) {
2025-09-21 20:36:19 +08:00
logger.error("SceneBgVideoLayer: VideoPlayer未初始化");
2025-09-11 00:03:22 +08:00
return;
2025-07-17 17:18:21 +08:00
}
2025-09-11 10:54:06 +08:00
// 默认参数
const config = {
loop: options?.loop ?? true,
size: options?.size,
position: options?.position,
};
2025-09-11 18:49:27 +08:00
// 先停止当前视频并清理状态
2025-09-11 10:54:06 +08:00
if (this.isPlaying) {
2025-09-11 18:49:27 +08:00
this.stop();
2025-09-11 10:54:06 +08:00
}
2025-09-11 18:49:27 +08:00
// 存储待处理的配置,在视频准备好后应用
this.pendingVideoConfig = {
size: config.size,
position: config.position,
2025-09-19 15:19:59 +08:00
scale: options?.scale,
2025-09-11 18:49:27 +08:00
};
2025-09-16 14:39:14 +08:00
const envData = DataManager.I.getDataById<EnvData>(DataId.Env);
let newPath = envData.cdn + "/" + "Girls/" + path + ".mp4";
2025-09-16 15:58:44 +08:00
// 加载远程资源
2025-09-16 14:39:14 +08:00
ResManager.I.loadRemoteVideo(
2025-09-11 00:03:22 +08:00
this.videoPlayer,
2025-09-16 14:39:14 +08:00
newPath,
2025-09-11 00:03:22 +08:00
(res: VideoClip) => {
2025-09-11 10:54:06 +08:00
this.videoPlayer.loop = config.loop;
2025-09-11 17:07:29 +08:00
2025-09-11 10:54:06 +08:00
// 设置锚点为中心,确保位置对齐
const uiTransform = this.videoPlayer.node.getComponent(UITransform);
if (uiTransform) {
uiTransform.setAnchorPoint(0.5, 0.5);
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 设置视频锚点为 (0.5, 0.5)");
2025-09-11 10:54:06 +08:00
}
2025-09-11 17:07:29 +08:00
2025-09-11 18:49:27 +08:00
// 立即开始播放,尺寸和位置将在 READY_TO_PLAY 事件中设置
2025-09-11 00:03:22 +08:00
this.videoPlayer.play();
this.isPlaying = true;
2025-09-21 20:05:36 +08:00
logger.log(`SceneBgVideoLayer: 开始加载视频 ${path}, 配置:`, config);
2025-09-11 00:03:22 +08:00
}
);
}
/**
* 暂停视频
*/
pause() {
if (this.videoPlayer && this.isPlaying) {
this.videoPlayer.pause();
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 暂停视频");
2025-09-11 00:03:22 +08:00
}
}
/**
* 继续播放视频
*/
resume() {
if (this.videoPlayer && !this.videoPlayer.isPlaying) {
this.videoPlayer.play();
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 继续播放视频");
2025-09-11 00:03:22 +08:00
}
}
/**
* 停止视频
*/
stop() {
if (this.videoPlayer) {
this.videoPlayer.stop();
2025-09-11 18:49:27 +08:00
this.clearVideoState();
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 停止视频");
2025-09-11 00:03:22 +08:00
}
}
/**
* 设置视频缩放
* @param scale 缩放值(数字或Vec3
*/
setVideoScale(scale: number | Vec3) {
if (this.videoPlayer) {
if (typeof scale === "number") {
this.videoPlayer.node.setScale(new Vec3(scale, scale, 1));
2025-09-21 20:05:36 +08:00
logger.log(`SceneBgVideoLayer: 设置视频缩放 ${scale}`);
2025-09-11 00:03:22 +08:00
} else {
this.videoPlayer.node.setScale(scale);
2025-09-21 20:05:36 +08:00
logger.log(
2025-09-11 00:03:22 +08:00
`SceneBgVideoLayer: 设置视频缩放 (${scale.x}, ${scale.y}, ${scale.z})`
);
}
}
}
/**
* 调整视频以填满指定区域
* @param targetSize 目标尺寸
* @param position 可选的位置
*/
adjustToFillArea(targetSize: Size, position?: Vec3) {
if (!this.videoPlayer) return;
2025-09-11 18:49:27 +08:00
// 如果视频正在播放,直接应用配置
if (this.isPlaying && this.videoPlayer.isPlaying) {
this.applyVideoConfiguration({
size: targetSize,
2025-09-19 15:19:59 +08:00
position: position,
2025-09-11 18:49:27 +08:00
});
} else {
// 如果视频还未准备好,存储配置等待应用
this.pendingVideoConfig = {
size: targetSize,
2025-09-19 15:19:59 +08:00
position: position,
2025-09-11 18:49:27 +08:00
};
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 视频未准备好,配置已存储等待应用");
2025-09-11 18:49:27 +08:00
}
}
2025-09-11 00:03:22 +08:00
2025-09-11 18:49:27 +08:00
/**
* 清除视频状态和待处理配置
*/
private clearVideoState() {
this.isPlaying = false;
this.pendingVideoConfig = null;
this.currentVideoData = null;
}
//================== 内部辅助方法 ==================
/**
* 应用视频配置(位置、尺寸、缩放)
* @param config 视频配置对象
*/
private applyVideoConfiguration(config: {
size?: { width: number; height: number } | Size;
position?: { x: number; y: number; z?: number } | Vec3;
scale?: number | Vec3;
}) {
if (!this.videoPlayer) return;
const uiTransform = this.videoPlayer.node.getComponent(UITransform);
if (!uiTransform) return;
// 应用位置设置
if (config.position) {
const pos = config.position;
let targetWorldPos: Vec3;
if (pos instanceof Vec3) {
targetWorldPos = pos;
} else {
targetWorldPos = new Vec3(pos.x, pos.y, pos.z || 0);
}
this.videoPlayer.node.setWorldPosition(targetWorldPos);
2025-09-21 20:05:36 +08:00
logger.log(
2025-09-19 15:19:59 +08:00
`SceneBgVideoLayer: 设置视频位置 (${targetWorldPos.x}, ${targetWorldPos.y}, ${targetWorldPos.z})`
);
2025-09-11 18:49:27 +08:00
}
// 应用尺寸和缩放设置
if (config.size) {
// 获取视频的原始尺寸(此时应该是准确的)
2025-09-11 00:03:22 +08:00
const videoSize = uiTransform.contentSize;
2025-09-19 15:19:59 +08:00
2025-09-11 00:03:22 +08:00
if (videoSize.width > 0 && videoSize.height > 0) {
2025-09-19 15:19:59 +08:00
const targetWidth =
config.size instanceof Size ? config.size.width : config.size.width;
const targetHeight =
config.size instanceof Size ? config.size.height : config.size.height;
2025-09-11 00:03:22 +08:00
2025-09-11 18:49:27 +08:00
// 计算缩放比例(填满整个区域)
const scaleX = targetWidth / videoSize.width;
const scaleY = targetHeight / videoSize.height;
2025-09-11 00:03:22 +08:00
const scale = Math.max(scaleX, scaleY);
this.videoPlayer.node.setScale(new Vec3(scale, scale, 1));
2025-09-21 20:05:36 +08:00
logger.log(
2025-09-19 15:19:59 +08:00
`SceneBgVideoLayer: 设置视频缩放 ${scale} (原始尺寸: ${videoSize.width}x${videoSize.height}, 目标尺寸: ${targetWidth}x${targetHeight})`
);
2025-09-11 18:49:27 +08:00
} else {
2025-09-21 20:05:36 +08:00
logger.warn("SceneBgVideoLayer: 无法获取视频尺寸,跳过缩放设置");
2025-09-11 00:03:22 +08:00
}
2025-09-11 18:49:27 +08:00
} else if (config.scale) {
// 如果显式传入了 scale 参数,直接应用
if (typeof config.scale === "number") {
this.videoPlayer.node.setScale(new Vec3(config.scale, config.scale, 1));
2025-09-21 20:05:36 +08:00
logger.log(`SceneBgVideoLayer: 设置显式缩放 ${config.scale}`);
2025-09-11 18:49:27 +08:00
} else {
this.videoPlayer.node.setScale(config.scale);
2025-09-21 20:05:36 +08:00
logger.log(
2025-09-19 15:19:59 +08:00
`SceneBgVideoLayer: 设置显式缩放 (${config.scale.x}, ${config.scale.y}, ${config.scale.z})`
);
2025-09-11 18:49:27 +08:00
}
} else {
// 没有指定尺寸或缩放,保持原始缩放
this.videoPlayer.node.setScale(Vec3.ONE);
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 保持默认缩放 (1, 1, 1)");
2025-08-13 16:17:44 +08:00
}
2025-09-11 00:03:22 +08:00
}
2025-08-13 16:17:44 +08:00
2025-09-11 00:03:22 +08:00
//================== 事件回调 ==================
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
private onVideoReadyToPlay = () => {
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 视频准备完成");
2025-09-19 15:19:59 +08:00
2025-09-11 18:49:27 +08:00
// 应用待处理的视频配置
if (this.pendingVideoConfig) {
this.applyVideoConfiguration(this.pendingVideoConfig);
// 清空待处理配置
this.pendingVideoConfig = null;
}
2025-09-19 15:19:59 +08:00
2025-09-11 00:03:22 +08:00
this.onVideoReady();
};
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
private onVideoEvent = (event: any, eventType: string) => {
switch (eventType) {
case "completed":
2025-09-21 20:05:36 +08:00
logger.log("SceneBgVideoLayer: 视频播放完成");
2025-09-11 18:49:27 +08:00
this.clearVideoState();
2025-09-11 00:03:22 +08:00
this.onVideoEnd();
break;
case "error":
2025-09-21 20:05:36 +08:00
logger.error("SceneBgVideoLayer: 视频播放错误");
2025-09-11 18:49:27 +08:00
this.clearVideoState();
2025-09-11 00:03:22 +08:00
this.onVideoError();
break;
}
};
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
/**
* 视频准备完成回调(可重写)
*/
protected onVideoReady() {
// 由子类重写或外部监听
}
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
/**
* 视频播放完成回调(可重写)
*/
protected onVideoEnd() {
// 由子类重写或外部监听
}
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
/**
* 视频播放错误回调(可重写)
*/
protected onVideoError() {
// 由子类重写或外部监听
}
2025-07-17 17:18:21 +08:00
2025-09-11 00:03:22 +08:00
//================== 其他方法 ==================
/**
* 获取当前播放状态
*/
getIsPlaying(): boolean {
return this.isPlaying;
}
/**
* 获取VideoPlayer组件引用
*/
getVideoPlayer(): VideoPlayer | null {
return this.videoPlayer;
}
/**
* 销毁时清理资源
*/
onDestroy() {
if (this.videoPlayer) {
2025-09-11 18:49:27 +08:00
this.videoPlayer.stop();
2025-09-11 00:03:22 +08:00
this.videoPlayer.node.off(
VideoPlayer.EventType.READY_TO_PLAY,
this.onVideoReadyToPlay,
this
);
//this.videoPlayer.node.off(VideoPlayer.EventType.VIDEO_PLAYER_EVENT, this.onVideoEvent, this);
}
2025-09-11 18:49:27 +08:00
this.clearVideoState();
2025-09-11 00:03:22 +08:00
SceneBgVideoLayer.handle = null;
}
2025-07-17 17:18:21 +08:00
}