Files
XRLib/Assets/Scripts/UVC/Factory/Playback/UI/UIPlaybackProgressBar.cs
2025-07-22 19:58:14 +09:00

143 lines
4.1 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UVC.Extension;
using UVC.UI;
namespace UVC.Factory.Playback.UI
{
public class UIPlaybackProgressBar : MonoBehaviour
{
private TextMeshProUGUI playTimeTxt;
private TextMeshProUGUI totalTimeTxt;
private SliderWithEvent progressBar;
private float progressBarPrevValue = 0;
private CanvasGroup canvasGroup;
private int time;
public Action<int> OnChangeValue { get; set; }
public int Value
{
get
{
return (int)progressBar.value;
}
set
{
if (progressBar.value != value)
{
progressBar.value = value;
UpdateTimeText();
progressBarPrevValue = value;
}
}
}
public int MaxValue
{
get => (int)progressBar.maxValue;
}
public bool Interactable
{
get => canvasGroup.interactable;
set => canvasGroup.interactable = value;
}
private void OnDestroy()
{
progressBar.onValueChanged.RemoveListener(OnChangeSlider);
progressBar.OnClickAction = null;
progressBar.OnDragAction = null;
progressBar.OnEndDragAction = null;
}
public void Init(int time)
{
this.time = time;
canvasGroup = GetComponent<CanvasGroup>();
playTimeTxt = transform.FindChildren("PlayTimeTxt").GetComponent<TextMeshProUGUI>();
totalTimeTxt = transform.FindChildren("TotalTimeTxt").GetComponent<TextMeshProUGUI>();
progressBar = GetComponentInChildren<SliderWithEvent>();
progressBar.onValueChanged.AddListener(OnChangeSlider);
progressBar.OnClickAction += OnClickProgressBar;
progressBar.OnDragAction += OnDragProgressBar;
progressBar.OnEndDragAction += OnEndDragProgressBar;
playTimeTxt.text = $"{time.ToString("00")}:00:00";
totalTimeTxt.text = $"{(time + 1).ToString("00")}:00:00";
progressBar.value = 0;
Interactable = true;
}
private void OnChangeSlider(float newValue)
{
if (!Interactable)
{
progressBar.value = progressBarPrevValue;
UpdateTimeText();
return;
}
}
private void OnClickProgressBar()
{
float snapedValue = SnapProgressBarValue();
progressBar.value = snapedValue;
UpdateTimeText();
if (progressBarPrevValue != snapedValue)
{
progressBarPrevValue = snapedValue;
if (OnChangeValue != null) OnChangeValue.Invoke((int)snapedValue);
}
}
private void OnDragProgressBar()
{
float snapedValue = SnapProgressBarValue();
progressBar.value = snapedValue;
UpdateTimeText();
}
private void OnEndDragProgressBar()
{
float snapedValue = SnapProgressBarValue();
progressBar.value = snapedValue;
UpdateTimeText();
if (progressBarPrevValue != snapedValue)
{
progressBarPrevValue = snapedValue;
if (OnChangeValue != null) OnChangeValue.Invoke((int)snapedValue);
}
}
private float SnapProgressBarValue()
{
float value = progressBar.value;
float interval = 60f;
value = Mathf.Round(value / interval) * interval;
return value;
}
private void UpdateTimeText()
{
int minute = (int)progressBar.value / 60;
int second = (int)progressBar.value % 60;
string timeStr = time.ToString("00");
if (progressBar.value == 3600)
{
timeStr = (time + 1).ToString("00");
minute = 0;
}
playTimeTxt.text = $"{timeStr}:{minute.ToString("00")}:{second.ToString("00")}";
}
}
}