fix: incorrect default value for single menu item with automatic value (#1147)

This commit is contained in:
nekobako 2024-09-16 06:23:15 +09:00 committed by GitHub
parent 3b28ea2b14
commit b83b89ce38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 13 deletions

View File

@ -96,10 +96,10 @@ namespace nadena.dev.modular_avatar.core.editor
{ {
defaultValue = list.FirstOrDefault(m => m.isDefault && !m.automaticValue)?.Control?.value ?? 0; defaultValue = list.FirstOrDefault(m => m.isDefault && !m.automaticValue)?.Control?.value ?? 0;
if (list.Count == 1) if (list.Count == 1 && list[0].isDefault && list[0].automaticValue)
// If we have only a single entry, it's probably an on-off toggle, so we'll implicitly let 0 // If we have only a single entry, it's probably an on-off toggle, so we'll implicitly let 1
// be the 'unselected' default value (if this is not default) // be the 'selected' default value (if this is default and automatic value)
defaultValue = 0; defaultValue = 1;
} }
HashSet<int> usedValues = new(); HashSet<int> usedValues = new();
@ -124,11 +124,7 @@ namespace nadena.dev.modular_avatar.core.editor
{ {
if (mami.automaticValue) if (mami.automaticValue)
{ {
if (list.Count == 1) if (mami.isDefault)
{
mami.Control.value = 1;
}
else if (mami.isDefault)
{ {
mami.Control.value = defaultValue; mami.Control.value = defaultValue;
} }

View File

@ -16,10 +16,14 @@ namespace UnitTests.ReactiveComponent.ParameterAssignment
[Test] [Test]
public void ManuallyAssignedParametersAreNotReplaced() public void ManuallyAssignedParametersAreNotReplaced()
{ {
TestAssignments( TestAssignments(new[] { (false, 1.0f) }, new[] { 1.0f });
new[] { (false, 1.0f), (false, 4.0f) }, TestAssignments(new[] { (false, 1.0f) }, new[] { 1.0f }, 0);
new[] { 1.0f, 4.0f }
); TestAssignments(new[] { (false, 4.0f) }, new[] { 4.0f });
TestAssignments(new[] { (false, 4.0f) }, new[] { 4.0f }, 0);
TestAssignments(new[] { (false, 1.0f), (false, 4.0f) }, new[] { 1.0f, 4.0f });
TestAssignments(new[] { (false, 1.0f), (false, 4.0f) }, new[] { 1.0f, 4.0f }, 0);
} }
[Test] [Test]
@ -98,6 +102,9 @@ namespace UnitTests.ReactiveComponent.ParameterAssignment
{ {
Assert.AreEqual(expected, mami.Control.value); Assert.AreEqual(expected, mami.Control.value);
} }
var expectedDefaultValue = defaultIndex.HasValue ? expectedAssignments[defaultIndex.Value] : 0;
Assert.AreEqual(expectedDefaultValue, avDesc.expressionParameters.parameters.Single().defaultValue);
} }
} }
} }