feat: improve material setter inspector (#1035)

This commit is contained in:
nekobako 2024-08-26 10:05:19 +09:00 committed by GitHub
parent a2b9b817ce
commit 46cf066e04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 41 deletions

View File

@ -28,20 +28,29 @@
.horizontal #f-object {
flex-grow: 1;
margin-bottom: 1px;
}
#f-material-index-int {
#f-material-index {
display: none;
}
#f-material-index-dropdown {
width: 100px;
}
#f-material-index-original {
flex-grow: 1;
}
#f-material {
flex-grow: 1;
}
.horizontal > Label {
align-self: center;
}
#f-object > * {
margin-left: 0;
width: 100px;
height: 19px;
margin: 1px -2px 1px 3px;
-unity-text-align: middle-left;
}

View File

@ -26,7 +26,9 @@ namespace nadena.dev.modular_avatar.core.editor.ShapeChanger
uxml.styleSheets.Add(uss);
uxml.BindProperty(property);
var f_material_index = uxml.Q<DropdownField>("f-material-index");
var f_material_index = uxml.Q<IntegerField>("f-material-index");
var f_material_index_dropdown = uxml.Q<DropdownField>("f-material-index-dropdown");
var f_material_index_original = uxml.Q<ObjectField>("f-material-index-original");
var f_object = uxml.Q<PropertyField>("f-object");
@ -36,78 +38,106 @@ namespace nadena.dev.modular_avatar.core.editor.ShapeChanger
});
UpdateMaterialDropdown();
// Link dropdown to material index field
var f_material_index_int = uxml.Q<IntegerField>("f-material-index-int");
f_material_index_int.RegisterValueChangedCallback(evt =>
{
f_material_index.SetValueWithoutNotify("" + evt.newValue);
});
// Link dropdown and original field to material index field
f_material_index.RegisterValueChangedCallback(evt =>
{
f_material_index_dropdown.SetValueWithoutNotify(evt.newValue.ToString());
UpdateOriginalMaterial();
});
f_material_index_dropdown.RegisterValueChangedCallback(evt =>
{
if (evt.newValue != null && int.TryParse(evt.newValue, out var i))
{
f_material_index_int.value = i;
f_material_index.value = i;
}
});
f_material_index_original.SetEnabled(false);
return uxml;
void UpdateMaterialDropdown()
{
var targetObject = AvatarObjectReference.Get(property.FindPropertyRelative("Object"));
Material[] sharedMaterials;
try
{
sharedMaterials = targetObject?.GetComponent<Renderer>()?.sharedMaterials;
}
catch (MissingComponentException e)
{
sharedMaterials = null;
}
var sharedMaterials = GetSharedMaterials();
if (sharedMaterials != null)
{
var matCount = sharedMaterials.Length;
f_material_index.SetEnabled(true);
f_material_index_dropdown.SetEnabled(true);
f_material_index.choices.Clear();
f_material_index_dropdown.choices.Clear();
for (int i = 0; i < matCount; i++)
{
f_material_index.choices.Add(i.ToString());
f_material_index_dropdown.choices.Add(i.ToString());
}
f_material_index.formatListItemCallback = idx_s =>
f_material_index_dropdown.formatListItemCallback = idx_s =>
{
if (string.IsNullOrWhiteSpace(idx_s)) return "";
var idx = int.Parse(idx_s);
if (idx < 0 || idx >= sharedMaterials.Length)
{
return idx + ": <???>";
return $"Element {idx_s}: <???>";
}
else if (sharedMaterials[idx] == null)
{
return idx + ": <none>";
return $"Element {idx_s}: <None>";
}
else
{
return idx + ": " + sharedMaterials[idx].name;
return $"Element {idx_s}: {sharedMaterials[idx].name}";
}
};
f_material_index.formatSelectedValueCallback = f_material_index.formatListItemCallback;
f_material_index_dropdown.formatSelectedValueCallback = idx_s => $"Element {idx_s}";
}
else
{
f_material_index.SetEnabled(false);
if (f_material_index.choices.Count == 0)
f_material_index_dropdown.SetEnabled(false);
if (f_material_index_dropdown.choices.Count == 0)
{
f_material_index.choices.Add("0");
f_material_index_dropdown.choices.Add("0");
}
f_material_index.formatListItemCallback = _ => "<Missing Renderer>";
f_material_index.formatSelectedValueCallback = f_material_index.formatListItemCallback;
f_material_index_dropdown.formatListItemCallback = idx_s => "<Missing Renderer>";
f_material_index_dropdown.formatSelectedValueCallback = f_material_index_dropdown.formatListItemCallback;
}
UpdateOriginalMaterial();
}
void UpdateOriginalMaterial()
{
var sharedMaterials = GetSharedMaterials();
if (sharedMaterials != null)
{
var idx = f_material_index.value;
if (idx < 0 || idx >= sharedMaterials.Length)
{
f_material_index_original.SetValueWithoutNotify(null);
}
else
{
f_material_index_original.SetValueWithoutNotify(sharedMaterials[idx]);
}
}
else
{
f_material_index_original.SetValueWithoutNotify(null);
}
}
Material[] GetSharedMaterials()
{
var targetObject = AvatarObjectReference.Get(property.FindPropertyRelative("Object"));
try
{
return targetObject?.GetComponent<Renderer>()?.sharedMaterials;
}
catch (MissingComponentException e)
{
return null;
}
}
}

View File

@ -2,10 +2,11 @@
<ui:VisualElement class="toggled-object-editor">
<ui:VisualElement class="horizontal">
<ed:PropertyField binding-path="Object" label="" name="f-object" class="f-object"/>
<ui:DropdownField name="f-material-index" binding-path="MaterialIndex"/>
<ui:VisualElement style="display:none">
<ed:IntegerField binding-path="MaterialIndex" name="f-material-index-int"/>
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement class="horizontal">
<ed:IntegerField name="f-material-index" binding-path="MaterialIndex"/>
<ui:DropdownField name="f-material-index-dropdown"/>
<ed:ObjectField name="f-material-index-original"/>
</ui:VisualElement>
<ui:VisualElement class="horizontal">
<ui:Label text="reactive_object.material-setter.set-to" class="ndmf-tr"/>