#define ENABLED
#if ENABLED
using System.Diagnostics.Contracts ;
using Unity.Entities ;
using UnityEditorInternal ;
using UnityEngine.PlayerLoop ;
using UnityEngine.Rendering.UI ;
using UnityEngine.UI ;
using Object = UnityEngine . Object ;
using Toggle = UnityEngine . UIElements . Toggle ;
namespace UnityEditor.Perception.GroundTruth
{
private Labeling m_Labeling ;
private SerializedProperty m_SerializedLabelsArray ;
//private SerializedProperty m_AutoLabelingBoolProperty;
private BindableElement m_OuterElement ;
private VisualElement m_ManualLabelingContainer ;
private VisualElement m_AutoLabelingContainer ;
private ListView m_CurrentLabelsListView ;
private PopupField < string > m_LabelingSchemesPopup ;
private Button m_AddButton ;
private Button m_AddAutoLabelToConfButton ;
private Toggle m_AutoLabelingToggle ;
private Toggle m_AutoLabelingToggle ;
private Labeling m_Labeling ;
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/" ;
private string m_UxmlPath ;
private List < string > m_CommonLabels = new List < string > ( ) ; //labels that are common between all selected Labeling objects (for multi editing)
public List < string > CommonLabels = > m_CommonLabels ;
public List < string > CommonLabels { get ; private set ; } = new List < string > ( ) ;
private List < Type > m_LabelConfigTypes = new List < Type > ( ) ;
private List < ScriptableObject > m_AllLabelConfigsInProject = new List < ScriptableObject > ( ) ;
private List < Type > m_LabelConfigTypes ;
private readonly List < ScriptableObject > m_AllLabelConfigsInProject = new List < ScriptableObject > ( ) ;
private List < AssetLabelingScheme > m_LabelingSchemes = new List < AssetLabelingScheme > ( ) ;
private readonly List < AssetLabelingScheme > m_LabelingSchemes = new List < AssetLabelingScheme > ( ) ;
private void OnEnable ( )
{
m_SerializedLabelsArray = mySerializedObject . FindProperty ( "labels" ) ;
//m_AutoLabelingBoolProperty = serializedObject.FindProperty(nameof(Labeling.useAutoLabeling));
m_Root = AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( m_UxmlPath ) . CloneTree ( ) ;
m_CurrentLabelsListView = m_Root . Q < ListView > ( "current-labels-listview" ) ;
m_AutoLabelingToggle = m_Root . Q < Toggle > ( "auto-or-manual-toggle" ) ;
m_ManualLabelingContainer = m_Root . Q < VisualElement > ( "manual-labeling" ) ;
m_AutoLabelingContainer = m_Root . Q < VisualElement > ( "automatic-labeling" ) ;
m_AddAutoLabelToConfButton = m_Root . Q < Button > ( "add-auto-label-to-config" ) ;
var dropdownParent = m_Root . Q < VisualElement > ( "drop-down-parent" ) ;
var dropdownParent = m_Root . Q < VisualElement > ( "drop-down-parent" ) ;
m_ItIsPossibleToAddMultipleAutoLabelsToConfig = false ;
AssesAutoLabelingStatus ( ) ;
if ( serializedObject . targetObjects . Length > 1 )
var suggestedOnNamePanel = m_Root . Q < VisualElement > ( "suggested-labels-from-name" ) ;
suggestedOnNamePanel . RemoveFromHierarchy ( ) ;
m_AddAutoLabelToConfButton . text = "Add Automatic Labels of All Selected Assets to Config..." ;
else
{
m_AddAutoLabelToConfButton . text = "Add to Label Config..." ;
}
m_AddAutoLabelToConfButton . clicked + = ( ) = >
{
AddToConfigWindow . ShowWindow ( CreateUnionOfAllLabels ( ) ) ;
} ;
string newLabel = FindNewLabelValue ( labelsUnion ) ;
var newLabel = FindNewLabelValue ( labelsUnion ) ;
foreach ( var targetObject in targets )
{
if ( targetObject is Labeling labeling )
RefreshManualLabelingData ( ) ;
} ;
m_AutoLabelingToggle . RegisterValueChangedCallback < bool > ( evt = >
m_AutoLabelingToggle . RegisterValueChangedCallback ( evt = >
bool SerializedObjectHasValidLabelingScheme ( SerializedObject serObj )
private bool SerializedObjectHasValidLabelingScheme ( SerializedObject serObj )
bool IsValidLabelingSchemeName ( string schemeName )
private bool IsValidLabelingSchemeName ( string schemeName )
void UpdateUiAspects ( )
private bool m_ItIsPossibleToAddMultipleAutoLabelsToConfig ;
private void UpdateUiAspects ( )
{
m_ManualLabelingContainer . SetEnabled ( ! m_AutoLabelingToggle . value ) ;
m_AutoLabelingContainer . SetEnabled ( m_AutoLabelingToggle . value ) ;
{
m_CurrentAutoLabel . style . display = DisplayStyle . None ;
m_AddAutoLabelToConfButton . SetEnabled ( false ) ;
m_AddAutoLabelToConfButton . SetEnabled ( true ) ;
}
if ( m_AutoLabelingToggle . value & & serializedObject . targetObjects . Length > 1 & & m_ItIsPossibleToAddMultipleAutoLabelsToConfig )
{
m_AddAutoLabelToConfButton . SetEnabled ( true ) ;
}
}
void UpdateCurrentAutoLabelValue ( SerializedObject serObj )
private void UpdateCurrentAutoLabelValue ( SerializedObject serObj )
{
var array = serObj . FindProperty ( nameof ( Labeling . labels ) ) ;
if ( array . arraySize > 0 )
}
void InitializeLabelingSchemes ( VisualElement parent )
private void InitializeLabelingSchemes ( VisualElement parent )
{
//this function should be called only once during the lifecycle of the editor element
m_LabelingSchemes . Add ( new AssetNameLabelingScheme ( ) ) ;
m_LabelingSchemesPopup . style . marginLeft = 0 ;
parent . Add ( m_LabelingSchemesPopup ) ;
m_LabelingSchemesPopup . RegisterValueChangedCallback < string > ( evt = > AssignAutomaticLabelToSelectedAssets ( ) ) ;
m_LabelingSchemesPopup . RegisterValueChangedCallback ( evt = > AssignAutomaticLabelToSelectedAssets ( ) ) ;
void AutoLabelToggleChanged ( )
private void AutoLabelToggleChanged ( )
{
UpdateUiAspects ( ) ;
RefreshManualLabelingData ( ) ;
}
void AssignAutomaticLabelToSelectedAssets ( )
private void AssignAutomaticLabelToSelectedAssets ( )
{
//the 0th index of this popup is "<Select Scheme>" and should not do anything
if ( m_LabelingSchemesPopup . index = = 0 )
m_ItIsPossibleToAddMultipleAutoLabelsToConfig = true ;
var labelingScheme = m_LabelingSchemes [ m_LabelingSchemesPopup . index - 1 ] ;
RefreshManualLabelingData ( ) ;
}
void AssignAutomaticLabelToSerializedObject ( SerializedObject serObj , AssetLabelingScheme labelingScheme )
{
var serLabelsArray = serObj . FindProperty ( nameof ( Labeling . labels ) ) ;
var label = labelingScheme . GenerateLabel ( serObj . targetObject ) ;
serLabelsArray . ClearArray ( ) ;
serLabelsArray . InsertArrayElementAtIndex ( 0 ) ;
serLabelsArray . GetArrayElementAtIndex ( 0 ) . stringValue = label ;
serObj . ApplyModifiedProperties ( ) ;
serObj . SetIsDifferentCacheDirty ( ) ;
}
void AssesAutoLabelingStatus ( )
private void AssesAutoLabelingStatus ( )
{
var enabledOrNot = true ;
if ( serializedObject . targetObjects . Length = = 1 )
else
{
string unifiedLabelingScheme = null ;
bool allAssetsUseSameLabelingScheme = true ;
var allAssetsUseSameLabelingScheme = true ;
foreach ( var targetObj in serializedObject . targetObjects )
{
// if (enabled)
// {
var schemeName = serObj . FindProperty ( nameof ( Labeling . autoLabelingSchemeType ) ) . stringValue ;
if ( schemeName = = string . Empty )
{
if ( allAssetsUseSameLabelingScheme )
{
//all selected assets are using auto labeling, all using the same scheme
//all selected assets have the same scheme recorded in their serialized objects
if ( enabledOrNot )
{
//all selected assets have the same scheme recorded in their serialized objects, and they all
//have auto labeling enabled
m_ItIsPossibleToAddMultipleAutoLabelsToConfig = true ;
}
//the selected assets are all using auto labeling, but not using the same scheme
//the selected DO NOT have the same scheme recorded in their serialized objects
HashSet < string > CreateUnionOfAllLabels ( )
private HashSet < string > CreateUnionOfAllLabels ( )
{
HashSet < String > result = new HashSet < string > ( ) ;
foreach ( var obj in targets )
}
return result ;
}
string FindNewLabelValue ( HashSet < string > labels )
private string FindNewLabelValue ( HashSet < string > labels )
{
string baseLabel = "New Label" ;
string label = baseLabel ;
public override VisualElement CreateInspectorGUI ( )
{
serializedObject . Update ( ) ;
var mySerializedObject = new SerializedObject ( serializedObject . targetObjects [ 0 ] ) ;
m_SerializedLabelsArray = mySerializedObject . FindProperty ( "labels" ) ;
RefreshCommonLabels ( ) ;
RefreshSuggestedLabelLists ( ) ;
RefreshLabelConfigsList ( ) ;
public void RemoveAddedLabelsFromSuggestedLists ( )
{
m_SuggestedLabelsBasedOnName . RemoveAll ( s = > m_ CommonLabels. Contains ( s ) ) ;
m_SuggestedLabelsBasedOnPath . RemoveAll ( s = > m_ CommonLabels. Contains ( s ) ) ;
m_SuggestedLabelsBasedOnName . RemoveAll ( s = > CommonLabels . Contains ( s ) ) ;
m_SuggestedLabelsBasedOnPath . RemoveAll ( s = > CommonLabels . Contains ( s ) ) ;
}
public void RefreshSuggestedLabelLists ( )
{
serializedObject . SetIsDifferentCacheDirty ( ) ;
serializedObject . Update ( ) ;
var mySerializedObject = new SerializedObject ( serializedObject . targetObjects [ 0 ] ) ;
m_SerializedLabelsArray = mySerializedObject . FindProperty ( nameof ( Labeling . labels ) ) ;
RefreshCommonLabels ( ) ;
RefreshSuggestedLabelLists ( ) ;
SetupSuggestedLabelsListViews ( ) ;
void RefreshCommonLabels ( )
{
m_CommonLabels . Clear ( ) ;
m_CommonLabels . AddRange ( ( ( Labeling ) serializedObject . targetObjects [ 0 ] ) . labels ) ;
CommonLabels . Clear ( ) ;
CommonLabels . AddRange ( ( ( Labeling ) serializedObject . targetObjects [ 0 ] ) . labels ) ;
m_CommonLabels = m_CommonLabels . Intersect ( ( ( Labeling ) obj ) . labels ) . ToList ( ) ;
CommonLabels = CommonLabels . Intersect ( ( ( Labeling ) obj ) . labels ) . ToList ( ) ;
m_CurrentLabelsListView . itemsSource = m_CommonLabels ;
var mySerializedObject = new SerializedObject ( serializedObject . targetObjects [ 0 ] ) ;
m_CurrentLabelsListView . itemsSource = CommonLabels ;
new AddedLabelEditor ( this , m_CurrentLabelsListView , mySerializedObject , m_SerializedLabelsArray ) ;
new AddedLabelEditor ( this , m_CurrentLabelsListView ) ;
void BindItem ( VisualElement e , int i )
{
addedLabel . m_LabelTextField . value = m_CommonLabels [ i ] ;
addedLabel . m_LabelTextField . value = CommonLabels [ i ] ;
}
}
m_CurrentLabelsListView . makeItem = MakeItem ;
m_CurrentLabelsListView . itemHeight = itemHeight ;
m_CurrentLabelsListView . itemsSource = m_ CommonLabels;
m_CurrentLabelsListView . itemsSource = CommonLabels ;
m_CurrentLabelsListView . selectionType = SelectionType . None ;
}
internal class AddedLabelEditor : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/" ;
private string m_UxmlPath ;
private Button m_RemoveButton ;
private Button m_AddToConfigButton ;
public AddedLabelEditor ( LabelingEditor editor , ListView listView , SerializedObject serializedLabelingObject , SerializedProperty labelsArrayProperty )
public AddedLabelEditor ( LabelingEditor editor , ListView listView )
m_UxmlPath = m_UxmlDir + "AddedLabelElement.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( m_UxmlPath ) . CloneTree ( this ) ;
var uxmlPath = m_UxmlDir + "AddedLabelElement.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( uxmlPath ) . CloneTree ( this ) ;
m_RemoveButton = this . Q < Button > ( "remove-button" ) ;
m_AddToConfigButton = this . Q < Button > ( "add-to-config-button" ) ;
var removeButton = this . Q < Button > ( "remove-button" ) ;
var addToConfigButton = this . Q < Button > ( "add-to-config-button" ) ;
ScrollView tmp = listView . Q < ScrollView > ( ) ;
tmp . verticalScroller . slider . RegisterCallback < MouseDownEvent > ( evt = >
{
Debug . Log ( "mouse down" ) ;
} ) ;
listView . RegisterCallback < FocusInEvent > ( evt = >
{
Debug . Log ( "list focused" ) ;
} ) ;
m_LabelTextField . RegisterCallback < FocusOutEvent > ( evt = >
{
Debug . Log ( "focus out" ) ;
} ) ;
m_LabelTextField . RegisterCallback < FocusInEvent > ( evt = >
{
Debug . Log ( "focus in" ) ;
} ) ;
m_LabelTextField . RegisterValueChangedCallback < string > ( ( cEvent ) = >
m_LabelTextField . RegisterValueChangedCallback ( ( cEvent ) = >
{
//Do not let the user define a duplicate label
if ( editor . CommonLabels . Contains ( cEvent . newValue ) & & editor . CommonLabels . IndexOf ( cEvent . newValue ) ! = m_IndexInList )
editor . RefreshManualLabelingData ( ) ;
} ) ;
m_A ddToConfigButton. clicked + = ( ) = >
a ddToConfigButton. clicked + = ( ) = >
m_R emoveButton. clicked + = ( ) = >
r emoveButton. clicked + = ( ) = >
List < string > m_C ommonLabels = new List < string > ( ) ;
List < string > c ommonLabels = new List < string > ( ) ;
m_C ommonLabels. Clear ( ) ;
c ommonLabels. Clear ( ) ;
if ( firstTarget )
if ( firstTarget ! = null )
m_C ommonLabels. AddRange ( firstTarget . labels ) ;
c ommonLabels. AddRange ( firstTarget . labels ) ;
m_CommonLabels = m_C ommonLabels. Intersect ( ( ( Labeling ) obj ) . labels ) . ToList ( ) ;
commonLabels = c ommonLabels. Intersect ( ( ( Labeling ) obj ) . labels ) . ToList ( ) ;
}
foreach ( var targetObject in editor . targets )
RemoveLabelFromLabelingSerObj ( labeling , m_CommonLabels ) ;
RemoveLabelFromLabelingSerObj ( labeling , commonLabels ) ;
}
}
editor . serializedObject . SetIsDifferentCacheDirty ( ) ;
internal class SuggestedLabelElement : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/" ;
private string m_UxmlPath ;
private Button m_AddButton ;
m_UxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( m_UxmlPath ) . CloneTree ( this ) ;
var uxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( uxmlPath ) . CloneTree ( this ) ;
m_AddButton = this . Q < Button > ( "add-button" ) ;
var addButton = this . Q < Button > ( "add-button" ) ;
m_AddButton . clicked + = ( ) = >
addButton . clicked + = ( ) = >
{
foreach ( var targetObject in editor . serializedObject . targetObjects )
{
}
}
editor . RefreshManualLabelingData ( ) ;
//editor.RefreshUi();
} ;
}
}
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/" ;
private string m_UxmlPath ;
private Label m_ConfigName ;
//private Toggle m_HiddenCollapsedToggle;
m_UxmlPath = m_UxmlDir + "ConfigElementForAddingLabelsFrom.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( m_UxmlPath ) . CloneTree ( this ) ;
var uxmlPath = m_UxmlDir + "ConfigElementForAddingLabelsFrom.uxml" ;
AssetDatabase . LoadAssetAtPath < VisualTreeAsset > ( uxmlPath ) . CloneTree ( this ) ;
m_ConfigName = this . Q < Label > ( "config-name" ) ;
m_ConfigName . text = config . name ;
var configName = this . Q < Label > ( "config-name" ) ;
configName . text = config . name ;
m_CollapseToggle = this . Q < VisualElement > ( "collapse-toggle" ) ;
var propertyInfo = config . GetType ( ) . GetProperty ( IdLabelConfig . publicLabelEntriesFieldName ) ;
}
}
}
}
#endif