fix: parameters lose their default value when others are moved around (#1302)

Closes: #1296
This commit is contained in:
bd_ 2024-10-19 17:15:43 -07:00 committed by GitHub
parent ab4d1fd2f4
commit 123523540e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,8 @@ namespace nadena.dev.modular_avatar.core.editor
private readonly DropdownField _boolField; private readonly DropdownField _boolField;
private ParameterSyncType _syncType; private ParameterSyncType _syncType;
private bool _hasInitialBinding;
public DefaultValueField() public DefaultValueField()
{ {
// Hidden binding elements // Hidden binding elements
@ -57,28 +58,39 @@ namespace nadena.dev.modular_avatar.core.editor
{ {
_numberField.style.display = DisplayStyle.Flex; _numberField.style.display = DisplayStyle.Flex;
_boolField.style.display = DisplayStyle.None; _boolField.style.display = DisplayStyle.None;
OnUpdateNumberValue(_numberField.value); OnUpdateNumberValue(_numberField.value, true);
} }
else else
{ {
_numberField.style.display = DisplayStyle.None; _numberField.style.display = DisplayStyle.None;
_boolField.style.display = DisplayStyle.Flex; _boolField.style.display = DisplayStyle.Flex;
OnUpdateBoolValue(_boolField.value); OnUpdateBoolValue(_boolField.value, true);
} }
} }
private void OnUpdateNumberValue(string value) private void OnUpdateNumberValue(string value, bool implicitUpdate = false)
{ {
// Upon initial creation, sometimes the OnUpdateSyncType fires before we receive the initial value event.
// In this case, suppress the update to avoid losing data.
if (implicitUpdate && !_hasInitialBinding) return;
var theValue = _defaultValueField.value;
if (string.IsNullOrWhiteSpace(value)) if (string.IsNullOrWhiteSpace(value))
{ {
_defaultValueField.value = 0; if (!implicitUpdate)
{
_defaultValueField.value = 0;
}
theValue = _defaultValueField.value;
_hasExplicitDefaultValueField.value = false; _hasExplicitDefaultValueField.value = false;
} }
else if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) else if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed)
&& !float.IsNaN(parsed) && !float.IsNaN(parsed)
&& !float.IsInfinity(parsed)) && !float.IsInfinity(parsed))
{ {
_defaultValueField.value = _syncType switch theValue = _defaultValueField.value = _syncType switch
{ {
ParameterSyncType.Int => Mathf.FloorToInt(Mathf.Clamp(parsed, 0, 255)), ParameterSyncType.Int => Mathf.FloorToInt(Mathf.Clamp(parsed, 0, 255)),
ParameterSyncType.Float => Mathf.Clamp(parsed, -1, 1), ParameterSyncType.Float => Mathf.Clamp(parsed, -1, 1),
@ -88,11 +100,15 @@ namespace nadena.dev.modular_avatar.core.editor
_hasExplicitDefaultValueField.value = true; _hasExplicitDefaultValueField.value = true;
} }
UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultValueField.value); UpdateVisibleField(theValue, _hasExplicitDefaultValueField.value);
} }
private void OnUpdateBoolValue(string value) private void OnUpdateBoolValue(string value, bool implicitUpdate = false)
{ {
// Upon initial creation, sometimes the OnUpdateSyncType fires before we receive the initial value event.
// In this case, suppress the update to avoid losing data.
if (implicitUpdate && !_hasInitialBinding) return;
_defaultValueField.value = value == V_True ? 1 : 0; _defaultValueField.value = value == V_True ? 1 : 0;
_hasExplicitDefaultValueField.value = value != V_None; _hasExplicitDefaultValueField.value = value != V_None;
@ -101,6 +117,8 @@ namespace nadena.dev.modular_avatar.core.editor
private void UpdateVisibleField(float value, bool hasExplicitValue) private void UpdateVisibleField(float value, bool hasExplicitValue)
{ {
_hasInitialBinding = true;
if (hasExplicitValue || Mathf.Abs(value) > 0.0000001) if (hasExplicitValue || Mathf.Abs(value) > 0.0000001)
{ {
_numberField.SetValueWithoutNotify(value.ToString(CultureInfo.InvariantCulture)); _numberField.SetValueWithoutNotify(value.ToString(CultureInfo.InvariantCulture));