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