Files
XRLib/Assets/Scripts/UVC/UIToolkit/ToolBar/Data/UTKToolBarButtonData.cs
2026-02-19 18:40:37 +09:00

199 lines
5.6 KiB
C#

#nullable enable
using System;
using UVC.UI.Commands;
namespace UVC.UIToolkit
{
/// <summary>
/// 모든 툴바 버튼의 공통 데이터를 정의하는 추상 클래스입니다.
/// Text, Icon, Enabled, Tooltip, Command 등의 공통 속성과 상태 변경 이벤트를 제공합니다.
/// </summary>
public abstract class UTKToolBarButtonData : IUTKToolBarItem, IDisposable
{
#region Fields
private string _text = "";
private string? _iconPath;
private bool _isEnabled = true;
private bool _disposed;
#endregion
#region Properties
/// <summary>아이템 고유 식별자</summary>
public string ItemId { get; private set; }
/// <summary>
/// 버튼 텍스트 (다국어 키). 변경 시 OnStateChanged 이벤트 발생.
/// </summary>
public string Text
{
get => _text;
set
{
if (_text != value)
{
_text = value;
OnStateChanged?.Invoke();
}
}
}
/// <summary>
/// 아이콘 경로. Material Icon 유니코드 또는 Resources 경로.
/// 변경 시 OnStateChanged 이벤트 발생.
/// </summary>
public string? IconPath
{
get => _iconPath;
set
{
if (_iconPath != value)
{
_iconPath = value;
OnStateChanged?.Invoke();
}
}
}
/// <summary>Material Icon 사용 여부 (true: 폰트 아이콘, false: 이미지)</summary>
public bool UseMaterialIcon { get; set; } = true;
/// <summary>
/// 활성화 상태. 변경 시 OnStateChanged 이벤트 발생.
/// </summary>
public bool IsEnabled
{
get => _isEnabled;
set
{
if (_isEnabled != value)
{
_isEnabled = value;
OnStateChanged?.Invoke();
}
}
}
/// <summary>툴팁 텍스트 (다국어 키)</summary>
public string? Tooltip { get; set; }
/// <summary>실행할 명령</summary>
public ICommand? ClickCommand { get; set; }
#endregion
#region Events
/// <summary>Text, Icon, Enabled 등 시각적 상태 변경 시 발생</summary>
public event Action? OnStateChanged;
/// <summary>버튼 클릭 시 발생</summary>
public event Action? OnClicked;
#endregion
#region Constructor
/// <summary>
/// UTKToolBarButtonData의 새 인스턴스를 초기화합니다.
/// </summary>
/// <param name="itemId">아이템 고유 식별자. null이면 GUID 자동 생성.</param>
protected UTKToolBarButtonData(string? itemId = null)
{
ItemId = itemId ?? Guid.NewGuid().ToString();
}
#endregion
#region Methods
/// <summary>
/// 클릭 실행. Command 실행 + OnClicked 이벤트 발생.
/// IUndoableCommand인 경우 UndoRedoManager를 통해 실행합니다.
/// </summary>
/// <param name="parameter">ClickCommand에 전달할 파라미터</param>
public virtual void ExecuteClick(object? parameter = null)
{
if (!IsEnabled) return;
if (ClickCommand != null)
{
if (ClickCommand is IUndoableCommand undoableCommand)
{
var undoRedoManager = UnityEngine.Object.FindAnyObjectByType<UVC.Studio.Manager.UndoRedoManager>();
if (undoRedoManager != null)
{
undoRedoManager.ExecuteCommand(undoableCommand, parameter);
}
else
{
ClickCommand.Execute(parameter);
}
}
else
{
ClickCommand.Execute(parameter);
}
}
OnClicked?.Invoke();
}
/// <summary>
/// OnStateChanged 이벤트를 수동으로 발생시킵니다.
/// 여러 속성을 변경 후 한 번에 UI 업데이트를 트리거할 때 사용합니다.
/// </summary>
public void NotifyStateChanged()
{
OnStateChanged?.Invoke();
}
/// <summary>
/// 모든 이벤트 핸들러를 해제합니다.
/// </summary>
public virtual void ClearEventHandlers()
{
OnStateChanged = null;
OnClicked = null;
}
#endregion
#region IDisposable
/// <summary>
/// 리소스를 정리합니다. Command가 IDisposable이면 함께 정리합니다.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 리소스 정리 구현.
/// </summary>
/// <param name="disposing">관리 리소스 정리 여부</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
_disposed = true;
if (disposing)
{
ClearEventHandlers();
if (ClickCommand is IDisposable disposableCommand)
{
disposableCommand.Dispose();
}
ClickCommand = null;
}
}
#endregion
}
}