@@ -112,6 +112,27 @@ void InitializeView()
112112 controlsContainer = new VisualElement { name = "controls" } ;
113113 mainContainer . Add ( controlsContainer ) ;
114114
115+ if ( nodeTarget . showControlsOnHover )
116+ {
117+ bool mouseOverControls = false ;
118+ controlsContainer . style . display = DisplayStyle . None ;
119+ RegisterCallback < MouseOverEvent > ( e => {
120+ controlsContainer . style . display = DisplayStyle . Flex ;
121+ mouseOverControls = true ;
122+ } ) ;
123+ RegisterCallback < MouseOutEvent > ( e => {
124+ var rect = GetPosition ( ) ;
125+ var graphMousePosition = owner . contentViewContainer . WorldToLocal ( e . mousePosition ) ;
126+ if ( rect . Contains ( graphMousePosition ) )
127+ return ;
128+ mouseOverControls = false ;
129+ schedule . Execute ( _ => {
130+ if ( ! mouseOverControls )
131+ controlsContainer . style . display = DisplayStyle . None ;
132+ } ) . ExecuteLater ( 500 ) ;
133+ } ) ;
134+ }
135+
115136 debugContainer = new VisualElement { name = "debug" } ;
116137 if ( nodeTarget . debug )
117138 mainContainer . Add ( debugContainer ) ;
@@ -501,10 +522,13 @@ public virtual void Enable()
501522 DrawDefaultInspector ( ) ;
502523 }
503524
525+ Dictionary < string , List < ( object value , VisualElement target ) > > visibleConditions = new Dictionary < string , List < ( object value , VisualElement target ) > > ( ) ;
526+
504527 protected virtual void DrawDefaultInspector ( )
505528 {
506529 var fields = nodeTarget . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . DeclaredOnly ) ;
507530
531+ visibleConditions . Clear ( ) ;
508532 foreach ( var field in fields )
509533 {
510534 //skip if the field is not serializable
@@ -523,23 +547,58 @@ protected virtual void DrawDefaultInspector()
523547 }
524548 }
525549
526- protected void AddControlField ( string fieldName , string label = null , Action valueChangedCallback = null )
550+ void UpdateFieldVisibility ( string fieldName , object newValue )
551+ {
552+ if ( visibleConditions . TryGetValue ( fieldName , out var list ) )
553+ {
554+ foreach ( var elem in list )
555+ {
556+ if ( newValue . Equals ( elem . value ) )
557+ elem . target . style . display = DisplayStyle . Flex ;
558+ else
559+ elem . target . style . display = DisplayStyle . None ;
560+ }
561+ }
562+ }
563+
564+ protected VisualElement AddControlField ( string fieldName , string label = null , Action valueChangedCallback = null )
527565 => AddControlField ( nodeTarget . GetType ( ) . GetField ( fieldName , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) , label , valueChangedCallback ) ;
528566
529- protected void AddControlField ( FieldInfo field , string label = null , Action valueChangedCallback = null )
567+ protected VisualElement AddControlField ( FieldInfo field , string label = null , Action valueChangedCallback = null )
530568 {
531569 if ( field == null )
532- return ;
570+ return null ;
533571
534572 var element = FieldFactory . CreateField ( field . FieldType , field . GetValue ( nodeTarget ) , ( newValue ) => {
535573 owner . RegisterCompleteObjectUndo ( "Updated " + newValue ) ;
536574 field . SetValue ( nodeTarget , newValue ) ;
537575 NotifyNodeChanged ( ) ;
538576 valueChangedCallback ? . Invoke ( ) ;
577+ UpdateFieldVisibility ( field . Name , newValue ) ;
539578 } , label ) ;
540579
541580 if ( element != null )
542581 controlsContainer . Add ( element ) ;
582+
583+ var visibleCondition = field . GetCustomAttribute ( typeof ( VisibleIf ) ) as VisibleIf ;
584+ if ( visibleCondition != null )
585+ {
586+ // Check if target field exists:
587+ var conditionField = nodeTarget . GetType ( ) . GetField ( visibleCondition . fieldName , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . DeclaredOnly ) ;
588+ if ( conditionField == null )
589+ Debug . LogError ( $ "[VisibleIf] Field { visibleCondition . fieldName } does not exists in node { nodeTarget . GetType ( ) } ") ;
590+ else
591+ {
592+ visibleConditions . TryGetValue ( visibleCondition . fieldName , out var list ) ;
593+ if ( list == null )
594+ list = visibleConditions [ visibleCondition . fieldName ] = new List < ( object value , VisualElement target ) > ( ) ;
595+ list . Add ( ( visibleCondition . value , element ) ) ;
596+ // TODO
597+ UpdateFieldVisibility ( visibleCondition . fieldName , conditionField . GetValue ( nodeTarget ) ) ;
598+ }
599+ }
600+
601+ return element ;
543602 }
544603
545604 internal void OnPortConnected ( PortView port )
0 commit comments