-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DYN-7838] Color range Node: Show Input ports with default value #15715
Open
ivaylo-matov
wants to merge
3
commits into
DynamoDS:master
Choose a base branch
from
ivaylo-matov:DYN-7838-Color-range-Node-Show-Input-ports-with-default-value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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; | ||
|
@@ -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(); | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
{ | ||
var colorsNode = InPorts[0].Connectors[0].Start.Owner; | ||
var colorsIndex = InPorts[0].Connectors[0].Start.Index; | ||
|
@@ -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; | ||
|
@@ -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>(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@QilongTang I think we should come up with something better than this pattern - for example default value attributes or something where the author only needs to specify the default value once and not need to consider it in their json constructor unless they want to.