feat: enhance default value field input

This commit is contained in:
nekobako 2024-09-09 14:51:25 +09:00
parent fed6a22d72
commit fcc46ace8e
2 changed files with 73 additions and 50 deletions

View File

@ -16,21 +16,20 @@ namespace nadena.dev.modular_avatar.core.editor
private const string V_True = "ON"; private const string V_True = "ON";
private const string V_False = "OFF"; private const string V_False = "OFF";
private readonly TextField _visibleField;
private readonly FloatField _defaultValueField; private readonly FloatField _defaultValueField;
private readonly DropdownField _boolField;
private readonly Toggle _hasExplicitDefaultSetField; private readonly Toggle _hasExplicitDefaultSetField;
private readonly TextField _notSyncedField;
private readonly TextField _intField;
private readonly TextField _floatField;
private readonly DropdownField _boolField;
public DefaultValueField() public DefaultValueField()
{ {
// Hidden binding elements // Hidden binding elements
_defaultValueField = new FloatField(); _defaultValueField = new FloatField();
_defaultValueField.style.display = DisplayStyle.None;
_hasExplicitDefaultSetField = new Toggle(); _hasExplicitDefaultSetField = new Toggle();
_boolField = new DropdownField(); _hasExplicitDefaultSetField.style.display = DisplayStyle.None;
_boolField.choices.Add(V_None);
_boolField.choices.Add(V_True);
_boolField.choices.Add(V_False);
_defaultValueField.RegisterValueChangedCallback( _defaultValueField.RegisterValueChangedCallback(
evt => UpdateVisibleField(evt.newValue, _hasExplicitDefaultSetField.value)); evt => UpdateVisibleField(evt.newValue, _hasExplicitDefaultSetField.value));
@ -40,43 +39,58 @@ namespace nadena.dev.modular_avatar.core.editor
evt => UpdateVisibleField(_defaultValueField.value, evt.newValue)); evt => UpdateVisibleField(_defaultValueField.value, evt.newValue));
_hasExplicitDefaultSetField.bindingPath = nameof(ParameterConfig.hasExplicitDefaultValue); _hasExplicitDefaultSetField.bindingPath = nameof(ParameterConfig.hasExplicitDefaultValue);
_visibleField = new TextField(); // Visible elements for input
_visibleField.RegisterValueChangedCallback(evt => _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)
{ {
if (string.IsNullOrWhiteSpace(evt.newValue)) if (string.IsNullOrWhiteSpace(evt.newValue))
{ {
_hasExplicitDefaultSetField.value = false;
_defaultValueField.value = 0; _defaultValueField.value = 0;
_hasExplicitDefaultSetField.value = false;
}
else if (float.TryParse(evt.newValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
{
_defaultValueField.value = value;
_hasExplicitDefaultSetField.value = true;
} }
else else
{ {
_hasExplicitDefaultSetField.value = true; UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultSetField.value);
_defaultValueField.value = float.Parse(evt.newValue, CultureInfo.InvariantCulture);
} }
}); }
_defaultValueField.style.width = 0; _notSyncedField.RegisterValueChangedCallback(NumberChangedCallback);
_defaultValueField.SetEnabled(false);
_hasExplicitDefaultSetField.style.width = 0; _intField.RegisterValueChangedCallback(NumberChangedCallback);
_hasExplicitDefaultSetField.SetEnabled(false);
_floatField.RegisterValueChangedCallback(NumberChangedCallback);
_boolField.RegisterValueChangedCallback(evt => _boolField.RegisterValueChangedCallback(evt =>
{ {
if (evt.newValue == V_True) _defaultValueField.value = evt.newValue == V_True ? 1 : 0;
_defaultValueField.value = 1;
else
_defaultValueField.value = 0;
_hasExplicitDefaultSetField.value = evt.newValue != V_None; _hasExplicitDefaultSetField.value = evt.newValue != V_None;
}); });
style.flexDirection = FlexDirection.Row;
Add(_visibleField);
Add(_boolField);
Add(_defaultValueField); Add(_defaultValueField);
Add(_hasExplicitDefaultSetField); Add(_hasExplicitDefaultSetField);
Add(_notSyncedField);
Add(_intField);
Add(_floatField);
Add(_boolField);
} }
public void ManualBindProperty(SerializedProperty property) public void ManualBindProperty(SerializedProperty property)
@ -92,18 +106,21 @@ namespace nadena.dev.modular_avatar.core.editor
hasExplicitValue = true; hasExplicitValue = true;
} }
var str = hasExplicitValue ? value.ToString(CultureInfo.InvariantCulture) : ""; _notSyncedField.SetValueWithoutNotify(hasExplicitValue
_visibleField.SetValueWithoutNotify(str); ? value.ToString(CultureInfo.InvariantCulture)
: string.Empty);
string boolStr; _intField.SetValueWithoutNotify(hasExplicitValue
if (!hasExplicitValue) ? Mathf.FloorToInt(Mathf.Clamp(value, 0, 255)).ToString(CultureInfo.InvariantCulture)
boolStr = V_None; : string.Empty);
else if (value > 0.5)
boolStr = V_True;
else
boolStr = V_False;
_boolField.SetValueWithoutNotify(boolStr); _floatField.SetValueWithoutNotify(hasExplicitValue
? Mathf.Clamp(value, -1, 1).ToString(CultureInfo.InvariantCulture)
: string.Empty);
_boolField.SetValueWithoutNotify(hasExplicitValue
? value != 0 ? V_True : V_False
: V_None);
} }
} }
} }

View File

@ -79,25 +79,31 @@
margin: 0; margin: 0;
} }
DefaultValueField TextField { DefaultValueField > * {
display: flex;
width: 60px; width: 60px;
height: 100%; height: 100%;
margin: 0; margin: 0;
} }
DefaultValueField DropdownField { .st-ty-Not-Synced DefaultValueField #default-value-int,
display: none; .st-ty-Not-Synced DefaultValueField #default-value-float,
width: 60px; .st-ty-Not-Synced DefaultValueField #default-value-bool,
height: 100%; .st-ty-Int DefaultValueField #default-value-not-synced,
margin: 0; .st-ty-Int DefaultValueField #default-value-float,
} .st-ty-Int DefaultValueField #default-value-bool,
.st-ty-Float DefaultValueField #default-value-not-synced,
.st-ty-Bool DefaultValueField TextField { .st-ty-Float DefaultValueField #default-value-int,
.st-ty-Float DefaultValueField #default-value-bool,
.st-ty-Bool DefaultValueField #default-value-not-synced,
.st-ty-Bool DefaultValueField #default-value-int,
.st-ty-Bool DefaultValueField #default-value-float {
display: none; display: none;
} }
.st-ty-Bool DefaultValueField DropdownField { .st-ty-Not-Synced DefaultValueField #default-value-not-synced,
.st-ty-Int DefaultValueField #default-value-int,
.st-ty-Float DefaultValueField #default-value-float,
.st-ty-Bool DefaultValueField #default-value-bool {
display: flex; display: flex;
} }