284 lines
8.4 KiB
C#
284 lines
8.4 KiB
C#
#nullable enable
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// Rect(사각형) 입력 필드 컴포넌트.
|
|
/// Unity RectField를 래핑하여 커스텀 스타일을 적용합니다.
|
|
/// 2D 공간에서 사각형의 위치(X, Y)와 크기(Width, Height)를 입력받습니다.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para><b>Rect란?</b></para>
|
|
/// <para>Rect는 2D 공간에서 직사각형 영역을 나타내는 구조체입니다.</para>
|
|
/// <list type="bullet">
|
|
/// <item><description><b>X, Y</b>: 사각형의 왼쪽 상단 모서리 위치</description></item>
|
|
/// <item><description><b>Width</b>: 사각형의 너비</description></item>
|
|
/// <item><description><b>Height</b>: 사각형의 높이</description></item>
|
|
/// </list>
|
|
/// <para>주로 UI 요소의 위치/크기, 스프라이트 영역, 화면 좌표 등에 사용됩니다.</para>
|
|
/// </remarks>
|
|
/// <example>
|
|
/// <para><b>C# 코드에서 사용:</b></para>
|
|
/// <code>
|
|
/// // 기본 Rect 필드 생성
|
|
/// var rectField = new UTKRectField();
|
|
/// rectField.label = "UI 영역";
|
|
/// rectField.Value = new Rect(0, 0, 100, 50);
|
|
///
|
|
/// // 값 변경 이벤트
|
|
/// rectField.OnValueChanged += (rect) => {
|
|
/// Debug.Log($"위치: ({rect.x}, {rect.y}), 크기: {rect.width} x {rect.height}");
|
|
/// };
|
|
///
|
|
/// // 라벨 생성자 사용
|
|
/// var namedField = new UTKRectField("클리핑 영역");
|
|
///
|
|
/// // 축 라벨 커스터마이징
|
|
/// rectField.XLabel = "Left";
|
|
/// rectField.YLabel = "Top";
|
|
/// rectField.WLabel = "Width";
|
|
/// rectField.HLabel = "Height";
|
|
///
|
|
/// // 읽기 전용 (사용자가 수정할 수 없음)
|
|
/// var readOnlyField = new UTKRectField("고정 영역");
|
|
/// readOnlyField.Value = new Rect(10, 10, 200, 100);
|
|
/// readOnlyField.IsReadOnly = true;
|
|
/// </code>
|
|
/// <para><b>UXML에서 사용:</b></para>
|
|
/// <code><![CDATA[
|
|
/// <!-- 기본 Rect 필드 -->
|
|
/// <utk:UTKRectField label="영역" />
|
|
///
|
|
/// <!-- 커스텀 라벨 -->
|
|
/// <utk:UTKRectField label="스프라이트 영역"
|
|
/// x-label="Left" y-label="Top"
|
|
/// w-label="W" h-label="H" />
|
|
///
|
|
/// <!-- 비활성화 상태 -->
|
|
/// <utk:UTKRectField label="비활성화" is-enabled="false" />
|
|
///
|
|
/// <!-- 읽기 전용 -->
|
|
/// <utk:UTKRectField label="고정 영역" is-readonly="true" />
|
|
/// ]]></code>
|
|
/// <para><b>실제 활용 예시 (스프라이트 영역 편집):</b></para>
|
|
/// <code>
|
|
/// // 스프라이트 UV 영역 편집기
|
|
/// var uvField = new UTKRectField("UV 영역");
|
|
/// uvField.Value = sprite.rect;
|
|
/// uvField.OnValueChanged += (newRect) => {
|
|
/// // 스프라이트 영역 업데이트
|
|
/// UpdateSpriteRect(sprite, newRect);
|
|
/// };
|
|
/// </code>
|
|
/// </example>
|
|
[UxmlElement]
|
|
public partial class UTKRectField : RectField, IDisposable
|
|
{
|
|
#region Constants
|
|
private const string USS_PATH = "UIToolkit/Input/UTKRectField";
|
|
#endregion
|
|
|
|
#region Fields
|
|
private bool _disposed;
|
|
private bool _isEnabled = true;
|
|
private bool _isReadOnly;
|
|
private string _xLabel = "X";
|
|
private string _yLabel = "Y";
|
|
private string _wLabel = "W";
|
|
private string _hLabel = "H";
|
|
#endregion
|
|
|
|
#region Events
|
|
/// <summary>값 변경 이벤트</summary>
|
|
public event Action<Rect>? OnValueChanged;
|
|
#endregion
|
|
|
|
#region Properties
|
|
/// <summary>활성화 상태</summary>
|
|
[UxmlAttribute("is-enabled")]
|
|
public bool IsEnabled
|
|
{
|
|
get => _isEnabled;
|
|
set
|
|
{
|
|
_isEnabled = value;
|
|
SetEnabled(value);
|
|
EnableInClassList("utk-rectfield--disabled", !value);
|
|
}
|
|
}
|
|
|
|
/// <summary>현재 값</summary>
|
|
public Rect Value
|
|
{
|
|
get => value;
|
|
set => this.value = value;
|
|
}
|
|
|
|
/// <summary>X 라벨</summary>
|
|
[UxmlAttribute("x-label")]
|
|
public string XLabel
|
|
{
|
|
get => _xLabel;
|
|
set
|
|
{
|
|
_xLabel = value;
|
|
UpdateAxisLabels();
|
|
}
|
|
}
|
|
|
|
/// <summary>Y 라벨</summary>
|
|
[UxmlAttribute("y-label")]
|
|
public string YLabel
|
|
{
|
|
get => _yLabel;
|
|
set
|
|
{
|
|
_yLabel = value;
|
|
UpdateAxisLabels();
|
|
}
|
|
}
|
|
|
|
/// <summary>W 라벨</summary>
|
|
[UxmlAttribute("w-label")]
|
|
public string WLabel
|
|
{
|
|
get => _wLabel;
|
|
set
|
|
{
|
|
_wLabel = value;
|
|
UpdateAxisLabels();
|
|
}
|
|
}
|
|
|
|
/// <summary>H 라벨</summary>
|
|
[UxmlAttribute("h-label")]
|
|
public string HLabel
|
|
{
|
|
get => _hLabel;
|
|
set
|
|
{
|
|
_hLabel = value;
|
|
UpdateAxisLabels();
|
|
}
|
|
}
|
|
|
|
/// <summary>읽기 전용 상태</summary>
|
|
[UxmlAttribute("is-readonly")]
|
|
public bool IsReadOnly
|
|
{
|
|
get => _isReadOnly;
|
|
set
|
|
{
|
|
_isReadOnly = value;
|
|
UpdateReadOnlyState();
|
|
EnableInClassList("utk-rectfield--readonly", value);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public UTKRectField() : base()
|
|
{
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|
if (uss != null)
|
|
{
|
|
styleSheets.Add(uss);
|
|
}
|
|
|
|
SetupStyles();
|
|
SetupEvents();
|
|
SubscribeToThemeChanges();
|
|
}
|
|
|
|
public UTKRectField(bool isReadOnly) : this()
|
|
{
|
|
_isReadOnly = isReadOnly;
|
|
UpdateReadOnlyState();
|
|
}
|
|
|
|
public UTKRectField(string label, bool isReadOnly = false) : this()
|
|
{
|
|
this.label = label;
|
|
_isReadOnly = isReadOnly;
|
|
UpdateReadOnlyState();
|
|
}
|
|
#endregion
|
|
|
|
#region Setup
|
|
private void SetupStyles()
|
|
{
|
|
AddToClassList("utk-rectfield");
|
|
|
|
// 초기 라벨 설정
|
|
schedule.Execute(() => UpdateAxisLabels());
|
|
}
|
|
|
|
private void SetupEvents()
|
|
{
|
|
RegisterCallback<ChangeEvent<Rect>>(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()
|
|
{
|
|
// RectField의 내부 FloatField들을 찾아서 라벨 변경 (X, Y, W, H 순서)
|
|
var floatFields = this.Query<FloatField>().ToList();
|
|
if (floatFields.Count >= 4)
|
|
{
|
|
floatFields[0].label = _xLabel;
|
|
floatFields[1].label = _yLabel;
|
|
floatFields[2].label = _wLabel;
|
|
floatFields[3].label = _hLabel;
|
|
}
|
|
}
|
|
|
|
private void UpdateReadOnlyState()
|
|
{
|
|
// 내부 FloatField들의 TextInput을 찾아서 읽기 전용 설정
|
|
var textInputs = this.Query<TextInputBaseField<float>>().ToList();
|
|
foreach (var textInput in textInputs)
|
|
{
|
|
textInput.isReadOnly = _isReadOnly;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Event Handlers
|
|
private void OnFieldValueChanged(ChangeEvent<Rect> evt)
|
|
{
|
|
OnValueChanged?.Invoke(evt.newValue);
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
OnValueChanged = null;
|
|
UnregisterCallback<ChangeEvent<Rect>>(OnFieldValueChanged);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|