-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNode.cpp
More file actions
169 lines (142 loc) · 4.2 KB
/
Node.cpp
File metadata and controls
169 lines (142 loc) · 4.2 KB
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
#include "Node.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonObject>
#include "Socket.h"
#include "Edge.h"
Node::Node(const QString &t) : title(t)
{
pos = QPoint(0, 0);
setDimensions(QSize(100, 100));
id = QUuid::createUuid().toString(QUuid::WithoutBraces);
variablePostFix = id.split("-").first();
evaldCode = "empty";
}
void Node::setPosition(int x, int y)
{
pos.setX(x);
pos.setY(y);
}
void Node::setColor(const QColor &col)
{
color = col;
}
QColor Node::getColor()
{
return color;
}
QPoint Node::getPosition()
{
return pos;
}
QJsonObject Node::serialize()
{
QJsonArray inputList, outputList;
foreach (auto input, inputs) inputList.append(input->serialize());
foreach (auto output, outputs) outputList.append(output->serialize());
QVariantMap map;
map.insert("id", id);
map.insert("title", title);
map.insert("pos_x", getPosition().x());
map.insert("pos_y", getPosition().y());
map.insert("inputs", inputList);
map.insert("outputs", outputList);
return QJsonObject::fromVariantMap(map);
}
Node* Node::deserialize(const QJsonObject object, NodeScene *nodeScene)
{
id = object.value("id").toString();
title = object.value("title").toString();
setPosition(object.value("pos_x").toInt(), object.value("pos_y").toInt());
// setColor(QColor("#F56565"));
foreach (auto input, object.value("inputs").toArray()) {
auto socket = new Socket(this);
socket->deserialize(input.toObject(), nodeScene);
addSocket(socket);
// addSocket(Type::INTO, static_cast<Location>(input.toObject().value("location").toInt()));
}
foreach (auto output, object.value("outputs").toArray()) {
auto socket = new Socket(this);
socket->deserialize(output.toObject(), nodeScene);
addSocket(socket);
// addSocket(Type::OUTO, static_cast<Location>(output.toObject().value("location").toInt()));
}
return this;
}
QVector<Socket*> Node::getInputSockets()
{
return inputs;
}
QVector<Socket*> Node::getOutputSockets()
{
return outputs;
}
const QString Node::getTitle()
{
return title;
}
void Node::addSocket(Socket *socket)
{
socket->setLocalSocketPosition(getSocketPositionFromLocation(socket->location));
if (socket->type == Type::INTO) {
inputs.append(socket);
} else {
outputs.append(socket);
}
}
QPoint Node::getSocketPositionFromLocation(Location location)
{
int socketX, socketY;
Location listL[] = {Location::LEFT_TOP, Location::LEFT_MIDDLE, Location::LEFT_BOTTOM};
if (std::find(std::begin(listL), std::end(listL), location) != std::end(listL)) {
socketX = 0;
}
Location listR[] = {Location::RIGHT_TOP, Location::RIGHT_MIDDLE, Location::RIGHT_BOTTOM};
if (std::find(std::begin(listR), std::end(listR), location) != std::end(listR)) {
socketX = dimensions.width();// - graphicsSocket->boundingRect().width() / 2, 10);
}
switch (location) {
case Location::LEFT_TOP:
case Location::RIGHT_TOP:
socketY = 0;
break;
case Location::LEFT_MIDDLE:
case Location::RIGHT_MIDDLE:
socketY = dimensions.height() / 2;
break;
case Location::LEFT_BOTTOM:
case Location::RIGHT_BOTTOM:
socketY = dimensions.height();
break;
default:
break;
}
return QPoint(socketX, socketY);
}
void Node::addSocket(Type type, Location location)
{
auto socket = new Socket(this);
socket->setLocation(location);
socket->setLocalSocketPosition(getSocketPositionFromLocation(location));
if (type == Type::INTO) {
socket->type = Type::INTO;
socket->index = inputs.length();
inputs.append(socket);
} else {
socket->type = Type::OUTO;
socket->index = outputs.length();
outputs.append(socket);
}
}
void Node::updateConnectedEdges()
{
foreach (auto socket, inputs + outputs) {
// if (socket->hasEdge()) socket->getEdge()->updateWorldPosition();
foreach (auto edge, socket->getEdges()) {
if (edge) {
// qDebug() << "Edge" << edge->id;
edge->updateWorldPosition();
}
}
}
}