EncyclopediaController.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.UI.InteractionHandler;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.UI.Encyclopedia
  8. {
  9. [HideMonoScript]
  10. public class EncyclopediaController : MonoBehaviour, IClickHandler
  11. {
  12. [ShowInInspector, HorizontalGroup("IndexLine", 0.7f), LabelText("Index")] public int menuIndex;
  13. [ShowInInspector, HorizontalGroup("IndexLine", 0.3f), HideLabel] public int subMenuIndex;
  14. public EncyclopediaUi encyclopediaUi;
  15. public GameObject categoryButtomPrefab;
  16. public List<EncyclopediaCategory> categories = new List<EncyclopediaCategory>();
  17. [Button("Build Menu"), ButtonGroup("Buttons")]
  18. public void BuildMenu()
  19. {
  20. if(encyclopediaUi != null) encyclopediaUi.Populate();
  21. else Debug.LogError("Missing Encyclopedia UI in Encyclopedia Controller", this.gameObject);
  22. }
  23. public void OnClick(string id)
  24. {
  25. for (int i = 0; i < categories.Count; i++)
  26. {
  27. if(id == categories[i].id)
  28. {
  29. menuIndex = i;
  30. Navigate();
  31. return;
  32. }
  33. var articles = categories[i].GetArticles();
  34. for (int a = 0; a < articles.Count; a++)
  35. {
  36. if(id == articles[a].id)
  37. {
  38. menuIndex = i;
  39. subMenuIndex = a;
  40. Navigate();
  41. return;
  42. }
  43. }
  44. }
  45. }
  46. [Button("Navigate"), ButtonGroup("Buttons")]
  47. public void Navigate()
  48. {
  49. if(encyclopediaUi != null) encyclopediaUi.Navigate();
  50. GenericEvents.Trigger("OpeningEncyclopediaArticle", menuIndex, subMenuIndex);
  51. }
  52. public EncyclopediaCategory GetCategory(int categoryIndex) => categories[categoryIndex];
  53. public EncyclopediaArticle GetArticle(int categoryIndex, int articleIndex)
  54. {
  55. var category = categories[categoryIndex];
  56. var articles = category.GetArticles();
  57. if(articleIndex < articles.Count) return articles[articleIndex];
  58. else return null;
  59. }
  60. }
  61. }