-
Notifications
You must be signed in to change notification settings - Fork 5
/
VisualNode.cpp
374 lines (305 loc) · 8.12 KB
/
VisualNode.cpp
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
#include "VisualNode.h"
using namespace VisNodeSys;
Node::Node(const std::string ID)
{
this->ID = ID;
if (ID.empty())
this->ID = NODE_CORE.GetUniqueHexID();
SetSize(ImVec2(200, 80));
SetName("VisualNode");
Type = "VisualNode";
}
Node::Node(const Node& Src)
{
ParentArea = Src.ParentArea;
ID = NODE_CORE.GetUniqueHexID();
Position = Src.Position;
Size = Src.Size;
ClientRegionMin = Src.ClientRegionMin;
ClientRegionMax = Src.ClientRegionMax;
Name = Src.Name;
Type = Src.Type;
Style = Src.Style;
bShouldBeDestroyed = false;
LeftTop = Src.LeftTop;
RightBottom = Src.RightBottom;
TitleBackgroundColor = Src.TitleBackgroundColor;
TitleBackgroundColorHovered = Src.TitleBackgroundColorHovered;
for (size_t i = 0; i < Src.Input.size(); i++)
{
Input.push_back(new NodeSocket(this, Src.Input[i]->GetType(), Src.Input[i]->GetName(), false, Src.Input[i]->OutputData));
}
for (size_t i = 0; i < Src.Output.size(); i++)
{
Output.push_back(new NodeSocket(this, Src.Output[i]->GetType(), Src.Output[i]->GetName(), true, Src.Output[i]->OutputData));
}
}
Node::~Node()
{
for (int i = 0; i < static_cast<int>(Input.size()); i++)
{
delete Input[i];
Input.erase(Input.begin() + i, Input.begin() + i + 1);
i--;
}
for (int i = 0; i < static_cast<int>(Output.size()); i++)
{
delete Output[i];
Output.erase(Output.begin() + i, Output.begin() + i + 1);
i--;
}
}
std::string Node::GetID()
{
return ID;
}
ImVec2 Node::GetPosition() const
{
return Position;
}
void Node::SetPosition(const ImVec2 NewValue)
{
Position = NewValue;
}
ImVec2 Node::GetSize() const
{
if (GetStyle() == CIRCLE)
return ImVec2(NODE_DIAMETER, NODE_DIAMETER);
return Size;
}
void Node::SetSize(const ImVec2 NewValue)
{
Size = NewValue;
}
std::string Node::GetName()
{
return Name;
}
void Node::SetName(const std::string NewValue)
{
if (NewValue.size() > NODE_NAME_MAX_LENGHT)
return;
Name = NewValue;
}
void Node::AddSocket(NodeSocket* Socket)
{
if (Socket == nullptr)
return;
if (Socket->bOutput)
Output.push_back(Socket);
else
Input.push_back(Socket);
}
void Node::Draw()
{
}
void Node::SocketEvent(NodeSocket* OwnSocket, NodeSocket* ConnectedSocket, NODE_SOCKET_EVENT EventType)
{
}
bool Node::CanConnect(NodeSocket* OwnSocket, NodeSocket* CandidateSocket, char** MsgToUser)
{
// Socket can't connect to itself.
if (OwnSocket == CandidateSocket)
return false;
// Nodes can't connect to themselves.
if (CandidateSocket->GetParent() == this)
return false;
// Output can't connect to output and input can't connect to input.
if (OwnSocket->bOutput == CandidateSocket->bOutput)
return false;
// Types must match.
if (OwnSocket->GetType() != CandidateSocket->GetType())
return false;
return true;
}
std::string Node::GetType() const
{
return Type;
}
Json::Value Node::ToJson()
{
Json::Value Result;
Result["ID"] = ID;
Result["nodeType"] = Type;
Result["nodeStyle"] = Style;
Result["position"]["x"] = Position.x;
Result["position"]["y"] = Position.y;
Result["size"]["x"] = Size.x;
Result["size"]["y"] = Size.y;
Result["name"] = Name;
for (size_t i = 0; i < Input.size(); i++)
{
Result["input"][std::to_string(i)]["ID"] = Input[i]->GetID();
Result["input"][std::to_string(i)]["name"] = Input[i]->GetName();
Result["input"][std::to_string(i)]["type"] = Input[i]->GetType();
}
for (size_t i = 0; i < Output.size(); i++)
{
Result["output"][std::to_string(i)]["ID"] = Output[i]->GetID();
Result["output"][std::to_string(i)]["name"] = Output[i]->GetName();
Result["output"][std::to_string(i)]["type"] = Output[i]->GetType();
}
return Result;
}
void Node::FromJson(Json::Value Json)
{
ID = Json["ID"].asCString();
Type = Json["nodeType"].asCString();
if (Json.isMember("nodeStyle"))
Style = NODE_STYLE(Json["nodeStyle"].asInt());
Position.x = Json["position"]["x"].asFloat();
Position.y = Json["position"]["y"].asFloat();
Size.x = Json["size"]["x"].asFloat();
Size.y = Json["size"]["y"].asFloat();
Name = Json["name"].asCString();
const std::vector<Json::String> InputsList = Json["input"].getMemberNames();
for (size_t i = 0; i < Input.size(); i++)
{
delete Input[i];
Input[i] = nullptr;
}
Input.resize(InputsList.size());
for (size_t i = 0; i < InputsList.size(); i++)
{
const std::string ID = Json["input"][std::to_string(i)]["ID"].asCString();
const std::string name = Json["input"][std::to_string(i)]["name"].asCString();
// This is a temporary solution for compatibility with old files.
std::string type = "FLOAT";
if (Json["input"][std::to_string(i)]["type"].type() == Json::stringValue)
type = Json["input"][std::to_string(i)]["type"].asCString();
Input[i] = new NodeSocket(this, type, name, false);
Input[i]->ID = ID;
}
const std::vector<Json::String> OutputsList = Json["output"].getMemberNames();
for (size_t i = 0; i < Output.size(); i++)
{
delete Output[i];
Output[i] = nullptr;
}
Output.resize(OutputsList.size());
for (size_t i = 0; i < OutputsList.size(); i++)
{
const std::string ID = Json["output"][std::to_string(i)]["ID"].asCString();
const std::string name = Json["output"][std::to_string(i)]["name"].asCString();
// This is a temporary solution for compatibility with old files.
std::string type = "FLOAT";
if (Json["output"][std::to_string(i)]["type"].type() == Json::stringValue)
type = Json["output"][std::to_string(i)]["type"].asCString();
Output[i] = new NodeSocket(this, type, name, true);
Output[i]->ID = ID;
}
}
void Node::UpdateClientRegion()
{
float LongestInputSocketTextW = 0.0f;
for (size_t i = 0; i < Input.size(); i++)
{
const ImVec2 TextSize = ImGui::CalcTextSize(Input[i]->GetName().c_str());
if (TextSize.x > LongestInputSocketTextW)
LongestInputSocketTextW = TextSize.x;
}
float LongestOutputSocketTextW = 0.0f;
for (size_t i = 0; i < Output.size(); i++)
{
const ImVec2 TextSize = ImGui::CalcTextSize(Output[i]->GetName().c_str());
if (TextSize.x > LongestOutputSocketTextW)
LongestOutputSocketTextW = TextSize.x;
}
ClientRegionMin.x = LeftTop.x + NODE_SOCKET_SIZE * 5.0f + LongestInputSocketTextW + 2.0f;
ClientRegionMax.x = RightBottom.x - NODE_SOCKET_SIZE * 5.0f - LongestOutputSocketTextW - 2.0f;
ClientRegionMin.y = LeftTop.y + NODE_TITLE_HEIGHT + 2.0f;
ClientRegionMax.y = RightBottom.y - 2.0f;
}
ImVec2 Node::GetClientRegionSize()
{
UpdateClientRegion();
return ClientRegionMax - ClientRegionMin;
}
ImVec2 Node::GetClientRegionPosition()
{
UpdateClientRegion();
return ClientRegionMin;
}
size_t Node::GetInputSocketCount() const
{
return Input.size();
}
size_t Node::GetOutputSocketCount() const
{
return Output.size();
}
std::vector<Node*> Node::GetNodesConnectedToInput() const
{
std::vector<Node*> Result;
for (size_t i = 0; i < Input.size(); i++)
{
for (size_t j = 0; j < Input[i]->ConnectedSockets.size(); j++)
{
if (IsNodeWithIDInList(Input[i]->ConnectedSockets[j]->GetParent()->GetID(), Result))
continue;
Result.push_back(Input[i]->ConnectedSockets[j]->GetParent());
}
}
return Result;
}
std::vector<Node*> Node::GetNodesConnectedToOutput() const
{
std::vector<Node*> Result;
for (size_t i = 0; i < Output.size(); i++)
{
for (size_t j = 0; j < Output[i]->ConnectedSockets.size(); j++)
{
if (IsNodeWithIDInList(Output[i]->ConnectedSockets[j]->GetParent()->GetID(), Result))
continue;
Result.push_back(Output[i]->ConnectedSockets[j]->GetParent());
}
}
return Result;
}
bool Node::OpenContextMenu()
{
return false;
}
NODE_STYLE Node::GetStyle() const
{
return Style;
}
void Node::SetStyle(const NODE_STYLE NewValue)
{
if (static_cast<int>(NewValue) < 0 || static_cast<int>(NewValue) >= 2)
return;
Style = NewValue;
}
bool Node::IsHovered() const
{
return bHovered;
}
void Node::SetIsHovered(const bool NewValue)
{
bHovered = NewValue;
}
bool Node::CouldBeMoved() const
{
return bCouldBeMoved;
}
void Node::SetCouldBeMoved(bool NewValue)
{
bCouldBeMoved = NewValue;
}
NodeArea* Node::GetParentArea() const
{
return ParentArea;
}
bool Node::CouldBeDestroyed() const
{
return bShouldBeDestroyed;
}
bool Node::IsNodeWithIDInList(const std::string ID, const std::vector<Node*> List)
{
for (size_t i = 0; i < List.size(); i++)
{
if (List[i]->GetID() == ID)
return true;
}
return false;
}