SelectCategoryScript.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class SelectCategoryScript : MonoBehaviour
  8. {
  9. [SerializeField] Button doneButton;
  10. [SerializeField] Button closeButton;
  11. [SerializeField] GameObject CategoryToSelectPrefab;
  12. [SerializeField] GameObject SelectCategoryPanel;
  13. [SerializeField] GameObject ContentPanel;
  14. [SerializeField] Text TotalsText;
  15. // Start is called before the first frame update
  16. private List<CategoryPanel.Category> categories;
  17. private void Start()
  18. {
  19. doneButton.onClick.AddListener(CloseDialogAction);
  20. closeButton.onClick.AddListener(CloseDialogAction);
  21. GetCategories();
  22. updateTotalsText();
  23. int pos = 0;
  24. foreach (CategoryPanel.Category cat in categories)
  25. {
  26. GameObject go = Instantiate(CategoryToSelectPrefab, Vector3.zero, Quaternion.identity);
  27. CategorySelection c = go.GetComponent<CategorySelection>();
  28. c.SetSelected(true);
  29. c.QuestionCount = cat.questionCount;
  30. c.SetCategoryText(cat.name, cat.questionCount);
  31. c.transform.SetSiblingIndex(pos++);
  32. c.transform.localScale = new Vector3(1, 1, 1);
  33. c.transform.SetParent(ContentPanel.transform, false);
  34. }
  35. }
  36. private void Update() {
  37. updateTotalsText();
  38. }
  39. private void updateTotalsText() {
  40. string totalsText = String.Format(LocalizationManager.Instance.GetText(TotalsText.GetComponent<TextLocalization>().key),
  41. GetSelectedCategoriesQuestionCount().ToString());
  42. TotalsText.text = totalsText;
  43. }
  44. private void GetCategories()
  45. {
  46. if (categories == null || categories.Count == 0) {
  47. categories = new List<CategoryPanel.Category>();
  48. categories = OnlineDatabase.Instance.GetCategories(categories, -1);
  49. }
  50. }
  51. internal void CloseDialogAction() {
  52. SelectCategoryPanel.GetComponent<selectCategoriesPanelController>().UpdateCategoryText(GetSelectedCategoriesQuestionCount());
  53. gameObject.SetActive(false);
  54. }
  55. internal int GetSelectedCategoriesQuestionCount()
  56. {
  57. IEnumerable<CategorySelection> selectedCategories = ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected());
  58. return selectedCategories.Sum(c => c.QuestionCount);
  59. }
  60. internal string GetCategoriesSelectedCount()
  61. {
  62. string returnValue;
  63. int selectionCount = GetSelectedCategoriesAsList().Count;
  64. if (selectionCount == categories.Count)
  65. {
  66. returnValue = LocalizationManager.Instance.GetText("ALL");
  67. }
  68. else
  69. {
  70. returnValue = selectionCount.ToString();
  71. }
  72. return returnValue;
  73. }
  74. private List<CategorySelection> GetSelectedCategoriesAsList()
  75. {
  76. return ContentPanel.GetComponentsInChildren<CategorySelection>().Where(c => c.isSelected()).ToList();
  77. }
  78. public List<CategoryPanel.Category> GetSelectedCategories() {
  79. GetCategories();
  80. List<string> categoriesName = categories.Select(c1 => c1.name).ToList();
  81. List<string> selectedCategoyNames = GetSelectedCategoriesAsList().Select(c2 => c2.getCategoryName()).ToList();
  82. List<string> names = new List<string>();
  83. if (selectedCategoyNames.Count == 0) {
  84. names = categoriesName;
  85. } else {
  86. names = categoriesName.Intersect(selectedCategoyNames).ToList();
  87. }
  88. return categories.Where(c => names.Contains(c.name)).ToList();
  89. }
  90. internal void ShowDialog()
  91. {
  92. gameObject.SetActive(true);
  93. }
  94. }