PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtInteraction.cpp
Go to the documentation of this file.
1#include "WtInteraction.h"
2
3WtInteraction::WtInteraction(WtNode& node, WtConnection& connection, WtFlowScene& scene, connectionPointData inPoint, connectionPointData outPoint, std::shared_ptr<Node> inNode, std::shared_ptr<Node> outNode)
4 : _node(&node)
5 , _connection(&connection)
6 , _scene(&scene)
7 , _inPoint(inPoint)
8 , _outPoint(outPoint)
9 , _inNode(inNode)
10 , _outNode(outNode)
11
12{}
13
15{
16 // 1) QtConnection requires a port
17 PortType requiredPort = connectionRequiredPort();
18
19 if (requiredPort == PortType::None)
20 return false;
21
22 // 1.5) Forbid connecting the node to itself
23 WtNode* nodeStart = _connection->getNode(oppositePort(requiredPort));
24
25 if (nodeStart == _node)
26 return false;
27
28 // 2) connection point is on top of the node port
29 portIndex = _inPoint.portIndex;
30
31 if (portIndex == INVALID_PORT)
32 return false;
33
34 // 3) WtNode port is vacant
35 if (!isNodePortAccessible(requiredPort, portIndex))
36 return false;
37
38 WtNode* nodeExp = requiredPort == PortType::In ? nodeStart : _node;
39 WtNode* nodeInp = requiredPort == PortType::In ? _node : nodeStart;
40
41 PortIndex portIndexExp = requiredPort == PortType::In ? _connection->getPortIndex(oppositePort(requiredPort)) : portIndex;
42 PortIndex portIndexInp = requiredPort == PortType::In ? portIndex : _connection->getPortIndex(oppositePort(requiredPort));
43
44 auto dataOut = nodeExp->nodeDataModel()->outData(portIndexExp);
45
46 if (!nodeInp->nodeDataModel()->tryInData(portIndexInp, dataOut))
47 return false;
48
49 // 4) WtConnection type equals node port type, or there is a registered type conversion that can translate between the two
50 auto connectionDataType = _connection->dataType(oppositePort(requiredPort));
51
52 auto const& modelTarget = _node->nodeDataModel();
53 NodeDataType candidateNodeDataType = modelTarget->dataType(requiredPort, portIndex);
54
55 if (connectionDataType.id != candidateNodeDataType.id)
56 {
57 if (requiredPort == PortType::In)
58 {
59 converter = _scene->registry().getTypeConverter(connectionDataType, candidateNodeDataType);
60 }
61 else if (requiredPort == PortType::Out)
62 {
63 converter = _scene->registry().getTypeConverter(candidateNodeDataType, connectionDataType);
64 }
65
66 return (converter != nullptr);
67 }
68
69 return true;
70}
71
73{
74 // 1) Check conditions from 'canConnect'
75 PortIndex portIndex = INVALID_PORT;
76
77 TypeConverter converter;
78
79 if (!canConnect(portIndex, converter))
80 {
81 return false;
82 }
83
84 // 1.5) If the connection is possible but a type conversion is needed,
85 // assign a convertor to connection
86 if (converter)
87 {
88 _connection->setTypeConverter(converter);
89 }
90
91 // 2) Assign node to required port in QtConnection
92 PortType requiredPort = connectionRequiredPort();
93 _node->nodeState().setConnection(requiredPort, portIndex, *_connection);
94
95 // 3) Assign QtConnection to empty port in NodeState
96 // The port is not longer required after this function
97 _connection->setNodeToPort(*_node, requiredPort, portIndex);
98
99 // 4) Poke model to intiate data transfer
100 auto outNode = _connection->getNode(PortType::Out);
101 if (outNode)
102 {
103 //PortIndex outPortIndex = _connection->getPortIndex(PortType::Out);
104 //outNode->onDataUpdated(outPortIndex);
105 setInData(_inPoint.portIndex);
106 }
107
108 return true;
109}
110
112{
113 auto const& state = _connection->connectionState();
114
115 return state.requiredPort();
116}
117
119{
120 WtNodeState const& nodeState = _node->nodeState();
121
122 auto const& entries = nodeState.getEntries(portType);
123
124 if (entries[portIndex].empty()) return true;
125
126 if (portType == PortType::Out)
127 {
128 const auto outPolicy = _node->nodeDataModel()->portOutConnectionPolicy(portIndex);
129 return outPolicy == WtNodeDataModel::ConnectionPolicy::Many;
130 }
131 else
132 {
133 const auto inPolicy = _node->nodeDataModel()->portInConnectionPolicy(portIndex);
135 }
136}
137
138
140{
141 // error
142 if (_inPoint.portShape == PortShape::Diamond || _inPoint.portShape == PortShape::Bullet)
143 {
144 _outNode->connect(_inNode->getImportNodes()[portIndex]);
145 }
146 else if (_inPoint.portShape == PortShape::Point)
147 {
148 auto outFieldNum = 0;
149 auto outPoints = _connection->getNode(PortType::Out)->flowNodeData().getPointsData();
150 for (auto point : outPoints)
151 {
152 if (point.portShape == PortShape::Point)
153 {
154 outFieldNum = point.portIndex;
155 break;
156 }
157 }
158
159 auto field = _outNode->getOutputFields()[_outPoint.portIndex - outFieldNum];
160
161 if (field != NULL)
162 {
163 auto node_data = _node->flowNodeData();
164
165 auto points = node_data.getPointsData();
166
167 int fieldNum = 0;
168
169 for (auto point : points)
170 {
171 if (point.portType == PortType::In)
172 {
173 if (point.portShape == PortShape::Bullet || point.portShape == PortShape::Diamond)
174 {
175 fieldNum++;
176 }
177 }
178 }
179 auto inField = _inNode->getInputFields()[_inPoint.portIndex - fieldNum];
180
181 field->connect(inField);
182 }
183 }
184}
std::function< SharedNodeData(SharedNodeData)> TypeConverter
int PortIndex
static const int INVALID_PORT
PortType oppositePort(PortType port)
PortType
bool canConnect(PortIndex &portIndex, TypeConverter &converter)
std::shared_ptr< Node > _inNode
connectionPointData _inPoint
WtFlowScene * _scene
bool isNodePortAccessible(PortType portType, PortIndex portIndex) const
connectionPointData _outPoint
std::shared_ptr< Node > _outNode
WtInteraction(WtNode &node, WtConnection &connection, WtFlowScene &scene, connectionPointData inPoint, connectionPointData outPoint, std::shared_ptr< Node > inNode, std::shared_ptr< Node > outNode)
PortType connectionRequiredPort() const
void setInData(PortIndex portIndex)
WtConnection * _connection
virtual bool tryInData(PortIndex portIndex, std::shared_ptr< WtNodeData > nodeData)
virtual std::shared_ptr< WtNodeData > outData(PortIndex port)=0
WtNodeDataModel * nodeDataModel() const
Definition WtNode.cpp:727
std::vector< ConnectionPtrSet > const & getEntries(PortType) const
Definition WtNode.cpp:582
std::string id