This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GraphEditDriver.cs
119 lines (104 loc) · 4.42 KB
/
GraphEditDriver.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
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using Godot.Collections;
using JetBrains.Annotations;
namespace GodotTestDriver.Drivers
{
/// <summary>
/// Driver for a <see cref="GraphEdit"/>.
/// </summary>
[PublicAPI]
public partial class GraphEditDriver<TGraphEdit, TGraphNodeDriver, TGraphNode> : ControlDriver<TGraphEdit>
where TGraphEdit : GraphEdit where TGraphNode : GraphNode where TGraphNodeDriver : GraphNodeDriver<TGraphNode>
{
private readonly Func<Func<TGraphNode>, string, TGraphNodeDriver> _nodeDriverProducer;
/// <summary>
/// Constructs a new driver.
/// </summary>
/// <param name="producer">a producer that produces the <see cref="GraphEdit"/> that this driver works on.</param>
/// <param name="nodeDriverProducer">a producer that produces a driver for a <see cref="GraphNode"/> child of the <see cref="GraphEdit"/></param>
/// <param name="description">a description for the node</param>
public GraphEditDriver(Func<TGraphEdit> producer,
Func<Func<TGraphNode>, string, TGraphNodeDriver> nodeDriverProducer,
string description = "") : base(producer, description)
{
_nodeDriverProducer = nodeDriverProducer;
}
/// <summary>
/// Checks if the graph edit has a connection from the given node to the given target node on the
/// given ports.
/// </summary>
public bool HasConnection(TGraphNodeDriver from, Port fromPort, TGraphNodeDriver to, Port toPort)
{
if (!fromPort.IsOutput)
{
throw new ArgumentException("fromPort must be an output port");
}
if (!toPort.IsInput)
{
throw new ArgumentException("toPort must be an input port");
}
var graphEdit = PresentRoot;
var fromRoot = from.PresentRoot;
var toRoot = to.PresentRoot;
return graphEdit.GetConnectionList()
.Any(connection =>
(string) connection["from"] == fromRoot.Name
&& (int) connection["from_port"] == fromPort.PortIndex
&& (string) connection["to"] == toRoot.Name
&& (int) connection["to_port"] == toPort.PortIndex);
}
/// <summary>
/// Checks if he graph edit has a connection originating from the given node on the given port.
/// </summary>
public bool HasConnectionFrom(TGraphNodeDriver from, Port fromPort)
{
if (!fromPort.IsOutput)
{
throw new ArgumentException("fromPort must be an output port");
}
var graphEdit = PresentRoot;
var fromRoot = from.PresentRoot;
return graphEdit.GetConnectionList().Cast<Dictionary>()
.Any(connection =>
(string) connection["from"] == fromRoot.Name
&& (int) connection["from_port"] == fromPort.PortIndex
);
}
/// <summary>
/// Checks if he graph edit has a connection originating from the given node on the given port.
/// </summary>
public bool HasConnectionTo(TGraphNodeDriver to, Port toPort)
{
if (!toPort.IsInput)
{
throw new ArgumentException("toPort must be an input port");
}
var graphEdit = PresentRoot;
var toRoot = to.PresentRoot;
return graphEdit.GetConnectionList().Cast<Dictionary>()
.Any(connection =>
(string) connection["to"] == toRoot.Name
&& (int) connection["to_port"] == toPort.PortIndex
);
}
public IEnumerable<TGraphNodeDriver> Nodes =>
BuildDrivers(root => root.GetChildren().OfType<TGraphNode>(),
node => _nodeDriverProducer(node, "-> GraphNode")
);
}
/// <summary>
/// Driver for a <see cref="GraphEdit"/>.
/// </summary>
[PublicAPI]
public partial class GraphEditDriver : GraphEditDriver<GraphEdit, GraphNodeDriver, GraphNode>
{
public GraphEditDriver(Func<GraphEdit> producer, string description = "") : base(producer,
(node, nodeDescription) => new GraphNodeDriver(node, $"{description}-> {nodeDescription}"),
description)
{
}
}
}