Skip to content

Commit

Permalink
feature: add auto-documentation to reroute nodes
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
derkork committed Sep 5, 2022
1 parent 3cd49d1 commit be0a8da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
47 changes: 47 additions & 0 deletions Widgets/RerouteNodeWidget.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Godot;
using GodotExt;
using OpenScadGraphEditor.Library;
Expand All @@ -13,6 +14,8 @@ public class RerouteNodeWidget : SmallNodeWidget
protected override Theme UseTheme => Resources.SimpleNodeWidgetTheme;
public override bool RenderTitle => false;

private ScadGraph _boundGraph;

public override void _Ready()
{
base._Ready();
Expand All @@ -30,13 +33,57 @@ public override void BindTo(ScadGraph graph, ScadNode node)
{
BoundNode = node;
Offset = node.Offset;
_boundGraph = graph;
RefreshType();
if (_portType.IsExpressionType())
{
HintTooltip = ((RerouteNode) node).Render(graph, 0);
}
}

protected override bool TryGetComment(out string comment)
{
comment = default;

if (_boundGraph == null || BoundNode == null)
{
return false;
}

if (BoundNode.TryGetComment(out comment))
{
return true; // if the node has a manual comment, use this one.
}

var referenceNode = BoundNode;

// try to find a predecessor that has a comment
// we will skip over reroute nodes that have no comment until we reach a non-reroute node.
do
{
var predecessor = _boundGraph.GetAllConnections().FirstOrDefault(it => it.To == referenceNode)?.From;
if (predecessor == null)
{
return false;
}

if (predecessor.TryGetComment(out comment))
{
return true;
}

if (predecessor is RerouteNode)
{
// try again
referenceNode = predecessor;
}
else
{
return false;
}
} while (true);
}

private void RefreshType()
{
_portType = BoundNode.GetPortType(PortId.Input(0));
Expand Down
7 changes: 6 additions & 1 deletion Widgets/ScadNodeWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public override void _Notification(int what)
}

// draw comment
if (BoundNode.TryGetComment(out var comment))
if (TryGetComment(out var comment))
{
// calculate how big the comment will be.
var commentSize = _commentFont.GetStringSize(comment);
Expand All @@ -408,6 +408,11 @@ public override void _Notification(int what)
}
}

protected virtual bool TryGetComment(out string comment)
{
return BoundNode.TryGetComment(out comment);
}

protected void RaiseSizeChanged(Vector2 newSize)
{
SizeChanged?.Invoke(newSize);
Expand Down

0 comments on commit be0a8da

Please sign in to comment.