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
|
|
|
/// Vector4(4D 벡터) 입력 필드 컴포넌트.
|
2026-01-08 20:15:57 +09:00
|
|
|
/// Unity Vector4Field를 래핑하여 커스텀 스타일을 적용합니다.
|
2026-01-21 20:43:54 +09:00
|
|
|
/// X, Y, Z, W 네 개의 float 값을 입력받습니다.
|
2026-01-08 20:15:57 +09:00
|
|
|
/// </summary>
|
2026-01-21 20:43:54 +09:00
|
|
|
/// <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;
|
2026-02-03 20:43:36 +09:00
|
|
|
///
|
|
|
|
|
/// // 읽기 전용 (사용자가 수정할 수 없음)
|
|
|
|
|
/// var readOnlyField = new UTKVector4Field("고정 파라미터");
|
|
|
|
|
/// readOnlyField.Value = new Vector4(1, 0.5f, 0.5f, 1);
|
|
|
|
|
/// readOnlyField.IsReadOnly = true;
|
2026-01-21 20:43:54 +09:00
|
|
|
/// </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" />
|
|
|
|
|
///
|
|
|
|
|
/// <!-- 비활성화 -->
|
2026-02-03 20:43:36 +09:00
|
|
|
/// <utk:UTKVector4Field label="비활성화" is-enabled="false" />
|
|
|
|
|
///
|
|
|
|
|
/// <!-- 읽기 전용 -->
|
|
|
|
|
/// <utk:UTKVector4Field label="고정값" is-readonly="true" />
|
2026-01-21 20:43:54 +09:00
|
|
|
/// </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>
|
2026-01-08 20:15:57 +09:00
|
|
|
[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;
|
2026-02-03 20:43:36 +09:00
|
|
|
private bool _isReadOnly = false;
|
2026-01-09 18:42:17 +09:00
|
|
|
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>
|
2026-01-23 19:04:12 +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-vector4field--disabled", !value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>현재 값</summary>
|
|
|
|
|
public Vector4 Value
|
|
|
|
|
{
|
|
|
|
|
get => value;
|
|
|
|
|
set => this.value = value;
|
|
|
|
|
}
|
2026-01-09 18:42:17 +09:00
|
|
|
|
|
|
|
|
/// <summary>X축 라벨</summary>
|
2026-01-23 19:04:12 +09:00
|
|
|
[UxmlAttribute("x-label")]
|
2026-01-09 18:42:17 +09:00
|
|
|
public string XLabel
|
|
|
|
|
{
|
|
|
|
|
get => _xLabel;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_xLabel = value;
|
|
|
|
|
UpdateAxisLabels();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Y축 라벨</summary>
|
2026-01-23 19:04:12 +09:00
|
|
|
[UxmlAttribute("y-label")]
|
2026-01-09 18:42:17 +09:00
|
|
|
public string YLabel
|
|
|
|
|
{
|
|
|
|
|
get => _yLabel;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_yLabel = value;
|
|
|
|
|
UpdateAxisLabels();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Z축 라벨</summary>
|
2026-01-23 19:04:12 +09:00
|
|
|
[UxmlAttribute("z-label")]
|
2026-01-09 18:42:17 +09:00
|
|
|
public string ZLabel
|
|
|
|
|
{
|
|
|
|
|
get => _zLabel;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_zLabel = value;
|
|
|
|
|
UpdateAxisLabels();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>W축 라벨</summary>
|
2026-01-23 19:04:12 +09:00
|
|
|
[UxmlAttribute("w-label")]
|
2026-01-09 18:42:17 +09:00
|
|
|
public string WLabel
|
|
|
|
|
{
|
|
|
|
|
get => _wLabel;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_wLabel = value;
|
|
|
|
|
UpdateAxisLabels();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-03 20:43:36 +09:00
|
|
|
|
|
|
|
|
/// <summary>읽기 전용 상태</summary>
|
|
|
|
|
[UxmlAttribute("is-readonly")]
|
|
|
|
|
public bool IsReadOnly
|
|
|
|
|
{
|
|
|
|
|
get => _isReadOnly;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isReadOnly = value;
|
|
|
|
|
UpdateReadOnlyState();
|
|
|
|
|
EnableInClassList("utk-vector4field--readonly", value);
|
|
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 20:43:36 +09:00
|
|
|
public UTKVector4Field(bool isReadOnly) : this()
|
|
|
|
|
{
|
|
|
|
|
_isReadOnly = isReadOnly;
|
|
|
|
|
UpdateReadOnlyState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UTKVector4Field(string label, bool isReadOnly = false) : this()
|
2026-01-08 20:15:57 +09:00
|
|
|
{
|
|
|
|
|
this.label = label;
|
2026-02-03 20:43:36 +09:00
|
|
|
_isReadOnly = isReadOnly;
|
|
|
|
|
UpdateReadOnlyState();
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Setup
|
|
|
|
|
private void SetupStyles()
|
|
|
|
|
{
|
|
|
|
|
AddToClassList("utk-vector4field");
|
2026-01-09 18:42:17 +09:00
|
|
|
|
|
|
|
|
// 초기 라벨 설정
|
|
|
|
|
schedule.Execute(() => UpdateAxisLabels());
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupEvents()
|
|
|
|
|
{
|
2026-02-03 20:43:36 +09:00
|
|
|
RegisterCallback<ChangeEvent<Vector4>>(OnFieldValueChanged);
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SubscribeToThemeChanges()
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
2026-02-10 20:48:49 +09:00
|
|
|
RegisterCallback<AttachToPanelEvent>(OnAttachToPanelForTheme);
|
|
|
|
|
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanelForTheme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnAttachToPanelForTheme(AttachToPanelEvent evt)
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDetachFromPanelForTheme(DetachFromPanelEvent evt)
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnThemeChanged(UTKTheme theme)
|
|
|
|
|
{
|
|
|
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
|
}
|
2026-01-09 18:42:17 +09:00
|
|
|
|
|
|
|
|
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-02-03 20:43:36 +09:00
|
|
|
|
|
|
|
|
private void UpdateReadOnlyState()
|
|
|
|
|
{
|
|
|
|
|
// 내부 FloatField들의 TextInput을 찾아서 읽기 전용 설정
|
|
|
|
|
var textInputs = this.Query<TextInputBaseField<float>>().ToList();
|
|
|
|
|
foreach (var textInput in textInputs)
|
|
|
|
|
{
|
|
|
|
|
textInput.isReadOnly = _isReadOnly;
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
2026-02-10 20:48:49 +09:00
|
|
|
UnregisterCallback<AttachToPanelEvent>(OnAttachToPanelForTheme);
|
|
|
|
|
UnregisterCallback<DetachFromPanelEvent>(OnDetachFromPanelForTheme);
|
2026-01-08 20:15:57 +09:00
|
|
|
OnValueChanged = null;
|
2026-02-03 20:43:36 +09:00
|
|
|
UnregisterCallback<ChangeEvent<Vector4>>(OnFieldValueChanged);
|
2026-01-08 20:15:57 +09:00
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|