Browse Source

Added interface for scripts to receive Encyclopedia Article data

James Peret 2 years ago
parent
commit
380b282167

+ 50 - 5
Runtime/Encyclopedia/EncyclopediaArticleUi.cs

@@ -6,17 +6,62 @@ using TMPro;
 using Sirenix.OdinInspector;
 
 [HideMonoScript]
-public class EncyclopediaArticleUi : MonoBehaviour
+public class EncyclopediaArticleUi : MonoBehaviour, IEncyclopediaArticleDataView
 {
+    public enum UiType
+    {
+        String,
+        Integer,
+        Float,
+        Image,
+    }
+    
     public string articleDataKey = "title";
-    public TextMeshProUGUI text;
+    public UiType uiType = UiType.String;
+    [ShowIf("@uiType != UiType.Image")] public TextMeshProUGUI textMeshPro;
+    [ShowIf("@uiType == UiType.Image")] public Image imageComponent;
     public bool showDebug = false;
 
     public void Setup(EncyclopediaArticle article)
     {
-        article.content.TryGetValue(articleDataKey, out string result);
-        text.text = result;
-        if(showDebug && result == null) Debug.Log($"Article did not contain content key {articleDataKey}. Content key count: {article.content.Keys.Count}");
+        string result = "";
+        Sprite spriteImage = null;
+        switch (uiType)
+        {
+            case UiType.String:
+                article.content.TryGetValue(articleDataKey, out result);
+                break;
+            case UiType.Integer:
+                article.integers.TryGetValue(articleDataKey, out int intValue);
+                result += intValue;
+                break;
+            case UiType.Float:
+                article.floats.TryGetValue(articleDataKey, out float floatValue);
+                result += floatValue;
+                break;
+            case UiType.Image:
+                article.images.TryGetValue(articleDataKey, out spriteImage);
+                break;
+            default:
+                break;
+        }
+        if(uiType != UiType.Image && textMeshPro != null) 
+        {
+            textMeshPro.text = result;
+            if(imageComponent != null) imageComponent.gameObject.SetActive(false);
+        }
+        else if(uiType == UiType.Image && imageComponent != null) 
+        {
+            if(spriteImage != null)
+            {
+                imageComponent.sprite = spriteImage;
+                imageComponent.gameObject.SetActive(true);
+            }
+            else imageComponent.gameObject.SetActive(false);
+            
+        }
+        if(showDebug && (result == null || result == "")) 
+            Debug.Log($"Article did not contain content key {articleDataKey}. Content key count: {article.content.Keys.Count}", this.gameObject);
     }
 
 }

+ 1 - 1
Runtime/Encyclopedia/EncyclopediaCategory.cs

@@ -20,7 +20,7 @@ public class EncyclopediaCategory : MonoBehaviour
 
     public virtual void UpdateArticles()
     {
-        
+        Debug.Log("Wrong class, no article here.");
     }
 
 }

+ 2 - 1
Runtime/Encyclopedia/EncyclopediaController.cs

@@ -17,7 +17,8 @@ public class EncyclopediaController : MonoBehaviour, IClickHandler
     [Button("Build Menu"), ButtonGroup("Buttons")]
     public void BuildMenu()
     {
-        Navigate();
+        if(encyclopediaUi != null) encyclopediaUi.Populate();
+        else Debug.LogError("Missing Encyclopedia UI in Encyclopedia Controller", this.gameObject);
     }
 
     public void OnClick(string id)

+ 8 - 5
Runtime/Encyclopedia/EncyclopediaUi.cs

@@ -45,6 +45,7 @@ public class EncyclopediaUi : MonoBehaviour
 
     public void Populate()
     {
+        if(showDebug) Debug.Log("Creating Encyclopedia UI Element");
         menuIndex = controller.menuIndex;
         subMenuIndex = controller.subMenuIndex;
         currentMenuIndex = controller.menuIndex;
@@ -60,7 +61,7 @@ public class EncyclopediaUi : MonoBehaviour
         menuButtonSelection = new List<SelectedButton>();
         Button[] oldButtons = menuContainer.GetComponentsInChildren<Button>();
         int counter = 0;
-        if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
+        //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} menu buttons");
         foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
         foreach (EncyclopediaCategory category in controller.categories)
         {
@@ -78,7 +79,7 @@ public class EncyclopediaUi : MonoBehaviour
             //menuIndex = controller.menuIndex;
             menuButtonSelection[menuIndex].Select();
         }
-        if(showDebug) Debug.Log($"Created {counter} menu buttons");
+        //if(showDebug) Debug.Log($"Created {counter} menu buttons");
     }
 
     public void CreateSubMenu()
@@ -89,7 +90,7 @@ public class EncyclopediaUi : MonoBehaviour
         int counter = 0;
         var category = controller.categories[controller.menuIndex];
         category.UpdateArticles();
-        if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
+        //if(showDebug) Debug.Log($"Destroying {oldButtons.Length} sub menu buttons");
         foreach (Button item in oldButtons) DestroyImmediate(item.gameObject);
         foreach (EncyclopediaArticle article in category.GetArticles())
         {
@@ -110,7 +111,7 @@ public class EncyclopediaUi : MonoBehaviour
             //subMenuIndex = controller.subMenuIndex;
             subMenuButtonSelection[subMenuIndex].Select();
         }
-        if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
+        //if(showDebug) Debug.Log($"Created {counter} sub menu buttons");
     }
 
     public void ShowArticle()
@@ -123,7 +124,9 @@ public class EncyclopediaUi : MonoBehaviour
         if(layout == null) return;
         HideViews();
         layout.view.SetActive(true);
-        foreach (var item in layout.view.GetComponentsInChildren<EncyclopediaArticleUi>(true))
+        var contentDataViews = layout.view.GetComponentsInChildren<IEncyclopediaArticleDataView>(true);
+        if(showDebug) Debug.Log($"Showing article content in layout {category.layoutName} and found {contentDataViews.Length} dataViewComponents");
+        foreach (var item in contentDataViews)
         {
             item.Setup(article);
         }

+ 8 - 0
Runtime/Encyclopedia/IEncyclopediaArticleDataView.cs

@@ -0,0 +1,8 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public interface IEncyclopediaArticleDataView
+{
+    void Setup(EncyclopediaArticle article);
+}

+ 11 - 0
Runtime/Encyclopedia/IEncyclopediaArticleDataView.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: ab821d703718065409eca34f6ddaff11
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: