123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Sirenix.OdinInspector;
- using KairoEngine.UI.InteractionHandler;
- namespace KairoEngine.UI.Encyclopedia
- {
- [HideMonoScript]
- public class EncyclopediaUi : MonoBehaviour
- {
- public EncyclopediaController controller;
- public GameObject menuGameObject;
- public GameObject subMenuGameObject;
- public GameObject viewsGameObject;
- public Transform menuContainer;
- public Transform subMenuContainer;
- public bool createOnStart = true;
- public bool repopulateOnEnable = false;
- public bool showDebug = false;
- public List<EncyclopediaUiView> views;
- private List<SelectedButton> menuButtonSelection;
- private List<SelectedButton> subMenuButtonSelection;
- private int menuIndex = -1;
- private int subMenuIndex = -1;
- private int currentMenuIndex = -1;
- private int currentSubMenuIndex = -1;
- private void Start()
- {
- if(menuGameObject == null) Debug.LogError("Missing Menu GameObject in EncyclopediaUi", this.gameObject);
- if(subMenuGameObject == null) Debug.LogError("Missing submenu GameObject in EncyclopediaUi", this.gameObject);
- if(viewsGameObject == null) Debug.LogError("Missing views GameObject in EncyclopediaUi", this.gameObject);
- if(createOnStart) Populate();
- }
- private void OnEnable()
- {
- if(repopulateOnEnable) Populate();
- }
- [Button("Show Submenu"), ButtonGroup("EditorActions")]
- public void ShowSubmenu() => subMenuGameObject.SetActive(true);
- [Button("Hide Submenu"), ButtonGroup("EditorActions")]
- public void HideSubmenu() => subMenuGameObject.SetActive(false);
- public void Populate()
- {
- if(showDebug) Debug.Log("Creating Encyclopedia UI Element");
- menuIndex = controller.menuIndex;
- subMenuIndex = controller.subMenuIndex;
- currentMenuIndex = controller.menuIndex;
- currentSubMenuIndex = controller.subMenuIndex;
- CreateMenu();
- CreateSubMenu();
- ShowArticle();
- }
- public void CreateMenu()
- {
- if(controller == null || menuContainer == null) return;
- menuButtonSelection = new List<SelectedButton>();
- int counter = 0;
- //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
- Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
- foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
- foreach (EncyclopediaCategory category in controller.categories)
- {
- var obj = Instantiate(controller.categoryButtomPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
- var btnData = obj.GetComponent<ButtonData>();
- if(btnData != null) btnData.Setup(category.title, category.icon);
- var handler = obj.GetComponent<ClickHandler>();
- if(handler != null) handler.Setup(category.id, (IClickHandler)controller);
- var selectedButton = obj.GetComponent<SelectedButton>();
- if(selectedButton != null) menuButtonSelection.Add(selectedButton);
- var categoryDataViews = obj.GetComponentsInChildren<IEncyclopediaCategoryDataView>(true);
- if(categoryDataViews != null) foreach (var view in categoryDataViews) view.Setup(category);
- counter += 1;
- }
- if(menuIndex < menuButtonSelection.Count)
- {
- //menuIndex = controller.menuIndex;
- menuButtonSelection[menuIndex].Select();
- }
- //if(showDebug) Debug.Log($"Created {counter} menu buttons");
- }
- public void CreateSubMenu()
- {
- if(controller == null || subMenuContainer == null) return;
- subMenuButtonSelection = new List<SelectedButton>();
- Button[] oldButtons = subMenuContainer.GetComponentsInChildren<Button>();
- int counter = 0;
- var category = controller.categories[controller.menuIndex];
- var oldCategory = controller.categories[currentMenuIndex];
- category.UpdateArticles();
- Transform container;
- var layout = GetView(category.layoutName);
- ResetViews();
- if(category.showSubMenu)
- {
- container = subMenuContainer.transform;
- ShowSubmenu();
- }
- else
- {
- currentMenuIndex = menuIndex;
- subMenuIndex = 0;
- currentSubMenuIndex = 0;
- container = layout.container;
- HideSubmenu();
- }
- layout.view.SetActive(true);
- //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
- foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
- foreach (EncyclopediaArticle article in category.GetArticles())
- {
- var obj = Instantiate(category.buttonPrefab, new Vector3(), Quaternion.identity, container);
- var btnData = obj.GetComponent<ButtonData>();
- Sprite icon = null;
- if(article.icon != null) icon = article.icon;
- else icon = category.icon;
- if(btnData != null) btnData.Setup(article.title, icon);
- var handler = obj.GetComponent<ClickHandler>();
- if(handler != null) handler.Setup(article.id, (IClickHandler)controller);
- var selectedButton = obj.GetComponent<SelectedButton>();
- if(selectedButton != null) subMenuButtonSelection.Add(selectedButton);
- var contentDataViews = obj.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
- foreach (var item in contentDataViews) item.Setup(article);
- if(article.unlocked == false)
- {
- var btn = obj.GetComponent<Button>();
- if(btn != null) btn.interactable = false;
- if(selectedButton != null) selectedButton.isInteractable = false;
- }
- else if(selectedButton != null) selectedButton.isInteractable = true;
- counter += 1;
- }
- if(category.showSubMenu && !oldCategory.showSubMenu)
- {
- subMenuIndex = 0;
- controller.subMenuIndex = 0;
- subMenuButtonSelection[subMenuIndex].Select();
- ShowArticle();
- }
- if(controller.subMenuIndex < subMenuButtonSelection.Count && menuIndex == currentMenuIndex)
- {
- //subMenuIndex = controller.subMenuIndex;
- subMenuButtonSelection[subMenuIndex].Select();
- }
- //if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
-
- }
- public void ShowArticle()
- {
- if(controller == null) return;
- var category = controller.categories[menuIndex];
- if(!category.showSubMenu) return;
- var articles = category.GetArticles();
- var article = articles[subMenuIndex];
- var layout = GetView(category.layoutName);
- if(layout == null) return;
- HideViews();
- layout.view.SetActive(true);
- var contentDataViews = layout.view.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
- foreach (var item in contentDataViews) item.Setup(article);
- foreach (var btn in subMenuButtonSelection) btn.Deselect();
- subMenuButtonSelection[subMenuIndex].Select();
- currentMenuIndex = menuIndex;
- currentSubMenuIndex = subMenuIndex;
- }
- public void Navigate()
- {
- if(controller == null) return;
- // Navigated to different category
- if(menuIndex != controller.menuIndex)
- {
- menuIndex = controller.menuIndex;
- CreateMenu();
- CreateSubMenu();
- }
- else if (currentMenuIndex != menuIndex && currentSubMenuIndex == subMenuIndex)
- {
- if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
- else subMenuIndex = controller.subMenuIndex;
- ShowArticle();
- }
- if(subMenuIndex != controller.subMenuIndex)
- {
- if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
- else subMenuIndex = controller.subMenuIndex;
- ShowArticle();
- }
- }
- public EncyclopediaUiView GetView(string title)
- {
- for (int i = 0; i < views.Count; i++)
- {
- if(views[i].title == title) return views[i];
- }
- return null;
- }
- public void HideViews()
- {
- foreach (var layout in views) layout.view.SetActive(false);
- }
- public void ResetViews()
- {
- HideViews();
- foreach (var view in views)
- {
- if(view.container != null)
- {
- Transform[] oldListElements = view.container.GetComponentsInChildren<Transform>(true);
- foreach (Transform item in oldListElements)
- if(item != view.container && item != null) DestroyImmediate(item.gameObject);
- }
- }
-
- }
- [System.Serializable]
- public class EncyclopediaUiView
- {
- [HorizontalGroup("Line", 0.4f), HideLabel] public string title;
- [HorizontalGroup("Line", 0.3f), HideLabel] public GameObject view;
- [HorizontalGroup("Line", 0.3f), HideLabel] public Transform container;
- }
- }
- }
|