Skip to content

Commit 938830e

Browse files
committed
Added vertical port capability
1 parent 300f413 commit 938830e

File tree

12 files changed

+189
-36
lines changed

12 files changed

+189
-36
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using UnityEngine;
2+
using GraphProcessor;
3+
4+
[System.Serializable, NodeMenuItem("Custom/Vertical")]
5+
public class VerticalNode : BaseNode
6+
{
7+
[Input, Vertical]
8+
public float input;
9+
10+
[Output, Vertical]
11+
public float output;
12+
[Output, Vertical]
13+
public float output2;
14+
[Output, Vertical]
15+
public float output3;
16+
17+
public override string name => "Vertical";
18+
19+
protected override void Process()
20+
{
21+
output = input * 42;
22+
}
23+
}

Assets/Examples/DefaultNodes/Nodes/VerticalNode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine;
2+
using GraphProcessor;
3+
4+
[System.Serializable, NodeMenuItem("Custom/Vertical 2")]
5+
public class VerticalNode2 : BaseNode
6+
{
7+
[Input, Vertical]
8+
public float input;
9+
10+
[Input]
11+
public float input2;
12+
13+
[Output, Vertical]
14+
public float output;
15+
16+
[Output]
17+
public float output2;
18+
19+
public override string name => "Vertical 2";
20+
21+
protected override void Process()
22+
{
23+
output = input * 42;
24+
}
25+
}

Assets/Examples/DefaultNodes/Nodes/VerticalNode2.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,19 @@ ParameterNodeView #controls EnumField > VisualElement > VisualElement {
215215
padding-bottom: 1px;
216216
height: 15px;
217217
}
218+
219+
#TopPortContainer, #BottomPortContainer
220+
{
221+
align-self: center;
222+
flex-direction: row;
223+
}
224+
225+
#TopPortContainer
226+
{
227+
margin-bottom: -1px;
228+
}
229+
230+
#BottomPortContainer
231+
{
232+
margin-top: -1px;
233+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ PortView > #connector > #cap:hover
7373

7474
.Port_Color {
7575
--port-color: #FF00FF;
76-
}
76+
}
77+
78+
.Vertical
79+
{
80+
height: 6px;
81+
}

Assets/com.alelievr.NodeGraphProcessor/Editor/Utils/BaseEdgeDragHelper.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public override bool HandleMouseDown(MouseDownEvent evt)
150150

151151
// Sort ports by position in the node
152152
foreach (var kp in compatiblePorts)
153-
kp.Value.Sort((e1, e2) => e1.layout.y.CompareTo(e2.layout.y));
153+
kp.Value.Sort((e1, e2) => e1.worldBound.y.CompareTo(e2.worldBound.y));
154154

