TabsController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.UI.InteractionHandler;
  6. namespace KairoEngine.UI
  7. {
  8. /// <summary>
  9. /// The tabs controller creates and controls a set of Tabs. Under the hood it uses a MenuUi component to create and control
  10. /// the tab buttons.
  11. /// </summary>
  12. [HideMonoScript, RequireComponent(typeof(MenuUI))]
  13. public class TabsController : MonoBehaviour, IClickHandler
  14. {
  15. [BoxGroup("Settings")] public MenuUI menuUI;
  16. [BoxGroup("Settings")] public int currentTab = 0;
  17. [BoxGroup("Settings")] public bool createOnStart = false;
  18. [BoxGroup("Settings")] public bool debug = false;
  19. public List<TabData> tabs = new List<TabData>();
  20. private void Start()
  21. {
  22. if(createOnStart)
  23. {
  24. DestroyTabMenu();
  25. CreateTabMenu();
  26. }
  27. else LinkButtons();
  28. }
  29. public void OnClick(string id)
  30. {
  31. int tabIndex = 0;
  32. for (int i = 0; i < tabs.Count; i++)
  33. {
  34. if(id == tabs[i].id)
  35. {
  36. tabIndex = i;
  37. break;
  38. }
  39. }
  40. if(tabIndex != currentTab && tabs[tabIndex].content.activeSelf == false) ChangeToTab(tabIndex);
  41. }
  42. public void ChangeToTab(int tabIndex)
  43. {
  44. tabs[currentTab].content.SetActive(false);
  45. tabs[tabIndex].content.SetActive(true);
  46. UpdateButton(tabs[currentTab].id, false);
  47. UpdateButton(tabs[tabIndex].id, true);
  48. currentTab = tabIndex;
  49. if(debug) Debug.Log("Showing tab " + tabs[currentTab].id);
  50. }
  51. [ButtonGroup("Buttons"), Button("Create Tab Menu")]
  52. public void CreateTabMenu()
  53. {
  54. menuUI.DestroyMenu();
  55. menuUI.buttons.Clear();
  56. List<string> categories = new List<string>();
  57. for (int i = 0; i < tabs.Count; i++)
  58. {
  59. var tab = tabs[i];
  60. var data = new MenuButtomData();
  61. data.title = tab.title;
  62. data.description = tab.description;
  63. data.action = tab.id;
  64. data.graphic = tab.icon;
  65. data.subMenuParent = false;
  66. data.showTooltip = true;
  67. data.tooltipType = "";
  68. menuUI.buttons.Add(data);
  69. }
  70. menuUI.CreateMenu();
  71. OnClick(tabs[currentTab].id);
  72. }
  73. [ButtonGroup("Buttons"), Button("Destroy Tab Menu")]
  74. public void DestroyTabMenu()
  75. {
  76. menuUI.DestroyMenu();
  77. menuUI.buttons.Clear();
  78. if(tabs[currentTab] == null) return;
  79. if(tabs[currentTab].content == null) return;
  80. tabs[currentTab].content.SetActive(false);
  81. }
  82. public void UpdateButton(string id, bool activate)
  83. {
  84. var btn = menuUI.GetButton(id);
  85. if(btn != null)
  86. {
  87. TabButton tabButton = btn.GetComponent<TabButton>();
  88. if(tabButton != null)
  89. {
  90. if(activate) tabButton.Activate();
  91. else tabButton.Deactivate();
  92. }
  93. else
  94. {
  95. if(debug) Debug.LogError("Could not find TabButton component in button", btn);
  96. }
  97. }
  98. else
  99. {
  100. if(debug) Debug.LogError("Could not find button with id " + id + " in MenuUI", this.gameObject);
  101. }
  102. }
  103. public void LinkButtons()
  104. {
  105. for (int i = 0; i < menuUI.buttons.Count; i++)
  106. {
  107. if(menuUI.buttons[i].button != null)
  108. {
  109. var clickHandler = menuUI.buttons[i].button.GetComponent<ClickHandler>();
  110. if(clickHandler != null)
  111. {
  112. clickHandler.receiver = this;
  113. clickHandler.title = tabs[i].id;
  114. }
  115. }
  116. }
  117. ChangeToTab(currentTab);
  118. }
  119. }
  120. }