Skip to content

Commit 3033e49

Browse files
committed
Added Setting Attribute to easily add setting fields
And also added two different use cases (one is without acustom node view, one with a custom node view)
1 parent 898f7a6 commit 3033e49

6 files changed

Lines changed: 75 additions & 4 deletions

File tree

Assets/Examples/DefaultNodes/Editor/IfNodeView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class IfNodeView : BaseNodeView
1212
{
1313
public override void Enable()
1414
{
15+
hasSettings = true; // or base.Enable();
1516
var node = nodeTarget as IfNode;
1617

1718
// Create your fields using node's variables and add them to the controlsContainer

Assets/Examples/DefaultNodes/Nodes/IfNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class IfNode : ConditionalNode
1717
[Output(name = "False")]
1818
public ConditionalLink @false;
1919

20+
[Setting("Compare Function")]
2021
public CompareFunction compareOperator;
2122

2223
public override string name => "If";

Assets/Examples/DefaultNodes/Nodes/MessageNode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ public class MessageNode : BaseNode
1414

1515
public override string name => "MessageNode";
1616

17+
[Setting("Message Type")]
18+
public NodeMessageType messageType = NodeMessageType.Error;
19+
1720
protected override void Process()
1821
{
1922
if (input != 42)
20-
AddMessage(k_InputIsNot42Error, NodeMessageType.Error);
23+
AddMessage(k_InputIsNot42Error, messageType);
2124
else
2225
RemoveMessage(k_InputIsNot42Error);
2326
}

Assets/com.alelievr.NodeGraphProcessor/Editor/Resources/GraphProcessorStyles/NodeSettings.uss

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
NodeSettingsView {
2-
width: 300px;
32
position: absolute;
3+
min-width: 110px;
4+
max-width: 300px;
45
-unity-slice-top: 40;
56
-unity-slice-left: 80;
67
-unity-slice-right: 25;
@@ -18,3 +19,26 @@ NodeSettingsView > #mainContainer {
1819
padding-right: 4px;
1920
padding-bottom: 4px;
2021
}
22+
23+
NodeSettingsView Label#header {
24+
padding-bottom: 4px;
25+
-unity-font-style: bold;
26+
}
27+
28+
NodeSettingsView Label.unity-text-element {
29+
min-width: 110px;
30+
font-size: 10px;
31+
padding-right: 6px;
32+
}
33+
34+
NodeSettingsView .unity-base-field > .unity-base-field__input {
35+
justify-content: flex-end;
36+
min-width: 20px;
37+
}
38+
39+
NodeSettingsView > #mainContainer > #contentContainer {
40+
padding-top: 4px;
41+
padding-left: 8px;
42+
padding-right: 8px;
43+
padding-bottom: 4px;
44+
}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class BaseNodeView : NodeView
3939
public event Action< PortView > onPortConnected;
4040
public event Action< PortView > onPortDisconnected;
4141

42-
protected virtual bool hasSettings => false;
42+
protected virtual bool hasSettings { get; set; }
4343

4444
public bool initializing = false; //Used for applying SetPosition on locked node at init.
4545

@@ -172,7 +172,12 @@ void InitializeSettings()
172172
settings.Add(CreateSettingsView());
173173
settingsContainer.Add(settings);
174174
Add(settingsContainer);
175+
176+
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
175177

178+
foreach(var field in fields)
179+
if(field.GetCustomAttribute(typeof(SettingAttribute)) != null)
180+
AddSettingField(field);
176181
}
177182
}
178183

@@ -554,6 +559,13 @@ protected virtual void DrawDefaultInspector(bool fromInspector = false)
554559

555560
foreach (var field in fields)
556561
{
562+
//skip if the field is a node setting
563+
if(field.GetCustomAttribute(typeof(SettingAttribute)) != null)
564+
{
565+
hasSettings = true;
566+
continue;
567+
}
568+
557569
//skip if the field is not serializable
558570
if(!field.IsPublic && field.GetCustomAttribute(typeof(SerializeField)) == null)
559571
{
@@ -734,6 +746,25 @@ void UpdateFieldValues()
734746
foreach (var kp in fieldControlsMap)
735747
UpdateOtherFieldValue(kp.Key, kp.Key.GetValue(nodeTarget));
736748
}
749+
750+
protected void AddSettingField(FieldInfo field)
751+
{
752+
if (field == null)
753+
return;
754+
755+
var label = field.GetCustomAttribute<SettingAttribute>().name;
756+
757+
var element = FieldFactory.CreateField(field.FieldType, field.GetValue(nodeTarget), (newValue) => {
758+
owner.RegisterCompleteObjectUndo("Updated " + newValue);
759+
field.SetValue(nodeTarget, newValue);
760+
}, label);
761+
762+
if(element != null)
763+
{
764+
settingsContainer.Add(element);
765+
element.name = field.Name;
766+
}
767+
}
737768

738769
internal void OnPortConnected(PortView port)
739770
{
@@ -970,7 +1001,7 @@ void UpdatePortsForField(string fieldName)
9701001
RefreshPorts();
9711002
}
9721003

973-
protected virtual VisualElement CreateSettingsView() => new Label("Settings");
1004+
protected virtual VisualElement CreateSettingsView() => new Label("Settings") {name = "header"};
9741005

9751006
/// <summary>
9761007
/// Send an event to the graph telling that the content of this node have changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,15 @@ public ShowInInspector(bool showInNode = false)
201201
public class ShowAsDrawer : Attribute
202202
{
203203
}
204+
205+
[AttributeUsage(AttributeTargets.Field)]
206+
public class SettingAttribute : Attribute
207+
{
208+
public string name;
209+
210+
public SettingAttribute(string name = null)
211+
{
212+
this.name = name;
213+
}
214+
}
204215
}

0 commit comments

Comments
 (0)