Skip to content

Commit fe070c5

Browse files
author
Antoine Lelièvre
committed
Added VisibleIf Atribute
1 parent 9114ab6 commit fe070c5

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseNodeView.cs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Assets/com.alelievr.NodeGraphProcessor/Runtime/Graph/Attributes.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,17 @@ public CustomStackNodeView(Type stackNodeType)
172172
this.stackNodeType = stackNodeType;
173173
}
174174
}
175+
176+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
177+
public class VisibleIf : Attribute
178+
{
179+
public string fieldName;
180+
public object value;
181+
182+
public VisibleIf(string fieldName, object value)
183+
{
184+
this.fieldName = fieldName;
185+
this.value = value;
186+
}
187+
}
175188
}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [0.7.0]
88

9+
### Added
10+
- VisibleIf attribute in nodes, allow you to show fields only when another field have a specific value.
11+
12+
## [0.7.0]
13+
914
### Added
1015
- Added a method to call the onProcess callback in the graph
1116
- Support of multiple [NodeMenuItemAttribute] on the same class

0 commit comments

Comments
 (0)