143 lines
4.2 KiB
C#
143 lines
4.2 KiB
C#
#nullable enable
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UVC.UIToolkit.Property
|
|
{
|
|
/// <summary>
|
|
/// 실수 범위 속성 아이템
|
|
/// Min, Max 두 개의 FloatField
|
|
/// </summary>
|
|
public class UTKFloatRangePropertyItem : UTKPropertyItemBase<UTKFloatRange>
|
|
{
|
|
#region Fields
|
|
private FloatField? _minField;
|
|
private FloatField? _maxField;
|
|
#endregion
|
|
|
|
#region Properties
|
|
public override UTKPropertyType PropertyType => UTKPropertyType.FloatRange;
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public UTKFloatRangePropertyItem(string id, string name, UTKFloatRange initialValue = default)
|
|
: base(id, name, initialValue)
|
|
{
|
|
}
|
|
|
|
public UTKFloatRangePropertyItem(string id, string name, float min, float max)
|
|
: base(id, name, new UTKFloatRange(min, max))
|
|
{
|
|
}
|
|
#endregion
|
|
|
|
#region Override Methods
|
|
public override VisualElement CreateUI()
|
|
{
|
|
var container = CreateContainer();
|
|
|
|
var label = CreateNameLabel();
|
|
container.Add(label);
|
|
|
|
var valueContainer = new VisualElement();
|
|
valueContainer.AddToClassList("utk-property-item__value");
|
|
valueContainer.style.flexDirection = FlexDirection.Row;
|
|
|
|
_minField = new FloatField();
|
|
_minField.name = "min-field";
|
|
_minField.value = Value.Min;
|
|
_minField.style.flexGrow = 1;
|
|
valueContainer.Add(_minField);
|
|
|
|
var separator = new Label("~");
|
|
separator.AddToClassList("utk-property-item__range-separator");
|
|
valueContainer.Add(separator);
|
|
|
|
_maxField = new FloatField();
|
|
_maxField.name = "max-field";
|
|
_maxField.value = Value.Max;
|
|
_maxField.style.flexGrow = 1;
|
|
valueContainer.Add(_maxField);
|
|
|
|
container.Add(valueContainer);
|
|
|
|
return container;
|
|
}
|
|
|
|
public override void BindUI(VisualElement element)
|
|
{
|
|
base.BindUI(element);
|
|
|
|
_minField = element.Q<FloatField>("min-field");
|
|
_maxField = element.Q<FloatField>("max-field");
|
|
|
|
if (_minField != null)
|
|
{
|
|
_minField.value = Value.Min;
|
|
_minField.SetEnabled(!IsReadOnly);
|
|
_minField.RegisterValueChangedCallback(OnMinChanged);
|
|
}
|
|
|
|
if (_maxField != null)
|
|
{
|
|
_maxField.value = Value.Max;
|
|
_maxField.SetEnabled(!IsReadOnly);
|
|
_maxField.RegisterValueChangedCallback(OnMaxChanged);
|
|
}
|
|
}
|
|
|
|
public override void UnbindUI(VisualElement element)
|
|
{
|
|
if (_minField != null)
|
|
{
|
|
_minField.UnregisterValueChangedCallback(OnMinChanged);
|
|
_minField = null;
|
|
}
|
|
|
|
if (_maxField != null)
|
|
{
|
|
_maxField.UnregisterValueChangedCallback(OnMaxChanged);
|
|
_maxField = null;
|
|
}
|
|
|
|
base.UnbindUI(element);
|
|
}
|
|
|
|
public override void RefreshUI()
|
|
{
|
|
if (_minField != null && !Mathf.Approximately(_minField.value, Value.Min))
|
|
{
|
|
_minField.SetValueWithoutNotify(Value.Min);
|
|
}
|
|
|
|
if (_maxField != null && !Mathf.Approximately(_maxField.value, Value.Max))
|
|
{
|
|
_maxField.SetValueWithoutNotify(Value.Max);
|
|
}
|
|
}
|
|
|
|
protected override void UpdateReadOnlyState()
|
|
{
|
|
base.UpdateReadOnlyState();
|
|
|
|
_minField?.SetEnabled(!IsReadOnly);
|
|
_maxField?.SetEnabled(!IsReadOnly);
|
|
}
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
private void OnMinChanged(ChangeEvent<float> evt)
|
|
{
|
|
var newValue = new UTKFloatRange(evt.newValue, Value.Max);
|
|
DebounceValueChange(newValue, 100).Forget();
|
|
}
|
|
|
|
private void OnMaxChanged(ChangeEvent<float> evt)
|
|
{
|
|
var newValue = new UTKFloatRange(Value.Min, evt.newValue);
|
|
DebounceValueChange(newValue, 100).Forget();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|