155155
// Only light compatible anchors when dragging an edge.
156156
graphView.ports.ForEach((p) => {
@@ -359,32 +359,41 @@ Rect GetPortBounds(BaseNodeView nodeView, int index, List<PortView> portList)
359359
var port = portList[index];
360360
var bounds = port.worldBound;
361361

362-
bounds.xMin = nodeView.worldBound.xMin;
363-
bounds.xMax = nodeView.worldBound.xMax;
364-
365-
if (index == 0)
366-
bounds.yMin = nodeView.worldBound.yMin;
367-
if (index == portList.Count - 1)
368-
bounds.yMax = nodeView.worldBound.yMax;
369-
370-
if (index > 0)
371-
{
372-
Rect above = portList[index - 1].worldBound;
373-
bounds.yMin = (above.yMax + bounds.yMin) / 2.0f;
374-
}
375-
if (index < portList.Count - 1)
376-
{
377-
Rect below = portList[index + 1].worldBound;
378-
bounds.yMax = (below.yMin + bounds.yMax) / 2.0f;
379-
}
380-
381362
if (port.orientation == Orientation.Horizontal)
382363
{
364+
// Increase horizontal port bounds
365+
bounds.xMin = nodeView.worldBound.xMin;
366+
bounds.xMax = nodeView.worldBound.xMax;
367+
368+
if (index == 0)
369+
bounds.yMin = nodeView.worldBound.yMin;
370+
if (index == portList.Count - 1)
371+
bounds.yMax = nodeView.worldBound.yMax;
372+
373+
if (index > 0)
374+
{
375+
Rect above = portList[index - 1].worldBound;
376+
bounds.yMin = (above.yMax + bounds.yMin) / 2.0f;
377+
}
378+
if (index < portList.Count - 1)
379+
{
380+
Rect below = portList[index + 1].worldBound;
381+
bounds.yMax = (below.yMin + bounds.yMax) / 2.0f;
382+
}
383+
383384
if (port.direction == Direction.Input)
384385
bounds.xMin -= kPortDetectionWidth;
385386
else
386387
bounds.xMax += kPortDetectionWidth;
387388
}
389+
else
390+
{
391+
// Increase vertical port bounds
392+
if (port.direction == Direction.Input)
393+
bounds.yMin -= kPortDetectionWidth;
394+
else
395+
bounds.yMax += kPortDetectionWidth;
396+
}
388397

389398
return bounds;
390399
}

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class BaseNodeView : NodeView
2929
public VisualElement controlsContainer;
3030
protected VisualElement debugContainer;
3131
protected VisualElement rightTitleContainer;
32+
protected VisualElement topPortContainer;
33+
protected VisualElement bottomPortContainer;
3234
private VisualElement inputContainerElement;
3335

3436
VisualElement settings;
@@ -83,8 +85,8 @@ public void Initialize(BaseGraphView owner, BaseNode node)
8385
if (!string.IsNullOrEmpty(node.layoutStyle))
8486
styleSheets.Add(Resources.Load<StyleSheet>(node.layoutStyle));
8587

86-
InitializePorts();
8788
InitializeView();
89+
InitializePorts();
8890
InitializeDebug();
8991

9092
// If the standard Enable method is still overwritten, we call it
@@ -127,6 +129,12 @@ void InitializeView()
127129
rightTitleContainer = new VisualElement{ name = "RightTitleContainer" };
128130
titleContainer.Add(rightTitleContainer);
129131

132+
topPortContainer = new VisualElement { name = "TopPortContainer" };
133+
this.Insert(0, topPortContainer);
134+
135+
bottomPortContainer = new VisualElement { name = "BottomPortContainer" };
136+
this.Add(bottomPortContainer);
137+
130138
if (nodeTarget.showControlsOnHover)
131139
{
132140
bool mouseOverControls = false;
@@ -274,18 +282,25 @@ public PortView GetPortViewFromFieldName(string fieldName, string identifier)
274282

275283
public PortView AddPort(FieldInfo fieldInfo, Direction direction, BaseEdgeConnectorListener listener, PortData portData)
276284
{
277-
// TODO: hardcoded value
278-
PortView p = PortView.CreatePV(Orientation.Horizontal, direction, fieldInfo, portData, listener);
285+
PortView p = PortView.CreatePV(direction, fieldInfo, portData, listener);
279286

280287
if (p.direction == Direction.Input)
281288
{
282289
inputPortViews.Add(p);
283-
inputContainer.Add(p);
290+
291+
if (portData.vertical)
292+
topPortContainer.Add(p);
293+
else
294+
inputContainer.Add(p);
284295
}
285296
else
286297
{
287298
outputPortViews.Add(p);
288-
outputContainer.Add(p);
299+
300+
if (portData.vertical)
301+
bottomPortContainer.Add(p);
302+
else
303+
outputContainer.Add(p);
289304
}
290305

291306
p.Initialize(this, portData?.displayName);
@@ -305,9 +320,19 @@ public PortView AddPort(FieldInfo fieldInfo, Direction direction, BaseEdgeConnec
305320
public void InsertPort(PortView portView, int index)
306321
{
307322
if (portView.direction == Direction.Input)
308-
inputContainer.Insert(index, portView);
323+
{
324+
if (portView.portData.vertical)
325+
topPortContainer.Insert(index, portView);
326+
else
327+
inputContainer.Insert(index, portView);
328+
}
309329
else
310-
outputContainer.Insert(index, portView);
330+
{
331+
if (portView.portData.vertical)
332+
bottomPortContainer.Insert(index, portView);
333+
else
334+
outputContainer.Insert(index, portView);
335+
}
311336
}
312337

313338
public void RemovePort(PortView p)

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class PortView : Port
3030

3131
readonly string portStyle = "GraphProcessorStyles/PortView";
3232

33-
PortView(Orientation orientation, Direction direction, FieldInfo fieldInfo, PortData portData, BaseEdgeConnectorListener edgeConnectorListener)
34-
: base(orientation, direction, Capacity.Multi, portData.displayType ?? fieldInfo.FieldType)
33+
PortView(Direction direction, FieldInfo fieldInfo, PortData portData, BaseEdgeConnectorListener edgeConnectorListener)
34+
: base(portData.vertical ? Orientation.Vertical : Orientation.Horizontal, direction, Capacity.Multi, portData.displayType ?? fieldInfo.FieldType)
3535
{
3636
this.fieldInfo = fieldInfo;
3737
this.listener = edgeConnectorListener;
@@ -47,12 +47,15 @@ public class PortView : Port
4747
if (userPortStyle != null)
4848
styleSheets.Add(userPortStyle);
4949

50+
if (portData.vertical)
51+
AddToClassList("Vertical");
52+
5053
this.tooltip = portData.tooltip;
5154
}
5255

53-
public static PortView CreatePV(Orientation orientation, Direction direction, FieldInfo fieldInfo, PortData portData, BaseEdgeConnectorListener edgeConnectorListener)
56+
public static PortView CreatePV(Direction direction, FieldInfo fieldInfo, PortData portData, BaseEdgeConnectorListener edgeConnectorListener)
5457
{
55-
var pv = new PortView(orientation, direction, fieldInfo, portData, edgeConnectorListener);
58+
var pv = new PortView(direction, fieldInfo, portData, edgeConnectorListener);
5659
pv.m_EdgeConnector = new BaseEdgeConnector(edgeConnectorListener);
5760
pv.AddManipulator(pv.m_EdgeConnector);
5861

@@ -64,6 +67,14 @@ public static PortView CreatePV(Orientation orientation, Direction direction, Fi
6467
portLabel.style.flexGrow = 1;
6568
}
6669

70+
// hide label when the port is vertical
71+
if (portData.vertical && portLabel != null)
72+
portLabel.style.display = DisplayStyle.None;
73+
74+
// Fixup picking mode for vertical top ports
75+
if (portData.vertical)
76+
pv.Q("connector").pickingMode = PickingMode.Position;
77+
6778
return pv;
6879
}
6980

Assets/com.alelievr.NodeGraphProcessor/Runtime/Elements/BaseNode.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ internal class NodeFieldInformation
132132
public bool isMultiple;
133133
public string tooltip;
134134
public CustomPortBehaviorDelegate behavior;
135+
public bool vertical;
135136

136-
public NodeFieldInformation(FieldInfo info, string name, bool input, bool isMultiple, string tooltip, CustomPortBehaviorDelegate behavior)
137+
public NodeFieldInformation(FieldInfo info, string name, bool input, bool isMultiple, string tooltip, bool vertical, CustomPortBehaviorDelegate behavior)
137138
{
138139
this.input = input;
139140
this.isMultiple = isMultiple;
@@ -142,6 +143,7 @@ public NodeFieldInformation(FieldInfo info, string name, bool input, bool isMult
142143
this.fieldName = info.Name;
143144
this.behavior = behavior;
144145
this.tooltip = tooltip;
146+
this.vertical = vertical;
145147
}
146148
}
147149

@@ -258,7 +260,7 @@ public virtual void InitializePorts()
258260
else
259261
{
260262
// If we don't have a custom behavior on the node, we just have to create a simple port
261-
AddPort(nodeField.input, nodeField.fieldName, new PortData { acceptMultipleEdges = nodeField.isMultiple, displayName = nodeField.name, tooltip = nodeField.tooltip });
263+
AddPort(nodeField.input, nodeField.fieldName, new PortData { acceptMultipleEdges = nodeField.isMultiple, displayName = nodeField.name, tooltip = nodeField.tooltip, vertical = nodeField.vertical });
262264
}
263265
}
264266
}
@@ -481,6 +483,7 @@ void InitializeInOutDatas()
481483
var outputAttribute = field.GetCustomAttribute< OutputAttribute >();
482484
var tooltipAttribute = field.GetCustomAttribute< TooltipAttribute >();
483485
var showInInspector = field.GetCustomAttribute< ShowInInspector >();
486+
var vertical = field.GetCustomAttribute< VerticalAttribute >();
484487
bool isMultiple = false;
485488
bool input = false;
486489
string name = field.Name;
@@ -503,7 +506,7 @@ void InitializeInOutDatas()
503506
name = outputAttribute.name;
504507

505508
// By default we set the behavior to null, if the field have a custom behavior, it will be set in the loop just below
506-
nodeFields[field.Name] = new NodeFieldInformation(field, name, input, isMultiple, tooltip, null);
509+
nodeFields[field.Name] = new NodeFieldInformation(field, name, input, isMultiple, tooltip, vertical != null, null);
507510
}
508511

509512
foreach (var method in methods)

0 commit comments

Comments
 (0)