2024-01-28 14:27:43 +09:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEditor.UIElements;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
|
|
|
|
{
|
|
|
|
|
internal class DefaultValueField : VisualElement
|
|
|
|
|
{
|
|
|
|
|
public new class UxmlFactory : UxmlFactory<DefaultValueField, UxmlTraits>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 10:04:20 +09:00
|
|
|
|
private const string V_None = " ";
|
2024-08-11 18:09:20 -07:00
|
|
|
|
private const string V_True = "ON";
|
|
|
|
|
private const string V_False = "OFF";
|
|
|
|
|
|
2024-01-28 14:27:43 +09:00
|
|
|
|
private readonly FloatField _defaultValueField;
|
|
|
|
|
private readonly Toggle _hasExplicitDefaultSetField;
|
2024-09-09 14:51:25 +09:00
|
|
|
|
private readonly TextField _notSyncedField;
|
|
|
|
|
private readonly TextField _intField;
|
|
|
|
|
private readonly TextField _floatField;
|
|
|
|
|
private readonly DropdownField _boolField;
|
2024-01-28 14:27:43 +09:00
|
|
|
|
|
|
|
|
|
public DefaultValueField()
|
|
|
|
|
{
|
|
|
|
|
// Hidden binding elements
|
|
|
|
|
_defaultValueField = new FloatField();
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_defaultValueField.style.display = DisplayStyle.None;
|
2024-01-28 14:27:43 +09:00
|
|
|
|
_hasExplicitDefaultSetField = new Toggle();
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_hasExplicitDefaultSetField.style.display = DisplayStyle.None;
|
2024-01-28 14:27:43 +09:00
|
|
|
|
|
|
|
|
|
_defaultValueField.RegisterValueChangedCallback(
|
|
|
|
|
evt => UpdateVisibleField(evt.newValue, _hasExplicitDefaultSetField.value));
|
|
|
|
|
_defaultValueField.bindingPath = nameof(ParameterConfig.defaultValue);
|
|
|
|
|
|
|
|
|
|
_hasExplicitDefaultSetField.RegisterValueChangedCallback(
|
|
|
|
|
evt => UpdateVisibleField(_defaultValueField.value, evt.newValue));
|
|
|
|
|
_hasExplicitDefaultSetField.bindingPath = nameof(ParameterConfig.hasExplicitDefaultValue);
|
|
|
|
|
|
2024-09-09 14:51:25 +09:00
|
|
|
|
// Visible elements for input
|
|
|
|
|
_notSyncedField = new TextField();
|
|
|
|
|
_notSyncedField.name = "default-value-not-synced";
|
|
|
|
|
_notSyncedField.isDelayed = true;
|
|
|
|
|
_intField = new TextField();
|
|
|
|
|
_intField.name = "default-value-int";
|
|
|
|
|
_intField.isDelayed = true;
|
|
|
|
|
_floatField = new TextField();
|
|
|
|
|
_floatField.name = "default-value-float";
|
|
|
|
|
_floatField.isDelayed = true;
|
|
|
|
|
_boolField = new DropdownField();
|
|
|
|
|
_boolField.name = "default-value-bool";
|
|
|
|
|
_boolField.choices.Add(V_None);
|
|
|
|
|
_boolField.choices.Add(V_True);
|
|
|
|
|
_boolField.choices.Add(V_False);
|
|
|
|
|
|
|
|
|
|
void NumberChangedCallback(ChangeEvent<string> evt)
|
2024-01-28 14:27:43 +09:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(evt.newValue))
|
|
|
|
|
{
|
|
|
|
|
_defaultValueField.value = 0;
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_hasExplicitDefaultSetField.value = false;
|
2024-01-28 14:27:43 +09:00
|
|
|
|
}
|
2024-09-09 14:51:25 +09:00
|
|
|
|
else if (float.TryParse(evt.newValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
|
2024-01-28 14:27:43 +09:00
|
|
|
|
{
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_defaultValueField.value = value;
|
2024-01-28 14:27:43 +09:00
|
|
|
|
_hasExplicitDefaultSetField.value = true;
|
|
|
|
|
}
|
2024-09-09 14:51:25 +09:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultSetField.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_notSyncedField.RegisterValueChangedCallback(NumberChangedCallback);
|
|
|
|
|
|
|
|
|
|
_intField.RegisterValueChangedCallback(NumberChangedCallback);
|
|
|
|
|
|
|
|
|
|
_floatField.RegisterValueChangedCallback(NumberChangedCallback);
|
2024-08-06 20:43:32 -07:00
|
|
|
|
|
|
|
|
|
_boolField.RegisterValueChangedCallback(evt =>
|
|
|
|
|
{
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_defaultValueField.value = evt.newValue == V_True ? 1 : 0;
|
2024-09-14 10:04:20 +09:00
|
|
|
|
_hasExplicitDefaultSetField.value = evt.newValue != V_None;
|
2024-08-06 20:43:32 -07:00
|
|
|
|
});
|
|
|
|
|
|
2024-01-28 14:27:43 +09:00
|
|
|
|
Add(_defaultValueField);
|
|
|
|
|
Add(_hasExplicitDefaultSetField);
|
2024-09-09 14:51:25 +09:00
|
|
|
|
Add(_notSyncedField);
|
|
|
|
|
Add(_intField);
|
|
|
|
|
Add(_floatField);
|
|
|
|
|
Add(_boolField);
|
2024-01-28 14:27:43 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ManualBindProperty(SerializedProperty property)
|
|
|
|
|
{
|
|
|
|
|
_defaultValueField.BindProperty(property);
|
|
|
|
|
_hasExplicitDefaultSetField.BindProperty(property);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateVisibleField(float value, bool hasExplicitValue)
|
|
|
|
|
{
|
|
|
|
|
if (Mathf.Abs(value) > 0.0000001)
|
|
|
|
|
{
|
|
|
|
|
hasExplicitValue = true;
|
|
|
|
|
}
|
2024-08-06 20:43:32 -07:00
|
|
|
|
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_notSyncedField.SetValueWithoutNotify(hasExplicitValue
|
|
|
|
|
? value.ToString(CultureInfo.InvariantCulture)
|
|
|
|
|
: string.Empty);
|
|
|
|
|
|
|
|
|
|
_intField.SetValueWithoutNotify(hasExplicitValue
|
|
|
|
|
? Mathf.FloorToInt(Mathf.Clamp(value, 0, 255)).ToString(CultureInfo.InvariantCulture)
|
|
|
|
|
: string.Empty);
|
|
|
|
|
|
|
|
|
|
_floatField.SetValueWithoutNotify(hasExplicitValue
|
|
|
|
|
? Mathf.Clamp(value, -1, 1).ToString(CultureInfo.InvariantCulture)
|
|
|
|
|
: string.Empty);
|
2024-08-06 20:43:32 -07:00
|
|
|
|
|
2024-09-09 14:51:25 +09:00
|
|
|
|
_boolField.SetValueWithoutNotify(hasExplicitValue
|
|
|
|
|
? value != 0 ? V_True : V_False
|
|
|
|
|
: V_None);
|
2024-01-28 14:27:43 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|