mirror of
https://github.com/bdunderscore/modular-avatar.git
synced 2025-04-24 13:29:01 +08:00
fix: format default value when update type or value on parameters inspector
This commit is contained in:
parent
fcc46ace8e
commit
b6b67c0e0d
@ -1,6 +1,4 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using UnityEditor;
|
|
||||||
using UnityEditor.UIElements;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
@ -17,110 +15,100 @@ namespace nadena.dev.modular_avatar.core.editor
|
|||||||
private const string V_False = "OFF";
|
private const string V_False = "OFF";
|
||||||
|
|
||||||
private readonly FloatField _defaultValueField;
|
private readonly FloatField _defaultValueField;
|
||||||
private readonly Toggle _hasExplicitDefaultSetField;
|
private readonly Toggle _hasExplicitDefaultValueField;
|
||||||
private readonly TextField _notSyncedField;
|
private readonly TextField _numberField;
|
||||||
private readonly TextField _intField;
|
|
||||||
private readonly TextField _floatField;
|
|
||||||
private readonly DropdownField _boolField;
|
private readonly DropdownField _boolField;
|
||||||
|
|
||||||
|
private ParameterSyncType _syncType;
|
||||||
|
|
||||||
public DefaultValueField()
|
public DefaultValueField()
|
||||||
{
|
{
|
||||||
// Hidden binding elements
|
// Hidden binding elements
|
||||||
_defaultValueField = new FloatField();
|
_defaultValueField = new FloatField();
|
||||||
_defaultValueField.style.display = DisplayStyle.None;
|
_defaultValueField.style.display = DisplayStyle.None;
|
||||||
_hasExplicitDefaultSetField = new Toggle();
|
|
||||||
_hasExplicitDefaultSetField.style.display = DisplayStyle.None;
|
|
||||||
|
|
||||||
_defaultValueField.RegisterValueChangedCallback(
|
|
||||||
evt => UpdateVisibleField(evt.newValue, _hasExplicitDefaultSetField.value));
|
|
||||||
_defaultValueField.bindingPath = nameof(ParameterConfig.defaultValue);
|
_defaultValueField.bindingPath = nameof(ParameterConfig.defaultValue);
|
||||||
|
_defaultValueField.RegisterValueChangedCallback(evt => UpdateVisibleField(evt.newValue, _hasExplicitDefaultValueField.value));
|
||||||
_hasExplicitDefaultSetField.RegisterValueChangedCallback(
|
_hasExplicitDefaultValueField = new Toggle();
|
||||||
evt => UpdateVisibleField(_defaultValueField.value, evt.newValue));
|
_hasExplicitDefaultValueField.style.display = DisplayStyle.None;
|
||||||
_hasExplicitDefaultSetField.bindingPath = nameof(ParameterConfig.hasExplicitDefaultValue);
|
_hasExplicitDefaultValueField.bindingPath = nameof(ParameterConfig.hasExplicitDefaultValue);
|
||||||
|
_hasExplicitDefaultValueField.RegisterValueChangedCallback(evt => UpdateVisibleField(_defaultValueField.value, evt.newValue));
|
||||||
|
|
||||||
// Visible elements for input
|
// Visible elements for input
|
||||||
_notSyncedField = new TextField();
|
_numberField = new TextField();
|
||||||
_notSyncedField.name = "default-value-not-synced";
|
_numberField.isDelayed = true;
|
||||||
_notSyncedField.isDelayed = true;
|
_numberField.RegisterValueChangedCallback(evt => OnUpdateNumberValue(evt.newValue));
|
||||||
_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 = new DropdownField();
|
||||||
_boolField.name = "default-value-bool";
|
|
||||||
_boolField.choices.Add(V_None);
|
_boolField.choices.Add(V_None);
|
||||||
_boolField.choices.Add(V_True);
|
_boolField.choices.Add(V_True);
|
||||||
_boolField.choices.Add(V_False);
|
_boolField.choices.Add(V_False);
|
||||||
|
_boolField.RegisterValueChangedCallback(evt => OnUpdateBoolValue(evt.newValue));
|
||||||
void NumberChangedCallback(ChangeEvent<string> evt)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(evt.newValue))
|
|
||||||
{
|
|
||||||
_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
|
|
||||||
{
|
|
||||||
UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultSetField.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_notSyncedField.RegisterValueChangedCallback(NumberChangedCallback);
|
|
||||||
|
|
||||||
_intField.RegisterValueChangedCallback(NumberChangedCallback);
|
|
||||||
|
|
||||||
_floatField.RegisterValueChangedCallback(NumberChangedCallback);
|
|
||||||
|
|
||||||
_boolField.RegisterValueChangedCallback(evt =>
|
|
||||||
{
|
|
||||||
_defaultValueField.value = evt.newValue == V_True ? 1 : 0;
|
|
||||||
_hasExplicitDefaultSetField.value = evt.newValue != V_None;
|
|
||||||
});
|
|
||||||
|
|
||||||
Add(_defaultValueField);
|
Add(_defaultValueField);
|
||||||
Add(_hasExplicitDefaultSetField);
|
Add(_hasExplicitDefaultValueField);
|
||||||
Add(_notSyncedField);
|
Add(_numberField);
|
||||||
Add(_intField);
|
|
||||||
Add(_floatField);
|
|
||||||
Add(_boolField);
|
Add(_boolField);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ManualBindProperty(SerializedProperty property)
|
public void OnUpdateSyncType(ParameterSyncType syncType)
|
||||||
{
|
{
|
||||||
_defaultValueField.BindProperty(property);
|
_syncType = syncType;
|
||||||
_hasExplicitDefaultSetField.BindProperty(property);
|
|
||||||
|
if (syncType != ParameterSyncType.Bool)
|
||||||
|
{
|
||||||
|
_numberField.style.display = DisplayStyle.Flex;
|
||||||
|
_boolField.style.display = DisplayStyle.None;
|
||||||
|
OnUpdateNumberValue(_numberField.value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_numberField.style.display = DisplayStyle.None;
|
||||||
|
_boolField.style.display = DisplayStyle.Flex;
|
||||||
|
OnUpdateBoolValue(_boolField.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnUpdateNumberValue(string value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
{
|
||||||
|
_defaultValueField.value = 0;
|
||||||
|
_hasExplicitDefaultValueField.value = false;
|
||||||
|
}
|
||||||
|
else if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed))
|
||||||
|
{
|
||||||
|
_defaultValueField.value = _syncType switch
|
||||||
|
{
|
||||||
|
ParameterSyncType.Int => Mathf.FloorToInt(Mathf.Clamp(parsed, 0, 255)),
|
||||||
|
ParameterSyncType.Float => Mathf.Clamp(parsed, -1, 1),
|
||||||
|
ParameterSyncType.Bool => parsed != 0 ? 1 : 0,
|
||||||
|
_ => parsed,
|
||||||
|
};
|
||||||
|
_hasExplicitDefaultValueField.value = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultValueField.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnUpdateBoolValue(string value)
|
||||||
|
{
|
||||||
|
_defaultValueField.value = value == V_True ? 1 : 0;
|
||||||
|
_hasExplicitDefaultValueField.value = value != V_None;
|
||||||
|
|
||||||
|
UpdateVisibleField(_defaultValueField.value, _hasExplicitDefaultValueField.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateVisibleField(float value, bool hasExplicitValue)
|
private void UpdateVisibleField(float value, bool hasExplicitValue)
|
||||||
{
|
{
|
||||||
if (Mathf.Abs(value) > 0.0000001)
|
if (hasExplicitValue || Mathf.Abs(value) > 0.0000001)
|
||||||
{
|
{
|
||||||
hasExplicitValue = true;
|
_numberField.SetValueWithoutNotify(value.ToString(CultureInfo.InvariantCulture));
|
||||||
|
_boolField.SetValueWithoutNotify(value != 0 ? V_True : V_False);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_numberField.SetValueWithoutNotify(string.Empty);
|
||||||
|
_boolField.SetValueWithoutNotify(V_None);
|
||||||
}
|
}
|
||||||
|
|
||||||
_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);
|
|
||||||
|
|
||||||
_boolField.SetValueWithoutNotify(hasExplicitValue
|
|
||||||
? value != 0 ? V_True : V_False
|
|
||||||
: V_None);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,14 +20,14 @@ namespace nadena.dev.modular_avatar.core.editor.Parameters
|
|||||||
Localization.UI.Localize(root);
|
Localization.UI.Localize(root);
|
||||||
root.styleSheets.Add(uss);
|
root.styleSheets.Add(uss);
|
||||||
|
|
||||||
var type_field = root.Q<DropdownField>("f-type");
|
var f_type = root.Q<DropdownField>("f-type");
|
||||||
|
var f_sync_type = root.Q<DropdownField>("f-sync-type");
|
||||||
var f_sync_type = root.Q<VisualElement>("f-sync-type");
|
var f_is_prefix = root.Q<VisualElement>("f-is-prefix");
|
||||||
SetupPairedDropdownField(
|
SetupPairedDropdownField(
|
||||||
root,
|
root,
|
||||||
type_field,
|
f_type,
|
||||||
f_sync_type,
|
f_sync_type,
|
||||||
root.Q<VisualElement>("f-is-prefix"),
|
f_is_prefix,
|
||||||
("Bool", "False", "params.syncmode.Bool"),
|
("Bool", "False", "params.syncmode.Bool"),
|
||||||
("Float", "False", "params.syncmode.Float"),
|
("Float", "False", "params.syncmode.Float"),
|
||||||
("Int", "False", "params.syncmode.Int"),
|
("Int", "False", "params.syncmode.Int"),
|
||||||
@ -35,15 +35,9 @@ namespace nadena.dev.modular_avatar.core.editor.Parameters
|
|||||||
(null, "True", "params.syncmode.PhysBonesPrefix")
|
(null, "True", "params.syncmode.PhysBonesPrefix")
|
||||||
);
|
);
|
||||||
|
|
||||||
f_sync_type.Q<DropdownField>().RegisterValueChangedCallback(evt =>
|
var f_default = root.Q<DefaultValueField>();
|
||||||
{
|
f_default.OnUpdateSyncType((ParameterSyncType)f_sync_type.index);
|
||||||
var is_anim_only = evt.newValue == "Not Synced";
|
f_sync_type.RegisterValueChangedCallback(evt => f_default.OnUpdateSyncType((ParameterSyncType)f_sync_type.index));
|
||||||
|
|
||||||
if (is_anim_only)
|
|
||||||
root.AddToClassList("st-anim-only");
|
|
||||||
else
|
|
||||||
root.RemoveFromClassList("st-anim-only");
|
|
||||||
});
|
|
||||||
|
|
||||||
var f_synced = root.Q<Toggle>("f-synced");
|
var f_synced = root.Q<Toggle>("f-synced");
|
||||||
var f_local_only = root.Q<Toggle>("f-local-only");
|
var f_local_only = root.Q<Toggle>("f-local-only");
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.st-anim-only .st-anim-only__hide {
|
.st-ty-Not-Synced .st-anim-only__hide {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,28 +85,6 @@ DefaultValueField > * {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.st-ty-Not-Synced DefaultValueField #default-value-int,
|
|
||||||
.st-ty-Not-Synced DefaultValueField #default-value-float,
|
|
||||||
.st-ty-Not-Synced DefaultValueField #default-value-bool,
|
|
||||||
.st-ty-Int DefaultValueField #default-value-not-synced,
|
|
||||||
.st-ty-Int DefaultValueField #default-value-float,
|
|
||||||
.st-ty-Int DefaultValueField #default-value-bool,
|
|
||||||
.st-ty-Float DefaultValueField #default-value-not-synced,
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
#f-local-only {
|
#f-local-only {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user