Files
XRLib/Assets/Scripts/UVC/UIToolkit/Input/UTKVector4Field.cs

186 lines
4.6 KiB
C#
Raw Normal View History

2026-01-08 20:15:57 +09:00
#nullable enable
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UVC.UIToolkit
{
/// <summary>
/// 4D 벡터 입력 필드 컴포넌트.
/// Unity Vector4Field를 래핑하여 커스텀 스타일을 적용합니다.
/// </summary>
[UxmlElement]
public partial class UTKVector4Field : Vector4Field, IDisposable
{
#region Constants
private const string USS_PATH = "UIToolkit/Input/UTKVector4Field";
#endregion
#region Fields
private bool _disposed;
private bool _isEnabled = true;
private string _xLabel = "X";
private string _yLabel = "Y";
private string _zLabel = "Z";
private string _wLabel = "W";
2026-01-08 20:15:57 +09:00
#endregion
#region Events
/// <summary>값 변경 이벤트</summary>
public event Action<Vector4>? OnValueChanged;
#endregion
#region Properties
/// <summary>활성화 상태</summary>
[UxmlAttribute]
public bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
SetEnabled(value);
EnableInClassList("utk-vector4field--disabled", !value);
}
}
/// <summary>현재 값</summary>
public Vector4 Value
{
get => value;
set => this.value = value;
}
/// <summary>X축 라벨</summary>
[UxmlAttribute]
public string XLabel
{
get => _xLabel;
set
{
_xLabel = value;
UpdateAxisLabels();
}
}
/// <summary>Y축 라벨</summary>
[UxmlAttribute]
public string YLabel
{
get => _yLabel;
set
{
_yLabel = value;
UpdateAxisLabels();
}
}
/// <summary>Z축 라벨</summary>
[UxmlAttribute]
public string ZLabel
{
get => _zLabel;
set
{
_zLabel = value;
UpdateAxisLabels();
}
}
/// <summary>W축 라벨</summary>
[UxmlAttribute]
public string WLabel
{
get => _wLabel;
set
{
_wLabel = value;
UpdateAxisLabels();
}
}
2026-01-08 20:15:57 +09:00
#endregion
#region Constructor
public UTKVector4Field() : base()
{
UTKThemeManager.Instance.ApplyThemeToElement(this);
var uss = Resources.Load<StyleSheet>(USS_PATH);
if (uss != null)
{
styleSheets.Add(uss);
}
SetupStyles();
SetupEvents();
SubscribeToThemeChanges();
}
public UTKVector4Field(string label) : this()
{
this.label = label;
}
#endregion
#region Setup
private void SetupStyles()
{
AddToClassList("utk-vector4field");
// 초기 라벨 설정
schedule.Execute(() => UpdateAxisLabels());
2026-01-08 20:15:57 +09:00
}
private void SetupEvents()
{
this.RegisterValueChangedCallback(OnFieldValueChanged);
}
private void SubscribeToThemeChanges()
{
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
RegisterCallback<DetachFromPanelEvent>(_ =>
{
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
});
}
private void OnThemeChanged(UTKTheme theme)
{
UTKThemeManager.Instance.ApplyThemeToElement(this);
}
private void UpdateAxisLabels()
{
// Vector4Field의 내부 FloatField들을 찾아서 라벨 변경
var floatFields = this.Query<FloatField>().ToList();
if (floatFields.Count >= 4)
{
floatFields[0].label = _xLabel;
floatFields[1].label = _yLabel;
floatFields[2].label = _zLabel;
floatFields[3].label = _wLabel;
}
}
2026-01-08 20:15:57 +09:00
#endregion
#region Event Handlers
private void OnFieldValueChanged(ChangeEvent<Vector4> evt)
{
OnValueChanged?.Invoke(evt.newValue);
}
#endregion
#region IDisposable
public void Dispose()
{
if (_disposed) return;
_disposed = true;
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
OnValueChanged = null;
}
#endregion
}
}