Health/Assets/Scripts/UI/GuideUI.cs

239 lines
7.4 KiB
C#
Raw Normal View History

2023-11-07 13:55:35 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2023-11-07 17:51:34 +00:00
using TMPro;
2023-11-07 13:55:35 +00:00
using UnityEngine;
2023-11-07 17:51:34 +00:00
using UnityEngine.UI;
using Yoga;
2023-11-07 13:55:35 +00:00
public class GuideUI : UIPanelBase
{
private GameObject _guide;
2023-11-07 15:28:19 +00:00
private Animator _effectAnimator;
2023-11-07 17:51:34 +00:00
private Transform _content;
2023-11-07 13:55:35 +00:00
2023-11-14 10:52:43 +00:00
//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Image _progressBar;
private DateTime _startTime;
private float _totalSeconds;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
private TextMeshProUGUI _successText;
private TextMeshProUGUI _totalText;
private TextMeshProUGUI _kcalText;
2023-11-07 13:55:35 +00:00
public GudieAnimationManager GuideMgr => _guide.GetComponent<GudieAnimationManager>();
private void Awake()
{
_guide = GameObject.FindWithTag("Guide");
if (_guide == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("Guide is null");
2023-11-07 13:55:35 +00:00
}
2023-11-07 15:28:19 +00:00
_effectAnimator = transform.Find("Content").GetComponentInChildren<Animator>();
2023-11-07 17:51:34 +00:00
_content = transform.Find("Content");
2023-11-14 10:52:43 +00:00
_progressBar = _content.Find("Guide/ProgressBar/Background/ProgressBar").GetComponent<Image>();
var detailPanel = _content.Find("DetailPanel");
_successText = detailPanel.Find("SuccessCount").GetComponent<TextMeshProUGUI>();
_totalText = detailPanel.Find("TotalCount").GetComponent<TextMeshProUGUI>();
_kcalText = detailPanel.Find("KcalNum").GetComponent<TextMeshProUGUI>();
2023-11-07 13:55:35 +00:00
}
private void OnEnable()
{
2023-11-10 08:12:37 +00:00
EventManager.Instance.AddEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.AddEventListener(YogaEventType.Action_Success, ShowSuccessEffect);
2023-11-10 08:12:37 +00:00
EventManager.Instance.AddEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.AddEventListener(YogaEventType.Action_Fail, ShowFailEffect);
EventManager.Instance.AddEventListener(YogaEventType.DEBUG_CONSOLE_ChangeCamMode, ChangeCameraCaptureMode);
2023-11-27 05:49:53 +00:00
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ч
EventManager.Instance.AddEventListener(YogaEventType.Sound_CountDownAudio, PlayCountDownAudio);
EventManager.Instance.AddEventListener(YogaEventType.Sound_CountDownEndAudio, PlayCountDownEndAudio);
2023-11-07 13:55:35 +00:00
}
private void OnDisable()
{
2023-11-10 08:12:37 +00:00
EventManager.Instance.RemoveEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.RemoveEventListener(YogaEventType.Action_Success, ShowSuccessEffect);
2023-11-10 08:12:37 +00:00
EventManager.Instance.RemoveEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.RemoveEventListener(YogaEventType.Action_Fail, ShowFailEffect);
EventManager.Instance.RemoveEventListener(YogaEventType.DEBUG_CONSOLE_ChangeCamMode, ChangeCameraCaptureMode);
2023-11-27 05:49:53 +00:00
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ч
EventManager.Instance.RemoveEventListener(YogaEventType.Sound_CountDownAudio, PlayCountDownAudio);
EventManager.Instance.RemoveEventListener(YogaEventType.Sound_CountDownEndAudio, PlayCountDownEndAudio);
2023-11-07 13:55:35 +00:00
}
#region Event Func
private void PlayAnimation(params object[] args)
{
var animeType = args.FirstOrDefault();
if (animeType == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("PlayAnimation animeType is null");
2023-11-07 13:55:35 +00:00
return;
}
2023-11-27 05:49:53 +00:00
if (animeType is AvatarAction)
{
GuideMgr.Play(Enum.GetName(typeof(AvatarAction), animeType));
return;
}
if (animeType is List<AvatarAction>)
{
var actionList = animeType as List<AvatarAction>;
if (actionList == null)
{
LogPrint.Error("PlayAnimation actionList is null");
return;
}
GuideMgr.PlayCurrentActionList(actionList);
return;
}
2023-11-07 13:55:35 +00:00
}
2023-11-07 17:51:34 +00:00
private void UpdateSuccessCount(params object[] args)
{
//args[0] = successCount args[1] = excutedCount args[2] = totalCount
if (args.Length < 3)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("UpdateSuccessCount args is not enough");
2023-11-07 17:51:34 +00:00
return;
}
var successCount = (int)args[0];
var excutedCount = (int)args[1];
2023-11-14 10:52:43 +00:00
if (_successText != null)
2023-11-07 17:51:34 +00:00
{
2023-11-14 10:52:43 +00:00
_successText.text = successCount.ToString();
2023-11-07 17:51:34 +00:00
}
2023-11-14 10:52:43 +00:00
if (_kcalText != null)
2023-11-07 17:51:34 +00:00
{
2023-11-14 10:52:43 +00:00
_kcalText.text = (successCount * 0.1f + (excutedCount - successCount) * 0.5f).ToString("F1");
2023-11-07 17:51:34 +00:00
}
}
2023-11-07 15:28:19 +00:00
private void ShowFailEffect()
{
2023-11-07 17:51:34 +00:00
_effectAnimator.Play("WindowEffectError", 0, 0f);//<2F>߿<EFBFBD><DFBF><EFBFBD>˸
2023-11-07 15:28:19 +00:00
}
private void ShowSuccessEffect()
{
2023-11-07 17:51:34 +00:00
_effectAnimator.Play("WindowEffectCorrect", 0, 0f);//<2F>߿<EFBFBD><DFBF><EFBFBD>˸
2023-11-07 15:28:19 +00:00
}
2023-11-07 13:55:35 +00:00
#endregion
public override void Init(object[] pageData)
{
base.Init(pageData);
if (_guide == null)
_guide = GameObject.FindWithTag("Guide");
2023-11-14 10:52:43 +00:00
if (pageData[0] == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("Guide is null");
2023-11-14 10:52:43 +00:00
return;
}
YogaData data = pageData[0] as YogaData;
_totalSeconds = data.TotalSeconds;
if (_totalSeconds == 0)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("TotalSeconds is 0");
2023-11-14 10:52:43 +00:00
return;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
if (_successText != null)
{
_successText.text = "0";
}
if (_totalText != null)
{
2023-11-27 05:49:53 +00:00
_totalText.text = data.MaxCheckPointCount.ToString();
2023-11-14 10:52:43 +00:00
}
if (_kcalText != null)
{
_kcalText.text = "0";
}
}
2023-11-27 05:49:53 +00:00
//<2F><><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>ʱ<EFBFBD><CAB1>Ч
private void PlayCountDownAudio()
{
AudioManager.Instance.PlaySE("CountDown");
}
//<2F><><EFBFBD>ſ<EFBFBD>ʼ<EFBFBD><CABC>Ч
private void PlayCountDownEndAudio()
{
AudioManager.Instance.PlaySE("CountDownEnd");
}
2023-11-14 10:52:43 +00:00
//<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
private void ProgressUpdate()
{
var passedTime = Math.Abs((_startTime - DateTime.Now).TotalSeconds);
_progressBar.fillAmount = (float)(passedTime / _totalSeconds);
2023-11-07 13:55:35 +00:00
}
public override void OnEnter()
{
base.OnEnter();
_guide.SetActive(true);
2023-11-10 08:12:37 +00:00
EventManager.Instance.Dispatch(YogaEventType.StartMotionCapture);
2023-11-27 05:49:53 +00:00
EventManager.Instance.Dispatch(YogaEventType.PlayAnimation, YogaManager.Instance.LevelData.Actions);
//EventManager.Instance.Dispatch(YogaEventType.PlayAnimation, YogaManager.Instance.LevelData.Action);
2023-11-14 10:52:43 +00:00
_startTime = DateTime.Now;
InvokeRepeating("ProgressUpdate", 0, 0.05f); //20fps
2023-11-10 07:17:23 +00:00
}
public override void OnExit()
{
base.OnExit();
2023-11-24 18:39:53 +00:00
if (_guide != null)
_guide.SetActive(false);
2023-11-10 07:17:23 +00:00
GuideMgr.Stop();
2023-11-10 08:12:37 +00:00
EventManager.Instance.Dispatch(YogaEventType.StopMotionCapture);
2023-11-07 13:55:35 +00:00
}
private void ChangeCameraCaptureMode()
{
Transform webCam = _content.Find("WebCameraCaptureManager");
Transform usbCam = _content.Find("USBCameraCaptureManager");
CaptureManagerBase manager;
if (webCam == null || usbCam == null)
{
LogPrint.Error("ChangeCameraCaptureMode webCam or usbCam is null");
return;
}
if (webCam.gameObject.activeSelf)
{
webCam.gameObject.SetActive(false);
usbCam.gameObject.SetActive(true);
manager = usbCam.GetComponent<MotionUSBCameraCaptureManager>();
}
else
{
webCam.gameObject.SetActive(true);
usbCam.gameObject.SetActive(false);
manager = webCam.GetComponent<MotionWebCaptureManager>();
}
manager.Init();
manager.IsOnCamCapture = true;
CVEstimator.Instance.StartEstimation();
}
2023-11-07 13:55:35 +00:00
}