using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.Perception.GroundTruth; using UnityEngine.UIElements; using Newtonsoft.Json.Linq; using UnityEditor.UIElements; namespace UnityEditor.Perception.GroundTruth { abstract class LabelConfigEditor : Editor where T : ILabelEntry { private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/"; private string m_UxmlPath; private int m_AddedLabelsItemHeight = 37; private int m_OtherLabelsItemHeight = 27; private List m_AddedLabels = new List(); protected SerializedProperty m_SerializedLabelsArray; private static HashSet allLabelsInProject = new HashSet(); private List m_LabelsNotPresentInConfig = new List(); private bool m_UiInitialized; private bool m_EditorHasUi; private VisualElement m_Root; private Button m_SaveButton; private Button m_AddNewLabelButton; private Button m_RemoveAllButton; private Button m_AddAllButton; private Button m_ImportFromFileButton; private Button m_ExportToFileButton; private ListView m_NonPresentLabelsListView; protected ListView m_LabelListView; protected Button m_MoveUpButton; protected Button m_MoveDownButton; protected VisualElement m_MoveButtons; protected VisualElement m_IdSpecificUi; protected EnumField m_StartingIdEnumField; protected Toggle m_AutoIdToggle; public void OnEnable() { m_SerializedLabelsArray = serializedObject.FindProperty(IdLabelConfig.labelEntriesFieldName); m_UiInitialized = false; ChangesHappeningInForeground = true; RefreshListDataAndPresentation(); Undo.undoRedoPerformed += () => { ChangesHappeningInForeground = true; RefreshListDataAndPresentation(); }; } private int m_PreviousLabelsArraySize = -1; /// /// This boolean is used to signify when changes in the model are triggered directly from the inspector UI by the user. /// In these cases, the scheduled model checker does not need to update the UI again. /// public bool ChangesHappeningInForeground { get; set; } private void CheckForModelChanges() { if (ChangesHappeningInForeground) { ChangesHappeningInForeground = false; m_PreviousLabelsArraySize = m_SerializedLabelsArray.arraySize; return; } if (m_SerializedLabelsArray.arraySize != m_PreviousLabelsArraySize) { RefreshListDataAndPresentation(); m_PreviousLabelsArraySize = m_SerializedLabelsArray.arraySize; } } protected abstract void InitUiExtended(); public abstract void PostRemoveOperations(); public override VisualElement CreateInspectorGUI() { if (!m_UiInitialized) { InitUi(); m_UiInitialized = true; } serializedObject.Update(); RefreshListDataAndPresentation(); return m_Root; } private void InitUi() { m_EditorHasUi = true; m_UxmlPath = m_UxmlDir + "LabelConfig_Main.uxml"; m_Root = AssetDatabase.LoadAssetAtPath(m_UxmlPath).CloneTree(); m_LabelListView = m_Root.Q("labels-listview"); m_NonPresentLabelsListView = m_Root.Q("labels-in-project-listview"); m_SaveButton = m_Root.Q