TabsController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  28. public void OnClick(string id)
  29. {
  30. TabData tab = null;
  31. int tabIndex = 0;
  32. for (int i = 0; i < tabs.Count; i++)
  33. {
  34. if(id == tabs[i].id)
  35. {
  36. tab = tabs[i];
  37. tabIndex = i;
  38. break;
  39. }
  40. }
  41. if(tab != null && (tabIndex != currentTab || tabs[tabIndex].content.activeSelf == false))
  42. {
  43. tabs[currentTab].content.SetActive(false);
  44. tab.content.SetActive(true);
  45. UpdateButton(tabs[currentTab].id, false);
  46. UpdateButton(tabs[tabIndex].id, true);
  47. currentTab = tabIndex;
  48. if(debug) Debug.Log("Showing tab " + tabs[currentTab].id);
  49. }
  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. tabs[currentTab].content.SetActive(false);
  79. }
  80. public void UpdateButton(string id, bool activate)
  81. {
  82. var btn = menuUI.GetButton(id);
  83. if(btn != null)
  84. {
  85. TabButton tabButton = btn.GetComponent<TabButton>();
  86. if(tabButton != null)
  87. {
  88. if(activate) tabButton.Activate();
  89. else tabButton.Deactivate();
  90. }
  91. else
  92. {
  93. if(debug) Debug.LogError("Could not find TabButton component in button", btn);
  94. }
  95. }
  96. else
  97. {
  98. if(debug) Debug.LogError("Could not find button with id " + id + " in MenuUI", this.gameObject);
  99. }
  100. }
  101. }
  102. }