2026-01-08 20:15:57 +09:00
|
|
|
#nullable enable
|
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
|
|
namespace UVC.UIToolkit
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// 정수(int) 입력 필드 컴포넌트.
|
2026-01-08 20:15:57 +09:00
|
|
|
/// Unity IntegerField를 래핑하여 커스텀 스타일을 적용합니다.
|
2026-01-21 20:43:54 +09:00
|
|
|
/// -2,147,483,648 ~ 2,147,483,647 범위의 정수를 입력받습니다.
|
2026-01-08 20:15:57 +09:00
|
|
|
/// </summary>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// <remarks>
|
|
|
|
|
/// <para><b>int(정수)란?</b></para>
|
|
|
|
|
/// <para>int는 32비트 부호 있는 정수 타입으로, 소수점이 없는 숫자를 저장합니다.</para>
|
|
|
|
|
/// <list type="bullet">
|
|
|
|
|
/// <item><description>개수, 수량, 인덱스 등 정수 값에 사용</description></item>
|
|
|
|
|
/// <item><description>큰 숫자가 필요하면 UTKLongField 사용</description></item>
|
|
|
|
|
/// <item><description>소수점이 필요하면 UTKFloatField 사용</description></item>
|
|
|
|
|
/// </list>
|
|
|
|
|
/// </remarks>
|
2026-01-13 20:39:45 +09:00
|
|
|
/// <example>
|
|
|
|
|
/// <para><b>C# 코드에서 사용:</b></para>
|
|
|
|
|
/// <code>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// // 기본 정수 필드 생성
|
2026-01-13 20:39:45 +09:00
|
|
|
/// var intField = new UTKIntegerField();
|
|
|
|
|
/// intField.label = "수량";
|
2026-01-21 20:43:54 +09:00
|
|
|
/// intField.Value = 10;
|
|
|
|
|
///
|
2026-01-13 20:39:45 +09:00
|
|
|
/// // 값 변경 이벤트
|
2026-01-21 20:43:54 +09:00
|
|
|
/// intField.OnValueChanged += (value) => {
|
|
|
|
|
/// Debug.Log($"수량: {value}");
|
|
|
|
|
/// };
|
|
|
|
|
///
|
|
|
|
|
/// // 라벨과 기본값을 지정하는 생성자
|
|
|
|
|
/// var countField = new UTKIntegerField("아이템 개수", 5);
|
|
|
|
|
///
|
|
|
|
|
/// // 현재 값 읽기/쓰기
|
|
|
|
|
/// int currentValue = intField.Value;
|
|
|
|
|
/// intField.Value = 20;
|
|
|
|
|
///
|
|
|
|
|
/// // 비활성화
|
|
|
|
|
/// intField.IsEnabled = false;
|
2026-01-13 20:39:45 +09:00
|
|
|
/// </code>
|
|
|
|
|
/// <para><b>UXML에서 사용:</b></para>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// <code><![CDATA[
|
|
|
|
|
/// <!-- 기본 정수 필드 -->
|
|
|
|
|
/// <utk:UTKIntegerField label="수량" value="10" />
|
|
|
|
|
///
|
|
|
|
|
/// <!-- 비활성화 상태 -->
|
|
|
|
|
/// <utk:UTKIntegerField label="고정 값" value="100" is-enabled="false" />
|
|
|
|
|
/// ]]></code>
|
|
|
|
|
/// <para><b>실제 활용 예시 (인벤토리 수량):</b></para>
|
2026-01-13 20:39:45 +09:00
|
|
|
/// <code>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// // 인벤토리 아이템 수량 편집
|
|
|
|
|
/// var quantityField = new UTKIntegerField("보유 수량", item.Quantity);
|
|
|
|
|
/// quantityField.OnValueChanged += (newQty) => {
|
|
|
|
|
/// item.Quantity = Mathf.Max(0, newQty); // 음수 방지
|
|
|
|
|
/// UpdateInventoryUI();
|
|
|
|
|
/// };
|
2026-01-13 20:39:45 +09:00
|
|
|
/// </code>
|
|
|
|
|
/// </example>
|
2026-01-08 20:15:57 +09:00
|
|
|
[UxmlElement]
|
|
|
|
|
public partial class UTKIntegerField : IntegerField, IDisposable
|
|
|
|
|
{
|
|
|
|
|
#region Constants
|
|
|
|
|
private const string USS_PATH = "UIToolkit/Input/UTKIntegerField";
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Fields
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
private bool _isEnabled = true;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
/// <summary>값 변경 이벤트</summary>
|
|
|
|
|
public event Action<int>? OnValueChanged;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
/// <summary>현재 값</summary>
|
|
|
|
|
public int Value
|
|
|
|
|
{
|
|
|
|
|
get => value;
|
|
|
|
|
set => this.value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>활성화 상태</summary>
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("is-enabled")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public bool IsEnabled
|
|
|
|
|
{
|
|
|
|
|
get => _isEnabled;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isEnabled = value;
|
|
|
|
|
SetEnabled(value);
|
|
|
|
|
EnableInClassList("utk-integer-field--disabled", !value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
public UTKIntegerField() : base()
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
|
|
|
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|
|
|
|
if (uss != null)
|
|
|
|
|
{
|
|
|
|
|
styleSheets.Add(uss);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetupStyles();
|
|
|
|
|
SetupEvents();
|
|
|
|
|
SubscribeToThemeChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UTKIntegerField(string label, int defaultValue = 0) : this()
|
|
|
|
|
{
|
|
|
|
|
this.label = label;
|
|
|
|
|
value = defaultValue;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Setup
|
|
|
|
|
private void SetupStyles()
|
|
|
|
|
{
|
|
|
|
|
AddToClassList("utk-integer-field");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupEvents()
|
|
|
|
|
{
|
2026-02-03 20:43:36 +09:00
|
|
|
RegisterCallback<ChangeEvent<int>>(OnFieldValueChanged);
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SubscribeToThemeChanges()
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
|
|
|
RegisterCallback<DetachFromPanelEvent>(_ =>
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnThemeChanged(UTKTheme theme)
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Event Handlers
|
|
|
|
|
private void OnFieldValueChanged(ChangeEvent<int> evt)
|
|
|
|
|
{
|
|
|
|
|
OnValueChanged?.Invoke(evt.newValue);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IDisposable
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_disposed) return;
|
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
|
|
|
OnValueChanged = null;
|
2026-02-03 20:43:36 +09:00
|
|
|
UnregisterCallback<ChangeEvent<int>>(OnFieldValueChanged);
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|