// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖 using System.Collections.Generic; using FairyGUI; using UnityEngine; using NBC; using NBF.Setting; using Unity.VisualScripting; using UIPanel = NBC.UIPanel; namespace NBF { [UIBind(UIDef.ID.SettingPanel)] public partial class SettingPanel : UIPanel { private readonly List tabList = new List(); private string _currentTab = ""; private List _canSelectIndex = new List(); private int _nowSelectIndex = -1; protected override void OnInit() { base.OnInit(); IsShowCursor = true; IsDontBack = true; var groupNames = Settings.Instance.GetAllTabs(); foreach (var group in groupNames) { var tab = new TabItemData { Key = $"TEXT_SETTINGS_{group}" }; tabList.Add(tab); } Menu.OnTabChange += ChangeTab; List.onClickItem.Set(OnClickListItem); } protected override void OnShow() { Settings.Instance.LoadAllSettings(); Game.Input.OnUICanceled += OnUICanceled; Menu.SetTabs(tabList); UseBottomMenu(); } private void ChangeTab(int index) { if (index < 0) return; Log.Info($"Change tab index={index}"); var tabListData = tabList[index]; _currentTab = tabListData.Key; ResetSettingList(); } private void ResetSettingList() { // TextTitle.text = Lan.Get(_currentGroup); _nowSelectIndex = -1; if (string.IsNullOrEmpty(_currentTab)) return; var options = Settings.Instance.GetOptionsByTab(_currentTab); Dictionary> groupOptions = new Dictionary>(); foreach (var option in options) { if (!groupOptions.TryGetValue(option.Group, out List list)) { list = new List(); groupOptions.Add(option.Group, list); } list.Add(option); } _canSelectIndex.Clear(); List.RemoveChildrenToPool(); var url = UIPackage.GetItemURL(UIPackName, "SettingSubTitleItem"); foreach (var key in groupOptions.Keys) { var value = groupOptions[key]; if (List.AddItemFromPool(url) is GLabel label) { label.SetLanguage(key); } foreach (var option in value) { if (option is InputOption) { if (List.AddItemFromPool(SettingInputItem.URL) is SettingInputItem item) { item.SetData(option as InputOption); var index = List.GetChildIndex(item); _canSelectIndex.Add(index); } } else { if (List.AddItemFromPool() is SettingItem item) { item.SetData(option); var index = List.GetChildIndex(item); _canSelectIndex.Add(index); } } } } } private void UseBottomMenu() { BottomMenu.Use(this); } private void OnApplySettings() { var options = Settings.Instance.GetOptionsByTab(_currentTab); Log.Info("OnApplySettings"); foreach (var option in options) { option.Apply(); } Notices.Success("TEXT_OP_SUCCESS"); } private void OnResetSettings() { var options = Settings.Instance.GetOptionsByTab(_currentTab); foreach (var option in options) { option.Reset(); } ResetSettingList(); Notices.Success("TEXT_OP_SUCCESS"); } private void OnUICanceled(string action) { if (action == InputDef.UI.SubPrev) { if (List.GetChildAt(List.selectedIndex) is SettingItem item) { item.OnPrev(); } } else if (action == InputDef.UI.SubNext) { if (List.GetChildAt(List.selectedIndex) is SettingItem item) { item.OnNext(); } } else if (action == InputDef.UI.Up) { ChangeListSelected(); } else if (action == InputDef.UI.Down) { ChangeListSelected(false); } else if (action == InputDef.UI.Back) { OnBack(); } else if (action == InputDef.UI.Enter) { OnApplySettings(); } else if (action == InputDef.UI.Reset) { MessageBox.Show("是否重置为默认?", (ret) => { if (ret) OnResetSettings(); }); } } private void ChangeListSelected(bool up = true) { if (!up) { _nowSelectIndex++; } else { _nowSelectIndex--; } if (_nowSelectIndex < 0) _nowSelectIndex = 0; else if (_nowSelectIndex >= _canSelectIndex.Count) _nowSelectIndex = _canSelectIndex.Count - 1; var targetIndex = _canSelectIndex[_nowSelectIndex]; SetListSelected(targetIndex); } private void SetListSelected(int selectedIndex) { if (selectedIndex >= 0 && selectedIndex < List.numItems) { List.selectedIndex = selectedIndex; List.ScrollToView(List.selectedIndex); } OnClickListItem(); } private void OnClickListItem() { var index = List.selectedIndex; if (index < 0) { return; } var settingItem = List.GetChildAt(index); if (settingItem is SettingItem item) { Introduce.TextName.SetLanguage(item.Option.Name); Introduce.TextIntroduce.SetLanguage(item.Option.Name); // Introduce. } } private void OnBack() { if (Settings.Instance.HaveNotAppleSettings()) { MessageBox.Show("还有未保存的信息", (b) => { if (b) { CancelAndBack(); Hide(); } }); } else { Hide(); } } private void CancelAndBack() { var groupNames = Settings.Instance.GetAllTabs(); foreach (var group in groupNames) { var options = Settings.Instance.GetOptionsByTab(group); foreach (var option in options) { if (option.HaveNotApple()) { option.Cancel(); } } } } protected override void OnHide() { base.OnHide(); Game.Input.OnUICanceled -= OnUICanceled; } protected override void OnDestroy() { base.OnDestroy(); Menu.OnTabChange -= ChangeTab; } } }