EncyclopediaController.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public static EncyclopediaController instance;
  13. [ShowInInspector, HorizontalGroup("IndexLine", 0.7f), LabelText("Index")] public int menuIndex;
  14. [ShowInInspector, HorizontalGroup("IndexLine", 0.3f), HideLabel] public int subMenuIndex;
  15. public EncyclopediaUi encyclopediaUi;
  16. public GameObject categoryButtomPrefab;
  17. public List<EncyclopediaCategory> categories = new List<EncyclopediaCategory>();
  18. private void Awake()
  19. {
  20. if(instance == null) instance = this;
  21. else Destroy(this);
  22. }
  23. [Button("Build Menu"), ButtonGroup("Buttons")]
  24. public void BuildMenu()
  25. {
  26. if(encyclopediaUi != null) encyclopediaUi.Populate();
  27. else Debug.LogError("Missing Encyclopedia UI in Encyclopedia Controller", this.gameObject);
  28. }
  29. public void OnClick(string id)
  30. {
  31. for (int i = 0; i < categories.Count; i++)
  32. {
  33. if(id == categories[i].id)
  34. {
  35. menuIndex = i;
  36. Navigate();
  37. return;
  38. }
  39. var articles = categories[i].GetArticles();
  40. for (int a = 0; a < articles.Count; a++)
  41. {
  42. if(id == articles[a].id)
  43. {
  44. menuIndex = i;
  45. subMenuIndex = a;
  46. Navigate();
  47. return;
  48. }
  49. }
  50. }
  51. }
  52. [Button("Navigate"), ButtonGroup("Buttons")]
  53. public void Navigate()
  54. {
  55. if(encyclopediaUi != null) encyclopediaUi.Navigate();
  56. GenericEvents.Trigger("OpeningEncyclopediaArticle", menuIndex, subMenuIndex);
  57. }
  58. public EncyclopediaCategory GetCategory(int categoryIndex) => categories[categoryIndex];
  59. public EncyclopediaArticle GetArticle(int categoryIndex, int articleIndex)
  60. {
  61. var category = categories[categoryIndex];
  62. var articles = category.GetArticles();
  63. if(articleIndex < articles.Count) return articles[articleIndex];
  64. else return null;
  65. }
  66. }
  67. }