123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.UI.InteractionHandler;
- namespace KairoEngine.UI
- {
- /// <summary>
- /// 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.
- /// </summary>
- [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<TabData> tabs = new List<TabData>();
- 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<string> categories = new List<string>();
-
- 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<TabButton>();
- 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<ClickHandler>();
- if(clickHandler != null)
- {
- clickHandler.receiver = this;
- clickHandler.title = tabs[i].id;
- }
- }
- }
- ChangeToTab(currentTab);
- }
- }
- }
|