-
Notifications
You must be signed in to change notification settings - Fork 11
/
UIState.cs
208 lines (175 loc) · 5.72 KB
/
UIState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using Dynamo.Controls;
using Dynamo.Graph.Nodes;
using Dynamo.UI.Commands;
using Dynamo.Wpf;
using Dynamo.Events;
using ProtoCore.AST.AssociativeAST;
using Autodesk.DesignScript.Runtime;
using NodeModelsEssentials.Controls;
using NodeModelsEssentials.Functions;
using Newtonsoft.Json;
namespace NodeModelsEssentials
{
[NodeName("UI.State")]
[NodeDescription("A sample Node Model with custom Wpf UI.")]
[NodeCategory("NodeModelsEssentials")]
[OutPortNames("Number", "Date")]
[OutPortTypes("double", "string")]
[OutPortDescriptions("Double", "Date")]
[IsDesignScriptCompatible]
public class CustomUINodeModelWpfState : NodeModel
{
#region private members
private double number;
private double maximumValue;
private double minimumValue;
private double step;
private string state;
private double executions;
//private double xPosition;
#endregion
#region properties
[IsVisibleInDynamoLibrary(false)]
public DelegateCommand IncreaseCommand { get; set; }
[IsVisibleInDynamoLibrary(false)]
public DelegateCommand DecreaseCommand { get; set; }
public string State
{
get { return state; }
set
{
state = value;
RaisePropertyChanged("State");
}
}
public double Executions
{
get { return executions; }
set
{
executions = value;
RaisePropertyChanged("Executions");
}
}
public double MinimumValue
{
get { return minimumValue; }
set
{
minimumValue = value;
RaisePropertyChanged("MinimumValue");
}
}
public double MaximumValue
{
get { return maximumValue; }
set
{
maximumValue = value;
RaisePropertyChanged("MaximumValue");
}
}
public double Number
{
get { return number; }
set
{
number = Math.Round(value, 2); ;
RaisePropertyChanged("Number");
//OnNodeModified();
}
}
#endregion
#region constructor
[JsonConstructor]
private CustomUINodeModelWpfState(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
{
}
public CustomUINodeModelWpfState()
{
RegisterAllPorts();
IncreaseCommand = new DelegateCommand(IncreaseNumber);
DecreaseCommand = new DelegateCommand(DecreaseNumber);
MinimumValue = 0.0;
MaximumValue = 100.0;
step = 10.0;
state = "Initialized";
// Create an event handler for pre- or post-graph execution
// Note that you need to include using Dynamo.Events; at the top of your file
ExecutionEvents.GraphPostExecution += ExecutionEvents_GraphPostExecution;
ExecutionEvents.GraphPreExecution += ExecutionEvents_GraphPreExecution;
CanUpdatePeriodically = true;
}
private void ExecutionEvents_GraphPreExecution(Dynamo.Session.IExecutionSession session)
{
Number++;
}
private void ExecutionEvents_GraphPostExecution(Dynamo.Session.IExecutionSession session)
{
Executions++;
this.OnNodeModified(forceExecute: true);
}
#endregion
#region public methods
[IsVisibleInDynamoLibrary(false)]
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAsNodes)
{
var doubleNode = AstFactory.BuildDoubleNode(Number);
var dateFuncNode = AstFactory.BuildFunctionCall(
new Func<string>(NodeModelsEssentialsFunctions.GetDate),
new List<AssociativeNode>() { }
);
//var funcNode = AstFactory.BuildFunctionCall(
// new Func<double, double, double>(NodeModelsEssentialsFunctions.Multiply),
// new List<AssociativeNode>() { doubleNode, doubleNode }
// );
return new[]
{
AstFactory.BuildAssignment(
GetAstIdentifierForOutputIndex(0), doubleNode),
AstFactory.BuildAssignment(
GetAstIdentifierForOutputIndex(1), dateFuncNode)
};
}
#endregion
#region command methods
private void IncreaseNumber(object obj)
{
if (Number + step >= MaximumValue)
{
Number = MaximumValue;
}
else
{
Number += step;
}
}
private void DecreaseNumber(object obj)
{
if (Number - step <= MinimumValue)
{
Number = MinimumValue;
}
else
{
Number += -step;
}
}
#endregion
}
/// <summary>
/// View customizer for CustomUINodeModel Node Model.
/// </summary>
public class CustomUINodeModelWpfStateViewCustomization : INodeViewCustomization<CustomUINodeModelWpfState>
{
public void CustomizeView(CustomUINodeModelWpfState model, NodeView nodeView)
{
var stateDynamoControl = new StateDynamoControl();
nodeView.inputGrid.Children.Add(stateDynamoControl);
stateDynamoControl.DataContext = model;
}
public void Dispose() { }
}
}