123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- using Sirenix.OdinInspector;
- using KairoEngine.UI.Tooltips;
- using KairoEngine.UI.InteractionHandler;
- namespace KairoEngine.UI
- {
- [HideMonoScript]
- public class ColumnMenuUI : MenuUI
- {
-
- [BoxGroup("Settings"), OnInspectorInit("EditorSetup")] public GameObject subMenuPrefab;
- [BoxGroup("Settings")] public GameObject subMenuButtonPrefab;
- [BoxGroup("Settings")] public RectTransform subMenusParent;
- [BoxGroup("Settings")] public bool instantiateMainMenuGroup = true;
- [BoxGroup("Settings")] public bool keepMenuButtonsSelected = false;
- [BoxGroup("Settings")] public bool keepSubMenuButtonsSelected = false;
- private GameObject mainMenu;
- private List<GameObject> subMenus = new List<GameObject>();
- private List<SelectedButton> selectButtons = new List<SelectedButton>();
- public override void Start()
- {
- useSubMenus = true;
- if(handler == null) handler = gameObject.GetComponents<IClickHandler>()[1];
- if(createMenuOnStart)
- {
- DestroyMenu();
- CreateMenu();
- }
- }
- public override void OnClick(string title)
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(buttons[i].title == title)
- {
- if(buttons[i].subMenuParent)
- {
- OpenSubMenu(title);
- SelectParentButton(i);
- }
- if(buttons[i].action != "" && handler != null)
- {
- SelectChildButton(i);
- handler.OnClick(buttons[i].action);
- }
- return;
- }
- }
- }
- public override void CreateMenu() => CreateMenus();
- public void CreateMenus()
- {
- if(instantiateMainMenuGroup) mainMenu = Instantiate(subMenuPrefab, menuParent);
- else mainMenu = menuParent.gameObject;
- for (int i = 0; i < buttons.Count; i++)
- {
- if(string.IsNullOrEmpty(buttons[i].parent))
- {
- var btn = CreateButton(buttons[i], mainMenu.transform, buttonPrefab);
- selectButtons.Add(btn.GetComponent<SelectedButton>());
- buttons[i].button = btn;
- var subMenu = Instantiate(subMenuPrefab, subMenusParent);
- subMenu.name = $"SubMenu {buttons[i].title}";
- for (int a = 0; a < buttons.Count; a++)
- {
- if(buttons[a].parent == buttons[i].title)
- {
- var subBtn = CreateButton(buttons[a], subMenu.transform, subMenuButtonPrefab ?? buttonPrefab);
- selectButtons.Add(subBtn.GetComponent<SelectedButton>());
- buttons[a].button = subBtn;
- }
- }
- subMenu.SetActive(false);
- subMenus.Add(subMenu);
- }
- }
- }
- public override void DestroyMenu() => DestroyMenus();
- public void DestroyMenus()
- {
- for (int i = 0; i < subMenus.Count; i++)
- {
- #if UNITY_EDITOR
- DestroyImmediate(subMenus[i]);
- #else
- Destroy(subMenus[i]);
- #endif
- }
- subMenus = new List<GameObject>();
- #if UNITY_EDITOR
- if(instantiateMainMenuGroup) DestroyImmediate(mainMenu);
- else foreach (var btn in buttons) DestroyImmediate(btn.button.gameObject);
- #else
- if(instantiateMainMenuGroup) Destroy(mainMenu);
- else foreach (var btn in buttons) Destroy(btn.button.gameObject);
- #endif
- buttons = new List<MenuButtomData>();
- }
- public void OpenSubMenu(string title)
- {
- for (int i = 0; i < subMenus.Count; i++)
- {
- if(subMenus[i] == null) continue;
- if($"SubMenu {title}" == subMenus[i].name) subMenus[i].SetActive(true);
- else subMenus[i].SetActive(false);
- }
- }
- public void CloseSubMenus()
- {
- for (int i = 0; i < subMenus.Count; i++)
- {
- if(subMenus[i] != null) subMenus[i].SetActive(false);
- }
- }
- public void EditorSetup()
- {
- useSubMenus = true;
- }
- public void SelectParentButton(int index)
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(i != index && buttons[i].subMenuParent) selectButtons[i].Deselect();
- else if(i == index) selectButtons[i].Select();
- }
- }
- public void SelectChildButton(int index)
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(i != index && !buttons[i].subMenuParent) selectButtons[i].Deselect();
- else if(i == index) selectButtons[i].Select();
- }
- }
- public void DeselectChildButtons()
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(!buttons[i].subMenuParent) selectButtons[i].Deselect();
- }
- }
- public void DeselectAllButtons()
- {
- for (int i = 0; i < buttons.Count; i++) selectButtons[i].Deselect();
- }
- }
- }
|