1 /* nuklear - v1.00 - public domain */
2 /* This is a simple node editor just to show a simple implementation and that
3  * it is possible to achieve it with this library. While all nodes inside this
4  * example use a simple color modifier as content you could change them
5  * to have your custom content depending on the node time.
6  * Biggest difference to most usual implementation is that this example does
7  * not have connectors on the right position of the property that it links.
8  * This is mainly done out of laziness and could be implemented as well but
9  * requires calculating the position of all rows and add connectors.
10  * In addition adding and removing nodes is quite limited at the
11  * moment since it is based on a simple fixed array. If this is to be converted
12  * into something more serious it is probably best to extend it.*/
13 struct node {
14     int ID;
15     char name[32];
16     struct nk_rect bounds;
17     float value;
18     struct nk_color color;
19     int input_count;
20     int output_count;
21     struct node *next;
22     struct node *prev;
23 };
24 
25 struct node_link {
26     int input_id;
27     int input_slot;
28     int output_id;
29     int output_slot;
30     struct nk_vec2 in;
31     struct nk_vec2 out;
32 };
33 
34 struct node_linking {
35     int active;
36     struct node *node;
37     int input_id;
38     int input_slot;
39 };
40 
41 struct node_editor {
42     int initialized;
43     struct node node_buf[32];
44     struct node_link links[64];
45     struct node *begin;
46     struct node *end;
47     int node_count;
48     int link_count;
49     struct nk_rect bounds;
50     struct node *selected;
51     int show_grid;
52     struct nk_vec2 scrolling;
53     struct node_linking linking;
54 };
55 static struct node_editor nodeEditor;
56 
57 static void
node_editor_push(struct node_editor * editor,struct node * node)58 node_editor_push(struct node_editor *editor, struct node *node)
59 {
60     if (!editor->begin) {
61         node->next = NULL;
62         node->prev = NULL;
63         editor->begin = node;
64         editor->end = node;
65     } else {
66         node->prev = editor->end;
67         if (editor->end)
68             editor->end->next = node;
69         node->next = NULL;
70         editor->end = node;
71     }
72 }
73 
74 static void
node_editor_pop(struct node_editor * editor,struct node * node)75 node_editor_pop(struct node_editor *editor, struct node *node)
76 {
77     if (node->next)
78         node->next->prev = node->prev;
79     if (node->prev)
80         node->prev->next = node->next;
81     if (editor->end == node)
82         editor->end = node->prev;
83     if (editor->begin == node)
84         editor->begin = node->next;
85     node->next = NULL;
86     node->prev = NULL;
87 }
88 
89 static struct node*
node_editor_find(struct node_editor * editor,int ID)90 node_editor_find(struct node_editor *editor, int ID)
91 {
92     struct node *iter = editor->begin;
93     while (iter) {
94         if (iter->ID == ID)
95             return iter;
96         iter = iter->next;
97     }
98     return NULL;
99 }
100 
101 static void
node_editor_add(struct node_editor * editor,const char * name,struct nk_rect bounds,struct nk_color col,int in_count,int out_count)102 node_editor_add(struct node_editor *editor, const char *name, struct nk_rect bounds,
103     struct nk_color col, int in_count, int out_count)
104 {
105     static int IDs = 0;
106     struct node *node;
107     NK_ASSERT((nk_size)editor->node_count < NK_LEN(editor->node_buf));
108     node = &editor->node_buf[editor->node_count++];
109     node->ID = IDs++;
110     node->value = 0;
111     node->color = nk_rgb(255, 0, 0);
112     node->input_count = in_count;
113     node->output_count = out_count;
114     node->color = col;
115     node->bounds = bounds;
116     strcpy(node->name, name);
117     node_editor_push(editor, node);
118 }
119 
120 static void
node_editor_link(struct node_editor * editor,int in_id,int in_slot,int out_id,int out_slot)121 node_editor_link(struct node_editor *editor, int in_id, int in_slot,
122     int out_id, int out_slot)
123 {
124     struct node_link *link;
125     NK_ASSERT((nk_size)editor->link_count < NK_LEN(editor->links));
126     link = &editor->links[editor->link_count++];
127     link->input_id = in_id;
128     link->input_slot = in_slot;
129     link->output_id = out_id;
130     link->output_slot = out_slot;
131 }
132 
133 static void
node_editor_init(struct node_editor * editor)134 node_editor_init(struct node_editor *editor)
135 {
136     memset(editor, 0, sizeof(*editor));
137     editor->begin = NULL;
138     editor->end = NULL;
139     node_editor_add(editor, "Source", nk_rect(40, 10, 180, 220), nk_rgb(255, 0, 0), 0, 1);
140     node_editor_add(editor, "Source", nk_rect(40, 260, 180, 220), nk_rgb(0, 255, 0), 0, 1);
141     node_editor_add(editor, "Combine", nk_rect(400, 100, 180, 220), nk_rgb(0,0,255), 2, 2);
142     node_editor_link(editor, 0, 0, 2, 0);
143     node_editor_link(editor, 1, 0, 2, 1);
144     editor->show_grid = nk_true;
145 }
146 
147 static int
node_editor(struct nk_context * ctx)148 node_editor(struct nk_context *ctx)
149 {
150     int n = 0;
151     struct nk_rect total_space;
152     const struct nk_input *in = &ctx->input;
153     struct nk_command_buffer *canvas;
154     struct node *updated = 0;
155     struct node_editor *nodedit = &nodeEditor;
156 
157     if (!nodeEditor.initialized) {
158         node_editor_init(&nodeEditor);
159         nodeEditor.initialized = 1;
160     }
161 
162     if (nk_begin(ctx, "NodeEdit", nk_rect(0, 0, 800, 600),
163         NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE))
164     {
165         /* allocate complete window space */
166         canvas = nk_window_get_canvas(ctx);
167         total_space = nk_window_get_content_region(ctx);
168         nk_layout_space_begin(ctx, NK_STATIC, total_space.h, nodedit->node_count);
169         {
170             struct node *it = nodedit->begin;
171             struct nk_rect size = nk_layout_space_bounds(ctx);
172             struct nk_panel *node = 0;
173 
174             if (nodedit->show_grid) {
175                 /* display grid */
176                 float x, y;
177                 const float grid_size = 32.0f;
178                 const struct nk_color grid_color = nk_rgb(50, 50, 50);
179                 for (x = (float)fmod(size.x - nodedit->scrolling.x, grid_size); x < size.w; x += grid_size)
180                     nk_stroke_line(canvas, x+size.x, size.y, x+size.x, size.y+size.h, 1.0f, grid_color);
181                 for (y = (float)fmod(size.y - nodedit->scrolling.y, grid_size); y < size.h; y += grid_size)
182                     nk_stroke_line(canvas, size.x, y+size.y, size.x+size.w, y+size.y, 1.0f, grid_color);
183             }
184 
185             /* execute each node as a movable group */
186             while (it) {
187                 /* calculate scrolled node window position and size */
188                 nk_layout_space_push(ctx, nk_rect(it->bounds.x - nodedit->scrolling.x,
189                     it->bounds.y - nodedit->scrolling.y, it->bounds.w, it->bounds.h));
190 
191                 /* execute node window */
192                 if (nk_group_begin(ctx, it->name, NK_WINDOW_MOVABLE|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER|NK_WINDOW_TITLE))
193                 {
194                     /* always have last selected node on top */
195 
196                     node = nk_window_get_panel(ctx);
197                     if (nk_input_mouse_clicked(in, NK_BUTTON_LEFT, node->bounds) &&
198                         (!(it->prev && nk_input_mouse_clicked(in, NK_BUTTON_LEFT,
199                         nk_layout_space_rect_to_screen(ctx, node->bounds)))) &&
200                         nodedit->end != it)
201                     {
202                         updated = it;
203                     }
204 
205                     /* ================= NODE CONTENT =====================*/
206                     nk_layout_row_dynamic(ctx, 25, 1);
207                     nk_button_color(ctx, it->color);
208                     it->color.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, it->color.r, 255, 1,1);
209                     it->color.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, it->color.g, 255, 1,1);
210                     it->color.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, it->color.b, 255, 1,1);
211                     it->color.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, it->color.a, 255, 1,1);
212                     /* ====================================================*/
213                     nk_group_end(ctx);
214                 }
215                 {
216                     /* node connector and linking */
217                     float space;
218                     struct nk_rect bounds;
219                     bounds = nk_layout_space_rect_to_local(ctx, node->bounds);
220                     bounds.x += nodedit->scrolling.x;
221                     bounds.y += nodedit->scrolling.y;
222                     it->bounds = bounds;
223 
224                     /* output connector */
225                     space = node->bounds.h / (float)((it->output_count) + 1);
226                     for (n = 0; n < it->output_count; ++n) {
227                         struct nk_rect circle;
228                         circle.x = node->bounds.x + node->bounds.w-4;
229                         circle.y = node->bounds.y + space * (float)(n+1);
230                         circle.w = 8; circle.h = 8;
231                         nk_fill_circle(canvas, circle, nk_rgb(100, 100, 100));
232 
233                         /* start linking process */
234                         if (nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, circle, nk_true)) {
235                             nodedit->linking.active = nk_true;
236                             nodedit->linking.node = it;
237                             nodedit->linking.input_id = it->ID;
238                             nodedit->linking.input_slot = n;
239                         }
240 
241                         /* draw curve from linked node slot to mouse position */
242                         if (nodedit->linking.active && nodedit->linking.node == it &&
243                             nodedit->linking.input_slot == n) {
244                             struct nk_vec2 l0 = nk_vec2(circle.x + 3, circle.y + 3);
245                             struct nk_vec2 l1 = in->mouse.pos;
246                             nk_stroke_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y,
247                                 l1.x - 50.0f, l1.y, l1.x, l1.y, 1.0f, nk_rgb(100, 100, 100));
248                         }
249                     }
250 
251                     /* input connector */
252                     space = node->bounds.h / (float)((it->input_count) + 1);
253                     for (n = 0; n < it->input_count; ++n) {
254                         struct nk_rect circle;
255                         circle.x = node->bounds.x-4;
256                         circle.y = node->bounds.y + space * (float)(n+1);
257                         circle.w = 8; circle.h = 8;
258                         nk_fill_circle(canvas, circle, nk_rgb(100, 100, 100));
259                         if (nk_input_is_mouse_released(in, NK_BUTTON_LEFT) &&
260                             nk_input_is_mouse_hovering_rect(in, circle) &&
261                             nodedit->linking.active && nodedit->linking.node != it) {
262                             nodedit->linking.active = nk_false;
263                             node_editor_link(nodedit, nodedit->linking.input_id,
264                                 nodedit->linking.input_slot, it->ID, n);
265                         }
266                     }
267                 }
268                 it = it->next;
269             }
270 
271             /* reset linking connection */
272             if (nodedit->linking.active && nk_input_is_mouse_released(in, NK_BUTTON_LEFT)) {
273                 nodedit->linking.active = nk_false;
274                 nodedit->linking.node = NULL;
275                 fprintf(stdout, "linking failed\n");
276             }
277 
278             /* draw each link */
279             for (n = 0; n < nodedit->link_count; ++n) {
280                 struct node_link *link = &nodedit->links[n];
281                 struct node *ni = node_editor_find(nodedit, link->input_id);
282                 struct node *no = node_editor_find(nodedit, link->output_id);
283                 float spacei = node->bounds.h / (float)((ni->output_count) + 1);
284                 float spaceo = node->bounds.h / (float)((no->input_count) + 1);
285                 struct nk_vec2 l0 = nk_layout_space_to_screen(ctx,
286                     nk_vec2(ni->bounds.x + ni->bounds.w, 3.0f + ni->bounds.y + spacei * (float)(link->input_slot+1)));
287                 struct nk_vec2 l1 = nk_layout_space_to_screen(ctx,
288                     nk_vec2(no->bounds.x, 3.0f + no->bounds.y + spaceo * (float)(link->output_slot+1)));
289 
290                 l0.x -= nodedit->scrolling.x;
291                 l0.y -= nodedit->scrolling.y;
292                 l1.x -= nodedit->scrolling.x;
293                 l1.y -= nodedit->scrolling.y;
294                 nk_stroke_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y,
295                     l1.x - 50.0f, l1.y, l1.x, l1.y, 1.0f, nk_rgb(100, 100, 100));
296             }
297 
298             if (updated) {
299                 /* reshuffle nodes to have least recently selected node on top */
300                 node_editor_pop(nodedit, updated);
301                 node_editor_push(nodedit, updated);
302             }
303 
304             /* node selection */
305             if (nk_input_mouse_clicked(in, NK_BUTTON_LEFT, nk_layout_space_bounds(ctx))) {
306                 it = nodedit->begin;
307                 nodedit->selected = NULL;
308                 nodedit->bounds = nk_rect(in->mouse.pos.x, in->mouse.pos.y, 100, 200);
309                 while (it) {
310                     struct nk_rect b = nk_layout_space_rect_to_screen(ctx, it->bounds);
311                     b.x -= nodedit->scrolling.x;
312                     b.y -= nodedit->scrolling.y;
313                     if (nk_input_is_mouse_hovering_rect(in, b))
314                         nodedit->selected = it;
315                     it = it->next;
316                 }
317             }
318 
319             /* contextual menu */
320             if (nk_contextual_begin(ctx, 0, nk_vec2(100, 220), nk_window_get_bounds(ctx))) {
321                 const char *grid_option[] = {"Show Grid", "Hide Grid"};
322                 nk_layout_row_dynamic(ctx, 25, 1);
323                 if (nk_contextual_item_label(ctx, "New", NK_TEXT_CENTERED))
324                     node_editor_add(nodedit, "New", nk_rect(400, 260, 180, 220),
325                             nk_rgb(255, 255, 255), 1, 2);
326                 if (nk_contextual_item_label(ctx, grid_option[nodedit->show_grid],NK_TEXT_CENTERED))
327                     nodedit->show_grid = !nodedit->show_grid;
328                 nk_contextual_end(ctx);
329             }
330         }
331         nk_layout_space_end(ctx);
332 
333         /* window content scrolling */
334         if (nk_input_is_mouse_hovering_rect(in, nk_window_get_bounds(ctx)) &&
335             nk_input_is_mouse_down(in, NK_BUTTON_MIDDLE)) {
336             nodedit->scrolling.x += in->mouse.delta.x;
337             nodedit->scrolling.y += in->mouse.delta.y;
338         }
339     }
340     nk_end(ctx);
341     return !nk_window_is_closed(ctx, "NodeEdit");
342 }
343 
344