using System; using System.Collections.Generic; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.Perception.Randomization.Parameters; using UnityEngine.UIElements; using Object = UnityEngine.Object; namespace UnityEditor.Perception.Randomization { class ParameterElement : VisualElement { VisualElement m_PropertiesContainer; SerializedProperty m_SerializedProperty; public ParameterElement(SerializedProperty property) { var template = AssetDatabase.LoadAssetAtPath( $"{StaticData.uxmlDir}/Parameter/ParameterElement.uxml"); template.CloneTree(this); m_SerializedProperty = property; m_PropertiesContainer = this.Q("properties"); CreatePropertyFields(); } Parameter parameter => (Parameter)StaticData.GetManagedReferenceValue(m_SerializedProperty); CategoricalParameterBase categoricalParameter => (CategoricalParameterBase)parameter; void CreatePropertyFields() { m_PropertiesContainer.Clear(); if (parameter is CategoricalParameterBase) { CreateCategoricalParameterFields(); return; } var nextSiblingProperty = m_SerializedProperty.Copy(); nextSiblingProperty.NextVisible(false); var currentProperty = m_SerializedProperty.Copy(); if (currentProperty.NextVisible(true)) do { if (SerializedProperty.EqualContents(currentProperty, nextSiblingProperty)) break; var propertyField = new PropertyField(currentProperty.Copy()); propertyField.Bind(currentProperty.serializedObject); m_PropertiesContainer.Add(propertyField); } while (currentProperty.NextVisible(false)); } void CreateCategoricalParameterFields() { var template = AssetDatabase.LoadAssetAtPath( $"{StaticData.uxmlDir}/Parameter/CategoricalParameterTemplate.uxml").CloneTree(); var optionsProperty = m_SerializedProperty.FindPropertyRelative("m_Categories"); var probabilitiesProperty = m_SerializedProperty.FindPropertyRelative("probabilities"); var probabilities = categoricalParameter.probabilities; var listView = template.Q("options"); listView.itemsSource = probabilities; listView.itemHeight = 44; listView.selectionType = SelectionType.None; listView.style.flexGrow = 1.0f; listView.style.height = new StyleLength(listView.itemHeight * 4); var uniformToggle = template.Q("uniform"); VisualElement MakeItem() { return new CategoricalOptionElement( optionsProperty, probabilitiesProperty); } listView.makeItem = MakeItem; void BindItem(VisualElement e, int i) { var optionElement = (CategoricalOptionElement)e; optionElement.BindProperties(i); var removeButton = optionElement.Q