Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/UX/Settings/UXSettings.cs

140 lines
4.0 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
2025-03-10 18:06:44 +08:00
using Net.BITKit.Localization;
2025-02-24 23:03:39 +08:00
using Newtonsoft.Json;
using Unity.Mathematics;
2024-11-03 16:42:23 +08:00
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using Types = FullscreenEditor.Types;
#endif
namespace BITKit.UX.Settings
{
2025-02-24 23:03:39 +08:00
public interface IUXSettings:IUXPanel{}
public class UXSettings<T,TValue> : UIToolkitSubPanel<T>,IUXSettings where T: IUXPanel
2024-11-03 16:42:23 +08:00
{
2025-03-10 18:06:44 +08:00
private readonly ILocalizationService _localizationService;
2025-02-24 23:03:39 +08:00
private readonly TValue _value;
private readonly IWrapper<TValue> _valueWrapper;
2025-03-10 18:06:44 +08:00
public UXSettings(IServiceProvider serviceProvider, IWrapper<TValue> valueWrapper, ILocalizationService localizationService) : base(serviceProvider)
2024-11-03 16:42:23 +08:00
{
2025-02-24 23:03:39 +08:00
_valueWrapper = valueWrapper;
2025-03-10 18:06:44 +08:00
_localizationService = localizationService;
2025-02-24 23:03:39 +08:00
_value = _valueWrapper.Value;
2024-11-03 16:42:23 +08:00
}
2025-02-24 23:03:39 +08:00
[UXBindPath("settings-container")]
private VisualElement _settingsContainer;
2024-11-03 16:42:23 +08:00
2025-02-24 23:03:39 +08:00
private void Save()
2024-11-03 16:42:23 +08:00
{
2025-02-24 23:03:39 +08:00
_valueWrapper.Value = _value;
2024-11-03 16:42:23 +08:00
}
2025-02-24 23:03:39 +08:00
protected override void OnInitiated()
2024-11-03 16:42:23 +08:00
{
2025-02-24 23:03:39 +08:00
base.OnInitiated();
_settingsContainer.Clear();
foreach (var propertyInfo in typeof(TValue).GetProperties())
2024-11-03 16:42:23 +08:00
{
2025-03-10 18:06:44 +08:00
if(propertyInfo.SetMethod is null)continue;
2025-02-24 23:03:39 +08:00
var name = propertyInfo.GetDisplayName();
var value = propertyInfo.GetValue(_valueWrapper.Value);
2025-03-10 18:06:44 +08:00
if (CreateVisualElement() is { } visualElement)
{
visualElement.viewDataKey = name;
visualElement.AddToClassList("localized");
}
VisualElement CreateVisualElement()
2024-11-03 16:42:23 +08:00
{
2025-03-10 18:06:44 +08:00
switch (propertyInfo.PropertyType)
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
case var type when type == typeof(bool):
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
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):
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
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):
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
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):
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
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:
2025-02-24 23:03:39 +08:00
{
2025-03-10 18:06:44 +08:00
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;
2025-02-24 23:03:39 +08:00
}
2025-03-10 18:06:44 +08:00
return null;
2024-11-03 16:42:23 +08:00
}
}
}
}
}