Skip to content

Commit

Permalink
added reroute nodes (no special view for them yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinKa committed Mar 15, 2019
1 parent 3a83371 commit 617cbd2
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 1 deletion.
43 changes: 43 additions & 0 deletions NetPrints/Graph/GraphUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,48 @@ public static void DisconnectInputExecPin(NodeInputExecPin pin)

pin.IncomingPins.Clear();
}

/// <summary>
/// Adds a data reroute node and does the necessary rewiring.
/// </summary>
/// <param name="pin">Data pin to add reroute node for.</param>
/// <returns>Reroute node created for the data pin.</returns>
public static RerouteNode AddRerouteNode(NodeInputDataPin pin)
{
if (pin?.IncomingPin == null)
{
throw new ArgumentException("Pin or its connected pin were null");
}

var rerouteNode = new RerouteNode(pin.Node.Method, 0, new Tuple<BaseType, BaseType>[]
{
new Tuple<BaseType, BaseType>(pin.PinType, pin.IncomingPin.PinType)
});

GraphUtil.ConnectDataPins(pin.IncomingPin, rerouteNode.InputDataPins[0]);
GraphUtil.ConnectDataPins(rerouteNode.OutputDataPins[0], pin);

return rerouteNode;
}

/// <summary>
/// Adds an execution reroute node and does the necessary rewiring.
/// </summary>
/// <param name="pin">Execution pin to add reroute node for.</param>
/// <returns>Reroute node created for the execution pin.</returns>
public static RerouteNode AddRerouteNode(NodeOutputExecPin pin)
{
if (pin?.OutgoingPin == null)
{
throw new ArgumentException("Pin or its connected pin were null");
}

var rerouteNode = new RerouteNode(pin.Node.Method, 1, null);

GraphUtil.ConnectExecPins(rerouteNode.OutputExecPins[0], pin.OutgoingPin);
GraphUtil.ConnectExecPins(pin, rerouteNode.InputExecPins[0]);

return rerouteNode;
}
}
}
1 change: 1 addition & 0 deletions NetPrints/Graph/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace NetPrints.Graph
[KnownType(typeof(MakeDelegateNode))]
[KnownType(typeof(TypeOfNode))]
[KnownType(typeof(ExplicitCastNode))]
[KnownType(typeof(RerouteNode))]
public abstract class Node
{
/// <summary>
Expand Down
43 changes: 43 additions & 0 deletions NetPrints/Graph/RerouteNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NetPrints.Core;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace NetPrints.Graph
{
/// <summary>
/// Node representing a reroute node. Does nothing by itself.
/// Used for layouting in the editor.
/// </summary>
[DataContract]
public class RerouteNode : Node
{
public int ExecRerouteCount { get => InputExecPins.Count; }
public int DataRerouteCount { get => InputDataPins.Count; }

public RerouteNode(Method method, int numExecs, IEnumerable<Tuple<BaseType, BaseType>> dataTypes)
: base(method)
{
for (int i = 0; i < numExecs; i++)
{
AddInputExecPin($"Exec{i}");
AddOutputExecPin($"Exec{i}");
}

if (dataTypes != null)
{
int index = 0;
foreach (var dataType in dataTypes)
{
AddInputDataPin($"Data{index}", dataType.Item1);
AddOutputDataPin($"Data{index}", dataType.Item2);
}
}
}

public override string ToString()
{
return "Reroute";
}
}
}
19 changes: 19 additions & 0 deletions NetPrints/Translator/MethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class MethodTranslator
(translator, node) => translator.TranslateStartForLoopNode(node as ForLoopNode),
(translator, node) => translator.TranslateContinueForLoopNode(node as ForLoopNode)} },

{ typeof(RerouteNode), new List<NodeTypeHandler> { (translator, node) => translator.TranslateRerouteNode(node as RerouteNode) } },

