Skip to content

Commit

Permalink
[DYN-7838] Color range Node: Show Input ports with default value (Dyn…
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaylo-matov authored Feb 21, 2025
1 parent 45fe04e commit 02b3fc6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
73 changes: 70 additions & 3 deletions src/Libraries/CoreNodeModels/ColorRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ namespace CoreNodeModels
[AlsoKnownAs("DSCoreNodesUI.ColorRange")]
public class ColorRange : NodeModel
{
private IEnumerable<Color> defaultColors;
private IEnumerable<Color> DefaultColors => defaultColors ??= DefaultColorRanges.Analysis.ToList();

private AssociativeNode defaultColorsNode;
private AssociativeNode DefaultColorsNode => defaultColorsNode ??= CreateDefaultColorsNode(DefaultColors);

private AssociativeNode defaultIndicesNode;
private AssociativeNode DefaultIndicesNode => defaultIndicesNode ??= CreateDefaultIndicesNode(DefaultColors);

public event Action RequestChangeColorRange;
protected virtual void OnRequestChangeColorRange()
{
Expand All @@ -42,6 +51,18 @@ protected virtual void OnRequestChangeColorRange()
[JsonConstructor]
private ColorRange(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
{
if (inPorts.Count() == 3 && outPorts.Count() == 1)
{
inPorts.ElementAt(0).DefaultValue = DefaultColorsNode;
inPorts.ElementAt(1).DefaultValue = DefaultIndicesNode;
}
else
{
// If information from json does not look correct, clear the default ports and add ones with default value
InPorts.Clear();
InitializePorts();
}

this.PropertyChanged += ColorRange_PropertyChanged;
foreach (var port in InPorts)
{
Expand All @@ -51,6 +72,8 @@ private ColorRange(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPor

public ColorRange()
{
// Initialize default values of the ports
InitializePorts();
RegisterAllPorts();

this.PropertyChanged += ColorRange_PropertyChanged;
Expand All @@ -60,6 +83,14 @@ public ColorRange()
}
}

private void InitializePorts()
{
InPorts.Add(new PortModel(PortType.Input, this, new PortData("colors", Resources.ColorRangePortDataColorsToolTip, DefaultColorsNode)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("indices", Resources.ColorRangePortDataIndicesToolTip, DefaultIndicesNode)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("value", Resources.ColorRangePortDataValueToolTip)));
OutPorts.Add(new PortModel(PortType.Output, this, new PortData("color", Resources.ColorRangePortDataResultToolTip)));
}

void Connectors_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnRequestChangeColorRange();
Expand Down Expand Up @@ -119,7 +150,7 @@ public ColorRange1D ComputeColorRange(EngineController engine)
List<double> parameters;

// If there are colors supplied
if (InPorts[0].IsConnected)
if (InPorts[0].Connectors.Any())
{
var colorsNode = InPorts[0].Connectors[0].Start.Owner;
var colorsIndex = InPorts[0].Connectors[0].Start.Index;
Expand All @@ -131,10 +162,14 @@ public ColorRange1D ComputeColorRange(EngineController engine)
{
colors = new List<Color>();
colors.AddRange(DefaultColorRanges.Analysis);

// Create an AssociativeNode for the default colors
InPorts[0].DefaultValue = DefaultColorsNode;
InPorts[0].UsingDefaultValue = true;
}

// If there are indices supplied
if (InPorts[1].IsConnected)
if (InPorts[1].Connectors.Any())
{
var valuesNode = InPorts[1].Connectors[0].Start.Owner;
var valuesIndex = InPorts[1].Connectors[0].Start.Index;
Expand All @@ -145,12 +180,44 @@ public ColorRange1D ComputeColorRange(EngineController engine)
else
{
parameters = CreateParametersForColors(colors);

// Create an AssociativeNode for the default indices
InPorts[1].DefaultValue = DefaultIndicesNode;
InPorts[1].UsingDefaultValue = true;
}

return ColorRange1D.ByColorsAndParameters(colors, parameters);
}

private static List<double> CreateParametersForColors(List<Color> colors)
private AssociativeNode CreateDefaultColorsNode(IEnumerable<Color> defaultColors)
{
return AstFactory.BuildExprList(
defaultColors.Select(color =>
AstFactory.BuildFunctionCall(
new Func<int, int, int, int, Color>(DSCore.Color.ByARGB),
new List<AssociativeNode>
{
AstFactory.BuildIntNode(color.Red),
AstFactory.BuildIntNode(color.Green),
AstFactory.BuildIntNode(color.Blue)
}
)
).ToList()
);
}

private AssociativeNode CreateDefaultIndicesNode(IEnumerable<Color> defaultColors)
{
var parameters = CreateParametersForColors(defaultColors);

return AstFactory.BuildExprList(
parameters.Select(AstFactory.BuildDoubleNode)
.Cast<AssociativeNode>()
.ToList()
);
}

private static List<double> CreateParametersForColors(IEnumerable<Color> colors)
{
var parameters = new List<double>();

Expand Down
28 changes: 26 additions & 2 deletions test/DynamoCoreWpfTests/NodeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ public void TestPortColors_NodeModel()
Assert.AreEqual(3, portVMs.Count);
Assert.AreEqual(1, outPorts.Count);

Assert.AreEqual(InPortViewModel.PortValueMarkerRed.Color, (portVMs[0] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerRed.Color, (portVMs[1] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (portVMs[0] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (portVMs[1] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (portVMs[2] as InPortViewModel).PortValueMarkerColor.Color);

Assert.False((outPorts[0] as OutPortViewModel).PortDefaultValueMarkerVisible);
Expand Down Expand Up @@ -670,5 +670,29 @@ public void TestSelectNeighborPins()

Assert.AreEqual(5, countAfter);
}
[Test]
public void ColorRange_InputPortsShowBlueIndicatorForDefaultValues()
{
Open(@"UI\color_range_ports.dyn");

var colorRangeVM = NodeViewWithGuid("423d7eaf-9308-4129-b11f-14c186fa4279");

var inPorts = colorRangeVM.ViewModel.InPorts;

// Assert that the first two input ports show blue markers on graph loaded.
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (inPorts[0] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (inPorts[1] as InPortViewModel).PortValueMarkerColor.Color);
Assert.IsTrue((inPorts[0] as InPortViewModel).PortDefaultValueMarkerVisible);
Assert.IsTrue((inPorts[1] as InPortViewModel).PortDefaultValueMarkerVisible);

Run();
DispatcherUtil.DoEvents();

// Assert that input ports retain default value indicators after graph execution.
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (inPorts[0] as InPortViewModel).PortValueMarkerColor.Color);
Assert.AreEqual(InPortViewModel.PortValueMarkerBlue.Color, (inPorts[1] as InPortViewModel).PortValueMarkerColor.Color);
Assert.IsTrue((inPorts[0] as InPortViewModel).PortDefaultValueMarkerVisible);
Assert.IsTrue((inPorts[1] as InPortViewModel).PortDefaultValueMarkerVisible);
}
}
}

0 comments on commit 02b3fc6

Please sign in to comment.