using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UIElements; public class NewGameScript : MonoBehaviour { Foldout foldout; private VisualElement root; readonly string tabContentName = "NewGameContent"; SliderInt WinConditionSlider; readonly List selectedCategories = new List(); public void SetupTabContentRoot(VisualElement e) { if (tabContentName == e.name) { root = e; foldout = root.Q("CategoryFoldout"); foldout.value = false; WinConditionSlider = root.Q("WinConditionSlider"); WinConditionSlider.RegisterValueChangedCallback(x => WinConditionSlider.label = x.newValue.ToString()); } } public void PopulateCategories() { Debug.Log("Populating categories"); selectedCategories.Clear(); foreach (Category c in DatabaseController.Instance.Categories.list) { Toggle categoryToggle = new Toggle(); categoryToggle.text = c.name + "(" + c.questionCount + ")"; categoryToggle.value = true; categoryToggle.style.backgroundColor = ConvertColor(c.r, c.g, c.b, c.a); categoryToggle.AddToClassList("categoryToggle"); categoryToggle.RegisterCallback(CategorySelectionChange); if (!foldout.contentContainer.Children().Any(e => e.name == c.name)) { foldout.Add(categoryToggle); } selectedCategories.Add(c); } foldout.text = "Kategorier (Alla " + GetSelectedQuestionCount() + ")"; } private Color ConvertColor(int r, int g, int b, int a) { return new Color(r / 255f, g / 255f, b / 255f, a / 255f); } private void CategorySelectionChange(ClickEvent evt) { Toggle toggle = evt.currentTarget as Toggle; if (!toggle.value) { selectedCategories.Remove(selectedCategories.Find(c => c.name == toggle.text[..toggle.text.IndexOf('(')])); foldout.text = "Kategorier (urval " + GetSelectedQuestionCount() + ")"; } else { selectedCategories.Add(DatabaseController.Instance.Categories.list.Find(c => c.name == toggle.text[..toggle.text.IndexOf('(')])); if (foldout.contentContainer.Children().Cast().All(t => t.value)) { foldout.text = "Kategorier (Alla " + GetSelectedQuestionCount() + ")"; } } } private int GetSelectedQuestionCount() { int sum = 0; selectedCategories.ForEach(c => sum += c.questionCount); return sum; } public void TabSelected(VisualElement element) { if (tabContentName == element.name) { PopulateCategories(); } } }