using System; using System.Collections.Generic; using UnityEditor; using UnityEditor.UIElements; using UnityEngine.Experimental.Perception.Randomization.Editor; using UnityEngine.Experimental.Perception.Randomization.Parameters; using UnityEngine.UIElements; namespace UnityEngine.Perception.Randomization.Editor { class ParameterElement : VisualElement { VisualElement m_PropertiesContainer; SerializedProperty m_SerializedProperty; Parameter parameter => (Parameter)StaticData.GetManagedReferenceValue(m_SerializedProperty); CategoricalParameterBase categoricalParameter => (CategoricalParameterBase)parameter; 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(); } void CreatePropertyFields() { m_PropertiesContainer.Clear(); if (parameter is CategoricalParameterBase) { CreateCategoricalParameterFields(); return; } var currentProperty = m_SerializedProperty.Copy(); var nextSiblingProperty = m_SerializedProperty.Copy(); nextSiblingProperty.NextVisible(false); if (currentProperty.NextVisible(true)) { do { if (SerializedProperty.EqualContents(currentProperty, nextSiblingProperty)) break; if (currentProperty.type.Contains("managedReference") && currentProperty.managedReferenceFieldTypename == StaticData.samplerSerializedFieldType) m_PropertiesContainer.Add(new SamplerElement(currentProperty.Copy(), parameter)); else { 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 = 22; listView.selectionType = SelectionType.None; listView.style.flexGrow = 1.0f; listView.style.height = new StyleLength(listView.itemHeight * 4); VisualElement MakeItem() => new CategoricalOptionElement( optionsProperty, probabilitiesProperty); listView.makeItem = MakeItem; void BindItem(VisualElement e, int i) { var optionElement = (CategoricalOptionElement)e; optionElement.BindProperties(i); var removeButton = optionElement.Q