839 lines
30 KiB
C#
839 lines
30 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// 커스텀 버튼 컴포넌트.
|
|
/// 다양한 스타일 변형(Filled, Outline, Ghost, Text 등)과 아이콘을 지원합니다.
|
|
/// Material Icons와 이미지 아이콘을 모두 사용할 수 있으며, 클릭 이벤트와 비동기 액션을 지원합니다.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <para><b>1. Filled Buttons (배경이 채워진 버튼)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var primaryBtn = new UTKButton("Primary", variant: ButtonVariant.Primary);
|
|
/// var normalBtn = new UTKButton("Normal", variant: ButtonVariant.Normal);
|
|
/// var dangerBtn = new UTKButton("Danger", variant: ButtonVariant.Danger);
|
|
///
|
|
/// primaryBtn.OnClicked += () => Debug.Log("Primary 클릭!");
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton text="Primary" variant="Primary" />
|
|
/// // <utk:UTKButton text="Normal" variant="Normal" />
|
|
/// // <utk:UTKButton text="Danger" variant="Danger" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>2. Outline Buttons (외곽선 버튼)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var outlinePrimary = new UTKButton("Outline Primary", variant: ButtonVariant.OutlinePrimary);
|
|
/// var outlineNormal = new UTKButton("Outline Normal", variant: ButtonVariant.OutlineNormal);
|
|
/// var outlineDanger = new UTKButton("Outline Danger", variant: ButtonVariant.OutlineDanger);
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton text="Outline Primary" variant="OutlinePrimary" />
|
|
/// // <utk:UTKButton text="Outline Normal" variant="OutlineNormal" />
|
|
/// // <utk:UTKButton text="Outline Danger" variant="OutlineDanger" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>3. Icon Only (아이콘만 표시)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var plusOneBtn = new UTKButton("", UTKMaterialIcons.PlusOne, ButtonVariant.Primary) { IconOnly = true };
|
|
/// var editBtn = new UTKButton("", UTKMaterialIcons.Edit, ButtonVariant.Normal) { IconOnly = true };
|
|
/// var closeBtn = new UTKButton("", UTKMaterialIcons.Close, ButtonVariant.Danger) { IconOnly = true };
|
|
/// var settingsBtn = new UTKButton("", UTKMaterialIcons.Settings, ButtonVariant.OutlinePrimary) { IconOnly = true };
|
|
/// var cancelBtn = new UTKButton("", UTKMaterialIcons.Cancel, ButtonVariant.OutlineNormal) { IconOnly = true };
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Close" icon-only="true" variant="Primary" />
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Check" icon-only="true" variant="Normal" />
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Settings" icon-only="true" variant="Danger" />
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Edit" icon-only="true" variant="OutlinePrimary" />
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Search" icon-only="true" variant="OutlineNormal" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>4. Ghost (반투명 배경)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var ghostBtn = new UTKButton("Ghost", variant: ButtonVariant.Ghost);
|
|
/// var ghostIconBtn = new UTKButton("", UTKMaterialIcons.Settings, ButtonVariant.Ghost) { IconOnly = true };
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton text="Ghost" variant="Ghost" />
|
|
/// // <utk:UTKButton icon="UTKMaterialIcons.Close" icon-only="true" variant="Ghost" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>5. Text (Label/Icon Only - 배경 없음)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var textOnlyBtn = new UTKButton("Text Only", variant: ButtonVariant.Text);
|
|
/// var linkStyleBtn = new UTKButton("Link Style", variant: ButtonVariant.Text);
|
|
/// var withIconBtn = new UTKButton("With Icon", UTKMaterialIcons.Bolt, ButtonVariant.Text);
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton text="Text Only" variant="Text" />
|
|
/// // <utk:UTKButton text="Link Style" variant="Text" />
|
|
/// // <utk:UTKButton text="With Icon" icon="close" variant="Text" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>6. Text Icon Only (Circle - 원형 배경의 아이콘 버튼)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var circleBtn1 = new UTKButton("", UTKMaterialIcons.Home, ButtonVariant.Text, 12) { IconOnly = true };
|
|
///
|
|
/// var circleBtn2 = new UTKButton("", UTKMaterialIcons.Home, ButtonVariant.Text, 12) { IconOnly = true };
|
|
/// var circleBtn3 = new UTKButton("", UTKMaterialIcons.Close, ButtonVariant.Text, 12) { IconOnly = true };
|
|
/// var circleBtn4 = new UTKButton("", UTKMaterialIcons.Check, ButtonVariant.Text, 12) { IconOnly = true };
|
|
/// var circleBtn5 = new UTKButton("", UTKMaterialIcons.Search, ButtonVariant.Text, 12) { IconOnly = true };
|
|
/// var circleBtn6 = new UTKButton("", UTKMaterialIcons.Edit, ButtonVariant.Text, 12) { IconOnly = true };
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton icon="home" icon-only="true" icon-size="12" variant="Text" />
|
|
/// // <utk:UTKButton icon="close" icon-only="true" icon-size="12" variant="Text" />
|
|
/// // <utk:UTKButton icon="check" icon-only="true" icon-size="12" variant="Text" />
|
|
/// // <utk:UTKButton icon="search" icon-only="true" icon-size="12" variant="Text" />
|
|
/// // <utk:UTKButton icon="edit" icon-only="true" icon-size="12" variant="Text" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>7. Disabled (비활성화)</b></para>
|
|
/// <code>
|
|
/// // C# 코드
|
|
/// var disabledBtn1 = new UTKButton("Disabled", variant: ButtonVariant.Primary);
|
|
/// disabledBtn1.SetEnabled(false);
|
|
///
|
|
/// var disabledBtn2 = new UTKButton("Disabled", variant: ButtonVariant.Normal);
|
|
/// disabledBtn2.SetEnabled(false);
|
|
///
|
|
/// var disabledBtn3 = new UTKButton("Disabled", variant: ButtonVariant.Danger);
|
|
/// disabledBtn3.SetEnabled(false);
|
|
///
|
|
/// // UXML 태그
|
|
/// // <utk:UTKButton text="Disabled" variant="Primary" is-enabled="false" />
|
|
/// // <utk:UTKButton text="Disabled" variant="Normal" is-enabled="false" />
|
|
/// // <utk:UTKButton text="Disabled" variant="Danger" is-enabled="false" />
|
|
/// </code>
|
|
///
|
|
/// <para><b>8. UTKButton Action 사용 (비동기 작업 지원)</b></para>
|
|
/// <code>
|
|
/// // Action 사용 - 일반 동기 작업
|
|
/// var saveBtn = new UTKButton("저장", UTKMaterialIcons.Save, ButtonVariant.Primary);
|
|
/// saveBtn.OnClicked += () => {
|
|
/// Debug.Log("저장 중...");
|
|
/// SaveData();
|
|
/// };
|
|
///
|
|
/// // Action 사용 - UniTask 비동기 작업
|
|
/// var loadBtn = new UTKButton("로드", UTKMaterialIcons.Download, ButtonVariant.Primary);
|
|
/// loadBtn.OnClicked += async () => {
|
|
/// Debug.Log("로딩 시작...");
|
|
/// await LoadDataAsync();
|
|
/// Debug.Log("로딩 완료!");
|
|
/// };
|
|
///
|
|
/// // CancellationToken을 사용한 비동기 작업
|
|
/// var cts = new CancellationTokenSource();
|
|
/// var processBtn = new UTKButton("처리", ButtonVariant.Primary);
|
|
/// processBtn.OnClicked += async () => {
|
|
/// try {
|
|
/// processBtn.SetEnabled(false); // 처리 중 비활성화
|
|
/// await ProcessDataAsync(cts.Token);
|
|
/// } catch (OperationCanceledException) {
|
|
/// Debug.Log("작업이 취소되었습니다.");
|
|
/// } finally {
|
|
/// processBtn.SetEnabled(true);
|
|
/// }
|
|
/// };
|
|
///
|
|
/// // UXML에서 로드 후 Action 할당
|
|
/// var root = GetComponent<UIDocument>().rootVisualElement;
|
|
/// var confirmBtn = root.Q<UTKButton>("confirm-button");
|
|
/// confirmBtn.OnClicked += () => OnConfirm();
|
|
///
|
|
/// // 여러 Action 구독
|
|
/// var multiBtn = new UTKButton("Multi Action", ButtonVariant.Primary);
|
|
/// multiBtn.OnClicked += Action1;
|
|
/// multiBtn.OnClicked += Action2;
|
|
/// multiBtn.OnClicked += Action3;
|
|
///
|
|
/// // Action 해제
|
|
/// multiBtn.OnClicked -= Action2;
|
|
/// </code>
|
|
///
|
|
/// <para><b>9. 아이콘 설정 방법</b></para>
|
|
/// <code>
|
|
/// // Material Icon 설정
|
|
/// var btn = new UTKButton("설정");
|
|
/// btn.SetMaterialIcon(UTKMaterialIcons.Settings);
|
|
///
|
|
/// // 이미지 아이콘 설정
|
|
/// btn.SetImageIcon(UTKImageIcons.BtnClose22);
|
|
/// btn.SetImageIconByName("icon_setting_22");
|
|
///
|
|
/// // 비동기 아이콘 설정
|
|
/// await btn.SetMaterialIconAsync(UTKMaterialIcons.Search);
|
|
/// await btn.SetImageIconAsync(UTKImageIcons.IconSetting22);
|
|
///
|
|
/// // 아이콘 크기 지정
|
|
/// var largeIconBtn = new UTKButton("Large Icon", UTKMaterialIcons.Settings, ButtonVariant.Primary, 28);
|
|
/// btn.IconSize = 24;
|
|
///
|
|
/// // 아이콘 제거
|
|
/// btn.ClearIcon();
|
|
///
|
|
/// // 주의: 생성자에서 icon 파라미터로 아이콘을 전달하면 다음 순서로 타입이 감지됩니다.
|
|
/// // 1. UTKMaterialIcons에서 먼저 검사 (Material Symbols Outlined 아이콘)
|
|
/// // 2. UTKImageIcons에서 검사 (이미지 기반 아이콘)
|
|
/// // 3. 둘 다 아니면 텍스트로 처리됨
|
|
/// </code>
|
|
///
|
|
/// <para><b>10. 기타 속성 설정</b></para>
|
|
/// <code>
|
|
/// // 버튼 크기
|
|
/// btn.Size = ButtonSize.Small;
|
|
/// btn.Size = ButtonSize.Medium;
|
|
/// btn.Size = ButtonSize.Large;
|
|
///
|
|
/// // 커스텀 배경색
|
|
/// btn.BackgroundColor = new Color(0.2f, 0.4f, 0.8f);
|
|
///
|
|
/// // 외곽선 굵기
|
|
/// btn.BorderWidth = 2;
|
|
///
|
|
/// // 텍스트 동적 변경
|
|
/// btn.Text = "새로운 텍스트";
|
|
///
|
|
/// // UXML에서 사용 시 네임스페이스 선언
|
|
/// // <ui:UXML xmlns:utk="UVC.UIToolkit">
|
|
/// // <utk:UTKButton text="버튼" />
|
|
/// // </ui:UXML>
|
|
/// </code>
|
|
/// </example>
|
|
[UxmlElement]
|
|
public partial class UTKButton : VisualElement, IDisposable
|
|
{
|
|
#region Constants
|
|
private const string USS_PATH = "UIToolkit/Button/UTKButton";
|
|
#endregion
|
|
|
|
#region Fields
|
|
private bool _disposed;
|
|
private Label? _iconLabel;
|
|
private Label? _textLabel;
|
|
private VisualElement? _imageIcon;
|
|
|
|
// UXML 직렬화용 backing field - 이름을 소스 생성기가 인식하지 못하게 변경
|
|
private string textValue = "";
|
|
private string iconValue = "";
|
|
private ButtonVariant variantValue = ButtonVariant.Normal;
|
|
private ButtonSize sizeValue = ButtonSize.Medium;
|
|
private Color? _backgroundColor;
|
|
private int borderWidthValue = -1;
|
|
private int? iconSizeValue = null;
|
|
private bool iconOnlyValue;
|
|
private bool isEnabledValue = true;
|
|
#endregion
|
|
|
|
#region Events
|
|
/// <summary>버튼 클릭 이벤트</summary>
|
|
public event Action? OnClicked;
|
|
#endregion
|
|
|
|
#region Properties
|
|
/// <summary>버튼 텍스트</summary>
|
|
[UxmlAttribute("text")]
|
|
public string Text
|
|
{
|
|
get => textValue;
|
|
set
|
|
{
|
|
textValue = value;
|
|
UpdateContent();
|
|
}
|
|
}
|
|
|
|
/// <summary>아이콘 (유니코드 문자 또는 텍스트)</summary>
|
|
[UxmlAttribute("icon")]
|
|
public string Icon
|
|
{
|
|
get => iconValue;
|
|
set
|
|
{
|
|
iconValue = value;
|
|
UpdateContent();
|
|
}
|
|
}
|
|
|
|
/// <summary>버튼 스타일 변형</summary>
|
|
[UxmlAttribute("variant")]
|
|
public ButtonVariant Variant
|
|
{
|
|
get => variantValue;
|
|
set
|
|
{
|
|
variantValue = value;
|
|
UpdateVariant();
|
|
}
|
|
}
|
|
|
|
/// <summary>버튼 크기</summary>
|
|
[UxmlAttribute("size")]
|
|
public ButtonSize Size
|
|
{
|
|
get => sizeValue;
|
|
set
|
|
{
|
|
sizeValue = value;
|
|
UpdateSize();
|
|
}
|
|
}
|
|
|
|
/// <summary>아이콘 크기</summary>
|
|
[UxmlAttribute("icon-size")]
|
|
public int IconSize
|
|
{
|
|
get => iconSizeValue ?? 12;
|
|
set
|
|
{
|
|
//if(iconSizeValue == value) return;
|
|
iconSizeValue = value;
|
|
string iconChar = UTKMaterialIcons.GetIcon(iconValue);
|
|
if (iconChar != string.Empty)
|
|
{
|
|
UpdateMaterialIconSize(iconSizeValue);
|
|
}
|
|
// 2순위: UTKImageIcons에 해당하는지 확인
|
|
else if (!string.IsNullOrEmpty(UTKImageIcons.GetPath(iconValue)))
|
|
{
|
|
UpdateImageIconSize(iconSizeValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>커스텀 배경 색상 (null이면 기본 스타일 사용)</summary>
|
|
public Color? BackgroundColor
|
|
{
|
|
get => _backgroundColor;
|
|
set
|
|
{
|
|
_backgroundColor = value;
|
|
UpdateCustomStyles();
|
|
}
|
|
}
|
|
|
|
/// <summary>외곽선 굵기 (-1이면 기본값 사용)</summary>
|
|
[UxmlAttribute("border-width")]
|
|
public int BorderWidth
|
|
{
|
|
get => borderWidthValue;
|
|
set
|
|
{
|
|
borderWidthValue = value;
|
|
UpdateCustomStyles();
|
|
}
|
|
}
|
|
|
|
/// <summary>아이콘만 표시 모드</summary>
|
|
[UxmlAttribute("icon-only")]
|
|
public bool IconOnly
|
|
{
|
|
get => iconOnlyValue;
|
|
set
|
|
{
|
|
iconOnlyValue = value;
|
|
UpdateContent();
|
|
UpdateIcon(iconValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>버튼 활성화 상태</summary>
|
|
[UxmlAttribute("is-enabled")]
|
|
public bool IsEnabled
|
|
{
|
|
get => isEnabledValue;
|
|
set
|
|
{
|
|
isEnabledValue = value;
|
|
SetEnabled(value);
|
|
EnableInClassList("utk-button--disabled", !value);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Enums
|
|
public enum ButtonVariant
|
|
{
|
|
Normal,
|
|
Primary,
|
|
Secondary,
|
|
Ghost,
|
|
Danger,
|
|
OutlineNormal,
|
|
OutlinePrimary,
|
|
OutlineDanger,
|
|
/// <summary>배경과 외곽선이 투명하고 텍스트/아이콘만 표시</summary>
|
|
Text
|
|
}
|
|
|
|
public enum ButtonSize
|
|
{
|
|
Small,
|
|
Medium,
|
|
Large
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public UTKButton()
|
|
{
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|
if (uss != null)
|
|
{
|
|
styleSheets.Add(uss);
|
|
}
|
|
|
|
CreateUI();
|
|
SetupEvents();
|
|
SubscribeToThemeChanges();
|
|
|
|
// UXML에서 로드될 때 속성이 설정된 후 UI 갱신
|
|
// Unity 6의 소스 생성기는 Deserialize에서 필드에 직접 값을 할당하므로
|
|
// AttachToPanelEvent를 사용하여 패널에 연결된 후 UI를 갱신
|
|
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 텍스트, 아이콘, 변형, 크기를 설정하여 버튼을 생성합니다.
|
|
/// </summary>
|
|
/// <param name="text">버튼에 표시할 텍스트</param>
|
|
/// <param name="icon">아이콘 이름 또는 경로 (Material Icon 또는 Image Icon)</param>
|
|
/// <param name="variant">버튼 스타일 변형</param>
|
|
/// <param name="iconSize">아이콘 크기 (생략 시 기본값 사용)</param>
|
|
/// <remarks>
|
|
/// 아이콘 타입 감지 순서:
|
|
/// 1. UTKMaterialIcons 검사 (Material Symbols Outlined 아이콘)
|
|
/// 2. UTKImageIcons 검사 (이미지 기반 아이콘)
|
|
/// 3. 둘 다 아니면 텍스트로 처리됨
|
|
/// </remarks>
|
|
public UTKButton(string text, string icon = "", ButtonVariant variant = ButtonVariant.Normal, int? iconSize = null) : this()
|
|
{
|
|
textValue = text;
|
|
iconValue = icon;
|
|
variantValue = variant;
|
|
UpdateContent();
|
|
UpdateVariant();
|
|
iconSizeValue = iconSize;
|
|
UpdateIcon(icon);
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region UI Creation
|
|
private void CreateUI()
|
|
{
|
|
AddToClassList("utk-button");
|
|
focusable = true;
|
|
pickingMode = PickingMode.Position;
|
|
|
|
_iconLabel = new Label
|
|
{
|
|
name = "icon",
|
|
pickingMode = PickingMode.Ignore
|
|
};
|
|
_iconLabel.AddToClassList("utk-button__icon");
|
|
Add(_iconLabel);
|
|
|
|
_textLabel = new Label
|
|
{
|
|
name = "text",
|
|
pickingMode = PickingMode.Ignore
|
|
};
|
|
_textLabel.AddToClassList("utk-button__text");
|
|
Add(_textLabel);
|
|
|
|
UpdateContent();
|
|
UpdateVariant();
|
|
UpdateSize();
|
|
}
|
|
|
|
private void SetupEvents()
|
|
{
|
|
RegisterCallback<ClickEvent>(OnClick);
|
|
RegisterCallback<KeyDownEvent>(OnKeyDown, TrickleDown.TrickleDown);
|
|
}
|
|
|
|
private void SubscribeToThemeChanges()
|
|
{
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
|
|
RegisterCallback<AttachToPanelEvent>(OnReattachToPanel);
|
|
}
|
|
|
|
private void OnDetachFromPanel(DetachFromPanelEvent evt)
|
|
{
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
}
|
|
|
|
private void OnReattachToPanel(AttachToPanelEvent evt)
|
|
{
|
|
// TreeView 가상화로 인해 재연결 시 테마 재적용
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged; // 중복 방지
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
}
|
|
|
|
private void OnThemeChanged(UTKTheme theme)
|
|
{
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
}
|
|
#endregion
|
|
|
|
#region Event Handlers
|
|
private void OnAttachToPanel(AttachToPanelEvent evt)
|
|
{
|
|
// UXML 속성이 설정된 후 한 번만 UI 갱신
|
|
UnregisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
|
UpdateContent();
|
|
UpdateVariant();
|
|
UpdateSize();
|
|
UpdateCustomStyles();
|
|
|
|
// IsEnabled 상태 적용
|
|
SetEnabled(isEnabledValue);
|
|
EnableInClassList("utk-button--disabled", !isEnabledValue);
|
|
}
|
|
|
|
private void OnClick(ClickEvent evt)
|
|
{
|
|
if (!isEnabledValue) return;
|
|
OnClicked?.Invoke();
|
|
evt.StopPropagation();
|
|
}
|
|
|
|
private void OnKeyDown(KeyDownEvent evt)
|
|
{
|
|
if (!isEnabledValue) return;
|
|
if (evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.Space)
|
|
{
|
|
OnClicked?.Invoke();
|
|
evt.StopPropagation();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Update Methods
|
|
private void UpdateContent()
|
|
{
|
|
bool hasIcon = !string.IsNullOrEmpty(iconValue);
|
|
bool hasText = !string.IsNullOrEmpty(textValue);
|
|
|
|
if (_iconLabel != null)
|
|
{
|
|
_iconLabel.text = iconValue;
|
|
_iconLabel.style.display = hasIcon ? DisplayStyle.Flex : DisplayStyle.None;
|
|
}
|
|
|
|
if (_textLabel != null)
|
|
{
|
|
_textLabel.text = textValue;
|
|
_textLabel.style.display = (iconOnlyValue || !hasText) ? DisplayStyle.None : DisplayStyle.Flex;
|
|
}
|
|
|
|
EnableInClassList("utk-button--icon-only", iconOnlyValue || (hasIcon && !hasText));
|
|
EnableInClassList("utk-button--has-icon", hasIcon && hasText && !iconOnlyValue);
|
|
}
|
|
|
|
private void UpdateVariant()
|
|
{
|
|
RemoveFromClassList("utk-button--normal");
|
|
RemoveFromClassList("utk-button--primary");
|
|
RemoveFromClassList("utk-button--secondary");
|
|
RemoveFromClassList("utk-button--ghost");
|
|
RemoveFromClassList("utk-button--danger");
|
|
RemoveFromClassList("utk-button--outline-normal");
|
|
RemoveFromClassList("utk-button--outline-primary");
|
|
RemoveFromClassList("utk-button--outline-danger");
|
|
RemoveFromClassList("utk-button--text");
|
|
|
|
var variantClass = variantValue switch
|
|
{
|
|
ButtonVariant.Primary => "utk-button--primary",
|
|
ButtonVariant.Secondary => "utk-button--secondary",
|
|
ButtonVariant.Ghost => "utk-button--ghost",
|
|
ButtonVariant.Danger => "utk-button--danger",
|
|
ButtonVariant.OutlineNormal => "utk-button--outline-normal",
|
|
ButtonVariant.OutlinePrimary => "utk-button--outline-primary",
|
|
ButtonVariant.OutlineDanger => "utk-button--outline-danger",
|
|
ButtonVariant.Text => "utk-button--text",
|
|
_ => "utk-button--normal"
|
|
};
|
|
AddToClassList(variantClass);
|
|
}
|
|
|
|
private void UpdateSize()
|
|
{
|
|
RemoveFromClassList("utk-button--small");
|
|
RemoveFromClassList("utk-button--medium");
|
|
RemoveFromClassList("utk-button--large");
|
|
|
|
var sizeClass = sizeValue switch
|
|
{
|
|
ButtonSize.Small => "utk-button--small",
|
|
ButtonSize.Large => "utk-button--large",
|
|
_ => "utk-button--medium"
|
|
};
|
|
AddToClassList(sizeClass);
|
|
}
|
|
|
|
private void UpdateCustomStyles()
|
|
{
|
|
if (_backgroundColor.HasValue)
|
|
{
|
|
style.backgroundColor = _backgroundColor.Value;
|
|
}
|
|
else
|
|
{
|
|
style.backgroundColor = StyleKeyword.Null;
|
|
}
|
|
|
|
if (borderWidthValue >= 0)
|
|
{
|
|
style.borderTopWidth = borderWidthValue;
|
|
style.borderBottomWidth = borderWidthValue;
|
|
style.borderLeftWidth = borderWidthValue;
|
|
style.borderRightWidth = borderWidthValue;
|
|
}
|
|
else
|
|
{
|
|
style.borderTopWidth = StyleKeyword.Null;
|
|
style.borderBottomWidth = StyleKeyword.Null;
|
|
style.borderLeftWidth = StyleKeyword.Null;
|
|
style.borderRightWidth = StyleKeyword.Null;
|
|
}
|
|
}
|
|
|
|
private void UpdateIcon(string icon)
|
|
{
|
|
// 아이콘 타입 자동 감지 및 적용
|
|
if (!string.IsNullOrEmpty(icon))
|
|
{
|
|
// 1순위: UTKMaterialIcons에 해당하는지 확인
|
|
string iconChar = UTKMaterialIcons.GetIcon(icon);
|
|
if (iconChar != string.Empty)
|
|
{
|
|
SetMaterialIcon(iconChar, iconSizeValue);
|
|
}
|
|
// 2순위: UTKImageIcons에 해당하는지 확인
|
|
else if (!string.IsNullOrEmpty(UTKImageIcons.GetPath(icon)))
|
|
{
|
|
SetImageIcon(icon, iconSizeValue);
|
|
}
|
|
// 3순위: 둘 다 아니면 현재 로직 유지 (UpdateContent에서 이미 처리됨)
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Icon Methods
|
|
|
|
/// <summary>
|
|
/// Material Icon을 설정합니다.
|
|
/// </summary>
|
|
/// <param name="icon">Material Icon 유니코드 문자 (예: UTKMaterialIcons.Settings)</param>
|
|
/// <param name="fontSize">아이콘 폰트 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public void SetMaterialIcon(string icon, int? fontSize = null)
|
|
{
|
|
ClearImageIcon();
|
|
Icon = icon;
|
|
UpdateMaterialIconSize(fontSize);
|
|
}
|
|
|
|
private void UpdateMaterialIconSize(int? fontSize)
|
|
{
|
|
if (_iconLabel != null)
|
|
{
|
|
UTKMaterialIcons.ApplyIconStyle(_iconLabel, fontSize ?? GetDefaultIconSize());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Material Icon을 비동기로 설정합니다.
|
|
/// </summary>
|
|
/// <param name="icon">Material Icon 유니코드 문자 (예: UTKMaterialIcons.Settings)</param>
|
|
/// <param name="ct">취소 토큰</param>
|
|
/// <param name="fontSize">아이콘 폰트 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public async UniTask SetMaterialIconAsync(string icon, CancellationToken ct = default, int? fontSize = null)
|
|
{
|
|
ClearImageIcon();
|
|
Icon = icon;
|
|
|
|
if (_iconLabel != null)
|
|
{
|
|
await UTKMaterialIcons.ApplyIconStyleAsync(_iconLabel, ct, fontSize ?? GetDefaultIconSize());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 아이콘 이름으로 Material Icon을 설정합니다.
|
|
/// </summary>
|
|
/// <param name="iconName">아이콘 이름 (예: "settings", "home")</param>
|
|
/// <param name="fontSize">아이콘 폰트 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public void SetMaterialIconByName(string iconName, int? fontSize = null)
|
|
{
|
|
string iconChar = UTKMaterialIcons.GetIcon(iconName);
|
|
if (iconChar != string.Empty)
|
|
{
|
|
SetMaterialIcon(iconChar, fontSize);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[UTKButton] Material icon '{iconName}'을(를) 찾을 수 없습니다.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Image Icon을 설정합니다.
|
|
/// </summary>
|
|
/// <param name="resourcePath">이미지 리소스 경로 (예: UTKImageIcons.IconSetting22)</param>
|
|
/// <param name="iconSize">아이콘 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public void SetImageIcon(string resourcePath, int? iconSize = null)
|
|
{
|
|
var texture = UTKImageIcons.LoadTexture(resourcePath);
|
|
if (texture != null)
|
|
{
|
|
ApplyImageIcon(texture, iconSize);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[UTKButton] Image icon '{resourcePath}'을(를) 로드할 수 없습니다.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Image Icon을 비동기로 설정합니다.
|
|
/// </summary>
|
|
/// <param name="resourcePath">이미지 리소스 경로 (예: UTKImageIcons.IconSetting22)</param>
|
|
/// <param name="ct">취소 토큰</param>
|
|
/// <param name="iconSize">아이콘 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public async UniTask SetImageIconAsync(string resourcePath, CancellationToken ct = default, int? iconSize = null)
|
|
{
|
|
var texture = await UTKImageIcons.LoadTextureAsync(resourcePath, ct);
|
|
if (texture != null)
|
|
{
|
|
ApplyImageIcon(texture, iconSize);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[UTKButton] Image icon '{resourcePath}'을(를) 로드할 수 없습니다.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 아이콘 이름으로 Image Icon을 설정합니다.
|
|
/// </summary>
|
|
/// <param name="iconName">아이콘 이름 (예: "icon_setting_22", "btn_close_16")</param>
|
|
/// <param name="iconSize">아이콘 크기 (null이면 버튼 크기에 맞춤)</param>
|
|
public void SetImageIconByName(string iconName, int? iconSize = null)
|
|
{
|
|
var texture = UTKImageIcons.LoadTextureByName(iconName);
|
|
if (texture != null)
|
|
{
|
|
ApplyImageIcon(texture, iconSize);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[UTKButton] Image icon '{iconName}'을(를) 찾을 수 없습니다.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 모든 아이콘을 제거합니다.
|
|
/// </summary>
|
|
public void ClearIcon()
|
|
{
|
|
Icon = "";
|
|
ClearImageIcon();
|
|
|
|
if (_iconLabel != null)
|
|
{
|
|
_iconLabel.style.display = DisplayStyle.None;
|
|
}
|
|
}
|
|
|
|
private void ApplyImageIcon(Texture2D texture, int? iconSize)
|
|
{
|
|
// 기존 아이콘 Label 숨기기
|
|
Icon = "";
|
|
if (_iconLabel != null)
|
|
{
|
|
_iconLabel.style.display = DisplayStyle.None;
|
|
}
|
|
|
|
// 이미지 아이콘용 VisualElement 생성 또는 재사용
|
|
if (_imageIcon == null)
|
|
{
|
|
_imageIcon = new VisualElement
|
|
{
|
|
name = "image-icon",
|
|
pickingMode = PickingMode.Ignore
|
|
};
|
|
_imageIcon.AddToClassList("utk-button__image-icon");
|
|
Insert(0, _imageIcon);
|
|
}
|
|
UpdateImageIconSize(iconSize);
|
|
_imageIcon.style.backgroundImage = new StyleBackground(texture);
|
|
_imageIcon.style.display = DisplayStyle.Flex;
|
|
|
|
EnableInClassList("utk-button--has-image-icon", true);
|
|
}
|
|
|
|
private void UpdateImageIconSize(int? iconSize)
|
|
{
|
|
if (_imageIcon != null)
|
|
{
|
|
var size = iconSize ?? GetDefaultIconSize();
|
|
_imageIcon.style.width = size;
|
|
_imageIcon.style.height = size;
|
|
}
|
|
}
|
|
|
|
private void ClearImageIcon()
|
|
{
|
|
if (_imageIcon != null)
|
|
{
|
|
_imageIcon.style.display = DisplayStyle.None;
|
|
}
|
|
EnableInClassList("utk-button--has-image-icon", false);
|
|
}
|
|
|
|
private int GetDefaultIconSize()
|
|
{
|
|
return sizeValue switch
|
|
{
|
|
ButtonSize.Small => 16,
|
|
ButtonSize.Large => 24,
|
|
_ => 20 // Medium
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
OnClicked = null;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|