using System; 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; using UnityEngine.UIElements; #if UNITY_EDITOR using Types = FullscreenEditor.Types; #endif namespace BITKit.UX.Settings { public interface IUXSettings:IUXPanel{} public class UXSettings : UIToolkitSubPanel,IUXSettings where T: IUXPanel { private readonly ILocalizationService _localizationService; private readonly TValue _value; private readonly IWrapper _valueWrapper; public UXSettings(IServiceProvider serviceProvider, IWrapper valueWrapper, ILocalizationService localizationService) : base(serviceProvider) { _valueWrapper = valueWrapper; _localizationService = localizationService; _value = _valueWrapper.Value; } [UXBindPath("settings-container")] private VisualElement _settingsContainer; private void Save() { _valueWrapper.Value = _value; } protected override void OnInitiated() { base.OnInitiated(); _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); if (CreateVisualElement() is { } visualElement) { visualElement.viewDataKey = name; visualElement.AddToClassList("localized"); } VisualElement CreateVisualElement() { switch (propertyInfo.PropertyType) { case var type when type == typeof(bool): { var checkBox = _settingsContainer.Create(); checkBox.label = _localizationService.GetLocalizedString(name); checkBox.SetValueWithoutNotify(value.As()); checkBox.RegisterValueChangedCallback(x => { propertyInfo.SetValue(_value, x.newValue); Save(); }); return checkBox; } break; case var type when type == typeof(float): { var slider = _settingsContainer.Create(); slider.label = _localizationService.GetLocalizedString(name); slider.showInputField = true; slider.showMixedValue = true; slider.lowValue = 1; slider.highValue = 100; slider.SetValueWithoutNotify(value.As()); slider.RegisterValueChangedCallback(x => { propertyInfo.SetValue(_value, x.newValue); Save(); }); return slider; } break; case var type when type == typeof(int): { var slider = _settingsContainer.Create(); slider.label = _localizationService.GetLocalizedString(name); slider.showInputField = true; slider.showMixedValue = true; slider.lowValue = 1; slider.highValue = 100; slider.SetValueWithoutNotify(value.As()); slider.RegisterValueChangedCallback(x => { propertyInfo.SetValue(_value, x.newValue); Save(); }); return slider; } break; case var type when type == typeof(string): { var textField = _settingsContainer.Create(); textField.label = _localizationService.GetLocalizedString(name); textField.SetValueWithoutNotify(value.As()); textField.RegisterValueChangedCallback(x => { propertyInfo.SetValue(_value, x.newValue); Save(); }); return textField; } break; case var type when type.IsEnum: { var enumField = _settingsContainer.Create(); 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; } return null; } } } } }