SelectCategoryScript.cs 3.7 KB

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