EncyclopediaUi.cs 9.8 KB

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