Files
XRLib/Assets/Scripts/UVC/UIToolkit/Input/UTKVector4Field.cs
2026-01-21 20:43:54 +09:00

249 lines
7.1 KiB
C#

#nullable enable
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace UVC.UIToolkit
{
/// <summary>
/// Vector4(4D 벡터) 입력 필드 컴포넌트.
/// Unity Vector4Field를 래핑하여 커스텀 스타일을 적용합니다.
/// X, Y, Z, W 네 개의 float 값을 입력받습니다.
/// </summary>
/// <remarks>
/// <para><b>Vector4란?</b></para>
/// <para>Vector4는 4개의 float 값을 저장하는 구조체입니다.</para>
/// <para>주로 다음 용도로 사용됩니다:</para>
/// <para>- 동차 좌표(Homogeneous coordinates): 3D 변환에서 이동을 포함한 행렬 연산</para>
/// <para>- 색상(RGBA): Red, Green, Blue, Alpha 값 (0~1 범위)</para>
/// <para>- 쉐이더 파라미터: 커스텀 쉐이더에 전달하는 4개의 값</para>
/// <para>- 사원수(Quaternion)와 유사한 구조로 회전 데이터 저장</para>
/// </remarks>
/// <example>
/// <para><b>C# 코드에서 사용:</b></para>
/// <code>
/// // 기본 Vector4 필드 생성
/// var vec4Field = new UTKVector4Field();
/// vec4Field.label = "쉐이더 파라미터";
///
/// // 초기값 설정
/// vec4Field.Value = new Vector4(1, 0.5f, 0.5f, 1);
///
/// // 값 변경 이벤트 처리
/// vec4Field.OnValueChanged += (vec) => {
/// Debug.Log($"파라미터: X={vec.x}, Y={vec.y}, Z={vec.z}, W={vec.w}");
/// };
///
/// // 라벨 커스터마이징 (RGBA 색상용)
/// vec4Field.XLabel = "R";
/// vec4Field.YLabel = "G";
/// vec4Field.ZLabel = "B";
/// vec4Field.WLabel = "A";
///
/// // 비활성화
/// vec4Field.IsEnabled = false;
/// </code>
/// <para><b>UXML에서 사용:</b></para>
/// <code>
/// <!-- 네임스페이스 선언 -->
/// <UXML xmlns:utk="UVC.UIToolkit">
/// <!-- 기본 Vector4 필드 -->
/// <utk:UTKVector4Field label="값" />
///
/// <!-- RGBA 색상용 커스텀 라벨 -->
/// <utk:UTKVector4Field label="색상"
/// x-label="R" y-label="G" z-label="B" w-label="A" />
///
/// <!-- 비활성화 -->
/// <utk:UTKVector4Field label="고정값" is-enabled="false" />
/// </UXML>
/// </code>
/// <para><b>실제 활용 예시:</b></para>
/// <code>
/// // 머티리얼 쉐이더 파라미터 설정
/// var shaderParam = new UTKVector4Field("_MainTex_ST");
/// shaderParam.XLabel = "Tile X";
/// shaderParam.YLabel = "Tile Y";
/// shaderParam.ZLabel = "Offset X";
/// shaderParam.WLabel = "Offset Y";
/// shaderParam.Value = new Vector4(1, 1, 0, 0); // 기본 타일링
/// shaderParam.OnValueChanged += (vec) => {
/// material.SetVector("_MainTex_ST", vec);
/// };
/// </code>
/// </example>
[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";
#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();
}
}
#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());
}
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;
}
}
#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
}
}