Browse Source

Added list view to encyclopedia UI

James Peret 2 years ago
parent
commit
0bf81246dd

+ 1 - 0
Runtime/Encyclopedia/DefaultEncyclopediaArticle.cs

@@ -24,6 +24,7 @@ namespace KairoEngine.UI.Encyclopedia
             article.description = description;
             article.icon = icon;
             article.content.Add("title", title);
+            article.content.Add("description", description);
             article.content.Add("author", author);
             article.content.Add("date", date);
             article.content.Add("text", text);

+ 50 - 6
Runtime/Encyclopedia/EncyclopediaUi.cs

@@ -61,9 +61,9 @@ namespace KairoEngine.UI.Encyclopedia
         {
             if(controller == null || menuContainer == null) return;
             menuButtonSelection = new List<SelectedButton>();
-            Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
             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)
             {
@@ -92,13 +92,31 @@ namespace KairoEngine.UI.Encyclopedia
             subMenuButtonSelection = new List<SelectedButton>();
             Button[] oldButtons = subMenuContainer.GetComponentsInChildren<Button>();
             int counter = 0;
-            var category = controller.categories[controller.menuIndex];
+            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, subMenuContainer.transform);
+                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;
@@ -114,23 +132,33 @@ namespace KairoEngine.UI.Encyclopedia
                 {
                     var btn = obj.GetComponent<Button>();
                     if(btn != null) btn.interactable = false;
-                    selectedButton.isInteractable = false;
+                    if(selectedButton != null) selectedButton.isInteractable = false;
                 }
-                else selectedButton.isInteractable = true;
+                else if(selectedButton != null) selectedButton.isInteractable = true;
                 counter += 1;
             }
+            if(category.showSubMenu && !oldCategory.showSubMenu)
+            {
+                Debug.Log("Last view was a list");
+                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);
@@ -183,11 +211,27 @@ namespace KairoEngine.UI.Encyclopedia
             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.6f), HideLabel] public GameObject view;
+            [HorizontalGroup("Line", 0.3f), HideLabel] public GameObject view;
+            [HorizontalGroup("Line", 0.3f), HideLabel] public Transform container;
         }
     }
 }