Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/SRDebugger/UI/Tabs/OptionsTabController.cs
2026-02-21 16:45:37 +08:00

189 lines
4.7 KiB
C#

using System.Collections.Generic;
using SRDebugger.Internal;
using SRDebugger.Services;
using SRDebugger.UI.Controls;
using SRDebugger.UI.Controls.Data;
using SRDebugger.UI.Other;
using SRF;
using UnityEngine;
using UnityEngine.UI;
namespace SRDebugger.UI.Tabs
{
public class OptionsTabController : SRMonoBehaviourEx
{
private readonly List<OptionsControlBase> _controls = new List<OptionsControlBase>();
private readonly Dictionary<OptionDefinition, OptionsControlBase> _options = new Dictionary<OptionDefinition, OptionsControlBase>();
private bool _queueRefresh;
private bool _selectionModeEnabled;
[RequiredField]
public ActionControl ActionControlPrefab;
[RequiredField]
public CategoryGroup CategoryGroupPrefab;
[RequiredField]
public RectTransform ContentContainer;
[RequiredField]
public GameObject NoOptionsNotice;
[RequiredField]
public Toggle PinButton;
[RequiredField]
public GameObject PinPromptSpacer;
[RequiredField]
public GameObject PinPromptText;
protected override void Start()
{
base.Start();
PinButton.onValueChanged.AddListener(SetSelectionModeEnabled);
PinPromptText.SetActive(false);
PinPromptSpacer.SetActive(false);
Populate();
SROptions.Current.PropertyChanged += SROptionPropertyChanged;
}
private void SROptionPropertyChanged(object sender, string propertyName)
{
_queueRefresh = true;
}
protected override void OnEnable()
{
base.OnEnable();
Service.Panel.VisibilityChanged += PanelOnVisibilityChanged;
}
protected override void OnDisable()
{
SetSelectionModeEnabled(false);
if (Service.Panel != null)
{
Service.Panel.VisibilityChanged -= PanelOnVisibilityChanged;
}
base.OnDisable();
}
protected override void Update()
{
base.Update();
if (_queueRefresh)
{
_queueRefresh = false;
Refresh();
}
}
private void PanelOnVisibilityChanged(IDebugPanelService debugPanelService, bool b)
{
if (!b)
{
SetSelectionModeEnabled(false);
Refresh();
}
else if (b && base.CachedGameObject.activeInHierarchy)
{
Refresh();
}
}
public void SetSelectionModeEnabled(bool isEnabled)
{
if (_selectionModeEnabled == isEnabled)
{
return;
}
_selectionModeEnabled = isEnabled;
PinButton.isOn = isEnabled;
PinPromptText.SetActive(isEnabled);
PinPromptSpacer.SetActive(isEnabled);
foreach (KeyValuePair<OptionDefinition, OptionsControlBase> option in _options)
{
option.Value.SelectionModeEnabled = isEnabled;
}
if (isEnabled)
{
return;
}
foreach (KeyValuePair<OptionDefinition, OptionsControlBase> option2 in _options)
{
OptionsControlBase value = option2.Value;
if (value.IsSelected && !Service.PinnedUI.HasPinned(option2.Key))
{
Service.PinnedUI.Pin(option2.Key);
}
else if (!value.IsSelected && Service.PinnedUI.HasPinned(option2.Key))
{
Service.PinnedUI.Unpin(option2.Key);
}
}
}
private void Refresh()
{
for (int i = 0; i < _options.Count; i++)
{
_controls[i].Refresh();
}
}
protected void Populate()
{
Dictionary<string, List<OptionDefinition>> dictionary = new Dictionary<string, List<OptionDefinition>>();
foreach (OptionDefinition option in Service.Options.Options)
{
List<OptionDefinition> value;
if (!dictionary.TryGetValue(option.Category, out value))
{
value = new List<OptionDefinition>();
dictionary.Add(option.Category, value);
}
value.Add(option);
}
bool flag = false;
foreach (KeyValuePair<string, List<OptionDefinition>> item in dictionary)
{
if (item.Value.Count != 0)
{
flag = true;
CreateCategory(item.Key, item.Value);
}
}
if (flag)
{
NoOptionsNotice.SetActive(false);
}
}
protected void CreateCategory(string title, List<OptionDefinition> options)
{
options.Sort((OptionDefinition d1, OptionDefinition d2) => d1.SortPriority.CompareTo(d2.SortPriority));
CategoryGroup categoryGroup = SRInstantiate.Instantiate(CategoryGroupPrefab);
categoryGroup.CachedTransform.SetParent(ContentContainer, false);
categoryGroup.Header.text = title;
foreach (OptionDefinition option in options)
{
OptionsControlBase optionsControlBase = OptionControlFactory.CreateControl(option, title);
if (optionsControlBase == null)
{
Debug.LogError("[SRDebugger.OptionsTab] Failed to create option control for {0}".Fmt(option.Name));
continue;
}
optionsControlBase.CachedTransform.SetParent(categoryGroup.Container, false);
optionsControlBase.IsSelected = Service.PinnedUI.HasPinned(option);
optionsControlBase.SelectionModeEnabled = false;
_options.Add(option, optionsControlBase);
_controls.Add(optionsControlBase);
}
}
}
}