SRTabController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. namespace SRDebugger.UI.Other
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using Controls;
  6. using SRF;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. public class SRTabController : SRMonoBehaviourEx
  10. {
  11. private readonly SRList<SRTab> _tabs = new SRList<SRTab>();
  12. private SRTab _activeTab;
  13. [RequiredField] public RectTransform TabButtonContainer;
  14. [RequiredField] public SRTabButton TabButtonPrefab;
  15. [RequiredField] public RectTransform TabContentsContainer;
  16. [RequiredField] public RectTransform TabHeaderContentContainer;
  17. [RequiredField] public Text TabHeaderText;
  18. public SRTab ActiveTab
  19. {
  20. get { return _activeTab; }
  21. set { MakeActive(value); }
  22. }
  23. public IList<SRTab> Tabs
  24. {
  25. get { return _tabs.AsReadOnly(); }
  26. }
  27. public event Action<SRTabController, SRTab> ActiveTabChanged;
  28. public void AddTab(SRTab tab, bool visibleInSidebar = true)
  29. {
  30. tab.CachedTransform.SetParent(TabContentsContainer, false);
  31. tab.CachedGameObject.SetActive(false);
  32. if (visibleInSidebar)
  33. {
  34. // Create a tab button for this tab
  35. var button = SRInstantiate.Instantiate(TabButtonPrefab);
  36. button.CachedTransform.SetParent(TabButtonContainer, false);
  37. button.TitleText.text = tab.Title.ToUpper();
  38. if (tab.IconExtraContent != null)
  39. {
  40. var extraContent = SRInstantiate.Instantiate(tab.IconExtraContent);
  41. extraContent.SetParent(button.ExtraContentContainer, false);
  42. }
  43. button.IconStyleComponent.StyleKey = tab.IconStyleKey;
  44. button.IsActive = false;
  45. button.Button.onClick.AddListener(() => MakeActive(tab));
  46. tab.TabButton = button;
  47. }
  48. _tabs.Add(tab);
  49. SortTabs();
  50. if (_tabs.Count == 1)
  51. {
  52. ActiveTab = tab;
  53. }
  54. }
  55. private void MakeActive(SRTab tab)
  56. {
  57. if (!_tabs.Contains(tab))
  58. {
  59. throw new ArgumentException("tab is not a member of this tab controller", "tab");
  60. }
  61. if (_activeTab != null)
  62. {
  63. _activeTab.CachedGameObject.SetActive(false);
  64. if (_activeTab.TabButton != null)
  65. {
  66. _activeTab.TabButton.IsActive = false;
  67. }
  68. if (_activeTab.HeaderExtraContent != null)
  69. {
  70. _activeTab.HeaderExtraContent.gameObject.SetActive(false);
  71. }
  72. }
  73. _activeTab = tab;
  74. if (_activeTab != null)
  75. {
  76. _activeTab.CachedGameObject.SetActive(true);
  77. TabHeaderText.text = _activeTab.LongTitle;
  78. if (_activeTab.TabButton != null)
  79. {
  80. _activeTab.TabButton.IsActive = true;
  81. }
  82. if (_activeTab.HeaderExtraContent != null)
  83. {
  84. _activeTab.HeaderExtraContent.SetParent(TabHeaderContentContainer, false);
  85. _activeTab.HeaderExtraContent.gameObject.SetActive(true);
  86. }
  87. }
  88. if (ActiveTabChanged != null)
  89. {
  90. ActiveTabChanged(this, _activeTab);
  91. }
  92. }
  93. private void SortTabs()
  94. {
  95. _tabs.Sort((t1, t2) => t1.SortIndex.CompareTo(t2.SortIndex));
  96. for (var i = 0; i < _tabs.Count; i++)
  97. {
  98. if (_tabs[i].TabButton != null)
  99. {
  100. _tabs[i].TabButton.CachedTransform.SetSiblingIndex(i);
  101. }
  102. }
  103. }
  104. }
  105. }