EncyclopediaArticleUi.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.UI.Encyclopedia
  8. {
  9. [HideMonoScript]
  10. public class EncyclopediaArticleUi : MonoBehaviour, IEncyclopediaArticleDataView
  11. {
  12. public enum UiType
  13. {
  14. String,
  15. Integer,
  16. Float,
  17. Image,
  18. }
  19. public string articleDataKey = "title";
  20. public UiType uiType = UiType.String;
  21. [ShowIf("@uiType != UiType.Image")] public TextMeshProUGUI textMeshPro;
  22. [ShowIf("@uiType == UiType.Image")] public Image imageComponent;
  23. public bool showDebug = false;
  24. public void Setup(EncyclopediaArticle article)
  25. {
  26. string result = "";
  27. Sprite spriteImage = null;
  28. switch (uiType)
  29. {
  30. case UiType.String:
  31. article.content.TryGetValue(articleDataKey, out result);
  32. break;
  33. case UiType.Integer:
  34. article.integers.TryGetValue(articleDataKey, out int intValue);
  35. result += intValue;
  36. break;
  37. case UiType.Float:
  38. article.floats.TryGetValue(articleDataKey, out float floatValue);
  39. result += floatValue;
  40. break;
  41. case UiType.Image:
  42. article.images.TryGetValue(articleDataKey, out spriteImage);
  43. break;
  44. default:
  45. break;
  46. }
  47. if(uiType != UiType.Image && textMeshPro != null)
  48. {
  49. textMeshPro.text = result;
  50. if(imageComponent != null) imageComponent.gameObject.SetActive(false);
  51. }
  52. else if(uiType == UiType.Image && imageComponent != null)
  53. {
  54. if(spriteImage != null)
  55. {
  56. imageComponent.sprite = spriteImage;
  57. imageComponent.gameObject.SetActive(true);
  58. }
  59. else imageComponent.gameObject.SetActive(false);
  60. }
  61. if(showDebug && (result == null || result == ""))
  62. Debug.Log($"Article did not contain content key {articleDataKey}. Content key count: {article.content.Keys.Count}", this.gameObject);
  63. }
  64. }
  65. }