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 _controls = new List(); private readonly Dictionary _options = new Dictionary(); 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 option in _options) { option.Value.SelectionModeEnabled = isEnabled; } if (isEnabled) { return; } foreach (KeyValuePair 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> dictionary = new Dictionary>(); foreach (OptionDefinition option in Service.Options.Options) { List value; if (!dictionary.TryGetValue(option.Category, out value)) { value = new List(); dictionary.Add(option.Category, value); } value.Add(option); } bool flag = false; foreach (KeyValuePair> 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 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); } } } }