-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsongraph.cpp
More file actions
228 lines (186 loc) · 7.25 KB
/
jsongraph.cpp
File metadata and controls
228 lines (186 loc) · 7.25 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
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
#include <string>
#include <variant>
#include <vector>
#include "jsongraph.hpp"
#include "jsongraph_parse.hpp"
#include "jsongraph_tests.hpp"
namespace jsongraph {
ImColor drag_color{255, 255, 127, 255};
// Interior check for dragging and dropping. We don't want to do a sphere check
// here - we want it to be fast and err on the side of allowing the drop to happen.
inline auto points_overlap = [](ImVec2 a, ImVec2 b, float buffer) -> bool {
if (a.x - b.x > buffer || b.x - a.x > buffer) return false;
if (a.y - b.y > buffer || b.y - a.y > buffer) return false;
return true;
};
// Move these to state? Or not for type resolution purposes
std::vector<graph_node> node_list = test_graph;
std::vector<graph_edge> edge_list;
bool is_dragging = false;
std::string new_edge_origin_window;
ImVec2 edge_origin_window_pos;
graph_edge new_edge;
// This is awful
std::string current_window_name = "";
auto start_drag = [&](const std::string& name, ImVec2 pos, const std::string& key) {
is_dragging = true;
// Lock the window
new_edge_origin_window = std::move(name);
edge_origin_window_pos = ImGui::GetWindowPos();
new_edge = graph_edge {
.output_name = name,
.output_key = key,
.input_name = "",
.input_key = "",
.out = pos,
.in = ImGui::GetIO().MousePos,
.color = drag_color
};
};
auto end_drag = [&](const std::string& name, ImVec2 pos, const std::string& key) {
if (new_edge.output_name == current_window_name)
return;
is_dragging = false;
new_edge_origin_window = ""; // clear window lock
new_edge.input_name = name;
new_edge.input_key = key;
new_edge.in = pos;
new_edge.color = IM_COL32_WHITE;
edge_list.push_back(std::move(new_edge));
};
void draw_row_connectors(graph_node& node, ImGuiID id/*todo: , std::vector<graph_edge>& edge_list*/) {
ImGuiIO& io = ImGui::GetIO(); (void)io; // TODO Does this involve a virtual function call? If not, leave as is.
ImDrawList* draw_list = ImGui::GetWindowDrawList();
const ImVec2 cur_pos = ImGui::GetCursorScreenPos();
const ImVec2 win_pos = ImGui::GetWindowPos();
const ImVec2 win_size = ImGui::GetWindowSize();
// why does 3/5 look better than 1/2?
float ylevel = cur_pos.y - 3*ImGui::GetTextLineHeight()/5;
const bool collapsed = ImGui::IsWindowCollapsed();
if (collapsed)
ylevel = win_pos.y + (win_size.y / 2);
const auto in = ImVec2(win_pos.x, ylevel);
const auto out = ImVec2(win_pos.x + win_size.x, ylevel);
float r = 5.0f;
draw_list->PushClipRectFullScreen();
draw_list->AddCircle(out, r, node.color, 16, 2.0f);
draw_list->AddCircle(in, r, node.color, 16, 2.0f);
draw_list->PopClipRect();
// No rendering edges yet, just updating their positions for when we do
for (graph_edge& edge : edge_list) {
if (edge.output_name == current_window_name && (collapsed || edge.output_key == node.key))
edge.out = out;
if (edge.input_name == current_window_name && (collapsed || edge.input_key == node.key))
edge.in = in;
}
if (!is_dragging && io.MouseDown[0] && points_overlap(io.MouseClickedPos[0], out, r)) {
start_drag(current_window_name, out, node.key);
} else if (is_dragging && !io.MouseDown[0] && points_overlap(io.MousePos, in, r)) {
end_drag(current_window_name, in, node.key);
} else {
new_edge.in = io.MousePos;
}
}
// Visitor stuff
template<class... Ts>
struct overloads : Ts... { using Ts::operator()...; };
void visit_row(graph_node& node);
const auto visitor = overloads {
[](const std::string& val){ ImGui::Text(val.c_str()); },
[](bool & val){ ImGui::Checkbox("##", &val); },
[](int & val){ ImGui::InputInt("##", &val); },
[](double & val){ ImGui::InputDouble("##", &val); },
[](std::vector<graph_node>& val){
for (graph_node& node : val)
visit_row(node);
}
};
// There must be a better way to do this.
void visit_row(graph_node& node) {
ImGui::PushID(node.key.c_str());
ImGuiID id = ImGui::GetItemID();
std::visit(visitor, node.value);
if (node.value.index() != 4 && node.key != "$comment") // kinda terrible
draw_row_connectors(node, id);
ImGui::PopID();
};
void render_edge_line(const graph_edge& e) {
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->PushClipRectFullScreen();
draw_list->AddLine(e.out, e.in, e.color, 2.5f);
draw_list->PopClipRect();
}
void render_edge(const graph_edge& e) {
float dist = sqrt((e.in.x - e.out.x)*(e.in.x - e.out.x)
+ (e.in.y - e.out.y)*(e.in.y - e.out.y));
auto outvec = ImVec2(e.out.x + (dist*0.5), e.out.y);
auto invec = ImVec2(e.in.x - (dist*0.5), e.in.y);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->PushClipRectFullScreen();
draw_list->AddBezierCubic(e.out, outvec, invec, e.in, e.color, 2.5f);
draw_list->PopClipRect();
}
void render_node(GLFWwindow* window, editor_state& state, graph_node& node) {
if (node.key == new_edge_origin_window)
ImGui::SetNextWindowPos(edge_origin_window_pos);
const auto flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;
ImGui::Begin(node.key.c_str(), nullptr, flags);
current_window_name = node.key;
visit_row(node);
ImGui::End();
}
int render(GLFWwindow* window, editor_state& state) {
glfwPollEvents();
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
{
ImGui_ImplGlfw_Sleep(10);
return -1;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (state.show_demo_window)
ImGui::ShowDemoWindow(&state.show_demo_window);
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File"))
{
ImGui::MenuItem("Save", "CTRL+S");
ImGui::MenuItem("Open", "CTRL+O");
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Debug"))
{
ImGui::Checkbox("ImGui Demo Window", &state.show_demo_window);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
for (graph_node& node : node_list)
render_node(window, state, node);
for (const graph_edge& edge : edge_list)
render_edge(edge);
ImGuiIO& io = ImGui::GetIO(); (void)io;
is_dragging = is_dragging && io.MouseDown[0];
if (is_dragging)
render_edge_line(new_edge);
else
new_edge_origin_window = ""; // TODO cleaner way to do this
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(state.clear_color.x * state.clear_color.w, state.clear_color.y * state.clear_color.w, state.clear_color.z * state.clear_color.w, state.clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
return 0;
}
void drop_callback(GLFWwindow* window, int count, const char** paths) {
if (count != 1) return;
node_list.clear();
edge_list.clear();
parse_file(paths[0], node_list, edge_list);
}
}