Files
Fishing2/Assets/Scripts/UI/Settings/SettingPanel.cs
2025-06-03 14:28:20 +08:00

217 lines
5.8 KiB
C#

// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖
using System.Collections.Generic;
using UnityEngine;
using NBC;
using Unity.VisualScripting;
namespace NBF
{
public partial class SettingPanel : UIPanel
{
public override string UIPackName => "Main";
public override string UIResName => "SettingPanel";
private List<TabListData> tabList = new List<TabListData>();
private string _currentGroup = "";
protected override void OnInit()
{
base.OnInit();
IsShowCursor = true;
var groupNames = Settings.Instance.GetAllGroups();
foreach (var group in groupNames)
{
var tab = new TabListData
{
Name = group
};
tabList.Add(tab);
}
Menu.OnTabChange += ChangeTab;
List.onClickItem.Set(OnClickListItem);
}
protected override void OnShow()
{
Settings.Instance.LoadAllSettings();
InputManager.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];
_currentGroup = tabListData.Name;
ResetSettingList();
}
private void ResetSettingList()
{
TextTitle.text = Lan.Get(_currentGroup);
List.RemoveChildrenToPool();
if (string.IsNullOrEmpty(_currentGroup)) return;
var options = Settings.Instance.GetOptionsByGroup(_currentGroup);
foreach (var option in options)
{
if (List.AddItemFromPool() is SettingItem item)
{
item.SetData(option);
}
}
}
private void UseBottomMenu()
{
BottomMenu.OnTab += () =>
{
var i = Random.Range(1, 13);
if (i < 3)
{
Notices.Success("离开晶科科技看就看");
}
else if (i < 6)
{
Notices.Warning("离开晶科科技看就看");
}
else if (i < 9)
{
Notices.Error("离开晶科科技看就看");
}
else if (i < 12)
{
Notices.Info("离开晶科科技看就看");
}
};
BottomMenu.OnBack += OnBack;
BottomMenu.OnEnter += OnApplySettings;
BottomMenu.Use();
}
private void OnApplySettings()
{
var options = Settings.Instance.GetOptionsByGroup(_currentGroup);
foreach (var option in options)
{
option.Apply();
}
}
private void OnResetSettings()
{
var options = Settings.Instance.GetOptionsByGroup(_currentGroup);
foreach (var option in options)
{
option.Reset();
}
}
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)
{
SetListSelected(List.selectedIndex - 1);
}
else if (action == InputDef.UI.Down)
{
if (List.selectedIndex < 0)
{
SetListSelected(0);
}
else
{
SetListSelected(List.selectedIndex + 1);
}
}
}
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.text = Lan.Get(item.Option.Name);
Introduce.TextIntroduce.text = Lan.Get(item.Option.Name);
// Introduce.
}
}
private void OnBack()
{
if (Settings.Instance.HaveNotAppleSettings())
{
MessageBox.Show("还有未保存的信息", (b) =>
{
if (b)
{
Hide();
}
});
}
else
{
Hide();
}
}
protected override void OnHide()
{
base.OnHide();
BottomMenu.OnBack -= OnBack;
BottomMenu.OnTab -= OnResetSettings;
BottomMenu.OnEnter -= OnApplySettings;
BottomMenu.UnUse();
InputManager.OnUICanceled -= OnUICanceled;
}
protected override void OnDestroy()
{
base.OnDestroy();
Menu.OnTabChange -= ChangeTab;
}
}
}