EncyclopediaUi.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.UI.InteractionHandler;
  7. namespace KairoEngine.UI.Encyclopedia
  8. {
  9. [HideMonoScript]
  10. public class EncyclopediaUi : MonoBehaviour
  11. {
  12. public EncyclopediaController controller;
  13. public GameObject menuGameObject;
  14. public GameObject subMenuGameObject;
  15. public GameObject viewsGameObject;
  16. public Transform menuContainer;
  17. public Transform subMenuContainer;
  18. public bool createOnStart = true;
  19. public bool repopulateOnEnable = false;
  20. public bool showDebug = false;
  21. public List<EncyclopediaUiView> views;
  22. private List<SelectedButton> menuButtonSelection;
  23. private List<SelectedButton> subMenuButtonSelection;
  24. private int menuIndex = -1;
  25. private int subMenuIndex = -1;
  26. private int currentMenuIndex = -1;
  27. private int currentSubMenuIndex = -1;
  28. private void Start()
  29. {
  30. if(menuGameObject == null) Debug.LogError("Missing Menu GameObject in EncyclopediaUi", this.gameObject);
  31. if(subMenuGameObject == null) Debug.LogError("Missing submenu GameObject in EncyclopediaUi", this.gameObject);
  32. if(viewsGameObject == null) Debug.LogError("Missing views GameObject in EncyclopediaUi", this.gameObject);
  33. if(createOnStart) Populate();
  34. }
  35. private void OnEnable()
  36. {
  37. if(repopulateOnEnable) Populate();
  38. }
  39. [Button("Show Submenu"), ButtonGroup("EditorActions")]
  40. public void ShowSubmenu() => subMenuGameObject.SetActive(true);
  41. [Button("Hide Submenu"), ButtonGroup("EditorActions")]
  42. public void HideSubmenu() => subMenuGameObject.SetActive(false);
  43. public void Populate()
  44. {
  45. if(showDebug) Debug.Log("Creating Encyclopedia UI Element");
  46. menuIndex = controller.menuIndex;
  47. subMenuIndex = controller.subMenuIndex;
  48. currentMenuIndex = controller.menuIndex;
  49. currentSubMenuIndex = controller.subMenuIndex;
  50. CreateMenu();
  51. CreateSubMenu();
  52. ShowArticle();
  53. }
  54. public void CreateMenu()
  55. {
  56. if(controller == null || menuContainer == null) return;
  57. menuButtonSelection = new List<SelectedButton>();
  58. int counter = 0;
  59. //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
  60. Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
  61. foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
  62. foreach (EncyclopediaCategory category in controller.categories)
  63. {
  64. var obj = Instantiate(controller.categoryButtomPrefab, new Vector3(), Quaternion.identity, menuContainer.transform);
  65. var btnData = obj.GetComponent<ButtonData>();
  66. if(btnData != null) btnData.Setup(category.title, category.icon);
  67. var handler = obj.GetComponent<ClickHandler>();
  68. if(handler != null) handler.Setup(category.id, (IClickHandler)controller);
  69. var selectedButton = obj.GetComponent<SelectedButton>();
  70. if(selectedButton != null) menuButtonSelection.Add(selectedButton);
  71. var categoryDataViews = obj.GetComponentsInChildren<IEncyclopediaCategoryDataView>(true);
  72. if(categoryDataViews != null) foreach (var view in categoryDataViews) view.Setup(category);
  73. counter += 1;
  74. }
  75. if(menuIndex < menuButtonSelection.Count)
  76. {
  77. //menuIndex = controller.menuIndex;
  78. menuButtonSelection[menuIndex].Select();
  79. }
  80. //if(showDebug) Debug.Log($"Created {counter} menu buttons");
  81. }
  82. public void CreateSubMenu()
  83. {
  84. if(controller == null || subMenuContainer == null) return;
  85. subMenuButtonSelection = new List<SelectedButton>();
  86. Button[] oldButtons = subMenuContainer.GetComponentsInChildren<Button>();
  87. int counter = 0;
  88. var category = controller.categories[controller.menuIndex];
  89. var oldCategory = controller.categories[currentMenuIndex];
  90. category.UpdateArticles();
  91. Transform container;
  92. var layout = GetView(category.layoutName);
  93. ResetViews();
  94. if(category.showSubMenu)
  95. {
  96. container = subMenuContainer.transform;
  97. ShowSubmenu();
  98. }
  99. else
  100. {
  101. currentMenuIndex = menuIndex;
  102. subMenuIndex = 0;
  103. currentSubMenuIndex = 0;
  104. container = layout.container;
  105. HideSubmenu();
  106. }
  107. layout.view.SetActive(true);
  108. //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
  109. foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
  110. foreach (EncyclopediaArticle article in category.GetArticles())
  111. {
  112. var obj = Instantiate(category.buttonPrefab, new Vector3(), Quaternion.identity, container);
  113. var btnData = obj.GetComponent<ButtonData>();
  114. Sprite icon = null;
  115. if(article.icon != null) icon = article.icon;
  116. else icon = category.icon;
  117. if(btnData != null) btnData.Setup(article.title, icon);
  118. var handler = obj.GetComponent<ClickHandler>();
  119. if(handler != null) handler.Setup(article.id, (IClickHandler)controller);
  120. var selectedButton = obj.GetComponent<SelectedButton>();
  121. if(selectedButton != null) subMenuButtonSelection.Add(selectedButton);
  122. var contentDataViews = obj.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
  123. foreach (var item in contentDataViews) item.Setup(article);
  124. if(article.unlocked == false)
  125. {
  126. var btn = obj.GetComponent<Button>();
  127. if(btn != null) btn.interactable = false;
  128. if(selectedButton != null) selectedButton.isInteractable = false;
  129. }
  130. else if(selectedButton != null) selectedButton.isInteractable = true;
  131. counter += 1;
  132. }
  133. if(category.showSubMenu && !oldCategory.showSubMenu)
  134. {
  135. subMenuIndex = 0;
  136. controller.subMenuIndex = 0;
  137. subMenuButtonSelection[subMenuIndex].Select();
  138. ShowArticle();
  139. }
  140. if(controller.subMenuIndex < subMenuButtonSelection.Count && menuIndex == currentMenuIndex)
  141. {
  142. //subMenuIndex = controller.subMenuIndex;
  143. subMenuButtonSelection[subMenuIndex].Select();
  144. }
  145. //if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
  146. }
  147. public void ShowArticle()
  148. {
  149. if(controller == null) return;
  150. var category = controller.categories[menuIndex];
  151. if(!category.showSubMenu) return;
  152. var articles = category.GetArticles();
  153. var article = articles[subMenuIndex];
  154. var layout = GetView(category.layoutName);
  155. if(layout == null) return;
  156. HideViews();
  157. layout.view.SetActive(true);
  158. var contentDataViews = layout.view.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
  159. foreach (var item in contentDataViews) item.Setup(article);
  160. foreach (var btn in subMenuButtonSelection) btn.Deselect();
  161. subMenuButtonSelection[subMenuIndex].Select();
  162. currentMenuIndex = menuIndex;
  163. currentSubMenuIndex = subMenuIndex;
  164. }
  165. public void Navigate()
  166. {
  167. if(controller == null) return;
  168. // Navigated to different category
  169. if(menuIndex != controller.menuIndex)
  170. {
  171. menuIndex = controller.menuIndex;
  172. CreateMenu();
  173. CreateSubMenu();
  174. }
  175. else if (currentMenuIndex != menuIndex && currentSubMenuIndex == subMenuIndex)
  176. {
  177. if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
  178. else subMenuIndex = controller.subMenuIndex;
  179. ShowArticle();
  180. }
  181. if(subMenuIndex != controller.subMenuIndex)
  182. {
  183. if(controller.subMenuIndex > controller.categories[menuIndex].GetArticles().Count) return;
  184. else subMenuIndex = controller.subMenuIndex;
  185. ShowArticle();
  186. }
  187. }
  188. public EncyclopediaUiView GetView(string title)
  189. {
  190. for (int i = 0; i < views.Count; i++)
  191. {
  192. if(views[i].title == title) return views[i];
  193. }
  194. return null;
  195. }
  196. public void HideViews()
  197. {
  198. foreach (var layout in views) layout.view.SetActive(false);
  199. }
  200. public void ResetViews()
  201. {
  202. HideViews();
  203. foreach (var view in views)
  204. {
  205. if(view.container != null)
  206. {
  207. Transform[] oldListElements = view.container.GetComponentsInChildren<Transform>(true);
  208. foreach (Transform item in oldListElements)
  209. if(item != view.container && item != null) DestroyImmediate(item.gameObject);
  210. }
  211. }
  212. }
  213. [System.Serializable]
  214. public class EncyclopediaUiView
  215. {
  216. [HorizontalGroup("Line", 0.4f), HideLabel] public string title;
  217. [HorizontalGroup("Line", 0.3f), HideLabel] public GameObject view;
  218. [HorizontalGroup("Line", 0.3f), HideLabel] public Transform container;
  219. }
  220. }
  221. }