2026-01-08 20:15:57 +09:00
|
|
|
#nullable enable
|
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
namespace UVC.UIToolkit
|
2026-01-08 20:15:57 +09:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 숫자 입력 필드에 위/아래 스테퍼 버튼이 붙은 컴포넌트
|
|
|
|
|
/// TextInput 오른쪽에 위, 아래 버튼이 세로로 배치됨
|
|
|
|
|
/// 마우스 호버 상태에서 휠로 값 조절 가능
|
|
|
|
|
/// </summary>
|
|
|
|
|
[UxmlElement]
|
|
|
|
|
public partial class UTKNumberStepper : VisualElement
|
|
|
|
|
{
|
|
|
|
|
#region Constants
|
|
|
|
|
private const string USS_PATH = "UIToolkit/Input/UTKNumberStepper";
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region UXML Attributes
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("value")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public int Value
|
|
|
|
|
{
|
|
|
|
|
get => _value;
|
|
|
|
|
set => SetValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("min-value")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public int MinValue
|
|
|
|
|
{
|
|
|
|
|
get => _minValue;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_minValue = value;
|
|
|
|
|
ClampValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("max-value")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public int MaxValue
|
|
|
|
|
{
|
|
|
|
|
get => _maxValue;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_maxValue = value;
|
|
|
|
|
ClampValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("step")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public int Step
|
|
|
|
|
{
|
|
|
|
|
get => _step;
|
|
|
|
|
set => _step = Math.Max(1, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
[UxmlAttribute("wrap-around")]
|
2026-01-08 20:15:57 +09:00
|
|
|
public bool WrapAround
|
|
|
|
|
{
|
|
|
|
|
get => _wrapAround;
|
|
|
|
|
set => _wrapAround = value;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Fields
|
|
|
|
|
private int _value;
|
|
|
|
|
private int _minValue = int.MinValue;
|
|
|
|
|
private int _maxValue = int.MaxValue;
|
|
|
|
|
private int _step = 1;
|
|
|
|
|
private bool _wrapAround;
|
|
|
|
|
private bool _isUpdating;
|
|
|
|
|
private bool _isHovered;
|
|
|
|
|
|
|
|
|
|
private TextField? _textField;
|
|
|
|
|
private Button? _upButton;
|
|
|
|
|
private Button? _downButton;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
/// <summary>값이 변경될 때 발생</summary>
|
|
|
|
|
public event Action<int>? OnValueChanged;
|
|
|
|
|
|
|
|
|
|
/// <summary>Tab 키가 눌렸을 때 발생 (다음 요소로 포커스 이동용)</summary>
|
|
|
|
|
public event Action? OnTabPressed;
|
|
|
|
|
|
|
|
|
|
/// <summary>Shift+Tab 키가 눌렸을 때 발생 (이전 요소로 포커스 이동용)</summary>
|
|
|
|
|
public event Action? OnShiftTabPressed;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
public UTKNumberStepper()
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
LoadStyleSheet();
|
|
|
|
|
CreateUI();
|
|
|
|
|
SetupEvents();
|
|
|
|
|
SubscribeToThemeChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UTKNumberStepper(int minValue, int maxValue, int initialValue = 0, int step = 1)
|
|
|
|
|
{
|
|
|
|
|
_minValue = minValue;
|
|
|
|
|
_maxValue = maxValue;
|
|
|
|
|
_step = Math.Max(1, step);
|
|
|
|
|
_value = Mathf.Clamp(initialValue, minValue, maxValue);
|
|
|
|
|
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
LoadStyleSheet();
|
|
|
|
|
CreateUI();
|
|
|
|
|
SetupEvents();
|
|
|
|
|
SubscribeToThemeChanges();
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadStyleSheet()
|
|
|
|
|
{
|
|
|
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|
|
|
|
if (uss != null)
|
|
|
|
|
{
|
|
|
|
|
styleSheets.Add(uss);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SubscribeToThemeChanges()
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
|
|
|
|
|
|
|
|
// 패널에서 분리될 때 이벤트 구독 해제
|
|
|
|
|
RegisterCallback<DetachFromPanelEvent>(_ =>
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnThemeChanged(UTKTheme theme)
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
public void SetValue(int newValue, bool notify = true)
|
|
|
|
|
{
|
|
|
|
|
int clampedValue = ClampValueInternal(newValue);
|
|
|
|
|
if (_value != clampedValue)
|
|
|
|
|
{
|
|
|
|
|
_value = clampedValue;
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
if (notify)
|
|
|
|
|
{
|
|
|
|
|
OnValueChanged?.Invoke(_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Increment()
|
|
|
|
|
{
|
|
|
|
|
if (_wrapAround && _value + _step > _maxValue)
|
|
|
|
|
{
|
|
|
|
|
SetValue(_minValue);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetValue(_value + _step);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Decrement()
|
|
|
|
|
{
|
|
|
|
|
if (_wrapAround && _value - _step < _minValue)
|
|
|
|
|
{
|
|
|
|
|
SetValue(_maxValue);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetValue(_value - _step);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetRange(int min, int max)
|
|
|
|
|
{
|
|
|
|
|
_minValue = min;
|
|
|
|
|
_maxValue = max;
|
|
|
|
|
ClampValue();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:18:47 +09:00
|
|
|
/// <summary>컴포넌트의 활성화 상태를 설정합니다.</summary>
|
|
|
|
|
public new void SetEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
base.SetEnabled(enabled);
|
|
|
|
|
EnableInClassList("utk-number-stepper--disabled", !enabled);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 20:15:57 +09:00
|
|
|
/// <summary>텍스트 필드에 포커스를 설정합니다.</summary>
|
|
|
|
|
public new void Focus()
|
|
|
|
|
{
|
|
|
|
|
_textField?.Focus();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Methods - UI Creation
|
|
|
|
|
private void CreateUI()
|
|
|
|
|
{
|
|
|
|
|
AddToClassList("utk-number-stepper");
|
|
|
|
|
|
|
|
|
|
// Text Field
|
|
|
|
|
_textField = new TextField { name = "stepper-input" };
|
|
|
|
|
_textField.AddToClassList("utk-number-stepper__input");
|
|
|
|
|
|
|
|
|
|
// TextField 내부 input 스타일링
|
|
|
|
|
_textField.RegisterCallback<AttachToPanelEvent>(_ =>
|
|
|
|
|
{
|
|
|
|
|
var input = _textField.Q<VisualElement>("unity-text-input");
|
|
|
|
|
if (input != null)
|
|
|
|
|
{
|
|
|
|
|
input.AddToClassList("utk-number-stepper__text-input");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Add(_textField);
|
|
|
|
|
|
|
|
|
|
// Button Container (위/아래 버튼을 세로로 배치)
|
|
|
|
|
var buttonContainer = new VisualElement { name = "stepper-buttons" };
|
|
|
|
|
buttonContainer.AddToClassList("utk-number-stepper__buttons");
|
|
|
|
|
|
|
|
|
|
// Up Button
|
2026-01-13 20:39:45 +09:00
|
|
|
_upButton = new Button { name = "stepper-up", text = UTKMaterialIcons.KeyboardArrowUp };
|
2026-01-08 20:15:57 +09:00
|
|
|
_upButton.AddToClassList("utk-number-stepper__btn");
|
|
|
|
|
_upButton.AddToClassList("utk-number-stepper__btn--up");
|
2026-01-13 20:39:45 +09:00
|
|
|
UTKMaterialIcons.ApplyIconStyle(_upButton, 14);
|
2026-01-08 20:15:57 +09:00
|
|
|
buttonContainer.Add(_upButton);
|
|
|
|
|
|
|
|
|
|
// Down Button
|
2026-01-13 20:39:45 +09:00
|
|
|
_downButton = new Button { name = "stepper-down", text = UTKMaterialIcons.KeyboardArrowDown };
|
2026-01-08 20:15:57 +09:00
|
|
|
_downButton.AddToClassList("utk-number-stepper__btn");
|
|
|
|
|
_downButton.AddToClassList("utk-number-stepper__btn--down");
|
2026-01-13 20:39:45 +09:00
|
|
|
UTKMaterialIcons.ApplyIconStyle(_downButton, 14);
|
2026-01-08 20:15:57 +09:00
|
|
|
buttonContainer.Add(_downButton);
|
|
|
|
|
|
|
|
|
|
Add(buttonContainer);
|
|
|
|
|
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupEvents()
|
|
|
|
|
{
|
|
|
|
|
_upButton?.RegisterCallback<ClickEvent>(_ => Increment());
|
|
|
|
|
_downButton?.RegisterCallback<ClickEvent>(_ => Decrement());
|
|
|
|
|
|
|
|
|
|
_textField?.RegisterValueChangedCallback(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (_isUpdating) return;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(evt.newValue, out int parsed))
|
|
|
|
|
{
|
|
|
|
|
SetValue(parsed);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 유효하지 않은 입력이면 이전 값으로 복원
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 키보드 이벤트 (위/아래 화살표)
|
|
|
|
|
_textField?.RegisterCallback<KeyDownEvent>(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (evt.keyCode == KeyCode.UpArrow)
|
|
|
|
|
{
|
|
|
|
|
Increment();
|
|
|
|
|
evt.StopPropagation();
|
|
|
|
|
}
|
|
|
|
|
else if (evt.keyCode == KeyCode.DownArrow)
|
|
|
|
|
{
|
|
|
|
|
Decrement();
|
|
|
|
|
evt.StopPropagation();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Tab 키 이벤트 - TrickleDown으로 먼저 캡처
|
|
|
|
|
_textField?.RegisterCallback<KeyDownEvent>(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (evt.keyCode == KeyCode.Tab)
|
|
|
|
|
{
|
|
|
|
|
if (evt.shiftKey && OnShiftTabPressed != null)
|
|
|
|
|
{
|
|
|
|
|
// Shift+Tab: 이전 요소로 이동
|
|
|
|
|
OnShiftTabPressed.Invoke();
|
|
|
|
|
evt.StopImmediatePropagation();
|
|
|
|
|
}
|
|
|
|
|
else if (!evt.shiftKey && OnTabPressed != null)
|
|
|
|
|
{
|
|
|
|
|
// Tab: 다음 요소로 이동
|
|
|
|
|
OnTabPressed.Invoke();
|
|
|
|
|
evt.StopImmediatePropagation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, TrickleDown.TrickleDown);
|
|
|
|
|
|
|
|
|
|
// 마우스 호버 상태 추적
|
|
|
|
|
RegisterCallback<MouseEnterEvent>(_ => _isHovered = true);
|
|
|
|
|
RegisterCallback<MouseLeaveEvent>(_ => _isHovered = false);
|
|
|
|
|
|
|
|
|
|
// 마우스 휠 이벤트 (호버 상태에서만)
|
|
|
|
|
RegisterCallback<WheelEvent>(evt =>
|
|
|
|
|
{
|
|
|
|
|
if (!_isHovered) return;
|
|
|
|
|
|
|
|
|
|
if (evt.delta.y < 0)
|
|
|
|
|
{
|
|
|
|
|
Increment();
|
|
|
|
|
}
|
|
|
|
|
else if (evt.delta.y > 0)
|
|
|
|
|
{
|
|
|
|
|
Decrement();
|
|
|
|
|
}
|
|
|
|
|
evt.StopPropagation();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Methods - Logic
|
|
|
|
|
private void UpdateDisplay()
|
|
|
|
|
{
|
|
|
|
|
if (_textField == null) return;
|
|
|
|
|
|
|
|
|
|
_isUpdating = true;
|
|
|
|
|
_textField.value = _value.ToString();
|
|
|
|
|
_isUpdating = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClampValue()
|
|
|
|
|
{
|
|
|
|
|
SetValue(_value, notify: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int ClampValueInternal(int value)
|
|
|
|
|
{
|
|
|
|
|
return Mathf.Clamp(value, _minValue, _maxValue);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|