Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active November 21, 2024 13:30
Show Gist options
  • Save baobao/5cd6a74a07bd663387a050d08e1c74c7 to your computer and use it in GitHub Desktop.
Save baobao/5cd6a74a07bd663387a050d08e1c74c7 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using SRDebugger;
using SRDebugger.Internal;
using SRDebugger.UI.Controls;
using SRDebugger.UI.Tabs;
using UnityEngine;
public abstract class CustomTabControllerBase : OptionsTabController
{
public abstract IOptions Option { get; }
private bool _requestRefresh;
protected override void OnDestroy()
{
base.OnDestroy();
PinButton.onValueChanged.RemoveListener(SetSelectionModeEnabled);
if (Option != null)
{
Option.PropertyChanged -= SROptionPropertyChanged;
}
}
protected override void Start()
{
PinButton.onValueChanged.AddListener(SetSelectionModeEnabled);
PinPromptText.SetActive(false);
PinPromptSpacer.SetActive(false);
PopulateWrapper();
if (Option != null)
{
Option.PropertyChanged -= SROptionPropertyChanged;
Option.PropertyChanged += SROptionPropertyChanged;
}
}
private void PopulateWrapper()
{
var definitionMap = new Dictionary<string, List<OptionDefinition>>();
if (Option == null) return;
foreach (var option in SRDebuggerUtil.ScanForOptions(Option))
{
if (!definitionMap.TryGetValue(option.Category, out var definitionList))
{
definitionList = new List<OptionDefinition>();
definitionMap.Add(option.Category, definitionList);
}
definitionList.Add(option);
}
var isCreated = false;
foreach (var kv in definitionMap)
{
if (kv.Value.Count == 0)
{
continue;
}
isCreated = true;
CreateCategory(kv.Key, kv.Value);
}
if (isCreated)
{
NoOptionsNotice.SetActive(false);
}
}
protected void SROptionPropertyChanged(object sender, string propertyName) => _requestRefresh = true;
protected override void Update()
{
if (_requestRefresh)
{
_requestRefresh = false;
Refresh();
}
}
private void Refresh()
{
foreach (var control in GetComponentsInChildren<OptionsControlBase>(true)) control.Refresh();
}
}
public interface IOptions
{
event SROptionsPropertyChanged PropertyChanged;
}
public abstract class SROptionsSingleton<T> : IOptions where T : IOptions, new()
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Clear() => _instance = default;
private static T _instance;
public static T Instance => _instance != null ? _instance : _instance = new T();
public event SROptionsPropertyChanged PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, propertyName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment