using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.IO;
using System.Collections.Generic;
using I2.Loc;
using System.Collections;
using System;
using ChivaXR;
using UnityEngine.Video;
namespace QFramework
{
public class MattersNeedingAttentionFromData : UIPanelData
{
public OperationStepDataInfo operationStepDataInfo;
///
/// 是否需要记录
///
public bool isNeedLog;
}
public partial class MattersNeedingAttentionFrom : UIPanel
{
private float heightBasic = 295.5898f;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as MattersNeedingAttentionFromData ?? new MattersNeedingAttentionFromData();
ConfirmBtn.onClick.AddListener(OnConfirmBtnClick);
VoiceBtn.onClick.AddListener(OnVoiceBtnClick);
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as MattersNeedingAttentionFromData ?? new MattersNeedingAttentionFromData();
OperationStepDataInfo tmpOperationStepDataInfo = mData.operationStepDataInfo;
//如果没有值则直接关掉
if (string.IsNullOrEmpty(tmpOperationStepDataInfo.stepMattersNeedingAttention) &&
string.IsNullOrEmpty(tmpOperationStepDataInfo.stepVideoNames) &&
string.IsNullOrEmpty(tmpOperationStepDataInfo.stepPictureNames))
{
CloseSelf();
return;
}
if (mData.isNeedLog)
{
LogSystemForm tmpLogSystemForm = UIKit.GetPanel();
if (tmpLogSystemForm == null)
{
tmpLogSystemForm = UIKit.OpenPanel();
}
tmpLogSystemForm.AddCurrentStepLog("注意事项");
}
List tmpVideoNames = tmpOperationStepDataInfo.GetVideoNames();
//注意事项视频
if (tmpVideoNames != null && tmpVideoNames.Count > 0)
{
VideoBg.gameObject.SetActive(true);
VideoBg.url = Path.Combine(GlobalConfig.videoPath, OperateSetting.Instance.m_CourseName, tmpVideoNames[0]);
VideoBg.Play();
//学习模式视频播放完毕自动关闭
if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Learn)
{
VideoBg.prepareCompleted += VideoBg_prepareCompleted;
}
}
else
{
VideoBg.gameObject.SetActive(false);
VideoBg.url = null;
}
//注意事项文字
if (!string.IsNullOrEmpty(tmpOperationStepDataInfo.stepMattersNeedingAttention))
{
//MattersNeedingAttentionText.text = tmpOperationStepDataInfo.stepMattersNeedingAttention;
LocalizedString mattersNeedingAttentionText = mData.operationStepDataInfo.id + "stepMattersNeedingAttention";
MattersNeedingAttentionText.text = mattersNeedingAttentionText;
MattersNeedingAttentionText.gameObject.SetActive(true);
//注意事项音频
StartCoroutine(AudioHelper.LoadAudioClip(OperateSetting.Instance.m_CourseName, tmpOperationStepDataInfo.audioClipName + "_note", AudioType.MPEG, LoadAudioCallBack));
}
else
{
MattersNeedingAttentionText.gameObject.SetActive(false);
}
List tmpPictureNames = tmpOperationStepDataInfo.GetPicturesNames();
//注意事项图片
if (tmpPictureNames != null && tmpPictureNames.Count > 0)
{
PictureBg.gameObject.SetActive(true);
SetInfoOfImage(LoadHelper.LoadSprite(Path.Combine(GlobalConfig.picturePath, OperateSetting.Instance.m_CourseName, tmpPictureNames[0])));
}
else
{
PictureBg.gameObject.SetActive(false);
}
GetComponent().UpdateScale();
if (LocalizationConfig.localization)
{
RefrushLocalization();
}
//流程结束自动关闭自己
ProcessManagement.Instance.processFinishEvent += ProcessFinsihedCloseSelf;
}
private void VideoBg_prepareCompleted(VideoPlayer source)
{
Debug.LogWarning(VideoBg.frameCount + "视频长度:" + (float)VideoBg.frameCount / VideoBg.frameRate);
StartCoroutine(WaitTimeCallBack((float)VideoBg.frameCount / VideoBg.frameRate, CloseSelf));
VideoBg.prepareCompleted -= VideoBg_prepareCompleted;
}
public void ProcessFinsihedCloseSelf()
{
CloseSelf();
}
///
/// 加载音频回调
///
///
private void LoadAudioCallBack(AudioClip clip)
{
if (clip != null)
{
SoundManager.Instance.PlayBg(clip);
}
//学习模式下自动关闭注意事项
if (OperateSetting.Instance.m_CurrentOperationMode != OperationMode.Learn) return;
if (!ProcessManagement.Instance.autoEnterNextProcess) return;
if (clip == null)
{
//音频空,等待3S,关闭注意事项
StartCoroutine(WaitTimeCallBack(3, () =>
{
CloseSelf();
}));
}
else
{
StartCoroutine(WaitTimeCallBack(clip.length, () =>
{
CloseSelf();
}));
}
}
IEnumerator WaitTimeCallBack(float duration, Action callBack)
{
yield return new WaitForSeconds(duration);
callBack?.Invoke();
}
///
/// 设置Image信息
///
///
private void SetInfoOfImage(Sprite _sprite)
{
if (_sprite == null) return;
float width = _sprite.texture.width * CalculateScale(_sprite.texture.height);
float height = _sprite.texture.height * CalculateScale(_sprite.texture.height);
PictrueImage.rectTransform.sizeDelta = new Vector2(width, height);
PictrueImage.sprite = _sprite;
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
SoundManager.Instance.BgVolume = 1;
StopAllCoroutines();
ProcessManagement.Instance.processFinishEvent -= ProcessFinsihedCloseSelf;
}
private void OnVoiceBtnClick()
{
Transform tmpVoice = VoiceBtn.transform.Find("Voice");
Transform tmpMute = VoiceBtn.transform.Find("Mute");
if (tmpVoice.gameObject.activeSelf)
{
tmpVoice.gameObject.SetActive(false);
tmpMute.gameObject.SetActive(true);
//AudioHelper.
SoundManager.Instance.BgVolume = 0;
if (VideoBg.url != null)
{
VideoBg.GetTargetAudioSource(0).volume = 0;
}
}
else
{
tmpVoice.gameObject.SetActive(true);
tmpMute.gameObject.SetActive(false);
SoundManager.Instance.BgVolume = 1;
if (VideoBg.url != null)
{
VideoBg.GetTargetAudioSource(0).volume = 1;
}
}
}
///
/// 确定按钮点击
///
private void OnConfirmBtnClick()
{
SoundManager.Instance.StopBg();
CloseSelf();
}
///
/// 计算比例,以高度为基数计算
///
///
private float CalculateScale(float _spriteHeight)
{
if (_spriteHeight <= heightBasic)
{
return 1;
}
else
{
return heightBasic / _spriteHeight;
}
}
#region 多语言
public void RefrushLocalization()
{
if (mData.operationStepDataInfo != null)
{
LocalizedString mattersNeedingAttentionText = mData.operationStepDataInfo.id + "stepMattersNeedingAttention";
MattersNeedingAttentionText.text = mattersNeedingAttentionText;
}
}
void OnEnable()
{
LocalizationManager.OnLocalizeEvent += LocalizationManager_OnLocalizeEvent;
LocalizationManager_OnLocalizeEvent();
}
void OnDisEnable()
{
LocalizationManager.OnLocalizeEvent -= LocalizationManager_OnLocalizeEvent;
}
private void LocalizationManager_OnLocalizeEvent()
{
if (LocalizationConfig.localization)
{
RefrushLocalization();
}
}
#endregion
}
}