-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
161 lines (125 loc) · 4.83 KB
/
MainWindow.cpp
File metadata and controls
161 lines (125 loc) · 4.83 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
#include "MainWindow.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>
#include <QMenuBar>
#include <QJsonDocument>
#include "GraphicsScene.h"
#include "NodeScene.h"
#include "Node.h"
#include "SceneNode.h"
#include "Socket.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
windowSplit = new QSplitter(Qt::Horizontal, this);
windowSplit->setHandleWidth(1);
QMenuBar bar;
bar.addAction("save");
setMenuBar(&bar);
auto scene = new NodeScene;
nodeEditorCanvas = new Canvas(this);
nodeEditorCanvas->setNodeScene(scene);
renderer = new Renderer(this);
connect(nodeEditorCanvas, &Canvas::nodeConnected, this, &MainWindow::refreshRenderer);
QSizePolicy policy;
renderer->setSizePolicy(policy);
// policy.setHorizontalPolicy(QSizePolicy::Expanding);
windowSplit->addWidget(nodeEditorCanvas);
windowSplit->addWidget(renderer);
windowSplit->setSizes(QList<int>({50, 50}));
// windowSplit->setStretchFactor(0, 2);
// windowSplit->setStretchFactor(1, 11);
// auto AA1 = new TimeNode;
// AA1->setPosition(-800, -120);
// auto AA2 = new SinNode;
// AA2->setPosition(-600, -120);
// auto A1 = new Vec3Node;
// A1->setPosition(-400, -100);
// auto A2 = new FloatNode;
// A2->setPosition(-400, 100);
// auto A = new SphereNode;
// A->setPosition(-200, 0);
// auto B = new PlaneNode;
// B->setPosition(-200, 200);
// auto C = new MinNode;
// C->setPosition(0, 0);
// auto D = new OutputNode;
// D->setPosition(200, 0);
// scene->addNode(AA1);
// scene->addNode(AA2);
// scene->addNode(A1);
// scene->addNode(A2);
// scene->addNode(A);
// scene->addNode(B);
// scene->addNode(C);
// scene->addNode(D);
// scene->connectSockets(AA1->getSocketByIndex(Type::OUTO, 0), AA2->getSocketByIndex(Type::INTO, 0));
// scene->connectSockets(AA2->getSocketByIndex(Type::OUTO, 0), A1->getSocketByIndex(Type::INTO, 1));
// scene->connectSockets(A1->getSocketByIndex(Type::OUTO, 0), A->getSocketByIndex(Type::INTO, 0));
// scene->connectSockets(A2->getSocketByIndex(Type::OUTO, 0), A->getSocketByIndex(Type::INTO, 1));
// scene->connectSockets(A->getSocketByIndex(Type::OUTO, 0), C->getSocketByIndex(Type::INTO, 0));
// scene->connectSockets(B->getSocketByIndex(Type::OUTO, 0), C->getSocketByIndex(Type::INTO, 1));
// scene->connectSockets(C->getSocketByIndex(Type::OUTO, 0), D->getSocketByIndex(Type::INTO, 0));
setMouseTracking(true);
setFocusPolicy(Qt::ClickFocus);
installEventFilter(this);
setContentsMargins(0, 0, 0, 0);
setCentralWidget(windowSplit);
}
MainWindow::~MainWindow()
{
delete nodeEditorCanvas;
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Escape) exit(0);
if (keyEvent->key() == Qt::Key_S && keyEvent->modifiers() == Qt::ControlModifier) {
QJsonDocument document;
document.setObject(nodeEditorCanvas->nodeScene->serialize());
QFile jsonFile("test.json");
jsonFile.open(QFile::WriteOnly);
jsonFile.write(document.toJson());
}
if (keyEvent->key() == Qt::Key_N && keyEvent->modifiers() == Qt::ControlModifier) {
nodeEditorCanvas->clearNodeScene();
}
if (keyEvent->key() == Qt::Key_L && keyEvent->modifiers() == Qt::ControlModifier) {
QFile jsonFile("test.json");
jsonFile.open(QFile::ReadOnly);
auto json = QJsonDocument().fromJson(jsonFile.readAll());
auto scene = new NodeScene;
scene->deserialize(json.object(), nullptr);
nodeEditorCanvas->setNodeScene(scene);
}
if (keyEvent->key() == Qt::Key_Space) {
// https://stackoverflow.com/questions/43831474/how-to-equally-distribute-the-width-of-qsplitter
if (toggleRatio) {
windowSplit->setSizes(QList<int>({50, 50}));
toggleRatio = false;
} else {
windowSplit->setSizes(QList<int>({70, 30}));
toggleRatio = true;
}
}
if (keyEvent->key() == Qt::Key_R) {
refreshRenderer();
}
}
return QObject::eventFilter(watched, event);
}
void MainWindow::refreshRenderer()
{
auto master = nodeEditorCanvas->nodeScene->getSceneNode();
if (!master->getInputSockets().first()->getEdges().count()) {
qDebug() << "Connect to the master node";
return;
}
QStringList code;
QString lastEvaldFunc = master->eval(code).first;
QString sceneFunc = QString("float GetDist(vec3 p) { %1 return %2; }").arg(code.join(""), lastEvaldFunc);
// qDebug() << sceneFunc;
renderer->recompileFragShader(sceneFunc);
}