Skip to content

Commit 8083793

Browse files
author
Antoine Lelievre
authored
Merge pull request #96 from alelievr/setting-attribute
Added Setting Attribute to easily add setting fields
2 parents abca94e + 3033e49 commit 8083793

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
@@ -40,7 +40,7 @@ public class BaseNodeView : NodeView
4040
public event Action< PortView > onPortConnected;
4141
public event Action< PortView > onPortDisconnected;
4242

43-
protected virtual bool hasSettings => false;
43+
protected virtual bool hasSettings { get; set; }
4444

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

@@ -173,7 +173,12 @@ void InitializeSettings()
173173
settings.Add(CreateSettingsView());
174174
settingsContainer.Add(settings);
175175
Add(settingsContainer);
176+
177+
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
176178

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

@@ -555,6 +560,13 @@ protected virtual void DrawDefaultInspector(bool fromInspector = false)
555560

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

740771
internal void OnPortConnected(PortView port)
741772
{
@@ -972,7 +1003,7 @@ void UpdatePortsForField(string fieldName)
9721003
RefreshPorts();
9731004
}
9741005

975-
protected virtual VisualElement CreateSettingsView() => new Label("Settings");
1006+
protected virtual VisualElement CreateSettingsView() => new Label("Settings") {name = "header"};
9761007

9771008
/// <summary>
9781009
/// 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)