namespace WallstopStudios.DataVisualizer.Editor { using System; using System.Collections.Generic; using System.Linq; using Data; using Styles; using UnityEngine; using UnityEngine.UIElements; using WallstopStudios.DataVisualizer.Helper; public sealed class NamespaceController { private const string TypeItemLabelName = "type-item-label"; public Type SelectedType => _selectedType; private readonly Dictionary _namespaceCache = new(); private readonly Dictionary> _managedTypes; private readonly Dictionary _namespaceOrder; private Type _selectedType; public NamespaceController( Dictionary> managedTypes, Dictionary namespaceOrder ) { // Need to store references, not copy, for double data binding _managedTypes = managedTypes ?? throw new ArgumentNullException(nameof(managedTypes)); _namespaceOrder = namespaceOrder ?? throw new ArgumentNullException(nameof(namespaceOrder)); _selectedType = null; } public void Clear() { _selectedType = null; } public void DecrementTypeSelection(DataVisualizer dataVisualizer) { if (!InternalDeselectAndGetCurrentIndex(out VisualElement parent, out int currentIndex)) { return; } currentIndex--; if (currentIndex < 0) { currentIndex = parent.childCount - 1; } Type type = InternalSelected(parent, currentIndex); SelectType(dataVisualizer, type); } public void IncrementTypeSelection(DataVisualizer dataVisualizer) { if (!InternalDeselectAndGetCurrentIndex(out VisualElement parent, out int currentIndex)) { return; } currentIndex++; if (parent.childCount <= currentIndex) { currentIndex = 0; } Type type = InternalSelected(parent, currentIndex); SelectType(dataVisualizer, type); } private bool InternalDeselectAndGetCurrentIndex( out VisualElement parent, out int currentIndex ) { if (!TryGet(_selectedType, out VisualElement element)) { parent = default; currentIndex = default; return false; } element.RemoveFromClassList(StyleConstants.SelectedClass); parent = element.parent; if (parent == null) { currentIndex = default; return false; } currentIndex = parent.IndexOf(element); return true; } private static Type InternalSelected(VisualElement parent, int index) { if (0 > index || index >= parent.childCount) { return null; } VisualElement element = parent.ElementAt(index); element.AddToClassList(StyleConstants.SelectedClass); return element.userData as Type; } public void SelectType(DataVisualizer dataVisualizer, Type type) { if (_selectedType == type) { return; } if (TryGet(_selectedType, out VisualElement currentSelection)) { currentSelection.RemoveFromClassList(StyleConstants.SelectedClass); currentSelection .Q