Skip to content

Commit

Permalink
added MakeArrayNode
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinKa committed Mar 17, 2019
1 parent 6b3ec0e commit 63ca35d
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 1 deletion.
89 changes: 89 additions & 0 deletions NetPrints/Graph/MakeArrayNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using NetPrints.Core;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace NetPrints.Graph
{
/// <summary>
/// Node representing the creation of an array.
/// </summary>
[DataContract]
public class MakeArrayNode : Node
{
/// <summary>
/// Specifier for the type of the elements of the array.
/// </summary>
[DataMember]
public BaseType ElementType
{
get;
private set;
}

/// <summary>
/// Specifier for the type of the array.
/// </summary>
public TypeSpecifier ArrayType
{
get
{
if (ElementType is TypeSpecifier typeSpec)
{
return new TypeSpecifier($"{typeSpec.Name}[]", typeSpec.IsEnum, typeSpec.IsInterface, typeSpec.GenericArguments);
}
else
{
throw new NotImplementedException("Can only have arrays of TypeSpecifier ElementType.");
}

}
}

/// <summary>
/// Creates a new node representing the creation of an array.
/// </summary>
/// <param name="method">Method the node is part of.</param>
/// <param name="elementType">Type specifier for the elements of the array.</param>
public MakeArrayNode(Method method, BaseType elementType)
: base(method)
{
ElementType = elementType;

AddOutputDataPin(elementType.ShortName, ArrayType);
}

/// <summary>
/// Adds an input data pin for an array element.
/// </summary>
public void AddElementPin()
{
AddInputDataPin($"Element{InputDataPins.Count}", ElementType);
}

/// <summary>
/// Removes the last input data pin for an array element.
/// Returns whether one was actually removed.
/// </summary>
/// <returns>Whether a pin was removed.</returns>
public bool RemoveElementPin()
{
if (InputDataPins.Count > 0)
{
// TODO: Add method for removing pins on Node
NodeInputDataPin inputDataPin = InputDataPins[InputDataPins.Count - 1];
GraphUtil.DisconnectInputDataPin(inputDataPin);
InputDataPins.Remove(inputDataPin);

return true;
}

return false;
}

public override string ToString()
{
return $"Make {ElementType.Name} Array";
}
}
}
1 change: 1 addition & 0 deletions NetPrints/Graph/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace NetPrints.Graph
[KnownType(typeof(TypeOfNode))]
[KnownType(typeof(ExplicitCastNode))]
[KnownType(typeof(RerouteNode))]
[KnownType(typeof(MakeArrayNode))]
public abstract class Node
{
/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions NetPrints/Translator/MethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class MethodTranslator
{ typeof(LiteralNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateLiteralNode(node as LiteralNode) } },
{ typeof(MakeDelegateNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateMakeDelegateNode(node as MakeDelegateNode) } },
{ typeof(TypeOfNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateTypeOfNode(node as TypeOfNode) } },
{ typeof(MakeArrayNode), new List<NodeTypeHandler> { (translator, node) => translator.PureTranslateMakeArrayNode(node as MakeArrayNode) } },
};

private int GetNextStateId()
Expand Down Expand Up @@ -758,6 +759,19 @@ public void PureTranslateTypeOfNode(TypeOfNode node)
builder.AppendLine($"{GetOrCreatePinName(node.TypePin)} = typeof({node.Type.FullCodeNameUnbound});");
}

public void PureTranslateMakeArrayNode(MakeArrayNode node)
{
builder.AppendLine($"{GetOrCreatePinName(node.OutputDataPins[0])} = new {node.ArrayType.FullCodeName}");
builder.AppendLine("{");

foreach (var inputDataPin in node.InputDataPins)
{
builder.AppendLine($"{GetPinIncomingValue(inputDataPin)},");
}

builder.AppendLine("};");
}

