forked from chickensoft-games/GodotTestDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphEditDriver.cs
139 lines (124 loc) · 5.16 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
namespace Chickensoft.GodotTestDriver.Drivers;
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using Godot.Collections;
using JetBrains.Annotations;
/// <summary>
/// Driver for a <see cref="GraphEdit"/>.
/// </summary>
/// <typeparam name="TGraphEdit">GraphEdit type.</typeparam>
/// <typeparam name="TGraphNodeDriver">GraphNodeDriver type.</typeparam>
/// <typeparam name="TGraphNode">GraphNode type.</typeparam>
[PublicAPI]
public 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 GraphEdit 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>
/// <param name="from">Source GraphNodeDriver.</param>
/// <param name="fromPort">Source port.</param>
/// <param name="to">Target GraphNodeDriver.</param>
/// <param name="toPort">Target port.</param>
/// <exception cref="ArgumentException" />
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_node"] == fromRoot.Name
&& (int)connection["from_port"] == fromPort.PortIndex
&& (string)connection["to_node"] == 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>
/// <param name="from">Source GraphNodeDriver.</param>
/// <param name="fromPort">Source port.</param>
/// <exception cref="ArgumentException"/>
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_node"] == 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>
/// <param name="to">Target GraphNodeDriver.</param>
/// <param name="toPort">Target port.</param>
/// <exception cref="ArgumentException"/>
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_node"] == toRoot.Name
&& (int)connection["to_port"] == toPort.PortIndex
);
}
/// <summary>
/// Drivers corresponding to the nodes in the graph.
/// </summary>
public IEnumerable<TGraphNodeDriver> Nodes =>
BuildDrivers(root => root.GetChildren().OfType<TGraphNode>(),
node => _nodeDriverProducer(node, "-> GraphNode")
);
}
/// <summary>
/// Driver for a <see cref="GraphEdit"/>.
/// </summary>
[PublicAPI]
public class GraphEditDriver : GraphEditDriver<GraphEdit, GraphNodeDriver, GraphNode>
{
/// <summary>
/// Constructs a new GraphEdit driver.
/// </summary>
/// <param name="producer">a producer that produces the <see cref="GraphEdit"/> that this driver works on.</param>
/// <param name="description">Driver description.</param>
public GraphEditDriver(Func<GraphEdit> producer, string description = "") : base(producer,
(node, nodeDescription) => new GraphNodeDriver(node, $"{description}-> {nodeDescription}"),
description)
{
}
}