#nullable enable
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace SHI.Modal
{
///
/// 시간 기반 재생을 제어하는 UI Toolkit 컴포넌트입니다.
///
[UxmlElement]
public partial class PlayBar : VisualElement, IDisposable
{
#region 상수 (Constants)
private const string UXML_PATH = "SHI/Modal/PlayBar";
private const int DEFAULT_INTERVAL_SECONDS = 2;
#endregion
#region UI 컴포넌트 참조
private Label? _startLabel;
private Label? _endLabel;
private Label? _currentTimeLabel;
private VisualElement? _progressTrack;
private VisualElement? _progressFill;
private Button? _playBtn;
private Button? _firstBtn;
private Button? _lastBtn;
private Button? _stopBtn;
private DropdownField? _intervalDropdown;
#endregion
#region 상태 (State)
private DateTime _startTime;
private DateTime _endTime;
private DateTime _currentTime;
private bool _isPlaying;
private int _intervalSeconds = DEFAULT_INTERVAL_SECONDS;
private IVisualElementScheduledItem? _playSchedule;
private ProgressDragManipulator? _dragManipulator;
#endregion
#region 외부 이벤트 (Public Events)
/// 재생이 시작될 때 발생
public event Action? OnPlayStarted;
/// 재생이 정지될 때 발생
public event Action? OnPlayStopped;
/// 재생 중 시간이 변경될 때 발생 (자동 재생 시)
public event Action? OnPlayProgress;
/// 사용자가 진행바를 드래그/클릭하여 위치를 변경할 때 발생
public event Action? OnPositionChanged;
#endregion
#region UxmlAttribute
[UxmlAttribute]
public bool IsVisible
{
get => style.display == DisplayStyle.Flex;
set => style.display = value ? DisplayStyle.Flex : DisplayStyle.None;
}
#endregion
#region 생성자 (Constructor)
public PlayBar()
{
var visualTree = Resources.Load(UXML_PATH);
Debug.Log("[PlayBar] UXML loaded and cloned." + (visualTree == null));
if (visualTree == null)
{
Debug.LogError($"[PlayBar] UXML not found at: {UXML_PATH}");
return;
}
visualTree.CloneTree(this);
InitializeUIReferences();
InitializeEventHandlers();
// 패널에 연결된 후 드롭다운 초기화
RegisterCallback(OnAttachToPanel);
}
private void OnAttachToPanel(AttachToPanelEvent evt)
{
UnregisterCallback(OnAttachToPanel);
InitializeDropdown();
}
#endregion
#region 초기화 (Initialization)
private void InitializeUIReferences()
{
_startLabel = this.Q