스타일 가이드 적용 완료. UTKCOlorPicker, UTKDatePicker 확인해야 함

This commit is contained in:
logonkhi
2026-01-12 20:16:17 +09:00
parent 6ae48ff30e
commit e1f2ac5b02
31 changed files with 1854 additions and 660 deletions

View File

@@ -1,5 +1,6 @@
#nullable enable
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
using UVC.UIToolkit.Modal;
@@ -18,10 +19,10 @@ namespace UVC.UIToolkit
#endregion
#region Fields
private static VisualElement? _root;
private bool _disposed;
private UTKModalBlocker? _blocker;
private VisualElement? _iconContainer;
private Label? _iconLabel;
private Label? _titleLabel;
private Label? _messageLabel;
private VisualElement? _buttonContainer;
@@ -29,6 +30,11 @@ namespace UVC.UIToolkit
private string _title = "";
private string _message = "";
private AlertType _alertType = AlertType.Info;
private string _confirmLabel = "OK";
private string _cancelLabel = "Cancel";
private EventCallback<KeyDownEvent>? _keyDownCallback;
private VisualElement? _keyEventTarget;
#endregion
#region Events
@@ -116,45 +122,234 @@ namespace UVC.UIToolkit
}
#endregion
#region Static Factory
#region Static Methods
/// <summary>
/// 기본 루트 요소 설정
/// </summary>
/// <param name="root">Alert를 표시할 기본 루트 요소</param>
public static void SetRoot(VisualElement root)
{
_root = root;
}
/// <summary>
/// 기본 루트 요소 반환
/// </summary>
public static VisualElement? GetRoot() => _root;
#endregion
#region Static Factory (without parent)
/// <summary>
/// Info 알림 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert ShowInfo(string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return Show(_root!, title, message, AlertType.Info, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Success 알림 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert ShowSuccess(string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return Show(_root!, title, message, AlertType.Success, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Warning 알림 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert ShowWarning(string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return Show(_root!, title, message, AlertType.Warning, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Error 알림 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert ShowError(string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return Show(_root!, title, message, AlertType.Error, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Confirm 대화상자 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert ShowConfirm(string title, string message, Action? onConfirm, Action? onCancel = null, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
ValidateRoot();
var alert = Show(_root!, title, message, AlertType.Confirm, null, closeOnBlockerClick, confirmLabel, cancelLabel);
alert.OnConfirm = onConfirm;
alert.OnCancel = onCancel;
return alert;
}
/// <summary>
/// 알림 표시 (SetRoot로 설정된 루트 사용)
/// </summary>
public static UTKAlert Show(string title, string message, AlertType type = AlertType.Info, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
ValidateRoot();
return Show(_root!, title, message, type, onClose, closeOnBlockerClick, confirmLabel, cancelLabel);
}
private static void ValidateRoot()
{
if (_root == null)
{
throw new InvalidOperationException("UTKAlert.SetRoot()를 먼저 호출하여 기본 루트 요소를 설정해야 합니다.");
}
}
#endregion
#region Static Factory Async (without parent)
/// <summary>
/// Info 알림 표시 (비동기, SetRoot로 설정된 루트 사용)
/// </summary>
public static UniTask ShowInfoAsync(string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return ShowAsync(_root!, title, message, AlertType.Info, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Success 알림 표시 (비동기, SetRoot로 설정된 루트 사용)
/// </summary>
public static UniTask ShowSuccessAsync(string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return ShowAsync(_root!, title, message, AlertType.Success, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Warning 알림 표시 (비동기, SetRoot로 설정된 루트 사용)
/// </summary>
public static UniTask ShowWarningAsync(string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return ShowAsync(_root!, title, message, AlertType.Warning, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Error 알림 표시 (비동기, SetRoot로 설정된 루트 사용)
/// </summary>
public static UniTask ShowErrorAsync(string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
ValidateRoot();
return ShowAsync(_root!, title, message, AlertType.Error, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Confirm 대화상자 표시 (비동기, SetRoot로 설정된 루트 사용)
/// </summary>
/// <returns>확인 버튼 클릭 시 true, 취소 버튼 클릭 시 false</returns>
public static UniTask<bool> ShowConfirmAsync(string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
ValidateRoot();
return ShowConfirmAsync(_root!, title, message, closeOnBlockerClick, confirmLabel, cancelLabel);
}
#endregion
#region Static Factory Async (with parent)
/// <summary>
/// Info 알림 표시 (비동기)
/// </summary>
public static UniTask ShowInfoAsync(VisualElement parent, string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return ShowAsync(parent, title, message, AlertType.Info, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Success 알림 표시 (비동기)
/// </summary>
public static UniTask ShowSuccessAsync(VisualElement parent, string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return ShowAsync(parent, title, message, AlertType.Success, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Warning 알림 표시 (비동기)
/// </summary>
public static UniTask ShowWarningAsync(VisualElement parent, string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return ShowAsync(parent, title, message, AlertType.Warning, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Error 알림 표시 (비동기)
/// </summary>
public static UniTask ShowErrorAsync(VisualElement parent, string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return ShowAsync(parent, title, message, AlertType.Error, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Confirm 대화상자 표시 (비동기)
/// </summary>
/// <returns>확인 버튼 클릭 시 true, 취소 버튼 클릭 시 false</returns>
public static UniTask<bool> ShowConfirmAsync(VisualElement parent, string title, string message, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
var tcs = new UniTaskCompletionSource<bool>();
var alert = Show(parent, title, message, AlertType.Confirm, null, closeOnBlockerClick, confirmLabel, cancelLabel);
alert.OnConfirm = () => tcs.TrySetResult(true);
alert.OnCancel = () => tcs.TrySetResult(false);
alert.OnClosed = () => tcs.TrySetResult(false);
return tcs.Task;
}
/// <summary>
/// 알림 표시 (비동기)
/// </summary>
public static UniTask ShowAsync(VisualElement parent, string title, string message, AlertType type, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
var tcs = new UniTaskCompletionSource();
var alert = Show(parent, title, message, type, () => tcs.TrySetResult(), closeOnBlockerClick, confirmLabel, cancelLabel);
return tcs.Task;
}
#endregion
#region Static Factory (with parent)
/// <summary>
/// Info 알림 표시
/// </summary>
public static UTKAlert ShowInfo(VisualElement parent, string title, string message, Action? onClose = null)
public static UTKAlert ShowInfo(VisualElement parent, string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return Show(parent, title, message, AlertType.Info, onClose);
return Show(parent, title, message, AlertType.Info, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Success 알림 표시
/// </summary>
public static UTKAlert ShowSuccess(VisualElement parent, string title, string message, Action? onClose = null)
public static UTKAlert ShowSuccess(VisualElement parent, string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return Show(parent, title, message, AlertType.Success, onClose);
return Show(parent, title, message, AlertType.Success, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Warning 알림 표시
/// </summary>
public static UTKAlert ShowWarning(VisualElement parent, string title, string message, Action? onClose = null)
public static UTKAlert ShowWarning(VisualElement parent, string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return Show(parent, title, message, AlertType.Warning, onClose);
return Show(parent, title, message, AlertType.Warning, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Error 알림 표시
/// </summary>
public static UTKAlert ShowError(VisualElement parent, string title, string message, Action? onClose = null)
public static UTKAlert ShowError(VisualElement parent, string title, string message, Action? onClose = null, bool closeOnBlockerClick = false, string confirmLabel = "OK")
{
return Show(parent, title, message, AlertType.Error, onClose);
return Show(parent, title, message, AlertType.Error, onClose, closeOnBlockerClick, confirmLabel);
}
/// <summary>
/// Confirm 대화상자 표시
/// </summary>
public static UTKAlert ShowConfirm(VisualElement parent, string title, string message, Action? onConfirm, Action? onCancel = null)
public static UTKAlert ShowConfirm(VisualElement parent, string title, string message, Action? onConfirm, Action? onCancel = null, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
var alert = Show(parent, title, message, AlertType.Confirm, null);
var alert = Show(parent, title, message, AlertType.Confirm, null, closeOnBlockerClick, confirmLabel, cancelLabel);
alert.OnConfirm = onConfirm;
alert.OnCancel = onCancel;
return alert;
@@ -163,13 +358,32 @@ namespace UVC.UIToolkit
/// <summary>
/// 알림 표시
/// </summary>
public static UTKAlert Show(VisualElement parent, string title, string message, AlertType type, Action? onClose)
/// <param name="parent">부모 요소</param>
/// <param name="title">제목</param>
/// <param name="message">메시지</param>
/// <param name="type">알림 유형</param>
/// <param name="onClose">닫힘 콜백</param>
/// <param name="closeOnBlockerClick">배경 클릭 시 닫힘 여부</param>
/// <param name="confirmLabel">확인 버튼 레이블</param>
/// <param name="cancelLabel">취소 버튼 레이블 (Confirm 타입에서만 사용)</param>
public static UTKAlert Show(VisualElement parent, string title, string message, AlertType type, Action? onClose, bool closeOnBlockerClick = false, string confirmLabel = "OK", string cancelLabel = "Cancel")
{
var alert = new UTKAlert(title, message, type);
alert.OnClosed = onClose;
alert._confirmLabel = confirmLabel;
alert._cancelLabel = cancelLabel;
alert.UpdateButtons();
alert._blocker = UTKModalBlocker.Show(parent, 0.5f, true);
alert._blocker.OnBlockerClicked += alert.Close;
alert._blocker = UTKModalBlocker.Show(parent, 0.5f, closeOnBlockerClick);
if (closeOnBlockerClick)
{
alert._blocker.OnBlockerClicked += alert.Close;
}
else
{
// closeOnBlockerClick이 false일 때 blocker 클릭 시 Alert로 포커스 유지
alert._blocker.OnBlockerClicked += () => alert.Focus();
}
alert._blocker.Add(alert);
// 중앙 정렬
@@ -178,6 +392,13 @@ namespace UVC.UIToolkit
alert.style.top = Length.Percent(50);
alert.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50));
// ESC 키 이벤트 등록
alert.RegisterEscapeKey(parent);
// Alert에 포커스 설정 (키보드 이벤트 수신을 위해)
alert.focusable = true;
alert.schedule.Execute(() => alert.Focus());
return alert;
}
#endregion
@@ -187,26 +408,21 @@ namespace UVC.UIToolkit
{
AddToClassList("utk-alert");
_iconContainer = new VisualElement { name = "icon-container" };
_iconContainer.AddToClassList("utk-alert__icon-container");
Add(_iconContainer);
_iconLabel = new Label { name = "icon" };
_iconLabel.AddToClassList("utk-alert__icon");
_iconContainer.Add(_iconLabel);
// Title (상단 왼쪽)
_titleLabel = new Label { name = "title" };
_titleLabel.AddToClassList("utk-alert__title");
Add(_titleLabel);
// Content (가운데 메시지)
var contentContainer = new VisualElement { name = "content" };
contentContainer.AddToClassList("utk-alert__content");
Add(contentContainer);
_titleLabel = new Label { name = "title" };
_titleLabel.AddToClassList("utk-alert__title");
contentContainer.Add(_titleLabel);
_messageLabel = new Label { name = "message" };
_messageLabel.AddToClassList("utk-alert__message");
contentContainer.Add(_messageLabel);
// Buttons
_buttonContainer = new VisualElement { name = "buttons" };
_buttonContainer.AddToClassList("utk-alert__buttons");
Add(_buttonContainer);
@@ -248,19 +464,6 @@ namespace UVC.UIToolkit
};
AddToClassList(typeClass);
// 아이콘 업데이트
if (_iconLabel != null)
{
_iconLabel.text = _alertType switch
{
AlertType.Success => "✓",
AlertType.Warning => "⚠",
AlertType.Error => "✕",
AlertType.Confirm => "?",
_ => ""
};
}
// 버튼 업데이트
UpdateButtons();
}
@@ -273,7 +476,7 @@ namespace UVC.UIToolkit
if (_alertType == AlertType.Confirm)
{
var cancelBtn = new UTKButton("Cancel", "", UTKButton.ButtonVariant.Normal);
var cancelBtn = new UTKButton(_cancelLabel, "", UTKButton.ButtonVariant.Normal);
cancelBtn.AddToClassList("utk-alert__btn");
cancelBtn.OnClicked += () =>
{
@@ -282,7 +485,7 @@ namespace UVC.UIToolkit
};
_buttonContainer.Add(cancelBtn);
var confirmBtn = new UTKButton("OK", "", UTKButton.ButtonVariant.Primary);
var confirmBtn = new UTKButton(_confirmLabel, "", UTKButton.ButtonVariant.Primary);
confirmBtn.AddToClassList("utk-alert__btn");
confirmBtn.OnClicked += () =>
{
@@ -293,18 +496,64 @@ namespace UVC.UIToolkit
}
else
{
var okBtn = new UTKButton("OK", "", UTKButton.ButtonVariant.Primary);
var okBtn = new UTKButton(_confirmLabel, "", UTKButton.ButtonVariant.Primary);
okBtn.AddToClassList("utk-alert__btn");
okBtn.OnClicked += Close;
_buttonContainer.Add(okBtn);
}
}
/// <summary>
/// ESC 키 이벤트 등록
/// </summary>
private void RegisterEscapeKey(VisualElement parent)
{
_keyEventTarget = parent;
_keyDownCallback = evt =>
{
if (evt.keyCode == KeyCode.Escape)
{
evt.StopPropagation();
HandleEscapeKey();
}
};
// 패널에 키 이벤트 등록 (포커스와 관계없이 캡처)
_keyEventTarget.RegisterCallback(_keyDownCallback, TrickleDown.TrickleDown);
}
/// <summary>
/// ESC 키 해제
/// </summary>
private void UnregisterEscapeKey()
{
if (_keyDownCallback != null && _keyEventTarget != null)
{
_keyEventTarget.UnregisterCallback(_keyDownCallback, TrickleDown.TrickleDown);
_keyDownCallback = null;
_keyEventTarget = null;
}
}
/// <summary>
/// ESC 키 처리
/// </summary>
private void HandleEscapeKey()
{
if (_alertType == AlertType.Confirm)
{
// Confirm 타입: 취소 처리
OnCancel?.Invoke();
}
Close();
}
/// <summary>
/// 알림 닫기
/// </summary>
public void Close()
{
UnregisterEscapeKey();
OnClosed?.Invoke();
if (_blocker != null)
{