NewGameScript.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. public class NewGameScript : MonoBehaviour
  8. {
  9. Foldout foldout;
  10. private VisualElement root;
  11. readonly string tabContentName = "NewGameContent";
  12. SliderInt WinConditionSlider;
  13. readonly List<Category> selectedCategories = new List<Category>();
  14. public void SetupTabContentRoot(VisualElement e)
  15. {
  16. if (tabContentName == e.name)
  17. {
  18. root = e;
  19. foldout = root.Q<Foldout>("CategoryFoldout");
  20. foldout.value = false;
  21. WinConditionSlider = root.Q<SliderInt>("WinConditionSlider");
  22. WinConditionSlider.RegisterValueChangedCallback(x => WinConditionSlider.label = x.newValue.ToString());
  23. }
  24. }
  25. public void PopulateCategories()
  26. {
  27. Debug.Log("Populating categories");
  28. selectedCategories.Clear();
  29. foreach (Category c in DatabaseController.Instance.Categories.list)
  30. {
  31. Toggle categoryToggle = new Toggle();
  32. categoryToggle.text = c.name + "(" + c.questionCount + ")";
  33. categoryToggle.value = true;
  34. categoryToggle.style.backgroundColor = ConvertColor(c.r, c.g, c.b, c.a);
  35. categoryToggle.AddToClassList("categoryToggle");
  36. categoryToggle.RegisterCallback<ClickEvent>(CategorySelectionChange);
  37. if (!foldout.contentContainer.Children().Any(e => e.name == c.name))
  38. {
  39. foldout.Add(categoryToggle);
  40. }
  41. selectedCategories.Add(c);
  42. }
  43. foldout.text = "Kategorier (Alla " + GetSelectedQuestionCount() + ")";
  44. }
  45. private Color ConvertColor(int r, int g, int b, int a)
  46. {
  47. return new Color(r / 255f, g / 255f, b / 255f, a / 255f);
  48. }
  49. private void CategorySelectionChange(ClickEvent evt)
  50. {
  51. Toggle toggle = evt.currentTarget as Toggle;
  52. if (!toggle.value)
  53. {
  54. selectedCategories.Remove(selectedCategories.Find(c => c.name == toggle.text[..toggle.text.IndexOf('(')]));
  55. foldout.text = "Kategorier (urval " + GetSelectedQuestionCount() + ")";
  56. }
  57. else
  58. {
  59. selectedCategories.Add(DatabaseController.Instance.Categories.list.Find(c => c.name == toggle.text[..toggle.text.IndexOf('(')]));
  60. if (foldout.contentContainer.Children().Cast<Toggle>().All(t => t.value))
  61. {
  62. foldout.text = "Kategorier (Alla " + GetSelectedQuestionCount() + ")";
  63. }
  64. }
  65. }
  66. private int GetSelectedQuestionCount()
  67. {
  68. int sum = 0;
  69. selectedCategories.ForEach(c => sum += c.questionCount);
  70. return sum;
  71. }
  72. public void TabSelected(VisualElement element)
  73. {
  74. if (tabContentName == element.name)
  75. {
  76. PopulateCategories();
  77. }
  78. }
  79. }