iron_ui_nodes.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. #include "iron_ui_nodes.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <iron_gpu.h>
  6. #include <iron_system.h>
  7. #include "iron_draw.h"
  8. #include "iron_ui.h"
  9. #include "iron_armpack.h"
  10. #include "iron_array.h"
  11. #include "iron_json.h"
  12. #include "iron_gc.h"
  13. static ui_nodes_t *current_nodes = NULL;
  14. static bool ui_nodes_elements_baked = false;
  15. static gpu_texture_t ui_socket_image;
  16. static bool ui_box_select = false;
  17. static int ui_box_select_x = 0;
  18. static int ui_box_select_y = 0;
  19. static const int ui_max_buttons = 9;
  20. static void (*ui_on_header_released)(ui_node_t *) = NULL;
  21. static void (*ui_nodes_on_node_remove)(ui_node_t *) = NULL;
  22. static int ui_node_id = -1;
  23. char *ui_clipboard = "";
  24. char_ptr_array_t *ui_nodes_exclude_remove = NULL; // No removal for listed node types
  25. bool ui_nodes_socket_released = false;
  26. char_ptr_array_t *(*ui_nodes_enum_texts)(char *) = NULL; // Retrieve combo items for buttons of type ENUM
  27. void (*ui_nodes_on_custom_button)(int, char *) = NULL; // Call external function
  28. ui_canvas_control_t *(*ui_nodes_on_canvas_control)(void) = NULL;
  29. void (*ui_nodes_on_canvas_released)(void) = NULL;
  30. void (*ui_nodes_on_socket_released)(int) = NULL;
  31. void (*ui_nodes_on_link_drag)(int, bool) = NULL;
  32. bool ui_nodes_grid_snap = true;
  33. int ui_nodes_grid_snap_w = 40;
  34. int ui_popup_x = 0;
  35. int ui_popup_y = 0;
  36. int ui_popup_w = 0;
  37. int ui_popup_h = 0;
  38. void (*ui_popup_commands)(ui_t *, void *, void *) = NULL;
  39. void *ui_popup_data;
  40. void *ui_popup_data2;
  41. int ui_popup_handle_node_id = -1;
  42. int ui_popup_handle_node_socket_id = -1;
  43. char *tr(char *id, void *map); // translator.ts
  44. char *ui_tr(char *id) {
  45. return tr(id, NULL);
  46. }
  47. void ui_nodes_init(ui_nodes_t *nodes) {
  48. current_nodes = nodes;
  49. memset(current_nodes, 0, sizeof(ui_nodes_t));
  50. current_nodes->zoom = 1.0;
  51. current_nodes->scale_factor = 1.0;
  52. current_nodes->ELEMENT_H = 25.0;
  53. current_nodes->snap_from_id = -1;
  54. current_nodes->snap_to_id = -1;
  55. current_nodes->link_drag_id = -1;
  56. current_nodes->nodes_selected_id = gc_alloc(sizeof(i32_array_t));
  57. current_nodes->handle = ui_handle_create();
  58. }
  59. float UI_NODES_SCALE() {
  60. return current_nodes->scale_factor * current_nodes->zoom;
  61. }
  62. float UI_NODES_PAN_X() {
  63. float zoom_pan = (1.0 - current_nodes->zoom) * current_nodes->uiw / 2.5;
  64. return current_nodes->pan_x * UI_NODES_SCALE() + zoom_pan;
  65. }
  66. float UI_NODES_PAN_Y() {
  67. float zoom_pan = (1.0 - current_nodes->zoom) * current_nodes->uih / 2.5;
  68. return current_nodes->pan_y * UI_NODES_SCALE() + zoom_pan;
  69. }
  70. float UI_LINE_H() {
  71. return current_nodes->ELEMENT_H * UI_NODES_SCALE();
  72. }
  73. float UI_BUTTONS_H(ui_node_t *node) {
  74. float h = 0.0;
  75. for (int i = 0; i < node->buttons->length; ++i) {
  76. ui_node_button_t *but = node->buttons->buffer[i];
  77. if (strcmp(but->type, "RGBA") == 0) {
  78. h += 102.0 * UI_NODES_SCALE() + UI_LINE_H() * 5.0; // Color wheel + controls
  79. }
  80. else if (strcmp(but->type, "VECTOR") == 0) {
  81. h += UI_LINE_H() * 4.0;
  82. }
  83. else if (strcmp(but->type, "CUSTOM") == 0) {
  84. h += UI_LINE_H() * but->height;
  85. }
  86. else {
  87. h += UI_LINE_H();
  88. }
  89. }
  90. return h;
  91. }
  92. float UI_OUTPUTS_H(int sockets_count, int length) {
  93. float h = 0.0;
  94. for (int i = 0; i < (length < 0 ? sockets_count : length); ++i) {
  95. h += UI_LINE_H();
  96. }
  97. return h;
  98. }
  99. bool ui_input_linked(ui_node_canvas_t *canvas, int node_id, int i) {
  100. for (int x = 0; x < canvas->links->length; ++x) {
  101. ui_node_link_t *l = canvas->links->buffer[x];
  102. if (l->to_id == node_id && l->to_socket == i) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. float UI_INPUTS_H(ui_node_canvas_t *canvas, ui_node_socket_t **sockets, int sockets_count, int length) {
  109. float h = 0.0;
  110. for (int i = 0; i < (length < 0 ? sockets_count : length); ++i) {
  111. if (strcmp(sockets[i]->type, "VECTOR") == 0 && sockets[i]->display == 1 && !ui_input_linked(canvas, sockets[i]->node_id, i)) {
  112. h += UI_LINE_H() * 4;
  113. }
  114. else {
  115. h += UI_LINE_H();
  116. }
  117. }
  118. return h;
  119. }
  120. float UI_NODE_H(ui_node_canvas_t *canvas, ui_node_t *node) {
  121. return UI_LINE_H() * 1.2 + UI_INPUTS_H(canvas, node->inputs->buffer, node->inputs->length, -1) + UI_OUTPUTS_H(node->outputs->length, -1) + UI_BUTTONS_H(node);
  122. }
  123. float UI_NODE_W(ui_node_t *node) {
  124. return (node->width != 0 ? node->width : 140.0) * UI_NODES_SCALE();
  125. }
  126. float UI_NODE_X(ui_node_t *node) {
  127. return node->x * UI_NODES_SCALE() + UI_NODES_PAN_X();
  128. }
  129. float UI_NODE_Y(ui_node_t *node) {
  130. return node->y * UI_NODES_SCALE() + UI_NODES_PAN_Y();
  131. }
  132. float UI_INPUT_Y(ui_node_canvas_t *canvas, ui_node_socket_t **sockets, int sockets_count, int pos) {
  133. return UI_LINE_H() * 1.62 + UI_INPUTS_H(canvas, sockets, sockets_count, pos);
  134. }
  135. float UI_OUTPUT_Y(int sockets_count, int pos) {
  136. return UI_LINE_H() * 1.62 + UI_OUTPUTS_H(sockets_count, pos);
  137. }
  138. float ui_p(float f) {
  139. return f * UI_NODES_SCALE();
  140. }
  141. ui_node_t *ui_get_node(ui_node_array_t *nodes, int id) {
  142. for (int i = 0; i < nodes->length; ++i) {
  143. if (nodes->buffer[i]->id == id) {
  144. return nodes->buffer[i];
  145. }
  146. }
  147. return NULL;
  148. }
  149. int ui_get_node_index(ui_node_t **nodes, int nodes_count, int id) {
  150. for (int i = 0; i < nodes_count; ++i) {
  151. if (nodes[i]->id == id) {
  152. return i;
  153. }
  154. }
  155. return -1;
  156. }
  157. int ui_next_node_id(ui_node_array_t *nodes) {
  158. if (ui_node_id == -1) {
  159. for (int i = 0; i < nodes->length; ++i) {
  160. if (ui_node_id < nodes->buffer[i]->id) {
  161. ui_node_id = nodes->buffer[i]->id;
  162. }
  163. }
  164. }
  165. return ++ui_node_id;
  166. }
  167. ui_node_link_t *ui_get_link(ui_node_link_array_t *links, int id) {
  168. for (int i = 0; i < links->length; ++i) {
  169. if (links->buffer[i]->id == id) {
  170. return links->buffer[i];
  171. }
  172. }
  173. return NULL;
  174. }
  175. int ui_get_link_index(ui_node_link_array_t *links, int id) {
  176. for (int i = 0; i < links->length; ++i) {
  177. if (links->buffer[i]->id == id) {
  178. return i;
  179. }
  180. }
  181. return -1;
  182. }
  183. int ui_next_link_id(ui_node_link_array_t *links) {
  184. int id = 0;
  185. for (int i = 0; i < links->length; ++i) {
  186. if (links->buffer[i]->id >= id) {
  187. id = links->buffer[i]->id + 1;
  188. }
  189. }
  190. return id;
  191. }
  192. int ui_get_socket_id(ui_node_array_t *nodes) {
  193. int id = 0;
  194. for (int i = 0; i < nodes->length; ++i) {
  195. ui_node_t *n = nodes->buffer[i];
  196. for (int j = 0; j < n->inputs->length; ++j) {
  197. if (n->inputs->buffer[j]->id >= id) {
  198. id = n->inputs->buffer[j]->id + 1;
  199. }
  200. }
  201. for (int j = 0; j < n->outputs->length; ++j) {
  202. if (n->outputs->buffer[j]->id >= id) {
  203. id = n->outputs->buffer[j]->id + 1;
  204. }
  205. }
  206. }
  207. return id;
  208. }
  209. void ui_nodes_bake_elements() {
  210. if (ui_socket_image.width != 0) {
  211. gpu_texture_destroy(&ui_socket_image);
  212. }
  213. gpu_render_target_init(&ui_socket_image, 24, 24, GPU_TEXTURE_FORMAT_RGBA32);
  214. draw_begin(&ui_socket_image, true, 0x00000000);
  215. draw_set_color(0xff111111);
  216. draw_filled_circle(12, 12, 11, 0);
  217. draw_set_color(0xffffffff);
  218. draw_filled_circle(12, 12, 9, 0);
  219. draw_end();
  220. ui_nodes_elements_baked = true;
  221. }
  222. ui_canvas_control_t *ui_on_default_canvas_control() {
  223. ui_t *current = ui_get_current();
  224. static ui_canvas_control_t c;
  225. c.pan_x = current->input_down_r ? current->input_dx : 0.0;
  226. c.pan_y = current->input_down_r ? current->input_dy : 0.0;
  227. c.zoom = -current->input_wheel_delta / 10.0;
  228. return &c;
  229. }
  230. void ui_draw_link(float x1, float y1, float x2, float y2, bool highlight) {
  231. ui_t *current = ui_get_current();
  232. int c1 = current->ops->theme->LABEL_COL;
  233. int c2 = current->ops->theme->ACCENT_COL;
  234. int c = highlight ? c1 : c2;
  235. draw_set_color(ui_color(ui_color_r(c), ui_color_g(c), ui_color_b(c), 210));
  236. if (current->ops->theme->LINK_STYLE == UI_LINK_STYLE_LINE) {
  237. draw_line_aa(x1, y1, x2, y2, 1.0);
  238. }
  239. else if (current->ops->theme->LINK_STYLE == UI_LINK_STYLE_CUBIC_BEZIER) {
  240. f32_array_t xa;
  241. f32_array_t ya;
  242. float x[] = { x1, x1 + fabs(x1 - x2) / 2.0, x2 - fabs(x1 - x2) / 2.0, x2 };
  243. float y[] = { y1, y1, y2, y2 };
  244. xa.buffer = x;
  245. ya.buffer = y;
  246. draw_cubic_bezier(&xa, &ya, 30, highlight ? 2.0 : 1.0);
  247. }
  248. }
  249. static void ui_remove_link_at(ui_node_canvas_t *canvas, int at) {
  250. canvas->links->buffer[at] = NULL;
  251. for (int i = at; i < canvas->links->length - 1; ++i) {
  252. canvas->links->buffer[i] = canvas->links->buffer[i + 1];
  253. }
  254. canvas->links->length--;
  255. }
  256. static void ui_remove_node_at(ui_node_canvas_t *canvas, int at) {
  257. canvas->nodes->buffer[at] = NULL;
  258. for (int i = at; i < canvas->nodes->length - 1; ++i) {
  259. canvas->nodes->buffer[i] = canvas->nodes->buffer[i + 1];
  260. }
  261. canvas->nodes->length--;
  262. }
  263. void ui_remove_node(ui_node_t *n, ui_node_canvas_t *canvas) {
  264. if (n == NULL) return;
  265. int i = 0;
  266. while (i < canvas->links->length) {
  267. ui_node_link_t *l = canvas->links->buffer[i];
  268. if (l->from_id == n->id || l->to_id == n->id) {
  269. ui_remove_link_at(canvas, i);
  270. }
  271. else {
  272. i++;
  273. }
  274. }
  275. ui_remove_node_at(canvas, ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, n->id));
  276. if (ui_nodes_on_node_remove != NULL) {
  277. (*ui_nodes_on_node_remove)(n);
  278. }
  279. }
  280. bool ui_is_selected(ui_node_t *node) {
  281. for (int i = 0; i < current_nodes->nodes_selected_id->length; ++i) {
  282. if (current_nodes->nodes_selected_id->buffer[i] == node->id) {
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. static void remove_from_selection(ui_node_t *node) {
  289. i32_array_remove(current_nodes->nodes_selected_id, node->id);
  290. }
  291. static void add_to_selection(ui_node_t *node) {
  292. i32_array_push(current_nodes->nodes_selected_id, node->id);
  293. }
  294. void ui_popup(int x, int y, int w, int h, void (*commands)(ui_t *, void *, void *), void *data, void *data2) {
  295. ui_popup_x = x;
  296. ui_popup_y = y;
  297. ui_popup_w = w;
  298. ui_popup_h = h;
  299. ui_popup_commands = commands;
  300. ui_popup_data = data;
  301. ui_popup_data2 = data2;
  302. }
  303. static void color_picker_callback(uint32_t color) {
  304. ui_t *current = ui_get_current();
  305. float *val = (float *)current_nodes->color_picker_callback_data;
  306. val[0] = ui_color_r(color) / 255.0f;
  307. val[1] = ui_color_g(color) / 255.0f;
  308. val[2] = ui_color_b(color) / 255.0f;
  309. current->changed = true;
  310. }
  311. void ui_color_wheel_picker(void *data) {
  312. current_nodes->color_picker_callback_data = data;
  313. current_nodes->color_picker_callback = &color_picker_callback;
  314. }
  315. static void rgba_popup_commands(ui_t *ui, void *data, void *data2) {
  316. ui_handle_t *nhandle = (ui_handle_t *)data;
  317. float *val = (float *)data2;
  318. nhandle->color = ui_color(val[0] * 255.0, val[1] * 255.0, val[2] * 255.0, 255.0);
  319. ui_color_wheel(nhandle, false, -1, -1, true, &ui_color_wheel_picker, val);
  320. val[0] = ui_color_r(nhandle->color) / 255.0f;
  321. val[1] = ui_color_g(nhandle->color) / 255.0f;
  322. val[2] = ui_color_b(nhandle->color) / 255.0f;
  323. }
  324. void ui_nodes_rgba_popup(ui_handle_t *nhandle, float *val, int x, int y) {
  325. ui_t *current = ui_get_current();
  326. ui_popup(x, y, 140.0 * current_nodes->scale_factor, current->ops->theme->ELEMENT_H * 10.0, &rgba_popup_commands, nhandle, val);
  327. }
  328. static float ui_nodes_snap(float f) {
  329. float w = ui_nodes_grid_snap_w * UI_NODES_SCALE();
  330. f = f * UI_NODES_SCALE();
  331. return roundf(f / w) * w;
  332. }
  333. static char_ptr_array_t enum_ar;
  334. static char enum_label[64];
  335. static char enum_texts_data[64][64];
  336. static char *enum_texts[64];
  337. static char_ptr_array_t temp_ar;
  338. static char temp_label[64];
  339. static char temp_texts_data[64][64];
  340. static char *temp_texts[64];
  341. void ui_draw_node(ui_node_t *node, ui_node_canvas_t *canvas) {
  342. ui_t *current = ui_get_current();
  343. float wx = current->_window_x;
  344. float wy = current->_window_y;
  345. float ui_x = current->_x;
  346. float ui_y = current->_y;
  347. float ui_w = current->_w;
  348. float w = UI_NODE_W(node);
  349. float h = UI_NODE_H(canvas, node);
  350. float nx = UI_NODE_X(node);
  351. float ny = UI_NODE_Y(node);
  352. char *text = ui_tr(node->name);
  353. float lineh = UI_LINE_H();
  354. // Disallow input if node is overlapped by another node
  355. current_nodes->_input_started = current->input_started;
  356. if (current->input_started) {
  357. for (int i = ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, node->id) + 1; i < canvas->nodes->length; ++i) {
  358. ui_node_t *n = canvas->nodes->buffer[i];
  359. if (UI_NODE_X(n) < current->input_x - current->_window_x && UI_NODE_X(n) + UI_NODE_W(n) > current->input_x - current->_window_x &&
  360. UI_NODE_Y(n) < current->input_y - current->_window_y && UI_NODE_Y(n) + UI_NODE_H(canvas, n) > current->input_y - current->_window_y) {
  361. current->input_started = false;
  362. break;
  363. }
  364. }
  365. }
  366. // Grid snap preview
  367. if (ui_nodes_grid_snap && ui_is_selected(node) && current_nodes->nodes_drag) {
  368. draw_set_color(current->ops->theme->BUTTON_COL);
  369. ui_draw_rect(false,
  370. ui_nodes_snap(node->x) + UI_NODES_PAN_X(),
  371. ui_nodes_snap(node->y) + UI_NODES_PAN_Y(),
  372. w + 2,
  373. h + 2
  374. );
  375. // nx = ui_nodes_snap(node->x) + UI_NODES_PAN_X();
  376. // ny = ui_nodes_snap(node->y) + UI_NODES_PAN_Y();
  377. }
  378. // Shadow
  379. ui_draw_shadow(nx, ny, w, h);
  380. // Outline
  381. draw_set_color(ui_is_selected(node) ? current->ops->theme->LABEL_COL : current->ops->theme->PRESSED_COL);
  382. ui_draw_rect(true, nx - 1, ny - 1, w + 2, h + 2);
  383. // Body
  384. draw_set_color(current->ops->theme->WINDOW_BG_COL);
  385. ui_draw_rect(true, nx, ny, w, h);
  386. // Header line
  387. draw_set_color(node->color);
  388. draw_filled_rect(nx, ny + lineh - ui_p(3), w, ui_p(3));
  389. // Title
  390. draw_set_color(current->ops->theme->TEXT_COL);
  391. float textw = draw_string_width(current->ops->font, current->font_size, text);
  392. draw_string(text, nx + ui_p(10), ny + ui_p(6));
  393. ny += lineh * 0.5;
  394. // Outputs
  395. for (int i = 0; i < node->outputs->length; ++i) {
  396. ui_node_socket_t *out = node->outputs->buffer[i];
  397. ny += lineh;
  398. draw_set_color(out->color);
  399. draw_scaled_image(&ui_socket_image, nx + w - ui_p(6), ny - ui_p(3), ui_p(12), ui_p(12));
  400. }
  401. ny -= lineh * node->outputs->length;
  402. draw_set_color(current->ops->theme->LABEL_COL);
  403. for (int i = 0; i < node->outputs->length; ++i) {
  404. ui_node_socket_t *out = node->outputs->buffer[i];
  405. ny += lineh;
  406. float strw = draw_string_width(current->ops->font, current->font_size, ui_tr(out->name));
  407. draw_string(ui_tr(out->name), nx + w - strw - ui_p(12), ny - ui_p(3));
  408. if (ui_nodes_on_socket_released != NULL && current->input_enabled && (current->input_released || current->input_released_r)) {
  409. if (current->input_x > wx + nx && current->input_x < wx + nx + w && current->input_y > wy + ny && current->input_y < wy + ny + lineh) {
  410. ui_nodes_on_socket_released(out->id);
  411. ui_nodes_socket_released = true;
  412. }
  413. }
  414. }
  415. // Buttons
  416. ui_handle_t *nhandle = ui_nest(current_nodes->handle, node->id);
  417. ny -= lineh / 3.0; // Fix align
  418. for (int buti = 0; buti < node->buttons->length; ++buti) {
  419. ui_node_button_t *but = node->buttons->buffer[buti];
  420. if (strcmp(but->type, "RGBA") == 0) {
  421. ny += lineh; // 18 + 2 separator
  422. current->_x = nx + 1; // Offset for node selection border
  423. current->_y = ny;
  424. current->_w = w;
  425. float *val = node->outputs->buffer[but->output]->default_value->buffer;
  426. nhandle->color = ui_color(val[0] * 255.0, val[1] * 255.0, val[2] * 255.0, 255.0);
  427. ui_color_wheel(nhandle, false, -1, -1, true, &ui_color_wheel_picker, val);
  428. val[0] = ui_color_r(nhandle->color) / 255.0f;
  429. val[1] = ui_color_g(nhandle->color) / 255.0f;
  430. val[2] = ui_color_b(nhandle->color) / 255.0f;
  431. }
  432. else if (strcmp(but->type, "VECTOR") == 0) {
  433. ny += lineh;
  434. current->_x = nx;
  435. current->_y = ny;
  436. current->_w = w;
  437. float min = but->min;
  438. float max = but->max;
  439. float text_off = current->ops->theme->TEXT_OFFSET;
  440. current->ops->theme->TEXT_OFFSET = 6;
  441. ui_text(ui_tr(but->name), UI_ALIGN_LEFT, 0);
  442. float *val = (float *)but->default_value->buffer;
  443. ui_handle_t *h = ui_nest(nhandle, buti);
  444. ui_handle_t *h0 = ui_nest(h, 0);
  445. if (h0->init) {
  446. h0->value = val[0];
  447. }
  448. ui_handle_t *h1 = ui_nest(h, 1);
  449. if (h1->init) {
  450. h1->value = val[1];
  451. }
  452. ui_handle_t *h2 = ui_nest(h, 2);
  453. if (h2->init) {
  454. h2->value = val[2];
  455. }
  456. val[0] = ui_slider(h0, "X", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  457. val[1] = ui_slider(h1, "Y", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  458. val[2] = ui_slider(h2, "Z", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  459. current->ops->theme->TEXT_OFFSET = text_off;
  460. if (but->output >= 0) {
  461. node->outputs->buffer[but->output]->default_value->buffer = but->default_value->buffer;
  462. }
  463. ny += lineh * 3.0;
  464. }
  465. else if (strcmp(but->type, "VALUE") == 0) {
  466. ny += lineh;
  467. current->_x = nx;
  468. current->_y = ny;
  469. current->_w = w;
  470. ui_node_socket_t *soc = node->outputs->buffer[but->output];
  471. float min = but->min;
  472. float max = but->max;
  473. float prec = but->precision;
  474. float text_off = current->ops->theme->TEXT_OFFSET;
  475. current->ops->theme->TEXT_OFFSET = 6;
  476. ui_handle_t *soc_handle = ui_nest(nhandle, buti);
  477. if (soc_handle->init) {
  478. soc_handle->value = ((float *)soc->default_value->buffer)[0];
  479. }
  480. ((float *)soc->default_value->buffer)[0] = ui_slider(soc_handle, "Value", min, max, true, prec, true, UI_ALIGN_LEFT, true);
  481. current->ops->theme->TEXT_OFFSET = text_off;
  482. }
  483. else if (strcmp(but->type, "STRING") == 0) {
  484. ny += lineh;
  485. current->_x = nx;
  486. current->_y = ny;
  487. current->_w = w;
  488. ui_node_socket_t *soc = but->output >= 0 ? node->outputs->buffer[but->output] : NULL;
  489. ui_handle_t *h = ui_nest(nhandle, buti);
  490. if (h->init) {
  491. h->text = soc != NULL ? soc->default_value->buffer : but->default_value->buffer != NULL ? but->default_value->buffer : "";
  492. }
  493. but->default_value->buffer = ui_text_input(h, ui_tr(but->name), UI_ALIGN_LEFT, true, false);
  494. but->default_value->length = strlen(but->default_value->buffer) + 1;
  495. if (soc != NULL) {
  496. soc->default_value->buffer = but->default_value->buffer;
  497. }
  498. }
  499. else if (strcmp(but->type, "ENUM") == 0) {
  500. ny += lineh;
  501. current->_x = nx;
  502. current->_y = ny;
  503. current->_w = w;
  504. ui_handle_t *but_handle = ui_nest(nhandle, buti);
  505. but_handle->position = ((float *)but->default_value->buffer)[0];
  506. bool combo_select = current->combo_selected_handle == NULL && ui_get_released(UI_ELEMENT_H());
  507. char *label = combo_select ? temp_label : enum_label;
  508. char (*texts_data)[64] = combo_select ? temp_texts_data : enum_texts_data;
  509. char **texts = combo_select ? temp_texts : enum_texts;
  510. char_ptr_array_t *ar = combo_select ? &temp_ar : &enum_ar;
  511. int texts_count = 0;
  512. if (but->data != NULL && but->data->length > 1) {
  513. int wi = 0;
  514. for (int i = 0; i < but->data->length; ++i) {
  515. char c = ((char *)but->data->buffer)[i];
  516. if (c == '\0') {
  517. texts_data[texts_count][wi] = '\0';
  518. texts_count++;
  519. break;
  520. }
  521. if (c == '\n') {
  522. texts_data[texts_count][wi] = '\0';
  523. texts_count++;
  524. wi = 0;
  525. continue;
  526. }
  527. texts_data[texts_count][wi] = c;
  528. wi++;
  529. }
  530. for (int i = 0; i < texts_count; ++i) {
  531. strcpy(texts_data[i], ui_tr(texts_data[i]));
  532. texts[i] = texts_data[i];
  533. }
  534. ar->buffer = texts;
  535. ar->length = texts_count;
  536. }
  537. else {
  538. gc_unroot(ar);
  539. ar = (*ui_nodes_enum_texts)(node->type);
  540. gc_root(ar);
  541. }
  542. strcpy(label, ui_tr(but->name));
  543. ((float *)but->default_value->buffer)[0] = ui_combo(but_handle, ar, label, false, UI_ALIGN_LEFT, true);
  544. }
  545. else if (strcmp(but->type, "BOOL") == 0) {
  546. ny += lineh;
  547. current->_x = nx;
  548. current->_y = ny;
  549. current->_w = w;
  550. ui_handle_t *h = ui_nest(nhandle, buti);
  551. if (h->init) {
  552. h->selected = ((float *)but->default_value->buffer)[0];
  553. }
  554. ((float *)but->default_value->buffer)[0] = ui_check(h, ui_tr(but->name), "");
  555. }
  556. else if (strcmp(but->type, "CUSTOM") == 0) { // Calls external function for custom button drawing
  557. ny += lineh;
  558. current->_x = nx;
  559. current->_y = ny;
  560. current->_w = w;
  561. ui_nodes_on_custom_button(node->id, but->name);
  562. ny += lineh * (but->height - 1); // but->height specifies vertical button size
  563. }
  564. }
  565. ny += lineh / 3.0; // Fix align
  566. // Inputs
  567. for (int i = 0; i < node->inputs->length; ++i) {
  568. ui_node_socket_t *inp = node->inputs->buffer[i];
  569. ny += lineh;
  570. draw_set_color(inp->color);
  571. draw_scaled_image(&ui_socket_image, nx - ui_p(6), ny - ui_p(3), ui_p(12), ui_p(12));
  572. bool is_linked = ui_input_linked(canvas, node->id, i);
  573. if (!is_linked && strcmp(inp->type, "VALUE") == 0) {
  574. current->_x = nx + ui_p(6);
  575. current->_y = ny - ui_p(current_nodes->ELEMENT_H / 3.0);
  576. current->_w = w - ui_p(6);
  577. ui_node_socket_t *soc = inp;
  578. float min = soc->min;
  579. float max = soc->max;
  580. float prec = soc->precision;
  581. float text_off = current->ops->theme->TEXT_OFFSET;
  582. current->ops->theme->TEXT_OFFSET = 6;
  583. ui_handle_t *_handle = ui_nest(nhandle, ui_max_buttons);
  584. ui_handle_t *soc_handle = ui_nest(_handle, i);
  585. if (soc_handle->init) {
  586. soc_handle->value = ((float *)soc->default_value->buffer)[0];
  587. }
  588. ((float *)soc->default_value->buffer)[0] = ui_slider(soc_handle, ui_tr(inp->name), min, max, true, prec, true, UI_ALIGN_LEFT, true);
  589. current->ops->theme->TEXT_OFFSET = text_off;
  590. }
  591. else if (!is_linked && strcmp(inp->type, "STRING") == 0) {
  592. current->_x = nx + ui_p(6);
  593. current->_y = ny - ui_p(9);
  594. current->_w = w - ui_p(6);
  595. ui_node_socket_t *soc = inp;
  596. float text_off = current->ops->theme->TEXT_OFFSET;
  597. current->ops->theme->TEXT_OFFSET = 6;
  598. ui_handle_t *_handle = ui_nest(nhandle, ui_max_buttons);
  599. ui_handle_t *h = ui_nest(_handle, i);
  600. if (h->init) {
  601. strcpy(h->text, soc->default_value->buffer);
  602. }
  603. soc->default_value->buffer = ui_text_input(h, ui_tr(inp->name), UI_ALIGN_LEFT, true, false);
  604. current->ops->theme->TEXT_OFFSET = text_off;
  605. }
  606. else if (!is_linked && strcmp(inp->type, "RGBA") == 0) {
  607. draw_set_color(current->ops->theme->LABEL_COL);
  608. draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
  609. ui_node_socket_t *soc = inp;
  610. draw_set_color(0xff000000);
  611. draw_filled_rect(nx + w - ui_p(38), ny - ui_p(6), ui_p(36), ui_p(18));
  612. float *val = (float *)soc->default_value->buffer;
  613. draw_set_color(ui_color(val[0] * 255, val[1] * 255, val[2] * 255, 255));
  614. float rx = nx + w - ui_p(37);
  615. float ry = ny - ui_p(5);
  616. float rw = ui_p(34);
  617. float rh = ui_p(16);
  618. draw_filled_rect(rx, ry, rw, rh);
  619. float ix = current->input_x - wx;
  620. float iy = current->input_y - wy;
  621. if (current->input_started && ix > rx && iy > ry && ix < rx + rw && iy < ry + rh) {
  622. current_nodes->_input_started = current->input_started = false;
  623. ui_nodes_rgba_popup(nhandle, soc->default_value->buffer, (int)(rx), (int)(ry + UI_ELEMENT_H()));
  624. ui_popup_handle_node_id = node->id;
  625. ui_popup_handle_node_socket_id = soc->id;
  626. }
  627. if (ui_popup_commands != NULL && ui_popup_handle_node_id == node->id && ui_popup_handle_node_socket_id == soc->id) {
  628. // armpack data may have been moved in memory
  629. ui_popup_data = nhandle;
  630. ui_popup_data2 = soc->default_value->buffer;
  631. }
  632. }
  633. else if (!is_linked && strcmp(inp->type, "VECTOR") == 0 && inp->display == 1) {
  634. draw_set_color(current->ops->theme->LABEL_COL);
  635. draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
  636. ny += lineh / 2;
  637. current->_x = nx;
  638. current->_y = ny;
  639. current->_w = w;
  640. float min = inp->min;
  641. float max = inp->max;
  642. float text_off = current->ops->theme->TEXT_OFFSET;
  643. current->ops->theme->TEXT_OFFSET = 6;
  644. float *val = (float *)inp->default_value->buffer;
  645. ui_handle_t *h = ui_nest(nhandle, ui_max_buttons);
  646. ui_handle_t *hi = ui_nest(h, i);
  647. ui_handle_t *h0 = ui_nest(hi, 0);
  648. if (h0->init) {
  649. h0->value = val[0];
  650. }
  651. ui_handle_t *h1 = ui_nest(hi, 1);
  652. if (h1->init) {
  653. h1->value = val[1];
  654. }
  655. ui_handle_t *h2 = ui_nest(hi, 2);
  656. if (h2->init) {
  657. h2->value = val[2];
  658. }
  659. val[0] = ui_slider(h0, "X", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  660. val[1] = ui_slider(h1, "Y", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  661. val[2] = ui_slider(h2, "Z", min, max, true, 100, true, UI_ALIGN_LEFT, true);
  662. current->ops->theme->TEXT_OFFSET = text_off;
  663. ny += lineh * 2.5;
  664. }
  665. else {
  666. draw_set_color(current->ops->theme->LABEL_COL);
  667. draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
  668. }
  669. if (ui_nodes_on_socket_released != NULL && current->input_enabled && (current->input_released || current->input_released_r)) {
  670. if (current->input_x > wx + nx && current->input_x < wx + nx + w && current->input_y > wy + ny && current->input_y < wy + ny + lineh) {
  671. ui_nodes_on_socket_released(inp->id);
  672. ui_nodes_socket_released = true;
  673. }
  674. }
  675. }
  676. current->_x = ui_x;
  677. current->_y = ui_y;
  678. current->_w = ui_w;
  679. current->input_started = current_nodes->_input_started;
  680. }
  681. bool ui_is_node_type_excluded(char *type) {
  682. for (int i = 0; i < ui_nodes_exclude_remove->length; ++i) {
  683. if (strcmp(ui_nodes_exclude_remove->buffer[i], type) == 0) {
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. void ui_node_canvas(ui_nodes_t *nodes, ui_node_canvas_t *canvas) {
  690. current_nodes = nodes;
  691. ui_t *current = ui_get_current();
  692. if (!ui_nodes_elements_baked) {
  693. gpu_texture_t *current = _draw_current;
  694. draw_end();
  695. ui_nodes_bake_elements();
  696. draw_begin(current, false, 0);
  697. }
  698. if (ui_nodes_exclude_remove == NULL) {
  699. ui_nodes_exclude_remove = gc_alloc(sizeof(char_ptr_array_t));
  700. gc_root(ui_nodes_exclude_remove);
  701. }
  702. float wx = current->_window_x;
  703. float wy = current->_window_y;
  704. bool _input_enabled = current->input_enabled;
  705. current->input_enabled = _input_enabled && ui_popup_commands == NULL;
  706. ui_canvas_control_t *controls = ui_nodes_on_canvas_control != NULL ? ui_nodes_on_canvas_control() : ui_on_default_canvas_control();
  707. ui_nodes_socket_released = false;
  708. // Pan canvas
  709. if (current->input_enabled && (controls->pan_x != 0.0 || controls->pan_y != 0.0)) {
  710. current_nodes->pan_x += controls->pan_x / UI_NODES_SCALE();
  711. current_nodes->pan_y += controls->pan_y / UI_NODES_SCALE();
  712. }
  713. // Zoom canvas
  714. if (current->input_enabled && controls->zoom != 0.0) {
  715. current_nodes->zoom += controls->zoom;
  716. if (current_nodes->zoom < 0.1) {
  717. current_nodes->zoom = 0.1;
  718. }
  719. else if (current_nodes->zoom > 1.0) {
  720. current_nodes->zoom = 1.0;
  721. }
  722. current_nodes->zoom = round(current_nodes->zoom * 100.0) / 100.0;
  723. current_nodes->uiw = current->_w;
  724. current_nodes->uih = current->_h;
  725. if (ui_touch_scroll) {
  726. // Zoom to finger location
  727. current_nodes->pan_x -= (current->input_x - current->_window_x - current->_window_w / 2.0) * controls->zoom * 5.0 * (1.0 - current_nodes->zoom);
  728. current_nodes->pan_y -= (current->input_y - current->_window_y - current->_window_h / 2.0) * controls->zoom * 5.0 * (1.0 - current_nodes->zoom);
  729. }
  730. }
  731. current_nodes->scale_factor = UI_SCALE();
  732. current_nodes->ELEMENT_H = current->ops->theme->ELEMENT_H + 2;
  733. ui_set_scale(UI_NODES_SCALE()); // Apply zoomed scale
  734. current->elements_baked = true;
  735. draw_set_font(current->ops->font, current->font_size);
  736. for (int i = 0; i < canvas->links->length; ++i) {
  737. ui_node_link_t *link = canvas->links->buffer[i];
  738. ui_node_t *from = ui_get_node(canvas->nodes, link->from_id);
  739. ui_node_t *to = ui_get_node(canvas->nodes, link->to_id);
  740. float from_x = from == NULL ? current->input_x : wx + UI_NODE_X(from) + UI_NODE_W(from);
  741. float from_y = from == NULL ? current->input_y : wy + UI_NODE_Y(from) + UI_OUTPUT_Y(from->outputs->length, link->from_socket);
  742. float to_x = to == NULL ? current->input_x : wx + UI_NODE_X(to);
  743. float to_y = to == NULL ? current->input_y : wy + UI_NODE_Y(to) + UI_INPUT_Y(canvas, to->inputs->buffer, to->inputs->length, link->to_socket) + UI_OUTPUTS_H(to->outputs->length, -1) + UI_BUTTONS_H(to);
  744. // Cull
  745. float left = to_x > from_x ? from_x : to_x;
  746. float right = to_x > from_x ? to_x : from_x;
  747. float top = to_y > from_y ? from_y : to_y;
  748. float bottom = to_y > from_y ? to_y : from_y;
  749. if (right < 0 || left > wx + current->_window_w ||
  750. bottom < 0 || top > wy + current->_window_h) {
  751. continue;
  752. }
  753. // Snap to nearest socket
  754. if (current_nodes->link_drag_id == link->id) {
  755. if (current_nodes->snap_from_id != -1) {
  756. from_x = current_nodes->snap_x;
  757. from_y = current_nodes->snap_y;
  758. }
  759. if (current_nodes->snap_to_id != -1) {
  760. to_x = current_nodes->snap_x;
  761. to_y = current_nodes->snap_y;
  762. }
  763. current_nodes->snap_from_id = current_nodes->snap_to_id = -1;
  764. for (int j = 0; j < canvas->nodes->length; ++j) {
  765. ui_node_t *node = canvas->nodes->buffer[j];
  766. ui_node_socket_t **inps = node->inputs->buffer;
  767. ui_node_socket_t **outs = node->outputs->buffer;
  768. float node_h = UI_NODE_H(canvas, node);
  769. float rx = wx + UI_NODE_X(node) - UI_LINE_H() / 2;
  770. float ry = wy + UI_NODE_Y(node) - UI_LINE_H() / 2;
  771. float rw = UI_NODE_W(node) + UI_LINE_H();
  772. float rh = node_h + UI_LINE_H();
  773. if (ui_input_in_rect(rx, ry, rw, rh)) {
  774. if (from == NULL && node->id != to->id) { // Snap to output
  775. for (int k = 0; k < node->outputs->length; ++k) {
  776. float sx = wx + UI_NODE_X(node) + UI_NODE_W(node);
  777. float sy = wy + UI_NODE_Y(node) + UI_OUTPUT_Y(node->outputs->length, k);
  778. float rx = sx - UI_LINE_H() / 2;
  779. float ry = sy - UI_LINE_H() / 2;
  780. if (ui_input_in_rect(rx, ry, UI_LINE_H(), UI_LINE_H())) {
  781. current_nodes->snap_x = sx;
  782. current_nodes->snap_y = sy;
  783. current_nodes->snap_from_id = node->id;
  784. current_nodes->snap_socket = k;
  785. break;
  786. }
  787. }
  788. }
  789. else if (to == NULL && node->id != from->id) { // Snap to input
  790. for (int k = 0; k < node->inputs->length; ++k) {
  791. float sx = wx + UI_NODE_X(node);
  792. float sy = wy + UI_NODE_Y(node) + UI_INPUT_Y(canvas, inps, node->inputs->length, k) + UI_OUTPUTS_H(node->outputs->length, -1) + UI_BUTTONS_H(node);
  793. float rx = sx - UI_LINE_H() / 2.0;
  794. float ry = sy - UI_LINE_H() / 2.0;
  795. if (ui_input_in_rect(rx, ry, UI_LINE_H(), UI_LINE_H())) {
  796. current_nodes->snap_x = sx;
  797. current_nodes->snap_y = sy;
  798. current_nodes->snap_to_id = node->id;
  799. current_nodes->snap_socket = k;
  800. break;
  801. }
  802. }
  803. }
  804. }
  805. }
  806. }
  807. bool selected = false;
  808. for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
  809. int n_id = current_nodes->nodes_selected_id->buffer[j];
  810. if (link->from_id == n_id || link->to_id == n_id) {
  811. selected = true;
  812. break;
  813. }
  814. }
  815. ui_draw_link(from_x - wx, from_y - wy, to_x - wx, to_y - wy, selected);
  816. }
  817. for (int i = 0; i < canvas->nodes->length; ++i) {
  818. ui_node_t *node = canvas->nodes->buffer[i];
  819. // Cull
  820. if (UI_NODE_X(node) > current->_window_w || UI_NODE_X(node) + UI_NODE_W(node) < 0 ||
  821. UI_NODE_Y(node) > current->_window_h || UI_NODE_Y(node) + UI_NODE_H(canvas, node) < 0) {
  822. if (!ui_is_selected(node)) {
  823. continue;
  824. }
  825. }
  826. ui_node_socket_t **inps = node->inputs->buffer;
  827. ui_node_socket_t **outs = node->outputs->buffer;
  828. // Drag node
  829. float node_h = UI_NODE_H(canvas, node);
  830. if (current->input_enabled && ui_input_in_rect(wx + UI_NODE_X(node) - UI_LINE_H() / 2.0, wy + UI_NODE_Y(node), UI_NODE_W(node) + UI_LINE_H(), UI_LINE_H())) {
  831. if (current->input_started) {
  832. if (current->is_shift_down || current->is_ctrl_down) {
  833. // Add to selection or deselect
  834. if (ui_is_selected(node)) {
  835. remove_from_selection(node);
  836. }
  837. else {
  838. add_to_selection(node);
  839. }
  840. }
  841. else if (current_nodes->nodes_selected_id->length <= 1) {
  842. // Selecting single node, otherwise wait for input release
  843. current_nodes->nodes_selected_id->length = 0;
  844. i32_array_push(current_nodes->nodes_selected_id, node->id);
  845. }
  846. current_nodes->move_on_top = node; // Place selected node on top
  847. current_nodes->nodes_drag = true;
  848. current_nodes->dragged = false;
  849. }
  850. else if (current->input_released && !current->is_shift_down && !current->is_ctrl_down && !current_nodes->dragged) {
  851. // No drag performed, select single node
  852. current_nodes->nodes_selected_id->length = 0;
  853. i32_array_push(current_nodes->nodes_selected_id, node->id);
  854. if (ui_on_header_released != NULL) {
  855. ui_on_header_released(node);
  856. }
  857. }
  858. }
  859. if (current->input_started && ui_input_in_rect(wx + UI_NODE_X(node) - UI_LINE_H() / 2, wy + UI_NODE_Y(node) - UI_LINE_H() / 2, UI_NODE_W(node) + UI_LINE_H(), node_h + UI_LINE_H())) {
  860. // Check sockets
  861. if (current_nodes->link_drag_id == -1) {
  862. for (int j = 0; j < node->outputs->length; ++j) {
  863. float sx = wx + UI_NODE_X(node) + UI_NODE_W(node);
  864. float sy = wy + UI_NODE_Y(node) + UI_OUTPUT_Y(node->outputs->length, j);
  865. if (ui_input_in_rect(sx - UI_LINE_H() / 2.0, sy - UI_LINE_H() / 2.0, UI_LINE_H(), UI_LINE_H())) {
  866. // New link from output
  867. ui_node_link_t *l = (ui_node_link_t *)malloc(sizeof(ui_node_link_t)); // TODO: store at canvas->links without malloc
  868. l->id = ui_next_link_id(canvas->links);
  869. l->from_id = node->id;
  870. l->from_socket = j;
  871. l->to_id = -1;
  872. l->to_socket = -1;
  873. any_array_push(canvas->links, l);
  874. current_nodes->link_drag_id = l->id;
  875. current_nodes->is_new_link = true;
  876. break;
  877. }
  878. }
  879. }
  880. if (current_nodes->link_drag_id == -1) {
  881. for (int j = 0; j < node->inputs->length; ++j) {
  882. float sx = wx + UI_NODE_X(node);
  883. float sy = wy + UI_NODE_Y(node) + UI_INPUT_Y(canvas, inps, node->inputs->length, j) + UI_OUTPUTS_H(node->outputs->length, -1) + UI_BUTTONS_H(node);
  884. if (ui_input_in_rect(sx - UI_LINE_H() / 2.0, sy - UI_LINE_H() / 2.0, UI_LINE_H(), UI_LINE_H())) {
  885. // Already has a link - disconnect
  886. for (int k = 0; k < canvas->links->length; ++k) {
  887. ui_node_link_t *l = canvas->links->buffer[k];
  888. if (l->to_id == node->id && l->to_socket == j) {
  889. l->to_id = l->to_socket = -1;
  890. current_nodes->link_drag_id = l->id;
  891. current_nodes->is_new_link = false;
  892. break;
  893. }
  894. }
  895. if (current_nodes->link_drag_id != -1) {
  896. break;
  897. }
  898. // New link from input
  899. ui_node_link_t *l = (ui_node_link_t *)malloc(sizeof(ui_node_link_t));
  900. l->id = ui_next_link_id(canvas->links);
  901. l->from_id = -1;
  902. l->from_socket = -1;
  903. l->to_id = node->id;
  904. l->to_socket = j;
  905. any_array_push(canvas->links, l);
  906. current_nodes->link_drag_id = l->id;
  907. current_nodes->is_new_link = true;
  908. break;
  909. }
  910. }
  911. }
  912. }
  913. else if (current->input_released) {
  914. if (current_nodes->snap_to_id != -1) { // Connect to input
  915. // Force single link per input
  916. for (int j = 0; j < canvas->links->length; ++j) {
  917. ui_node_link_t *l = canvas->links->buffer[j];
  918. if (l->to_id == current_nodes->snap_to_id && l->to_socket == current_nodes->snap_socket) {
  919. ui_remove_link_at(canvas, ui_get_link_index(canvas->links, l->id));
  920. break;
  921. }
  922. }
  923. ui_node_link_t *link_drag = ui_get_link(canvas->links, current_nodes->link_drag_id);
  924. link_drag->to_id = current_nodes->snap_to_id;
  925. link_drag->to_socket = current_nodes->snap_socket;
  926. current->changed = true;
  927. }
  928. else if (current_nodes->snap_from_id != -1) { // Connect to output
  929. ui_node_link_t *link_drag = ui_get_link(canvas->links, current_nodes->link_drag_id);
  930. link_drag->from_id = current_nodes->snap_from_id;
  931. link_drag->from_socket = current_nodes->snap_socket;
  932. current->changed = true;
  933. }
  934. else if (current_nodes->link_drag_id != -1) { // Remove dragged link
  935. current->changed = true;
  936. if (ui_nodes_on_link_drag != NULL) {
  937. ui_nodes_on_link_drag(current_nodes->link_drag_id, current_nodes->is_new_link);
  938. }
  939. ui_remove_link_at(canvas, ui_get_link_index(canvas->links, current_nodes->link_drag_id));
  940. }
  941. current_nodes->snap_to_id = current_nodes->snap_from_id = -1;
  942. current_nodes->link_drag_id = -1;
  943. current_nodes->nodes_drag = false;
  944. }
  945. if (current_nodes->nodes_drag && ui_is_selected(node) && !current->input_down_r) {
  946. if (current->input_dx != 0 || current->input_dy != 0) {
  947. current_nodes->dragged = true;
  948. node->x += current->input_dx / UI_NODES_SCALE();
  949. node->y += current->input_dy / UI_NODES_SCALE();
  950. // Absolute
  951. // node->x = (current->input_x - current->_window_x - UI_NODES_PAN_X()) / UI_NODES_SCALE();
  952. // node->y = (current->input_y - current->_window_y - UI_NODES_PAN_Y()) / UI_NODES_SCALE();
  953. }
  954. }
  955. if (ui_nodes_grid_snap && current->input_released && ui_is_selected(node)) {
  956. node->x = ui_nodes_snap(node->x) / UI_NODES_SCALE();
  957. node->y = ui_nodes_snap(node->y) / UI_NODES_SCALE();
  958. }
  959. ui_draw_node(node, canvas);
  960. }
  961. if (ui_nodes_on_canvas_released != NULL && current->input_enabled && (current->input_released || current->input_released_r) && !ui_nodes_socket_released) {
  962. ui_nodes_on_canvas_released();
  963. }
  964. if (ui_box_select) {
  965. draw_set_color(0x223333dd);
  966. draw_filled_rect(ui_box_select_x, ui_box_select_y, current->input_x - ui_box_select_x - current->_window_x, current->input_y - ui_box_select_y - current->_window_y);
  967. draw_set_color(0x773333dd);
  968. draw_rect(ui_box_select_x, ui_box_select_y, current->input_x - ui_box_select_x - current->_window_x, current->input_y - ui_box_select_y - current->_window_y, 1);
  969. draw_set_color(0xffffffff);
  970. }
  971. if (current->input_enabled && current->input_started && !current->is_alt_down &&
  972. current_nodes->link_drag_id == -1 && !current_nodes->nodes_drag && !current->changed &&
  973. ui_input_in_rect(current->_window_x, current->_window_y, current->_window_w, current->_window_h)) {
  974. ui_box_select = true;
  975. ui_box_select_x = current->input_x - current->_window_x;
  976. ui_box_select_y = current->input_y - current->_window_y;
  977. }
  978. else if (ui_box_select && !current->input_down) {
  979. ui_box_select = false;
  980. int left = ui_box_select_x;
  981. int top = ui_box_select_y;
  982. int right = current->input_x - current->_window_x;
  983. int bottom = current->input_y - current->_window_y;
  984. if (left > right) {
  985. int t = left;
  986. left = right;
  987. right = t;
  988. }
  989. if (top > bottom) {
  990. int t = top;
  991. top = bottom;
  992. bottom = t;
  993. }
  994. ui_node_t *nodes[32];
  995. int nodes_count = 0;
  996. for (int j = 0; j < canvas->nodes->length; ++j) {
  997. ui_node_t *n = canvas->nodes->buffer[j];
  998. if (UI_NODE_X(n) + UI_NODE_W(n) > left && UI_NODE_X(n) < right &&
  999. UI_NODE_Y(n) + UI_NODE_H(canvas, n) > top && UI_NODE_Y(n) < bottom) {
  1000. nodes[nodes_count] = n;
  1001. nodes_count++;
  1002. }
  1003. }
  1004. if (current->is_shift_down || current->is_ctrl_down) {
  1005. for (int j = 0; j < nodes_count; ++j) {
  1006. add_to_selection(nodes[j]);
  1007. }
  1008. }
  1009. else {
  1010. current_nodes->nodes_selected_id->length = 0;
  1011. for (int j = 0; j < nodes_count; ++j) {
  1012. add_to_selection(nodes[j]);
  1013. }
  1014. }
  1015. }
  1016. // Place selected node on top
  1017. if (current_nodes->move_on_top != NULL) {
  1018. int index = ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, current_nodes->move_on_top->id);
  1019. ui_remove_node_at(canvas, index);
  1020. any_array_push(canvas->nodes, current_nodes->move_on_top);
  1021. current_nodes->move_on_top = NULL;
  1022. }
  1023. // Node copy & paste
  1024. bool cut_selected = false;
  1025. if (ui_is_copy && !current->is_typing) {
  1026. ui_node_t *copy_nodes[32];
  1027. int copy_nodes_count = 0;
  1028. for (int i = 0; i < current_nodes->nodes_selected_id->length; ++i) {
  1029. int id = current_nodes->nodes_selected_id->buffer[i];
  1030. ui_node_t *n = ui_get_node(canvas->nodes, id);
  1031. if (ui_is_node_type_excluded(n->type)) {
  1032. continue;
  1033. }
  1034. copy_nodes[copy_nodes_count] = n;
  1035. copy_nodes_count++;
  1036. }
  1037. ui_node_link_t *copy_links[64];
  1038. int copy_links_count = 0;
  1039. for (int i = 0; i < canvas->links->length; ++i) {
  1040. ui_node_link_t *l = canvas->links->buffer[i];
  1041. ui_node_t *from = NULL;
  1042. for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
  1043. if (current_nodes->nodes_selected_id->buffer[j] == l->from_id) {
  1044. from = ui_get_node(canvas->nodes, l->from_id);
  1045. break;
  1046. }
  1047. }
  1048. ui_node_t *to = NULL;
  1049. for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
  1050. if (current_nodes->nodes_selected_id->buffer[j] == l->to_id) {
  1051. to = ui_get_node(canvas->nodes, l->to_id);
  1052. break;
  1053. }
  1054. }
  1055. if (from != NULL && !ui_is_node_type_excluded(from->type) &&
  1056. to != NULL && !ui_is_node_type_excluded(to->type)) {
  1057. copy_links[copy_links_count] = l;
  1058. copy_links_count++;
  1059. }
  1060. }
  1061. if (copy_nodes_count > 0) {
  1062. ui_node_canvas_t copy_canvas = {};
  1063. copy_canvas.name = canvas->name;
  1064. ui_node_array_t nodes = { .buffer = copy_nodes, .length = copy_nodes_count };
  1065. ui_node_link_array_t links = { .buffer = copy_links, .length = copy_links_count };
  1066. copy_canvas.nodes = &nodes;
  1067. copy_canvas.links = &links;
  1068. gc_unroot(ui_clipboard);
  1069. ui_clipboard = ui_node_canvas_to_json(&copy_canvas);
  1070. gc_root(ui_clipboard);
  1071. }
  1072. cut_selected = ui_is_cut;
  1073. ui_is_copy = false;
  1074. ui_is_cut = false;
  1075. }
  1076. if (ui_is_paste && !current->is_typing) {
  1077. ui_is_paste = false;
  1078. bool is_json = ui_clipboard[0] == '{';
  1079. if (is_json) {
  1080. ui_node_canvas_t *paste_canvas = json_parse(ui_clipboard);
  1081. gc_root(paste_canvas); // TODO
  1082. // Convert button data from string to u8 array
  1083. for (int i = 0; i < paste_canvas->nodes->length; ++i) {
  1084. for (int j = 0; j < paste_canvas->nodes->buffer[i]->buttons->length; ++j) {
  1085. ui_node_button_t *but = paste_canvas->nodes->buffer[i]->buttons->buffer[j];
  1086. if (but->data != NULL) {
  1087. but->data = u8_array_create_from_raw(but->data, strlen(but->data) + 1);
  1088. }
  1089. }
  1090. }
  1091. for (int i = 0; i < paste_canvas->links->length; ++i) {
  1092. ui_node_link_t *l = paste_canvas->links->buffer[i];
  1093. // Assign unique link id
  1094. l->id = ui_next_link_id(canvas->links);
  1095. any_array_push(canvas->links, l);
  1096. }
  1097. int offset_x = (int)(((int)(current->input_x / UI_SCALE()) * UI_NODES_SCALE() - wx - UI_NODES_PAN_X()) / UI_NODES_SCALE()) - paste_canvas->nodes->buffer[paste_canvas->nodes->length - 1]->x;
  1098. int offset_y = (int)(((int)(current->input_y / UI_SCALE()) * UI_NODES_SCALE() - wy - UI_NODES_PAN_Y()) / UI_NODES_SCALE()) - paste_canvas->nodes->buffer[paste_canvas->nodes->length - 1]->y;
  1099. for (int i = 0; i < paste_canvas->nodes->length; ++i) {
  1100. ui_node_t *n = paste_canvas->nodes->buffer[i];
  1101. // Assign unique node id
  1102. int old_id = n->id;
  1103. n->id = ui_next_node_id(canvas->nodes);
  1104. for (int j = 0; j < n->inputs->length; ++j) {
  1105. ui_node_socket_t *soc = n->inputs->buffer[j];
  1106. soc->id = ui_get_socket_id(canvas->nodes);
  1107. soc->node_id = n->id;
  1108. }
  1109. for (int j = 0; j < n->outputs->length; ++j) {
  1110. ui_node_socket_t *soc = n->outputs->buffer[j];
  1111. soc->id = ui_get_socket_id(canvas->nodes);
  1112. soc->node_id = n->id;
  1113. }
  1114. for (int j = 0; j < paste_canvas->links->length; ++j) {
  1115. ui_node_link_t *l = paste_canvas->links->buffer[j];
  1116. if (l->from_id == old_id) l->from_id = n->id;
  1117. else if (l->to_id == old_id) l->to_id = n->id;
  1118. }
  1119. n->x += offset_x;
  1120. n->y += offset_y;
  1121. any_array_push(canvas->nodes, n);
  1122. }
  1123. current_nodes->nodes_drag = true;
  1124. current_nodes->nodes_selected_id->length = 0;
  1125. for (int i = 0; i < paste_canvas->nodes->length; ++i) {
  1126. i32_array_push(current_nodes->nodes_selected_id, paste_canvas->nodes->buffer[i]->id);
  1127. }
  1128. current->changed = true;
  1129. }
  1130. }
  1131. // Select all nodes
  1132. if (current->is_ctrl_down && current->key_code == IRON_KEY_A && !current->is_typing) {
  1133. current_nodes->nodes_selected_id->length = 0;
  1134. for (int i = 0; i < canvas->nodes->length; ++i) {
  1135. add_to_selection(canvas->nodes->buffer[i]);
  1136. }
  1137. }
  1138. // Node removal
  1139. bool node_removal = current->input_enabled && (current->is_backspace_down || current->is_delete_down) && !current->is_typing;
  1140. if (node_removal || cut_selected) {
  1141. int i = current_nodes->nodes_selected_id->length - 1;
  1142. while (i >= 0) {
  1143. int nid = current_nodes->nodes_selected_id->buffer[i--];
  1144. ui_node_t *n = ui_get_node(canvas->nodes, nid);
  1145. if (ui_is_node_type_excluded(n->type)) {
  1146. continue;
  1147. }
  1148. ui_remove_node(n, canvas);
  1149. current->changed = true;
  1150. }
  1151. current_nodes->nodes_selected_id->length = 0;
  1152. }
  1153. ui_set_scale(current_nodes->scale_factor); // Restore non-zoomed scale
  1154. current->elements_baked = true;
  1155. current->input_enabled = _input_enabled;
  1156. if (ui_popup_commands != NULL) {
  1157. current->_x = ui_popup_x;
  1158. current->_y = ui_popup_y;
  1159. current->_w = ui_popup_w;
  1160. ui_draw_shadow(current->_x - 5, current->_y - 5, current->_w + 10, ui_popup_h * UI_SCALE() + 10);
  1161. draw_set_color(current->ops->theme->SEPARATOR_COL);
  1162. ui_draw_rect(true, current->_x - 5, current->_y - 5, current->_w + 10, ui_popup_h * UI_SCALE() + 10);
  1163. (*ui_popup_commands)(current, ui_popup_data, ui_popup_data2);
  1164. bool hide = (current->input_started || current->input_started_r) && (current->input_x - wx < ui_popup_x - 6 || current->input_x - wx > ui_popup_x + ui_popup_w + 6 || current->input_y - wy < ui_popup_y - 6 || current->input_y - wy > ui_popup_y + ui_popup_h * UI_SCALE() + 6);
  1165. if (hide || current->is_escape_down) {
  1166. ui_popup_commands = NULL;
  1167. }
  1168. }
  1169. }
  1170. void ui_node_canvas_encode(ui_node_canvas_t *canvas) {
  1171. // armpack_encode_start(encoded);
  1172. armpack_encode_map(3);
  1173. armpack_encode_string("name");
  1174. armpack_encode_string(canvas->name);
  1175. armpack_encode_string("nodes");
  1176. armpack_encode_array(canvas->nodes->length);
  1177. for (int i = 0; i < canvas->nodes->length; ++i) {
  1178. armpack_encode_map(10);
  1179. armpack_encode_string("id");
  1180. armpack_encode_i32(canvas->nodes->buffer[i]->id);
  1181. armpack_encode_string("name");
  1182. armpack_encode_string(canvas->nodes->buffer[i]->name);
  1183. armpack_encode_string("type");
  1184. armpack_encode_string(canvas->nodes->buffer[i]->type);
  1185. armpack_encode_string("x");
  1186. armpack_encode_f32(canvas->nodes->buffer[i]->x);
  1187. armpack_encode_string("y");
  1188. armpack_encode_f32(canvas->nodes->buffer[i]->y);
  1189. armpack_encode_string("color");
  1190. armpack_encode_i32(canvas->nodes->buffer[i]->color);
  1191. armpack_encode_string("inputs");
  1192. armpack_encode_array(canvas->nodes->buffer[i]->inputs->length);
  1193. for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
  1194. armpack_encode_map(10);
  1195. armpack_encode_string("id");
  1196. armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->id);
  1197. armpack_encode_string("node_id");
  1198. armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->node_id);
  1199. armpack_encode_string("name");
  1200. armpack_encode_string(canvas->nodes->buffer[i]->inputs->buffer[j]->name);
  1201. armpack_encode_string("type");
  1202. armpack_encode_string(canvas->nodes->buffer[i]->inputs->buffer[j]->type);
  1203. armpack_encode_string("color");
  1204. armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->color);
  1205. armpack_encode_string("default_value");
  1206. armpack_encode_array_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
  1207. armpack_encode_string("min");
  1208. armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->min);
  1209. armpack_encode_string("max");
  1210. armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->max);
  1211. armpack_encode_string("precision");
  1212. armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->precision);
  1213. armpack_encode_string("display");
  1214. armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->display);
  1215. }
  1216. armpack_encode_string("outputs");
  1217. armpack_encode_array(canvas->nodes->buffer[i]->outputs->length);
  1218. for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
  1219. armpack_encode_map(10);
  1220. armpack_encode_string("id");
  1221. armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->id);
  1222. armpack_encode_string("node_id");
  1223. armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->node_id);
  1224. armpack_encode_string("name");
  1225. armpack_encode_string(canvas->nodes->buffer[i]->outputs->buffer[j]->name);
  1226. armpack_encode_string("type");
  1227. armpack_encode_string(canvas->nodes->buffer[i]->outputs->buffer[j]->type);
  1228. armpack_encode_string("color");
  1229. armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->color);
  1230. armpack_encode_string("default_value");
  1231. armpack_encode_array_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
  1232. armpack_encode_string("min");
  1233. armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->min);
  1234. armpack_encode_string("max");
  1235. armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->max);
  1236. armpack_encode_string("precision");
  1237. armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->precision);
  1238. armpack_encode_string("display");
  1239. armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->display);
  1240. }
  1241. armpack_encode_string("buttons");
  1242. armpack_encode_array(canvas->nodes->buffer[i]->buttons->length);
  1243. for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
  1244. armpack_encode_map(9);
  1245. armpack_encode_string("name");
  1246. armpack_encode_string(canvas->nodes->buffer[i]->buttons->buffer[j]->name);
  1247. armpack_encode_string("type");
  1248. armpack_encode_string(canvas->nodes->buffer[i]->buttons->buffer[j]->type);
  1249. armpack_encode_string("output");
  1250. armpack_encode_i32(canvas->nodes->buffer[i]->buttons->buffer[j]->output);
  1251. armpack_encode_string("default_value");
  1252. armpack_encode_array_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
  1253. armpack_encode_string("data");
  1254. u8_array_t *u8 = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
  1255. armpack_encode_array_u8(u8);
  1256. armpack_encode_string("min");
  1257. armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->min);
  1258. armpack_encode_string("max");
  1259. armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->max);
  1260. armpack_encode_string("precision");
  1261. armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->precision);
  1262. armpack_encode_string("height");
  1263. armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->height);
  1264. }
  1265. armpack_encode_string("width");
  1266. armpack_encode_i32(canvas->nodes->buffer[i]->width);
  1267. }
  1268. armpack_encode_string("links");
  1269. armpack_encode_array(canvas->links->length);
  1270. for (int i = 0; i < canvas->links->length; ++i) {
  1271. armpack_encode_map(5);
  1272. armpack_encode_string("id");
  1273. armpack_encode_i32(canvas->links->buffer[i]->id);
  1274. armpack_encode_string("from_id");
  1275. armpack_encode_i32(canvas->links->buffer[i]->from_id);
  1276. armpack_encode_string("from_socket");
  1277. armpack_encode_i32(canvas->links->buffer[i]->from_socket);
  1278. armpack_encode_string("to_id");
  1279. armpack_encode_i32(canvas->links->buffer[i]->to_id);
  1280. armpack_encode_string("to_socket");
  1281. armpack_encode_i32(canvas->links->buffer[i]->to_socket);
  1282. }
  1283. }
  1284. uint32_t ui_node_canvas_encoded_size(ui_node_canvas_t *canvas) {
  1285. uint32_t size = 0;
  1286. size += armpack_size_map();
  1287. size += armpack_size_string("name");
  1288. size += armpack_size_string(canvas->name);
  1289. size += armpack_size_string("nodes");
  1290. size += armpack_size_array();
  1291. for (int i = 0; i < canvas->nodes->length; ++i) {
  1292. size += armpack_size_map();
  1293. size += armpack_size_string("id");
  1294. size += armpack_size_i32();
  1295. size += armpack_size_string("name");
  1296. size += armpack_size_string(canvas->nodes->buffer[i]->name);
  1297. size += armpack_size_string("type");
  1298. size += armpack_size_string(canvas->nodes->buffer[i]->type);
  1299. size += armpack_size_string("x");
  1300. size += armpack_size_f32();
  1301. size += armpack_size_string("y");
  1302. size += armpack_size_f32();
  1303. size += armpack_size_string("color");
  1304. size += armpack_size_i32();
  1305. size += armpack_size_string("inputs");
  1306. size += armpack_size_array();
  1307. for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
  1308. size += armpack_size_map();
  1309. size += armpack_size_string("id");
  1310. size += armpack_size_i32();
  1311. size += armpack_size_string("node_id");
  1312. size += armpack_size_i32();
  1313. size += armpack_size_string("name");
  1314. size += armpack_size_string(canvas->nodes->buffer[i]->inputs->buffer[j]->name);
  1315. size += armpack_size_string("type");
  1316. size += armpack_size_string(canvas->nodes->buffer[i]->inputs->buffer[j]->type);
  1317. size += armpack_size_string("color");
  1318. size += armpack_size_i32();
  1319. size += armpack_size_string("default_value");
  1320. size += armpack_size_array_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
  1321. size += armpack_size_string("min");
  1322. size += armpack_size_f32();
  1323. size += armpack_size_string("max");
  1324. size += armpack_size_f32();
  1325. size += armpack_size_string("precision");
  1326. size += armpack_size_f32();
  1327. size += armpack_size_string("display");
  1328. size += armpack_size_i32();
  1329. }
  1330. size += armpack_size_string("outputs");
  1331. size += armpack_size_array();
  1332. for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
  1333. size += armpack_size_map();
  1334. size += armpack_size_string("id");
  1335. size += armpack_size_i32();
  1336. size += armpack_size_string("node_id");
  1337. size += armpack_size_i32();
  1338. size += armpack_size_string("name");
  1339. size += armpack_size_string(canvas->nodes->buffer[i]->outputs->buffer[j]->name);
  1340. size += armpack_size_string("type");
  1341. size += armpack_size_string(canvas->nodes->buffer[i]->outputs->buffer[j]->type);
  1342. size += armpack_size_string("color");
  1343. size += armpack_size_i32();
  1344. size += armpack_size_string("default_value");
  1345. size += armpack_size_array_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
  1346. size += armpack_size_string("min");
  1347. size += armpack_size_f32();
  1348. size += armpack_size_string("max");
  1349. size += armpack_size_f32();
  1350. size += armpack_size_string("precision");
  1351. size += armpack_size_f32();
  1352. size += armpack_size_string("display");
  1353. size += armpack_size_i32();
  1354. }
  1355. size += armpack_size_string("buttons");
  1356. size += armpack_size_array();
  1357. for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
  1358. size += armpack_size_map();
  1359. size += armpack_size_string("name");
  1360. size += armpack_size_string(canvas->nodes->buffer[i]->buttons->buffer[j]->name);
  1361. size += armpack_size_string("type");
  1362. size += armpack_size_string(canvas->nodes->buffer[i]->buttons->buffer[j]->type);
  1363. size += armpack_size_string("output");
  1364. size += armpack_size_i32();
  1365. size += armpack_size_string("default_value");
  1366. size += armpack_size_array_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
  1367. size += armpack_size_string("data");
  1368. u8_array_t *u8 = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
  1369. size += armpack_size_array_u8(u8);
  1370. size += armpack_size_string("min");
  1371. size += armpack_size_f32();
  1372. size += armpack_size_string("max");
  1373. size += armpack_size_f32();
  1374. size += armpack_size_string("precision");
  1375. size += armpack_size_f32();
  1376. size += armpack_size_string("height");
  1377. size += armpack_size_f32();
  1378. }
  1379. size += armpack_size_string("width");
  1380. size += armpack_size_f32();
  1381. }
  1382. size += armpack_size_string("links");
  1383. size += armpack_size_array();
  1384. for (int i = 0; i < canvas->links->length; ++i) {
  1385. size += armpack_size_map();
  1386. size += armpack_size_string("id");
  1387. size += armpack_size_i32();
  1388. size += armpack_size_string("from_id");
  1389. size += armpack_size_i32();
  1390. size += armpack_size_string("from_socket");
  1391. size += armpack_size_i32();
  1392. size += armpack_size_string("to_id");
  1393. size += armpack_size_i32();
  1394. size += armpack_size_string("to_socket");
  1395. size += armpack_size_i32();
  1396. }
  1397. return size;
  1398. }
  1399. char *ui_node_canvas_to_json(ui_node_canvas_t *canvas) {
  1400. json_encode_begin();
  1401. json_encode_string("name", canvas->name);
  1402. json_encode_begin_array("nodes");
  1403. for (int i = 0; i < canvas->nodes->length; ++i) {
  1404. json_encode_begin_object();
  1405. json_encode_i32("id", canvas->nodes->buffer[i]->id);
  1406. json_encode_string("name", canvas->nodes->buffer[i]->name);
  1407. json_encode_string("type", canvas->nodes->buffer[i]->type);
  1408. json_encode_i32("x", canvas->nodes->buffer[i]->x);
  1409. json_encode_i32("y", canvas->nodes->buffer[i]->y);
  1410. json_encode_i32("color", canvas->nodes->buffer[i]->color);
  1411. json_encode_begin_array("inputs");
  1412. for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
  1413. json_encode_begin_object();
  1414. json_encode_i32("id", canvas->nodes->buffer[i]->inputs->buffer[j]->id);
  1415. json_encode_i32("node_id", canvas->nodes->buffer[i]->inputs->buffer[j]->node_id);
  1416. json_encode_string("name", canvas->nodes->buffer[i]->inputs->buffer[j]->name);
  1417. json_encode_string("type", canvas->nodes->buffer[i]->inputs->buffer[j]->type);
  1418. json_encode_i32("color", canvas->nodes->buffer[i]->inputs->buffer[j]->color);
  1419. json_encode_f32_array("default_value", canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
  1420. json_encode_f32("min", canvas->nodes->buffer[i]->inputs->buffer[j]->min);
  1421. json_encode_f32("max", canvas->nodes->buffer[i]->inputs->buffer[j]->max);
  1422. json_encode_f32("precision", canvas->nodes->buffer[i]->inputs->buffer[j]->precision);
  1423. json_encode_i32("display", canvas->nodes->buffer[i]->inputs->buffer[j]->display);
  1424. json_encode_end_object();
  1425. }
  1426. json_encode_end_array();
  1427. json_encode_begin_array("outputs");
  1428. for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
  1429. json_encode_begin_object();
  1430. json_encode_i32("id", canvas->nodes->buffer[i]->outputs->buffer[j]->id);
  1431. json_encode_i32("node_id", canvas->nodes->buffer[i]->outputs->buffer[j]->node_id);
  1432. json_encode_string("name", canvas->nodes->buffer[i]->outputs->buffer[j]->name);
  1433. json_encode_string("type", canvas->nodes->buffer[i]->outputs->buffer[j]->type);
  1434. json_encode_i32("color", canvas->nodes->buffer[i]->outputs->buffer[j]->color);
  1435. json_encode_f32_array("default_value", canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
  1436. json_encode_f32("min", canvas->nodes->buffer[i]->outputs->buffer[j]->min);
  1437. json_encode_f32("max", canvas->nodes->buffer[i]->outputs->buffer[j]->max);
  1438. json_encode_f32("precision", canvas->nodes->buffer[i]->outputs->buffer[j]->precision);
  1439. json_encode_i32("display", canvas->nodes->buffer[i]->outputs->buffer[j]->display);
  1440. json_encode_end_object();
  1441. }
  1442. json_encode_end_array();
  1443. json_encode_begin_array("buttons");
  1444. for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
  1445. json_encode_begin_object();
  1446. json_encode_string("name", canvas->nodes->buffer[i]->buttons->buffer[j]->name);
  1447. json_encode_string("type", canvas->nodes->buffer[i]->buttons->buffer[j]->type);
  1448. json_encode_i32("output", canvas->nodes->buffer[i]->buttons->buffer[j]->output);
  1449. json_encode_f32_array("default_value", canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
  1450. u8_array_t *data = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
  1451. if (data != NULL) {
  1452. json_encode_string("data", data->buffer);
  1453. }
  1454. else {
  1455. json_encode_null("data");
  1456. }
  1457. json_encode_f32("min", canvas->nodes->buffer[i]->buttons->buffer[j]->min);
  1458. json_encode_f32("max", canvas->nodes->buffer[i]->buttons->buffer[j]->max);
  1459. json_encode_f32("precision", canvas->nodes->buffer[i]->buttons->buffer[j]->precision);
  1460. json_encode_f32("height", canvas->nodes->buffer[i]->buttons->buffer[j]->height);
  1461. json_encode_end_object();
  1462. }
  1463. json_encode_end_array();
  1464. json_encode_f32("width", canvas->nodes->buffer[i]->width);
  1465. json_encode_end_object();
  1466. }
  1467. json_encode_end_array();
  1468. json_encode_begin_array("links");
  1469. for (int i = 0; i < canvas->links->length; ++i) {
  1470. json_encode_begin_object();
  1471. json_encode_i32("id", canvas->links->buffer[i]->id);
  1472. json_encode_i32("from_id", canvas->links->buffer[i]->from_id);
  1473. json_encode_i32("from_socket", canvas->links->buffer[i]->from_socket);
  1474. json_encode_i32("to_id", canvas->links->buffer[i]->to_id);
  1475. json_encode_i32("to_socket", canvas->links->buffer[i]->to_socket);
  1476. json_encode_end_object();
  1477. }
  1478. json_encode_end_array();
  1479. return json_encode_end();
  1480. }