This commit is contained in:
CortexCore
2025-03-24 14:42:40 +08:00
parent 18239a5ae4
commit 9845d20f7f
99 changed files with 5418 additions and 5512 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Net.BITKit.Localization;
using Newtonsoft.Json;
using Unity.Mathematics;
using UnityEngine;
@@ -15,11 +16,13 @@ namespace BITKit.UX.Settings
public interface IUXSettings:IUXPanel{}
public class UXSettings<T,TValue> : UIToolkitSubPanel<T>,IUXSettings where T: IUXPanel
{
private readonly ILocalizationService _localizationService;
private readonly TValue _value;
private readonly IWrapper<TValue> _valueWrapper;
public UXSettings(IServiceProvider serviceProvider, IWrapper<TValue> valueWrapper) : base(serviceProvider)
public UXSettings(IServiceProvider serviceProvider, IWrapper<TValue> valueWrapper, ILocalizationService localizationService) : base(serviceProvider)
{
_valueWrapper = valueWrapper;
_localizationService = localizationService;
_value = _valueWrapper.Value;
}
@@ -36,80 +39,98 @@ namespace BITKit.UX.Settings
_settingsContainer.Clear();
foreach (var propertyInfo in typeof(TValue).GetProperties())
{
if(propertyInfo.SetMethod is null)continue;
var name = propertyInfo.GetDisplayName();
var value = propertyInfo.GetValue(_valueWrapper.Value);
switch (propertyInfo.PropertyType)
if (CreateVisualElement() is { } visualElement)
{
case var type when type == typeof(bool):
visualElement.viewDataKey = name;
visualElement.AddToClassList("localized");
}
VisualElement CreateVisualElement()
{
switch (propertyInfo.PropertyType)
{
var checkBox = _settingsContainer.Create<Toggle>();
checkBox.label = name;
checkBox.SetValueWithoutNotify(value.As<bool>());
checkBox.RegisterValueChangedCallback(x =>
case var type when type == typeof(bool):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(float):
{
var slider = _settingsContainer.Create<Slider>();
slider.label = name;
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<float>());
slider.RegisterValueChangedCallback(x =>
var checkBox = _settingsContainer.Create<Toggle>();
checkBox.label = _localizationService.GetLocalizedString(name);
checkBox.SetValueWithoutNotify(value.As<bool>());
checkBox.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return checkBox;
}
break;
case var type when type == typeof(float):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(int):
{
var slider = _settingsContainer.Create<SliderInt>();
slider.label = name;
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<int>());
slider.RegisterValueChangedCallback(x =>
var slider = _settingsContainer.Create<Slider>();
slider.label = _localizationService.GetLocalizedString(name);
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<float>());
slider.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return slider;
}
break;
case var type when type == typeof(int):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type == typeof(string):
{
var textField = _settingsContainer.Create<TextField>();
textField.label = name;
textField.SetValueWithoutNotify(value.As<string>());
textField.RegisterValueChangedCallback(x =>
var slider = _settingsContainer.Create<SliderInt>();
slider.label = _localizationService.GetLocalizedString(name);
slider.showInputField = true;
slider.showMixedValue = true;
slider.lowValue = 1;
slider.highValue = 100;
slider.SetValueWithoutNotify(value.As<int>());
slider.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return slider;
}
break;
case var type when type == typeof(string):
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
}
break;
case var type when type.IsEnum:
{
var enumField = _settingsContainer.Create<EnumField>();
enumField.label = name;
enumField.Init(value as Enum);
enumField.SetValueWithoutNotify(value as Enum);
enumField.RegisterValueChangedCallback(x =>
var textField = _settingsContainer.Create<TextField>();
textField.label = _localizationService.GetLocalizedString(name);
textField.SetValueWithoutNotify(value.As<string>());
textField.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return textField;
}
break;
case var type when type.IsEnum:
{
propertyInfo.SetValue(_value,x.newValue);
Save();
});
var enumField = _settingsContainer.Create<EnumField>();
enumField.label = _localizationService.GetLocalizedString(name);
enumField.Init(value as Enum);
enumField.SetValueWithoutNotify(value as Enum);
enumField.RegisterValueChangedCallback(x =>
{
propertyInfo.SetValue(_value, x.newValue);
Save();
});
return enumField;
}
break;
}
break;
return null;
}
}
}