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

278 lines
8.1 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>
2026-01-21 20:43:54 +09:00
/// Bounds(경계 상자) 입력 필드 컴포넌트.
2026-01-08 20:15:57 +09:00
/// Unity BoundsField를 래핑하여 커스텀 스타일을 적용합니다.
2026-01-21 20:43:54 +09:00
/// 3D 공간에서 객체의 경계를 정의하는 Center(중심점)와 Extents(크기)를 입력받습니다.
2026-01-08 20:15:57 +09:00
/// </summary>
2026-01-21 20:43:54 +09:00
/// <remarks>
/// <para><b>Bounds란?</b></para>
/// <para>Bounds는 3D 공간에서 축 정렬 경계 상자(AABB)를 나타냅니다.</para>
/// <para>- Center: 경계 상자의 중심점 (Vector3)</para>
/// <para>- Extents: 중심에서 각 축 방향으로의 거리 (Vector3), Size의 절반 값</para>
/// <para>- Size: 경계 상자의 전체 크기 (Extents * 2)</para>
/// </remarks>
/// <example>
/// <para><b>C# 코드에서 사용:</b></para>
/// <code>
/// // 기본 Bounds 필드 생성
/// var boundsField = new UTKBoundsField();
/// boundsField.label = "충돌 영역";
///
/// // 초기값 설정 (중심점: 0,1,0 / 크기: 2,2,2)
/// boundsField.Value = new Bounds(
/// new Vector3(0, 1, 0), // center
/// new Vector3(2, 2, 2) // size (extents의 2배)
/// );
///
/// // 값 변경 이벤트 처리
/// boundsField.OnValueChanged += (bounds) => {
/// Debug.Log($"중심점: {bounds.center}");
/// Debug.Log($"크기: {bounds.size}");
/// Debug.Log($"최소점: {bounds.min}, 최대점: {bounds.max}");
/// };
///
/// // 라벨 커스터마이징
/// boundsField.CenterLabel = "중심";
/// boundsField.ExtentsLabel = "범위";
/// boundsField.XLabel = "가로";
/// boundsField.YLabel = "높이";
/// boundsField.ZLabel = "깊이";
///
/// // 비활성화
/// boundsField.IsEnabled = false;
/// </code>
/// <para><b>UXML에서 사용:</b></para>
/// <code>
/// <!-- 네임스페이스 선언 -->
/// <UXML xmlns:utk="UVC.UIToolkit">
/// <!-- 기본 Bounds 필드 -->
/// <utk:UTKBoundsField label="경계 영역" />
///
/// <!-- 커스텀 라벨 -->
/// <utk:UTKBoundsField
/// label="충돌 박스"
/// center-label="위치"
/// extents-label="크기" />
///
/// <!-- 비활성화 -->
/// <utk:UTKBoundsField label="읽기전용" is-enabled="false" />
/// </UXML>
/// </code>
/// <para><b>실제 활용 예시:</b></para>
/// <code>
/// // 게임 오브젝트의 콜라이더 경계 설정
/// var colliderBounds = new UTKBoundsField("콜라이더 경계");
/// colliderBounds.OnValueChanged += (bounds) => {
/// var boxCollider = targetObject.GetComponent<BoxCollider>();
/// if (boxCollider != null)
/// {
/// boxCollider.center = bounds.center;
/// boxCollider.size = bounds.size;
/// }
/// };
/// </code>
/// </example>
2026-01-08 20:15:57 +09:00
[UxmlElement]
public partial class UTKBoundsField : BoundsField, IDisposable
{
#region Constants
private const string USS_PATH = "UIToolkit/Input/UTKBoundsField";
#endregion
#region Fields
private bool _disposed;
private bool _isEnabled = true;
private string _centerLabel = "Center";
private string _extentsLabel = "Extents";
private string _xLabel = "X";
private string _yLabel = "Y";
private string _zLabel = "Z";
2026-01-08 20:15:57 +09:00
#endregion
#region Events
/// <summary>값 변경 이벤트</summary>
public event Action<Bounds>? OnValueChanged;
#endregion
#region Properties
/// <summary>활성화 상태</summary>
[UxmlAttribute]
public bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
SetEnabled(value);
EnableInClassList("utk-boundsfield--disabled", !value);
}
}
/// <summary>현재 값</summary>
public Bounds Value
{
get => value;
set => this.value = value;
}
/// <summary>Center 라벨</summary>
[UxmlAttribute]
public string CenterLabel
{
get => _centerLabel;
set
{
_centerLabel = value;
UpdateAxisLabels();
}
}
/// <summary>Extents 라벨</summary>
[UxmlAttribute]
public string ExtentsLabel
{
get => _extentsLabel;
set
{
_extentsLabel = value;
UpdateAxisLabels();
}
}
/// <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();
}
}
2026-01-08 20:15:57 +09:00
#endregion
#region Constructor
public UTKBoundsField() : base()
{
UTKThemeManager.Instance.ApplyThemeToElement(this);
var uss = Resources.Load<StyleSheet>(USS_PATH);
if (uss != null)
{
styleSheets.Add(uss);
}
SetupStyles();
SetupEvents();
SubscribeToThemeChanges();
}
public UTKBoundsField(string label) : this()
{
this.label = label;
}
#endregion
#region Setup
private void SetupStyles()
{
AddToClassList("utk-boundsfield");
// 초기 라벨 설정
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()
{
// BoundsField의 내부 Vector3Field들을 찾아서 라벨 변경 (Center, Extents 순서)
var vector3Fields = this.Query<Vector3Field>().ToList();
if (vector3Fields.Count >= 2)
{
vector3Fields[0].label = _centerLabel;
vector3Fields[1].label = _extentsLabel;
// 각 Vector3Field의 내부 FloatField들을 찾아서 X, Y, Z 라벨 변경
foreach (var vector3Field in vector3Fields)
{
var floatFields = vector3Field.Query<FloatField>().ToList();
if (floatFields.Count >= 3)
{
floatFields[0].label = _xLabel;
floatFields[1].label = _yLabel;
floatFields[2].label = _zLabel;
}
}
}
}
2026-01-08 20:15:57 +09:00
#endregion
#region Event Handlers
private void OnFieldValueChanged(ChangeEvent<Bounds> evt)
{
OnValueChanged?.Invoke(evt.newValue);
}
#endregion
#region IDisposable
public void Dispose()
{
if (_disposed) return;
_disposed = true;
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
OnValueChanged = null;
}
#endregion
}
}