{ typeof(VariableGetterNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateVariableGetterNode(node as VariableGetterNode) } },
{ typeof(LiteralNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateLiteralNode(node as LiteralNode) } },
{ typeof(MakeDelegateNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateMakeDelegateNode(node as MakeDelegateNode) } },
Expand Down Expand Up @@ -730,5 +732,22 @@ public void PureTranslateTypeOfNode(TypeOfNode node)
{
builder.AppendLine($"{GetOrCreatePinName(node.TypePin)} = typeof({node.Type.FullCodeNameUnbound});");
}

public void TranslateRerouteNode(RerouteNode node)
{
if ((node.ExecRerouteCount != 1 && node.DataRerouteCount != 1) || (node.ExecRerouteCount == 1 && node.DataRerouteCount == 1))
{
throw new NotImplementedException("Only implemented reroute nodes with 1 execution or 1 data pin.");
}

if (node.DataRerouteCount == 1)
{
builder.AppendLine($"{GetOrCreatePinName(node.OutputDataPins[0])} = {GetOrCreatePinName(node.InputDataPins[0].IncomingPin)};");
}
else if (node.ExecRerouteCount == 1)
{
WriteGotoOutputPin(node.OutputExecPins[0]);
}
}
}
}
2 changes: 1 addition & 1 deletion NetPrintsEditor/Controls/MethodEditorControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

<ItemsControl.ItemTemplate>
<DataTemplate>
<Path MouseEnter="CablePath_MouseEnter" MouseLeave="CablePath_MouseLeave" x:Name="cablePath" Visibility="{Binding IsCableVisible, Converter={StaticResource ResourceKey=BoolToVis}}"
<Path MouseDown="CablePath_MouseDown" MouseEnter="CablePath_MouseEnter" MouseLeave="CablePath_MouseLeave" x:Name="cablePath" Visibility="{Binding IsCableVisible, Converter={StaticResource ResourceKey=BoolToVis}}"
Stroke="{Binding Brush}" StrokeThickness="4" Opacity="0.7" StrokeStartLineCap="Round" StrokeEndLineCap="Round">
<Path.Data>
<PathGeometry>
Expand Down
16 changes: 16 additions & 0 deletions NetPrintsEditor/Controls/MethodEditorControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,21 @@ private void CablePath_MouseLeave(object sender, MouseEventArgs e)
path.Opacity = 0.7;
}
}

private void CablePath_MouseDown(object sender, MouseButtonEventArgs e)
{
var element = sender as FrameworkElement;
var pin = element?.DataContext as NodePinVM;

if (pin == null)
{
throw new Exception("Could not find cable's pin.");
}

if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed &&e.ClickCount == 2)
{
pin.AddRerouteNode();
}
}
}
}
24 changes: 24 additions & 0 deletions NetPrintsEditor/ViewModels/NodePinVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ public NodePinVM(NodePin pin)
Pin = pin;
}

/// <summary>
/// Adds a reroute node for this pin. Only valid
/// for input data pins and output execution pins.
/// </summary>
public void AddRerouteNode()
{
if (Pin is NodeInputDataPin dataPin)
{
RerouteNode rerouteNode = GraphUtil.AddRerouteNode(dataPin);
rerouteNode.PositionX = (Pin.Node.PositionX + dataPin.IncomingPin.Node.PositionX) / 2;
rerouteNode.PositionY = (Pin.Node.PositionY + dataPin.IncomingPin.Node.PositionY) / 2;
}
else if (Pin is NodeOutputExecPin execPin)
{
RerouteNode rerouteNode = GraphUtil.AddRerouteNode(execPin);
rerouteNode.PositionX = (Pin.Node.PositionX + execPin.OutgoingPin.Node.PositionX) / 2;
rerouteNode.PositionY = (Pin.Node.PositionY + execPin.OutgoingPin.Node.PositionY) / 2;
}
else
{
throw new Exception("Can't add reroute node for invalid pin type");
}
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;

Expand Down

0 comments on commit 617cbd2

Please sign in to comment.