UIToolkit Sample uxml로 전환
This commit is contained in:
@@ -123,14 +123,15 @@ namespace UVC.UIToolkit
|
||||
private Label? _textLabel;
|
||||
private VisualElement? _imageIcon;
|
||||
|
||||
private string _text = "";
|
||||
private string _icon = "";
|
||||
private ButtonVariant _variant = ButtonVariant.Normal;
|
||||
private ButtonSize _size = ButtonSize.Medium;
|
||||
// UXML 직렬화용 backing field - 이름을 소스 생성기가 인식하지 못하게 변경
|
||||
private string textValue = "";
|
||||
private string iconValue = "";
|
||||
private ButtonVariant variantValue = ButtonVariant.Normal;
|
||||
private ButtonSize sizeValue = ButtonSize.Medium;
|
||||
private Color? _backgroundColor;
|
||||
private int _borderWidth = -1;
|
||||
private bool _iconOnly;
|
||||
private bool _isEnabled = true;
|
||||
private int borderWidthValue = -1;
|
||||
private bool iconOnlyValue;
|
||||
private bool isEnabledValue = true;
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
@@ -140,49 +141,49 @@ namespace UVC.UIToolkit
|
||||
|
||||
#region Properties
|
||||
/// <summary>버튼 텍스트</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("text")]
|
||||
public string Text
|
||||
{
|
||||
get => _text;
|
||||
get => textValue;
|
||||
set
|
||||
{
|
||||
_text = value;
|
||||
textValue = value;
|
||||
UpdateContent();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>아이콘 (유니코드 문자 또는 텍스트)</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("icon")]
|
||||
public string Icon
|
||||
{
|
||||
get => _icon;
|
||||
get => iconValue;
|
||||
set
|
||||
{
|
||||
_icon = value;
|
||||
iconValue = value;
|
||||
UpdateContent();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>버튼 스타일 변형</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("variant")]
|
||||
public ButtonVariant Variant
|
||||
{
|
||||
get => _variant;
|
||||
get => variantValue;
|
||||
set
|
||||
{
|
||||
_variant = value;
|
||||
variantValue = value;
|
||||
UpdateVariant();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>버튼 크기</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("size")]
|
||||
public ButtonSize Size
|
||||
{
|
||||
get => _size;
|
||||
get => sizeValue;
|
||||
set
|
||||
{
|
||||
_size = value;
|
||||
sizeValue = value;
|
||||
UpdateSize();
|
||||
}
|
||||
}
|
||||
@@ -199,37 +200,37 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>외곽선 굵기 (-1이면 기본값 사용)</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("border-width")]
|
||||
public int BorderWidth
|
||||
{
|
||||
get => _borderWidth;
|
||||
get => borderWidthValue;
|
||||
set
|
||||
{
|
||||
_borderWidth = value;
|
||||
borderWidthValue = value;
|
||||
UpdateCustomStyles();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>아이콘만 표시 모드</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("icon-only")]
|
||||
public bool IconOnly
|
||||
{
|
||||
get => _iconOnly;
|
||||
get => iconOnlyValue;
|
||||
set
|
||||
{
|
||||
_iconOnly = value;
|
||||
iconOnlyValue = value;
|
||||
UpdateContent();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>버튼 활성화 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-enabled")]
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _isEnabled;
|
||||
get => isEnabledValue;
|
||||
set
|
||||
{
|
||||
_isEnabled = value;
|
||||
isEnabledValue = value;
|
||||
SetEnabled(value);
|
||||
EnableInClassList("utk-button--disabled", !value);
|
||||
}
|
||||
@@ -275,12 +276,9 @@ namespace UVC.UIToolkit
|
||||
SubscribeToThemeChanges();
|
||||
|
||||
// UXML에서 로드될 때 속성이 설정된 후 UI 갱신
|
||||
RegisterCallback<AttachToPanelEvent>(_ =>
|
||||
{
|
||||
UpdateContent();
|
||||
UpdateVariant();
|
||||
UpdateSize();
|
||||
});
|
||||
// Unity 6의 소스 생성기는 Deserialize에서 필드에 직접 값을 할당하므로
|
||||
// AttachToPanelEvent를 사용하여 패널에 연결된 후 UI를 갱신
|
||||
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -298,9 +296,9 @@ namespace UVC.UIToolkit
|
||||
/// </remarks>
|
||||
public UTKButton(string text, string icon = "", ButtonVariant variant = ButtonVariant.Normal, int? iconSize = null) : this()
|
||||
{
|
||||
_text = text;
|
||||
_icon = icon;
|
||||
_variant = variant;
|
||||
textValue = text;
|
||||
iconValue = icon;
|
||||
variantValue = variant;
|
||||
UpdateContent();
|
||||
UpdateVariant();
|
||||
|
||||
@@ -372,16 +370,30 @@ namespace UVC.UIToolkit
|
||||
#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 (!_isEnabled) return;
|
||||
if (!isEnabledValue) return;
|
||||
OnClicked?.Invoke();
|
||||
evt.StopPropagation();
|
||||
}
|
||||
|
||||
private void OnKeyDown(KeyDownEvent evt)
|
||||
{
|
||||
if (!_isEnabled) return;
|
||||
if (!isEnabledValue) return;
|
||||
if (evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.Space)
|
||||
{
|
||||
OnClicked?.Invoke();
|
||||
@@ -393,23 +405,23 @@ namespace UVC.UIToolkit
|
||||
#region Update Methods
|
||||
private void UpdateContent()
|
||||
{
|
||||
bool hasIcon = !string.IsNullOrEmpty(_icon);
|
||||
bool hasText = !string.IsNullOrEmpty(_text);
|
||||
bool hasIcon = !string.IsNullOrEmpty(iconValue);
|
||||
bool hasText = !string.IsNullOrEmpty(textValue);
|
||||
|
||||
if (_iconLabel != null)
|
||||
{
|
||||
_iconLabel.text = _icon;
|
||||
_iconLabel.text = iconValue;
|
||||
_iconLabel.style.display = hasIcon ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
|
||||
if (_textLabel != null)
|
||||
{
|
||||
_textLabel.text = _text;
|
||||
_textLabel.style.display = (_iconOnly || !hasText) ? DisplayStyle.None : DisplayStyle.Flex;
|
||||
_textLabel.text = textValue;
|
||||
_textLabel.style.display = (iconOnlyValue || !hasText) ? DisplayStyle.None : DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
EnableInClassList("utk-button--icon-only", _iconOnly || (hasIcon && !hasText));
|
||||
EnableInClassList("utk-button--has-icon", hasIcon && hasText && !_iconOnly);
|
||||
EnableInClassList("utk-button--icon-only", iconOnlyValue || (hasIcon && !hasText));
|
||||
EnableInClassList("utk-button--has-icon", hasIcon && hasText && !iconOnlyValue);
|
||||
}
|
||||
|
||||
private void UpdateVariant()
|
||||
@@ -424,7 +436,7 @@ namespace UVC.UIToolkit
|
||||
RemoveFromClassList("utk-button--outline-danger");
|
||||
RemoveFromClassList("utk-button--text");
|
||||
|
||||
var variantClass = _variant switch
|
||||
var variantClass = variantValue switch
|
||||
{
|
||||
ButtonVariant.Primary => "utk-button--primary",
|
||||
ButtonVariant.Secondary => "utk-button--secondary",
|
||||
@@ -445,7 +457,7 @@ namespace UVC.UIToolkit
|
||||
RemoveFromClassList("utk-button--medium");
|
||||
RemoveFromClassList("utk-button--large");
|
||||
|
||||
var sizeClass = _size switch
|
||||
var sizeClass = sizeValue switch
|
||||
{
|
||||
ButtonSize.Small => "utk-button--small",
|
||||
ButtonSize.Large => "utk-button--large",
|
||||
@@ -465,12 +477,12 @@ namespace UVC.UIToolkit
|
||||
style.backgroundColor = StyleKeyword.Null;
|
||||
}
|
||||
|
||||
if (_borderWidth >= 0)
|
||||
if (borderWidthValue >= 0)
|
||||
{
|
||||
style.borderTopWidth = _borderWidth;
|
||||
style.borderBottomWidth = _borderWidth;
|
||||
style.borderLeftWidth = _borderWidth;
|
||||
style.borderRightWidth = _borderWidth;
|
||||
style.borderTopWidth = borderWidthValue;
|
||||
style.borderBottomWidth = borderWidthValue;
|
||||
style.borderLeftWidth = borderWidthValue;
|
||||
style.borderRightWidth = borderWidthValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -645,7 +657,7 @@ namespace UVC.UIToolkit
|
||||
|
||||
private int GetDefaultIconSize()
|
||||
{
|
||||
return _size switch
|
||||
return sizeValue switch
|
||||
{
|
||||
ButtonSize.Small => 16,
|
||||
ButtonSize.Large => 24,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UVC.UIToolkit.Common;
|
||||
|
||||
namespace UVC.UIToolkit
|
||||
{
|
||||
@@ -62,7 +61,7 @@ namespace UVC.UIToolkit
|
||||
|
||||
#region Properties
|
||||
/// <summary>라벨 텍스트</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("text")]
|
||||
public string Text
|
||||
{
|
||||
get => _text;
|
||||
@@ -74,7 +73,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>체크 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-checked")]
|
||||
public bool IsChecked
|
||||
{
|
||||
get => _isChecked;
|
||||
@@ -82,7 +81,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>불확정 상태 (일부 선택됨)</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-indeterminate")]
|
||||
public bool IsIndeterminate
|
||||
{
|
||||
get => _isIndeterminate;
|
||||
@@ -94,7 +93,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>활성화 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-enabled")]
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _isEnabled;
|
||||
@@ -123,11 +122,9 @@ namespace UVC.UIToolkit
|
||||
SubscribeToThemeChanges();
|
||||
|
||||
// UXML에서 로드될 때 속성이 설정된 후 UI 갱신
|
||||
RegisterCallback<AttachToPanelEvent>(_ =>
|
||||
{
|
||||
if (_label != null) _label.text = _text;
|
||||
UpdateState();
|
||||
});
|
||||
// Unity 6의 소스 생성기는 Deserialize에서 필드에 직접 값을 할당하므로
|
||||
// AttachToPanelEvent를 사용하여 패널에 연결된 후 UI를 갱신
|
||||
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
||||
}
|
||||
|
||||
public UTKCheckBox(string text, bool isChecked = false) : this()
|
||||
@@ -182,6 +179,18 @@ namespace UVC.UIToolkit
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
private void OnAttachToPanel(AttachToPanelEvent evt)
|
||||
{
|
||||
// UXML 속성이 설정된 후 한 번만 UI 갱신
|
||||
UnregisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
||||
if (_label != null) _label.text = _text;
|
||||
UpdateState();
|
||||
|
||||
// IsEnabled 상태 적용
|
||||
SetEnabled(_isEnabled);
|
||||
EnableInClassList("utk-checkbox--disabled", !_isEnabled);
|
||||
}
|
||||
|
||||
private void OnClick(ClickEvent evt)
|
||||
{
|
||||
if (!_isEnabled) return;
|
||||
|
||||
@@ -55,6 +55,7 @@ namespace UVC.UIToolkit
|
||||
|
||||
#region Properties
|
||||
/// <summary>라벨 텍스트</summary>
|
||||
[UxmlAttribute("text")]
|
||||
public string Text
|
||||
{
|
||||
get => text;
|
||||
@@ -62,6 +63,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>체크 상태</summary>
|
||||
[UxmlAttribute("is-checked")]
|
||||
public bool IsChecked
|
||||
{
|
||||
get => value;
|
||||
@@ -69,7 +71,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>활성화 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-enabled")]
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _isEnabled;
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace UVC.UIToolkit
|
||||
|
||||
#region Properties
|
||||
/// <summary>토글 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-on")]
|
||||
public bool IsOn
|
||||
{
|
||||
get => value;
|
||||
@@ -59,7 +59,7 @@ namespace UVC.UIToolkit
|
||||
}
|
||||
|
||||
/// <summary>활성화 상태</summary>
|
||||
[UxmlAttribute]
|
||||
[UxmlAttribute("is-enabled")]
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _isEnabled;
|
||||
|
||||
Reference in New Issue
Block a user