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; [UXBindPath("confirm-button",true)] private Button _confirmButton; private void Save() { if (_confirmButton is null) { _valueWrapper.Value = _value; } } protected override void OnInitiated() { base.OnInitiated(); if (_confirmButton is not null) { _confirmButton.clicked += OnConfirmButtonOnclicked; } Rebuild(); } private void OnConfirmButtonOnclicked() { _valueWrapper.Value = _value; } private void Rebuild() { _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 = 0.01f; 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; case var type when type == typeof(int2): { var vector2Field = _settingsContainer.Create(); vector2Field.label = _localizationService.GetLocalizedString(name); var int2Value = value.As(); vector2Field.value = new Vector2(int2Value.x, int2Value.y); vector2Field.RegisterValueChangedCallback(x => { propertyInfo.SetValue(_value, new int2((int)x.newValue.x, (int)x.newValue.y)); Save(); }); return vector2Field; } } return null; } } } } }