smart-interactive-display/Assets/GadGame/Scripts/Coffee/CoffeeController.cs

127 lines
4.1 KiB
C#
Raw Normal View History

2024-06-19 04:29:32 -07:00
using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using GadGame.Event.Type;
using TMPro;
using UnityEngine;
2024-06-19 20:25:06 -07:00
using UnityEngine.Serialization;
2024-06-19 04:29:32 -07:00
using UnityEngine.UI;
namespace GadGame.Scripts.Coffee
{
public class CoffeeController : MonoBehaviour
{
2024-06-19 20:25:06 -07:00
[SerializeField] private VoidEvent _engageReadyEvent;
[SerializeField] private VoidEvent _generateImageSuccessEvent;
2024-06-19 04:29:32 -07:00
[SerializeField] private BoolEvent _playPassByAnimEvent;
[SerializeField] private BoolEvent _playVideoEvent;
[SerializeField] private FloatEvent _readyCountDownEvent;
2024-06-19 20:25:06 -07:00
[SerializeField] private StringEvent _encodeImageEvent;
2024-06-19 04:29:32 -07:00
[SerializeField] private CanvasGroup _idleBg;
2024-06-19 20:25:06 -07:00
[SerializeField] private LoadImageEncoded _userImager;
2024-06-19 04:29:32 -07:00
[SerializeField] private Image _process;
[SerializeField] private TextMeshProUGUI _hintText;
[SerializeField] private Image _loading;
2024-06-19 20:30:19 -07:00
[SerializeField] private float _loadingSpeed = 100;
2024-06-19 04:29:32 -07:00
[SerializeField] private string[] _texts;
[SerializeField] private string[] _loadingTexts;
private int _indexText;
private bool _isLoading;
private float _timer;
private void Awake()
{
_idleBg.alpha = 1;
}
private void OnEnable()
{
2024-06-19 20:25:06 -07:00
_engageReadyEvent.Register(OnEngageReady);
_generateImageSuccessEvent.Register(OnGenerateImageSuccess);
2024-06-19 04:29:32 -07:00
_playPassByAnimEvent.Register(Play);
_playVideoEvent.Register(SetPlayVideo);
_readyCountDownEvent.Register(SetReadyCountDown);
2024-06-19 20:25:06 -07:00
_encodeImageEvent.Register(OnGetEncodeImage);
2024-06-19 04:29:32 -07:00
}
private void OnDisable()
{
2024-06-19 20:25:06 -07:00
_engageReadyEvent.Unregister(OnEngageReady);
_generateImageSuccessEvent.Unregister(OnGenerateImageSuccess);
2024-06-19 04:29:32 -07:00
_playPassByAnimEvent.Unregister(Play);
_playVideoEvent.Unregister(SetPlayVideo);
_readyCountDownEvent.Unregister(SetReadyCountDown);
2024-06-19 20:25:06 -07:00
_encodeImageEvent.Unregister(OnGetEncodeImage);
2024-06-19 04:29:32 -07:00
}
private void Update()
{
if(!_isLoading) return;
_timer += Time.deltaTime;
if (_timer >= 3)
{
_timer = 0;
_indexText++;
if (_indexText > _loadingTexts.Length - 1)
{
_indexText = 0;
}
_hintText.text = _loadingTexts[_indexText];
}
}
private void Play(bool engage) {
// videoPlayer.gameObject.SetActive(!passBy);
// _transform.DOAnchorPosX(engage ? -1000 : 0, 1);
_hintText.text = _texts[0];
}
2024-06-19 20:25:06 -07:00
private void OnGenerateImageSuccess()
2024-06-19 04:29:32 -07:00
{
_loading.DOFade(0, 0.5f);
_hintText.text = "Mô tả";
}
2024-06-19 20:25:06 -07:00
private void OnGetEncodeImage(string encode)
{
_userImager.LoadImage(encode);
}
2024-06-19 04:29:32 -07:00
private void OnEngageReady()
{
_process.fillAmount = 0;
_isLoading = true;
_hintText.text = _loadingTexts[_indexText];
_loading.DOFade(1, 1f);
2024-06-19 20:30:19 -07:00
_loading.transform.DOLocalRotate(new Vector3(0, 0, _loadingSpeed), 1, RotateMode.FastBeyond360)
2024-06-19 04:29:32 -07:00
.SetLoops(-1)
.SetRelative(true)
.SetEase(Ease.Linear);
}
private async void SetPlayVideo(bool value){
if(value) {
while (_idleBg.alpha < 1)
{
_idleBg.alpha += Time.deltaTime * 3;
await UniTask.Yield();
}
_idleBg.alpha = 1;
} else {
while (_idleBg.alpha > 0)
{
_idleBg.alpha -= Time.deltaTime * 3;
await UniTask.Yield();
}
_idleBg.alpha = 0;
}
}
private void SetReadyCountDown(float progress){
_hintText.text = _texts[1];
_process.fillAmount = 1- progress ;
}
}
}