using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.UI.InteractionHandler; namespace KairoEngine.UI { /// /// The tabs controller creates and controls a set of Tabs. Under the hood it uses a MenuUi component to create and control /// the tab buttons. /// [HideMonoScript, RequireComponent(typeof(MenuUI))] public class TabsController : MonoBehaviour, IClickHandler { [BoxGroup("Settings")] public MenuUI menuUI; [BoxGroup("Settings")] public int currentTab = 0; [BoxGroup("Settings")] public bool createOnStart = false; [BoxGroup("Settings")] public bool debug = false; public List tabs = new List(); private void Start() { if(createOnStart) { DestroyTabMenu(); CreateTabMenu(); } else LinkButtons(); } public void OnClick(string id) { int tabIndex = 0; for (int i = 0; i < tabs.Count; i++) { if(id == tabs[i].id) { tabIndex = i; break; } } if(tabIndex != currentTab && tabs[tabIndex].content.activeSelf == false) ChangeToTab(tabIndex); } public void ChangeToTab(int tabIndex) { tabs[currentTab].content.SetActive(false); tabs[tabIndex].content.SetActive(true); UpdateButton(tabs[currentTab].id, false); UpdateButton(tabs[tabIndex].id, true); currentTab = tabIndex; if(debug) Debug.Log("Showing tab " + tabs[currentTab].id); } [ButtonGroup("Buttons"), Button("Create Tab Menu")] public void CreateTabMenu() { menuUI.DestroyMenu(); menuUI.buttons.Clear(); List categories = new List(); for (int i = 0; i < tabs.Count; i++) { var tab = tabs[i]; var data = new MenuButtomData(); data.title = tab.title; data.description = tab.description; data.action = tab.id; data.graphic = tab.icon; data.subMenuParent = false; data.showTooltip = true; data.tooltipType = ""; menuUI.buttons.Add(data); } menuUI.CreateMenu(); OnClick(tabs[currentTab].id); } [ButtonGroup("Buttons"), Button("Destroy Tab Menu")] public void DestroyTabMenu() { menuUI.DestroyMenu(); menuUI.buttons.Clear(); if(tabs[currentTab] == null) return; if(tabs[currentTab].content == null) return; tabs[currentTab].content.SetActive(false); } public void UpdateButton(string id, bool activate) { var btn = menuUI.GetButton(id); if(btn != null) { TabButton tabButton = btn.GetComponent(); if(tabButton != null) { if(activate) tabButton.Activate(); else tabButton.Deactivate(); } else { if(debug) Debug.LogError("Could not find TabButton component in button", btn); } } else { if(debug) Debug.LogError("Could not find button with id " + id + " in MenuUI", this.gameObject); } } public void LinkButtons() { for (int i = 0; i < menuUI.buttons.Count; i++) { if(menuUI.buttons[i].button != null) { var clickHandler = menuUI.buttons[i].button.GetComponent(); if(clickHandler != null) { clickHandler.receiver = this; clickHandler.title = tabs[i].id; } } } ChangeToTab(currentTab); } } }