| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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<Category> selectedCategories = new List<Category>();
- public void SetupTabContentRoot(VisualElement e)
- {
- if (tabContentName == e.name)
- {
- root = e;
- foldout = root.Q<Foldout>("CategoryFoldout");
- foldout.value = false;
- WinConditionSlider = root.Q<SliderInt>("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<ClickEvent>(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<Toggle>().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();
- }
- }
- }
|