-
Notifications
You must be signed in to change notification settings - Fork 53
/
Operator.cs
550 lines (448 loc) · 19.9 KB
/
Operator.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright (c) 2016 Framefield. All rights reserved.
// Released under the MIT license. (see LICENSE.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
namespace Framefield.Core
{
public class Connection
{
public Operator SourceOp { get; set; }
public OperatorPart SourceOpPart { get; set; }
public Operator TargetOp { get; set; }
public OperatorPart TargetOpPart { get; set; }
public int Index { get; set; }
public Guid ID { get; internal set; }
internal Connection(Guid id, Operator sourceOp, OperatorPart sourceOpPart, Operator targetOp, OperatorPart targetOpPart, int connectionIdx)
{
ID = id;
SourceOp = sourceOp;
SourceOpPart = sourceOpPart;
TargetOp = targetOp;
TargetOpPart = targetOpPart;
Index = connectionIdx;
}
public Connection(Operator sourceOp, OperatorPart sourceOpPart, Operator targetOp, OperatorPart targetOpPart, int connectionIdx)
: this(Guid.NewGuid(), sourceOp, sourceOpPart, targetOp, targetOpPart, connectionIdx)
{
}
}
public class OperatorChangedEventArgs : System.EventArgs
{
public Operator Operator { get; private set; }
public OperatorChangedEventArgs(Operator op)
{
Operator = op;
}
}
public delegate void OperatorChangedDelegate(object obj, OperatorChangedEventArgs args);
public class OperatorPartChangedEventArgs : System.EventArgs
{
public OperatorPart OperatorPart { get; private set; }
public OperatorPartChangedEventArgs(OperatorPart opPart)
{
OperatorPart = opPart;
}
}
public delegate void OperatorPartChangedDelegate(object obj, OperatorPartChangedEventArgs args);
public class ConnectionChangedEventArgs : EventArgs
{
public Connection Connection { get; private set; }
public ConnectionChangedEventArgs(Connection connection)
{
Connection = connection;
}
}
public class PositionChangedEventArgs : EventArgs
{
public Point Position { get; private set; }
public PositionChangedEventArgs(Point position)
{
Position = position;
}
}
public class WidthChangedEventArgs : EventArgs
{
public double Width { get; private set; }
public WidthChangedEventArgs(double width)
{
Width = width;
}
}
public class VisibleChangedEventArgs : EventArgs
{
public bool Visible { get; private set; }
public VisibleChangedEventArgs(bool visible)
{
Visible = visible;
}
}
public delegate void VisibleChangedDelegate(object o, VisibleChangedEventArgs e);
public class OperatorPartStateChangedEventArgs : EventArgs
{
public IOperatorPartState OperatorPartState { get; private set; }
public OperatorPartStateChangedEventArgs(IOperatorPartState state)
{
OperatorPartState = state;
}
}
public delegate void OperatorPartStateChangedDelegate(object o, OperatorPartStateChangedEventArgs e);
public class Operator : INotifyPropertyChanged, IDisposable
{
#region Events
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler<OperatorChangedEventArgs> OperatorAddedEvent;
public event EventHandler<OperatorChangedEventArgs> OperatorRemovedEvent;
public event EventHandler<OperatorPartChangedEventArgs> InputAddedEvent;
public event EventHandler<OperatorPartChangedEventArgs> InputRemovedEvent;
public event EventHandler<OperatorPartChangedEventArgs> OutputAddedEvent;
public event EventHandler<OperatorPartChangedEventArgs> OutputRemovedEvent;
public event EventHandler<ConnectionChangedEventArgs> ConnectionAddedEvent;
public event EventHandler<ConnectionChangedEventArgs> ConnectionRemovedEvent;
public event EventHandler<PositionChangedEventArgs> PositionChangedEvent;
public event EventHandler<WidthChangedEventArgs> WidthChangedEvent;
public event EventHandler<VisibleChangedEventArgs> VisibleChangedEvent;
public event EventHandler<EventArgs> DisabledEvent;
public event EventHandler<OperatorPartStateChangedEventArgs> OperatorPartStateChangedEvent;
public event EventHandler<EventArgs> ModifiedEvent;
#endregion
#region Properties
public Guid ID { get; private set; }
public MetaOperator Definition { get; private set; }
public Operator Parent { get; set; }
public FunctionType FunctionType
{
get
{
var functionType = FunctionType.Generic;
if (Outputs.Count > 0)
functionType = Outputs[0].Type;
return functionType;
}
}
public List<OperatorPart> Inputs { get; internal set; }
public List<OperatorPart> Outputs { get; private set; }
public List<Operator> InternalOps { get; private set; }
public List<OperatorPart> InternalParts { get; private set; }
public string Name
{
get { return (Parent != null) ? Parent.Definition.GetName(ID) : String.Empty; }
set
{
if (Parent != null)
{
Parent.Definition.SetName(ID, value);
EventExt.Raise(PropertyChanged, this, new PropertyChangedEventArgs("Name"));
}
}
}
public Point Position { get { return (Parent != null) ? Parent.Definition.GetPosition(ID) : new Point(); } internal set { if (Parent != null) Parent.Definition.SetPosition(ID, value); } }
public double Width { get { return (Parent != null) ? Parent.Definition.GetWidth(ID) : 0.0; } internal set { if (Parent != null) Parent.Definition.SetWidth(ID, value); } }
public bool Visible { get { return (Parent == null) || Parent.Definition.GetVisible(ID); } internal set { if (Parent != null) Parent.Definition.SetVisible(ID, value); } }
public bool Disabled { get { return (Parent != null) && Parent.Definition.GetDisabled(ID); } internal set { if (Parent != null) Parent.Definition.SetDisabled(ID, value); } }
public IOperatorPartState GetOperatorPartState(Guid opPartId)
{
if (Parent != null)
return Parent.Definition.GetOperatorPartState(ID, opPartId);
return null;
}
public void SetOperatorPartState(Guid opPartId, IOperatorPartState value)
{
if (Parent != null)
Parent.Definition.SetOperatorPartState(ID, opPartId, value);
}
#endregion
#region ctors
public Operator(Guid id, MetaOperator metaOp, List<OperatorPart> inputs, List<OperatorPart> outputs,
List<Operator> internalOps, List<OperatorPart> internalParts)
{
ID = id;
Definition = metaOp;
Inputs = inputs;
Outputs = outputs;
InternalOps = internalOps;
InternalParts = internalParts;
Name = "";
Position = new Point(100, 100);
Width = 75; // This should be Grid.Size * 3
UpdateOutputIndices();
// Forward Modified-events of parameters as Manipulated-Event
foreach (var opPart in Inputs.Union(Outputs).Union(InternalParts))
{
opPart.Parent = this;
opPart.ManipulatedEvent += (o, a) => EventExt.Raise(ModifiedEvent, this, EventArgs.Empty);
}
foreach (var op in InternalOps) op.Parent = this;
ConnectTypeChangedHanderWith(Inputs);
}
#endregion
public void Dispose()
{
Inputs.ForEach(i => i.Dispose());
Outputs.ForEach(o => o.Dispose());
InternalOps.ForEach(o => o.Dispose());
InternalParts.ForEach(p => p.Dispose());
Definition.RemoveInstance(this);
}
#region input/output handling
public void RemoveInput(OperatorPart opPart)
{
Definition.RemoveInput(opPart.ID);
}
public void RemoveOutput(OperatorPart opPart)
{
Definition.RemoveOutput(opPart.ID);
}
internal void AddInput(OperatorPart input)
{
InsertInput(Inputs.Count, input);
}
internal void InsertInput(int index, OperatorPart input)
{
if ((index < 0) || (index > Inputs.Count))
throw new IndexOutOfRangeException();
Inputs.Insert(index, input);
input.Parent = this;
EventExt.Raise(InputAddedEvent, this, new OperatorPartChangedEventArgs(input));
}
internal void RemoveInputInternal(OperatorPart input)
{
var inputOp = input.Parent;
var parentOp = inputOp.Parent;
// remove possible connection to this input in parent op
if (parentOp != null)
{
while (input.Connections.Count > 0)
{
var sourceOpPart = input.Connections[0];
var sourceOp = sourceOpPart.Parent;
var connection = new Connection(sourceOp, sourceOpPart, this, input, 0);
parentOp.RemoveConnection(connection);
}
}
Inputs.Remove(input);
EventExt.Raise(InputRemovedEvent, this, new OperatorPartChangedEventArgs(input));
}
internal void AddOutput(OperatorPart output)
{
InsertOutput(Outputs.Count, output);
}
internal void InsertOutput(int index, OperatorPart output)
{
if ((index < 0) || (index > Outputs.Count))
throw new IndexOutOfRangeException();
Outputs.Insert(index, output);
output.Parent = this;
UpdateOutputIndices();
EventExt.Raise(OutputAddedEvent, this, new OperatorPartChangedEventArgs(output));
}
private void UpdateOutputIndices()
{
for (int idx = 0; idx < Outputs.Count; ++idx)
{
Outputs[idx].Func.EvaluationIndex = idx;
}
}
internal void RemoveOutputInternal(OperatorPart output)
{
var outputOp = this;
var parentOp = outputOp.Parent;
// remove possible connection from this output in parent op
if (parentOp != null)
{
var connectionsToRemove = (from con in parentOp.Connections
where con.SourceOp == outputOp
where con.SourceOpPart == output
select con).ToList();
foreach (var connection in connectionsToRemove)
{
parentOp.RemoveConnection(connection);
}
}
Outputs.Remove(output);
UpdateOutputIndices();
EventExt.Raise(OutputRemovedEvent, this, new OperatorPartChangedEventArgs(output));
}
#endregion
#region Operator handling
internal void AddOperator(Operator op)
{
InternalOps.Add(op);
op.Parent = this;
EventExt.Raise(OperatorAddedEvent, this, new OperatorChangedEventArgs(op));
}
internal void RemoveOperator(Operator op)
{
Definition.RemoveOperator(op.ID);
}
internal void RemoveOperatorInternal(Operator op, bool disposeOpResources)
{
InternalOps.Remove(op);
EventExt.Raise(OperatorRemovedEvent, this, new OperatorChangedEventArgs(op));
if (disposeOpResources)
op.Dispose(); // clean up all resources locked by op
}
internal void MoveOperatorTo(Guid opIdToMove, Guid newParentId)
{
var opToMove = InternalOps.Single(op => op.ID == opIdToMove);
var newParent = InternalOps.Single(op => op.ID == newParentId);
RemoveOperatorInternal(opToMove, disposeOpResources: false);
newParent.AddOperator(opToMove);
}
#endregion
#region Connections
internal void InsertConnectionAt(Connection connection)
{
if (connection.SourceOp == this) connection.SourceOp = null;
if (connection.TargetOp == this) connection.TargetOp = null;
Definition.InsertConnectionAt(new MetaConnection(connection), connection.Index);
}
internal Tuple<Operator, OperatorPart> FindOpPart(Guid opId, Guid opPartId)
{
var op = (opId == Guid.Empty) ? this : (from o in InternalOps where o.ID == opId select o).Single();
var allOpParts = op.InternalParts.Union(op.Inputs).Union(op.Outputs);
var opPart = (from part in allOpParts where part.ID == opPartId select part).Single();
return Tuple.Create(op, opPart);
}
internal void InsertConnectionAtInternal(Connection connection)
{
connection.TargetOpPart.InsertConnectionAt(connection.SourceOpPart, connection.Index);
_connectionsChanged = true;
TriggerConnectionAdded(new ConnectionChangedEventArgs(connection));
}
internal void ReplaceConnectionAt(Connection connection)
{
// first extract prev connection that should be replaced
var prevSourceOpPart = connection.TargetOpPart.Connections[connection.Index];
var opResult = InternalOps.Find(op => op.Outputs.Exists(opPart => opPart == prevSourceOpPart));
var prevSourceOp = opResult ?? this;
RemoveConnection(new Connection(prevSourceOp, prevSourceOpPart, connection.TargetOp, connection.TargetOpPart, connection.Index));
InsertConnectionAt(connection);
}
internal void RemoveConnection(Connection connection)
{
if (connection.SourceOp == this) connection.SourceOp = null;
if (connection.TargetOp == this) connection.TargetOp = null;
Definition.RemoveConnection(new MetaConnection(connection), connection.Index);
}
internal void RemoveConnectionInternal(Connection connection)
{
connection.TargetOpPart.RemoveConnectionAt(connection.Index);
_connectionsChanged = true;
TriggerConnectionRemoved(new ConnectionChangedEventArgs(connection));
}
private bool _connectionsChanged = true;
private readonly List<Connection> _connections = new List<Connection>();
public IEnumerable<Connection> Connections
{
get
{
if (!_connectionsChanged)
return _connections;
var groupedConnections = from metaCon in Definition.Connections
group metaCon by (metaCon.TargetOpID.ToString() + metaCon.TargetOpPartID.ToString())
into groupedCons
select groupedCons;
_connections.Clear();
foreach (var conGroup in groupedConnections)
{
int index = 0;
foreach (var con in conGroup)
{
var sourceOp = InternalOps.SingleOrDefault(op => op.ID == con.SourceOpID);
var sourceOpPart = (sourceOp != null)
? sourceOp.Outputs.Single(part => part.ID == con.SourceOpPartID)
: Inputs.Single(part => part.ID == con.SourceOpPartID);
var targetOp = InternalOps.SingleOrDefault(op => op.ID == con.TargetOpID);
var targetOpPart = (targetOp != null)
? targetOp.Inputs.Single(part => part.ID == con.TargetOpPartID)
: Outputs.Single(part => part.ID == con.TargetOpPartID);
_connections.Add(new Connection(con.ID, sourceOp, sourceOpPart, targetOp, targetOpPart, index));
index += 1;
}
}
_connectionsChanged = false;
return _connections;
}
}
#endregion
#region private stuff
private void TriggerConnectionAdded(ConnectionChangedEventArgs args)
{
EventExt.Raise(ConnectionAddedEvent, this, args);
}
private void TriggerConnectionRemoved(ConnectionChangedEventArgs args)
{
EventExt.Raise(ConnectionRemovedEvent, this, args);
}
internal void TriggerPositionChanged(PositionChangedEventArgs args)
{
EventExt.Raise(PositionChangedEvent, this, args);
}
internal void TriggerWidthChanged(WidthChangedEventArgs args)
{
EventExt.Raise(WidthChangedEvent, this, args);
}
internal void TriggerVisibleChanged(VisibleChangedEventArgs args)
{
EventExt.Raise(VisibleChangedEvent, this, args);
}
protected void TriggerDisabledEvent()
{
EventExt.Raise(DisabledEvent, this, EventArgs.Empty);
}
internal void TriggerOperatorPartStateChanged(OperatorPartStateChangedEventArgs args)
{
EventExt.Raise(OperatorPartStateChangedEvent, this, args);
}
internal void DisableOutputs(bool disabled)
{
foreach (var output in Outputs)
{
output.Disabled = disabled;
}
TriggerDisabledEvent();
}
private void ConnectTypeChangedHanderWith(IEnumerable<OperatorPart> opParts)
{
foreach (var opPart in opParts)
{
opPart.TypeChangedEvent += InputTypeChangedHandler;
}
}
private void InputTypeChangedHandler(object sender, OperatorPart.TypeChangedEventArgs args)
{
var targetPart = sender as OperatorPart;
if (Parent != null && targetPart.Type != args.Type)
{
int index = 0; //targetPart.Connections.IndexOf(targetPart);
var sourcePart = targetPart.Connections[index];
var sourceOp = sourcePart.Parent;
Parent.RemoveConnection(new Connection(sourceOp, sourcePart, targetPart.Parent, targetPart, index));
}
}
#endregion
public override string ToString()
{
return Name != String.Empty ? (Definition.Name + " '" + Name + "'") : Definition.Name;
}
public MetaInput GetMetaInput(OperatorPart input)
{
int index = Inputs.IndexOf(input);
return (index != -1) ? Definition.Inputs[index] : null;
}
public MetaOutput GetMetaOutput(OperatorPart input)
{
int index = Outputs.IndexOf(input);
return (index != -1) ? Definition.Outputs[index] : null;
}
public MetaOperatorPart GetMetaOperatorPart(OperatorPart opPart)
{
int index = InternalParts.IndexOf(opPart);
return (index != -1) ? Definition.OperatorParts[index].Item2 : null;
}
}
}