-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructorInvokeElement.cs
269 lines (231 loc) · 9.7 KB
/
ConstructorInvokeElement.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
using MetaphysicsIndustries.Crystalline;
using MetaphysicsIndustries.Epiphany;
using MetaphysicsIndustries.Utilities;
namespace MetaphysicsIndustries.Amethyst
{
[Serializable]
public class ConstructorInvokeElement : AmethystElement
{
public ConstructorInvokeElement()
: base(new ConstructorInvokeNode(), new SizeV(80, 80))
{
}
public new ConstructorInvokeNode Node
{
get { return (ConstructorInvokeNode)base.Node; }
}
[Serializable]
public class ConstructorInvokeNode : Node
{
public ConstructorInvokeNode()
: base("Constructor Invoke")
{
}
protected override void InitConnections()
{
}
private ConstructorInfo _constructor;
public ConstructorInfo Constructor
{
get { return _constructor; }
set
{
if (_constructor != value)
{
_constructor = value;
UpdateConnections();
}
}
}
protected void UpdateConnections()
{
ClearConnections();
if (Constructor == null) { return; }
Type icType = typeof(InputConnection<>);
//if (!Constructor.IsStatic)
//{
// AddInputConnection("this", Constructor.DeclaringType);
//}
ParameterInfo[] param = Constructor.GetParameters();
int i = 0;
foreach (ParameterInfo pi in param)
{
string name = pi.Name;
if (string.IsNullOrEmpty(name))
{
name = "param_" + i.ToString();
}
InputConnectionBase conn = AddInputConnection(name, pi.ParameterType);
ParametersByInputConnection[conn] = pi;
}
UpdateOutputConnection();
InitDependencies();
}
private void ClearConnections()
{
foreach (InputConnectionBase conn in InputConnectionBases)
{
conn.InboundConnection = null;
conn.Dependants.Clear();
}
foreach (OutputConnectionBase conn in OutputConnectionBases)
{
conn.OutboundConnections.Clear();
conn.Dependencies.Clear();
}
InputConnectionBases.Clear();
OutputConnectionBases.Clear();
InputConnectionsByName.Clear();
//OutputConnectionsByName.Clear();
ParametersByInputConnection.Clear();
Output = null;
}
private OutputConnectionBase UpdateOutputConnection()
{
Type icType = typeof(OutputConnection<>);
Type type = icType.MakeGenericType(Constructor.DeclaringType);
OutputConnectionBase conn = (OutputConnectionBase)type.Assembly.CreateInstance(type.FullName, false, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, null, new object[] { "Output" }, null, null);
OutputConnectionBases.Add(conn);
Output = conn;
//OutputConnectionsByName[name] = conn;
return conn;
}
private InputConnectionBase AddInputConnection(string name, Type connectionType)
{
Type icType = typeof(InputConnection<>);
Type type = icType.MakeGenericType(connectionType);
InputConnectionBase conn = (InputConnectionBase)type.Assembly.CreateInstance(type.FullName, false, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, null, new object[] { name }, null, null);
InputConnectionBases.Add(conn);
InputConnectionsByName[name] = conn;
return conn;
}
private Dictionary<string, InputConnectionBase> _inputConnectionsByName = new Dictionary<string, InputConnectionBase>();
public Dictionary<string, InputConnectionBase> InputConnectionsByName
{
get { return _inputConnectionsByName; }
}
//private Dictionary<string, OutputConnectionBase> _outputConnectionsByName = new Dictionary<string, OutputConnectionBase>();
//public Dictionary<string, OutputConnectionBase> OutputConnectionsByName
//{
// get { return _outputConnectionsByName; }
//}
private Dictionary<InputConnectionBase, ParameterInfo> _parametersByInputConnection = new Dictionary<InputConnectionBase, ParameterInfo>();
public Dictionary<InputConnectionBase, ParameterInfo> ParametersByInputConnection
{
get { return _parametersByInputConnection; }
}
private OutputConnectionBase _output;
public OutputConnectionBase Output
{
get { return _output; }
protected set
{
if (_output != value)
{
_output = value;
}
}
}
public override void Execute(Dictionary<InputConnectionBase, object> inputs, Dictionary<OutputConnectionBase, object> outputs)
{
List<InputConnectionBase> inputConnectionsInOrder = new List<InputConnectionBase>();
List<object> inputValuesInOrder = new List<object>();
inputConnectionsInOrder.AddRange(ParametersByInputConnection.Keys);
foreach (InputConnectionBase conn in InputConnectionBases)
{
if (ParametersByInputConnection.ContainsKey(conn))
{
ParameterInfo pi = ParametersByInputConnection[conn];
inputConnectionsInOrder[pi.Position] = conn;
}
}
foreach (InputConnectionBase conn in inputConnectionsInOrder)
{
inputValuesInOrder.Add(inputs[conn]);
}
object returnValue = Constructor.Invoke(inputValuesInOrder.ToArray());
outputs[Output] = returnValue;
}
}
public override string Text
{
get { return base.Text; }
}
public override bool ShallProcessDoubleClick
{
get { return true; }
}
public override void ProcessDoubleClick(AmethystControl control)
{
ConstructorInvokeForm form = new ConstructorInvokeForm(Node.Constructor);
if (form.ShowDialog(control) == DialogResult.OK)
{
Node.Constructor = form.Constructor;
UpdateTerminals();
}
}
private void UpdateTerminals()
{
ClearTerminals();
int y = 20;
foreach (InputConnectionBase conn in Node.InputConnectionBases)
{
InputTerminal term = new InputTerminal(conn);
term.Position = y;
term.Side = BoxOrientation.Left;
term.DisplayText = conn.Name;
Terminals.Add(term);
y += 20;
}
//if (Node.InputConnectionsByName.ContainsKey("this"))
//{
// TerminalsByConnection[Node.InputConnectionsByName["this"]].Size *= 2;
//}
y = 30;
foreach (OutputConnectionBase conn in Node.OutputConnectionBases)
{
OutputTerminal term = new OutputTerminal(conn);
term.Position = y;
term.Side = BoxOrientation.Right;
term.DisplayText = conn.Name;
Terminals.Add(term);
y += 20;
}
Size = new SizeV(Width, Math.Max((Node.InputConnectionBases.Count + 1) * 20, (Node.OutputConnectionBases.Count + 1) * 20 + 10));
}
private void ClearTerminals()
{
foreach (Terminal term in Terminals)
{
if (term is InputTerminal)
{
InputTerminal term2 = (InputTerminal)term;
AmethystPath path = term2.Path;
if (path != null)
{
term2.Path = null;
ParentAmethystControl.DisconnectAndRemoveEntity(path);
}
}
else if (term is OutputTerminal)
{
OutputTerminal term2 = (OutputTerminal)term;
AmethystPath[] paths = new AmethystPath[term2.AmethystPaths.Count];
term2.AmethystPaths.CopyTo(paths, 0);
foreach (AmethystPath path in paths)
{
ParentAmethystControl.DisconnectAndRemoveEntity(path);
}
term2.AmethystPaths.Clear();
}
}
Terminals.Clear();
}
}
}