forked from jhergel/icesl-nodes
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNodeWindow.cpp
More file actions
202 lines (173 loc) · 6.6 KB
/
Copy pathNodeWindow.cpp
File metadata and controls
202 lines (173 loc) · 6.6 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
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
#include <LibSL/LibSL.h>
#include <LibSL/LibSL_gl.h>
#include "NodeWindow.h"
#include "NodeLua.h"
#include "imgui/imgui.h"
using namespace std;
//------------------------------------------------------------------
//render the node
bool NodeWindow::display()
{
ImVec2 offsetGUI = ImVec2(m_size[0], m_size[1]);
ImGui::Begin(m_name.c_str(), &m_show, offsetGUI);
m_drawList = ImGui::GetWindowDrawList();
handlePosAndSize();
m_pos = v2i(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y);
m_size = v2i(ImGui::GetWindowSize().x, ImGui::GetWindowSize().y);
ImVec2 cursor = ImGui::GetCursorScreenPos();
ImGui::SetCursorScreenPos(cursor);
for (auto st : node->getTweaks()) {
st.second->drawUi();
}
float err_cont = 5;
ImVec2 minc = ImVec2(ImGui::GetWindowPos().x - err_cont / 2, ImGui::GetWindowPos().y - err_cont / 2);
ImVec2 maxc = ImVec2(ImGui::GetWindowPos().x + ImGui::GetWindowSize().x + err_cont / 2, ImGui::GetWindowPos().y + ImGui::GetWindowSize().y + err_cont / 2);
//if(ImGui::Button("reload")){
// node->reloadProgram();
//}
ImGui::End();
//Node* n = node;
ForIndex(i, previousConnectedWindow.size()) {
NodeWindow* w = previousConnectedWindow[i];
if (w == nullptr)continue;
string s = node->getPrevNamed()[node->getInputName()[i]].second;
Node* n = w->node;
int outputSlot = n->getIndiceOutByName(s);
ImVec2 p1 = w->GetOutputSlotPos(outputSlot);
ImVec2 p2 = GetInputSlotPos(i);
ImVec2 p3 = ImVec2(p1.x + 50, p1.y);
ImVec2 p4 = ImVec2(p2.x - 50, p2.y);
m_drawList->AddBezierCurve(p1, p3, p4, p2, ImColor(200, 200, 100), 3.0f);
}
if (node->isInErrorState()) {
m_drawList->AddRect(minc, maxc, ImColor(150, 0, 0, 150), 10.0, 0xFF, err_cont);
}
displayNodeName();
return !m_show;
}
//------------------------------------------------------------------
void NodeWindow::renderAndPick(NodeSelecter &ns, bool mouseDown)
{
ImColor color = ImColor(150, 150, 150, 150);
v2i Mpos = v2i(ImGui::GetMousePos().x, ImGui::GetMousePos().y);
//draw input circles
ForIndex(i, node->getInputName().size()) {
v2i Cpos = v2i(GetInputSlotPos(i).x, GetInputSlotPos(i).y);
if (sqLength(Cpos - Mpos) < 100) {
color = ImColor(150, 0, 0, 150);
if (mouseDown) {
if (!ns.inputHasBeenPicked) {
ns.nodePickedInput.nodeWindow = this;
ns.nodePickedInput.pos = i;
ns.inputHasBeenPicked = true;
}
}
}
if (ns.nodePickedInput.nodeWindow == this && ns.nodePickedInput.pos == i)color = ImColor(0, 150, 0, 150);
m_drawList->AddCircleFilled(GetInputSlotPos(i), 10, color, 64);
color = ImColor(150, 150, 150, 150);
}
//draw output circles
ForIndex(i, node->getoutputName().size()) {
v2i Cpos = v2i(GetOutputSlotPos(i).x, GetOutputSlotPos(i).y);
if (sqLength(Cpos - Mpos) < 100) {
color = ImColor(150, 0, 0, 150);
if (mouseDown) {
if (!ns.outputHasBeenPicked) {
ns.nodePickedOutput.nodeWindow = this;
ns.nodePickedOutput.pos = i;
ns.outputHasBeenPicked = true;
}
}
}
if (ns.nodePickedOutput.nodeWindow == this && ns.nodePickedOutput.pos == i)color = ImColor(0, 150, 0, 150);
m_drawList->AddCircleFilled(GetOutputSlotPos(i), 10, color, 64);
color = ImColor(150, 150, 150, 150);
}
if (ns.nodePickedInput.nodeWindow == this) {
int pos = ns.nodePickedInput.pos;
ImVec2 p1 = GetInputSlotPos(pos);
ImVec2 p2 = ImGui::GetMousePos();
ImVec2 p3 = ImVec2(p1.x - 50, p1.y);
ImVec2 p4 = ImVec2(p2.x + 50, p2.y);
m_drawList->AddBezierCurve(p1, p3, p4, p2, ImColor(200, 200, 100), 3.0f);
}
if (ns.nodePickedOutput.nodeWindow == this) {
int pos = ns.nodePickedInput.pos;
ImVec2 p1 = GetOutputSlotPos(pos);
ImVec2 p2 = ImGui::GetMousePos();
ImVec2 p3 = ImVec2(p1.x + 50, p1.y);
ImVec2 p4 = ImVec2(p2.x - 50, p2.y);
m_drawList->AddBezierCurve(p1, p3, p4, p2, ImColor(200, 200, 100), 3.0f);
}
}
// ---------------------------------------------------------
void NodeWindow::displayNodeName()
{
float MaxInSize = 0;
float MaxOutSize = 0;
float char_size = 7.0;
ForIndex(i, node->getInputName().size()) {
MaxInSize = max(MaxInSize, node->getInputName()[i].size() * char_size);
}
ForIndex(i, node->getoutputName().size()) {
//right alignement: displace the cursor by the number of char * the size of a char - the circle ray
MaxOutSize = max(MaxOutSize, node->getoutputName()[i].size() * char_size);
}
bool open = true;
string name = m_name + "around";
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 0.0));
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0, 0.0, 0.0, 1));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ForIndex(i, node->getInputName().size()) {
ImGui::Begin(name.c_str(), &open, ImVec2(m_size[0], m_size[1]), 0.5,
ImGuiWindowFlags_NoInputs
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoTitleBar
//|ImGuiWindowFlags_NoBringToFrontOnFocus
| ImGuiWindowFlags_AlwaysAutoResize
);
float posX = GetInputSlotPos(i).x;// + node->getInputName()[i].size() * char_size;
float posY = GetInputSlotPos(i).y;
ImGui::SetWindowPos(ImVec2(posX, posY));
ImGui::Text(node->getInputName()[i].c_str());
ImGui::End();
}
ForIndex(i, node->getoutputName().size()) {
ImGui::Begin(name.c_str(), &open, ImVec2(m_size[0], m_size[1]), 0.5,
ImGuiWindowFlags_NoInputs
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoTitleBar
//|ImGuiWindowFlags_NoBringToFrontOnFocus
| ImGuiWindowFlags_AlwaysAutoResize
);
float posX = GetOutputSlotPos(i).x;
float posY = GetOutputSlotPos(i).y;
ImGui::SetWindowPos(ImVec2(posX, posY));
ImGui::Text(node->getoutputName()[i].c_str());
ImGui::End();
}
ImGui::PopStyleVar();
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}
//------------------------------------------------------------------
//connect two nodes. Cycle are not possible
void NodeWindow::connectPreviousWindow(NodeWindow* prev, int inpos, int outpos)
{
if (prev->node->isAscendent(node))return;//prevent cycle
previousConnectedWindow[inpos] = prev;
Node* n = prev->node;
node->connect(n, n->getoutputName()[outpos], inpos);
n->onChange();
}
//------------------------------------------------------------------
//connect two nodes. Cycle are not possible
void NodeWindow::connectPreviousWindow(NodeWindow* prev, string in, string out)
{
int outpos = prev->node->getIndiceOutByName(out);
int inpos = this->node->getIndiceInByName(in);
connectPreviousWindow(prev, inpos, outpos);
}