node_editor.c 14 KB

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