public void TranslateRerouteNode(RerouteNode node)
{
if ((node.ExecRerouteCount != 1 && node.DataRerouteCount != 1) || (node.ExecRerouteCount == 1 && node.DataRerouteCount == 1))
Expand Down
3 changes: 2 additions & 1 deletion NetPrints/Translator/TranslatorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public static string ObjectToLiteral(object obj, TypeSpecifier type)
public static string GetUniqueVariableName(string name, IList<string> names)
{
// Don't allow illegal characters in the name
name = name.Replace("+", "_");
// TODO: Make this more general
name = name.Replace("+", "_").Replace("[", "").Replace("]", "Array").Replace(",", "");

int i = 1;

Expand Down
1 change: 1 addition & 0 deletions NetPrintsEditor/Controls/MethodEditorControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public ObservableRangeCollection<object> Suggestions
TypeSpecifier.FromType<TypeOfNode>(),
TypeSpecifier.FromType<ExplicitCastNode>(),
TypeSpecifier.FromType<ReturnNode>(),
TypeSpecifier.FromType<MakeArrayNode>(),
};

public MethodEditorControl()
Expand Down
5 changes: 5 additions & 0 deletions NetPrintsEditor/Controls/NodeControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

<StackPanel Orientation="Horizontal" Visibility="{Binding ElementName=nodeWindow, Path=Node.ShowLeftPinButtons, Converter={StaticResource ResourceKey=BoolToVis}}">
<Button Width="24" Height="24" Content="+" Click="OnLeftPinsPlusClicked" />
<Button Width="24" Height="24" Content="-" Click="OnLeftPinsMinusClicked" />
</StackPanel>
</StackPanel>

<StackPanel Grid.Column="1">
Expand Down
10 changes: 10 additions & 0 deletions NetPrintsEditor/Controls/NodeControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,15 @@ private void NodeVariants_SelectionChanged(object sender, SelectionChangedEventA
));
}
}

private void OnLeftPinsPlusClicked(object sender, RoutedEventArgs e)
{
Node.LeftPinsPlusClicked();
}

private void OnLeftPinsMinusClicked(object sender, RoutedEventArgs e)
{
Node.LeftPinsMinusClicked();
}
}
}
26 changes: 26 additions & 0 deletions NetPrintsEditor/Controls/SearchableComboBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ private void OnListItemSelected(object sender, MouseButtonEventArgs e)
0
));
}
else if (t == TypeSpecifier.FromType<MakeArrayNode>())
{
SelectTypeDialog selectTypeDialog = new SelectTypeDialog();
if (selectTypeDialog.ShowDialog() == true)
{
TypeSpecifier selectedType = selectTypeDialog.SelectedType;

if (selectedType.Equals(null))
{
throw new Exception($"Type {selectTypeDialog.SelectedType} was not found using reflection.");
}

// TypeOfNode(Method method, TypeSpecifier type)

UndoRedoStack.Instance.DoCommand(NetPrintsCommands.AddNode, new NetPrintsCommands.AddNodeParameters
(
typeof(MakeArrayNode),
null,
0,
0,

// Parameters
selectedType
));
}
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions NetPrintsEditor/Converters/SuggestionListConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
text = "NetPrints - Return";
iconPath = "Return_16x.png";
}
else if (t == TypeSpecifier.FromType<MakeArrayNode>())
{
text = "NetPrints - Make Array";
iconPath = "ListView_16x.png";
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions NetPrintsEditor/NetPrintsEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<None Remove="Resources\Create_16x.png" />
<None Remove="Resources\Delegate_16x.png" />
<None Remove="Resources\If_16x.png" />
<None Remove="Resources\ListView_16x.png" />
<None Remove="Resources\Loop_16x.png" />
<None Remove="Resources\Method_16x.png" />
<None Remove="Resources\Operator_16x.png" />
Expand All @@ -32,6 +33,7 @@
<Resource Include="Resources\Create_16x.png" />
<Resource Include="Resources\Delegate_16x.png" />
<Resource Include="Resources\If_16x.png" />
<Resource Include="Resources\ListView_16x.png" />
<Resource Include="Resources\Loop_16x.png" />
<Resource Include="Resources\Method_16x.png" />
<Resource Include="Resources\Operator_16x.png" />
Expand Down
Binary file added NetPrintsEditor/Resources/ListView_16x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions NetPrintsEditor/ViewModels/NodeVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public Node Node
OnPropertyChanged(nameof(ToolTip));
OnPropertyChanged(nameof(Overloads));
OnPropertyChanged(nameof(IsRerouteNode));
OnPropertyChanged(nameof(ShowLeftPinButtons));
}
}
}
Expand Down Expand Up @@ -362,6 +363,31 @@ public double PositionY
}
}

/// <summary>
/// Whether we should show the +/- buttons under the
/// left pins.
/// </summary>
public bool ShowLeftPinButtons
{
get => node is MakeArrayNode;
}

/// <summary>
/// Called when the left pins' plus button was clicked.
/// </summary>
public void LeftPinsPlusClicked()
{
(node as MakeArrayNode)?.AddElementPin();
}

/// <summary>
/// Called when the right pins' minus button was clicked.
/// </summary>
public void LeftPinsMinusClicked()
{
(node as MakeArrayNode)?.RemoveElementPin();
}

private Node node;

public NodeVM(Node node)
Expand Down

0 comments on commit 63ca35d

Please sign in to comment.