| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606 |
- #include "iron_ui_nodes.h"
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <iron_gpu.h>
- #include <iron_system.h>
- #include "iron_draw.h"
- #include "iron_ui.h"
- #include "iron_armpack.h"
- #include "iron_array.h"
- #include "iron_json.h"
- #include "iron_gc.h"
- static ui_nodes_t *current_nodes = NULL;
- static bool ui_nodes_elements_baked = false;
- static gpu_texture_t ui_socket_image;
- static bool ui_box_select = false;
- static int ui_box_select_x = 0;
- static int ui_box_select_y = 0;
- static const int ui_max_buttons = 9;
- static void (*ui_on_header_released)(ui_node_t *) = NULL;
- static void (*ui_nodes_on_node_remove)(ui_node_t *) = NULL;
- static int ui_node_id = -1;
- char *ui_clipboard = "";
- char_ptr_array_t *ui_nodes_exclude_remove = NULL; // No removal for listed node types
- bool ui_nodes_socket_released = false;
- char_ptr_array_t *(*ui_nodes_enum_texts)(char *) = NULL; // Retrieve combo items for buttons of type ENUM
- void (*ui_nodes_on_custom_button)(int, char *) = NULL; // Call external function
- ui_canvas_control_t *(*ui_nodes_on_canvas_control)(void) = NULL;
- void (*ui_nodes_on_canvas_released)(void) = NULL;
- void (*ui_nodes_on_socket_released)(int) = NULL;
- void (*ui_nodes_on_link_drag)(int, bool) = NULL;
- bool ui_nodes_grid_snap = true;
- int ui_nodes_grid_snap_w = 40;
- int ui_popup_x = 0;
- int ui_popup_y = 0;
- int ui_popup_w = 0;
- int ui_popup_h = 0;
- void (*ui_popup_commands)(ui_t *, void *, void *) = NULL;
- void *ui_popup_data;
- void *ui_popup_data2;
- int ui_popup_handle_node_id = -1;
- int ui_popup_handle_node_socket_id = -1;
- char *tr(char *id, void *map); // translator.ts
- char *ui_tr(char *id) {
- return tr(id, NULL);
- }
- void ui_nodes_init(ui_nodes_t *nodes) {
- current_nodes = nodes;
- memset(current_nodes, 0, sizeof(ui_nodes_t));
- current_nodes->zoom = 1.0;
- current_nodes->scale_factor = 1.0;
- current_nodes->ELEMENT_H = 25.0;
- current_nodes->snap_from_id = -1;
- current_nodes->snap_to_id = -1;
- current_nodes->link_drag_id = -1;
- current_nodes->nodes_selected_id = gc_alloc(sizeof(i32_array_t));
- current_nodes->handle = ui_handle_create();
- }
- float UI_NODES_SCALE() {
- return current_nodes->scale_factor * current_nodes->zoom;
- }
- float UI_NODES_PAN_X() {
- float zoom_pan = (1.0 - current_nodes->zoom) * current_nodes->uiw / 2.5;
- return current_nodes->pan_x * UI_NODES_SCALE() + zoom_pan;
- }
- float UI_NODES_PAN_Y() {
- float zoom_pan = (1.0 - current_nodes->zoom) * current_nodes->uih / 2.5;
- return current_nodes->pan_y * UI_NODES_SCALE() + zoom_pan;
- }
- float UI_LINE_H() {
- return current_nodes->ELEMENT_H * UI_NODES_SCALE();
- }
- float UI_BUTTONS_H(ui_node_t *node) {
- float h = 0.0;
- for (int i = 0; i < node->buttons->length; ++i) {
- ui_node_button_t *but = node->buttons->buffer[i];
- if (strcmp(but->type, "RGBA") == 0) {
- h += 102.0 * UI_NODES_SCALE() + UI_LINE_H() * 5.0; // Color wheel + controls
- }
- else if (strcmp(but->type, "VECTOR") == 0) {
- h += UI_LINE_H() * 4.0;
- }
- else if (strcmp(but->type, "CUSTOM") == 0) {
- h += UI_LINE_H() * but->height;
- }
- else {
- h += UI_LINE_H();
- }
- }
- return h;
- }
- float UI_OUTPUTS_H(int sockets_count, int length) {
- float h = 0.0;
- for (int i = 0; i < (length < 0 ? sockets_count : length); ++i) {
- h += UI_LINE_H();
- }
- return h;
- }
- bool ui_input_linked(ui_node_canvas_t *canvas, int node_id, int i) {
- for (int x = 0; x < canvas->links->length; ++x) {
- ui_node_link_t *l = canvas->links->buffer[x];
- if (l->to_id == node_id && l->to_socket == i) {
- return true;
- }
- }
- return false;
- }
- float UI_INPUTS_H(ui_node_canvas_t *canvas, ui_node_socket_t **sockets, int sockets_count, int length) {
- float h = 0.0;
- for (int i = 0; i < (length < 0 ? sockets_count : length); ++i) {
- if (strcmp(sockets[i]->type, "VECTOR") == 0 && sockets[i]->display == 1 && !ui_input_linked(canvas, sockets[i]->node_id, i)) {
- h += UI_LINE_H() * 4;
- }
- else {
- h += UI_LINE_H();
- }
- }
- return h;
- }
- float UI_NODE_H(ui_node_canvas_t *canvas, ui_node_t *node) {
- 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);
- }
- float UI_NODE_W(ui_node_t *node) {
- return (node->width != 0 ? node->width : 140.0) * UI_NODES_SCALE();
- }
- float UI_NODE_X(ui_node_t *node) {
- return node->x * UI_NODES_SCALE() + UI_NODES_PAN_X();
- }
- float UI_NODE_Y(ui_node_t *node) {
- return node->y * UI_NODES_SCALE() + UI_NODES_PAN_Y();
- }
- float UI_INPUT_Y(ui_node_canvas_t *canvas, ui_node_socket_t **sockets, int sockets_count, int pos) {
- return UI_LINE_H() * 1.62 + UI_INPUTS_H(canvas, sockets, sockets_count, pos);
- }
- float UI_OUTPUT_Y(int sockets_count, int pos) {
- return UI_LINE_H() * 1.62 + UI_OUTPUTS_H(sockets_count, pos);
- }
- float ui_p(float f) {
- return f * UI_NODES_SCALE();
- }
- ui_node_t *ui_get_node(ui_node_array_t *nodes, int id) {
- for (int i = 0; i < nodes->length; ++i) {
- if (nodes->buffer[i]->id == id) {
- return nodes->buffer[i];
- }
- }
- return NULL;
- }
- int ui_get_node_index(ui_node_t **nodes, int nodes_count, int id) {
- for (int i = 0; i < nodes_count; ++i) {
- if (nodes[i]->id == id) {
- return i;
- }
- }
- return -1;
- }
- int ui_next_node_id(ui_node_array_t *nodes) {
- if (ui_node_id == -1) {
- for (int i = 0; i < nodes->length; ++i) {
- if (ui_node_id < nodes->buffer[i]->id) {
- ui_node_id = nodes->buffer[i]->id;
- }
- }
- }
- return ++ui_node_id;
- }
- ui_node_link_t *ui_get_link(ui_node_link_array_t *links, int id) {
- for (int i = 0; i < links->length; ++i) {
- if (links->buffer[i]->id == id) {
- return links->buffer[i];
- }
- }
- return NULL;
- }
- int ui_get_link_index(ui_node_link_array_t *links, int id) {
- for (int i = 0; i < links->length; ++i) {
- if (links->buffer[i]->id == id) {
- return i;
- }
- }
- return -1;
- }
- int ui_next_link_id(ui_node_link_array_t *links) {
- int id = 0;
- for (int i = 0; i < links->length; ++i) {
- if (links->buffer[i]->id >= id) {
- id = links->buffer[i]->id + 1;
- }
- }
- return id;
- }
- int ui_get_socket_id(ui_node_array_t *nodes) {
- int id = 0;
- for (int i = 0; i < nodes->length; ++i) {
- ui_node_t *n = nodes->buffer[i];
- for (int j = 0; j < n->inputs->length; ++j) {
- if (n->inputs->buffer[j]->id >= id) {
- id = n->inputs->buffer[j]->id + 1;
- }
- }
- for (int j = 0; j < n->outputs->length; ++j) {
- if (n->outputs->buffer[j]->id >= id) {
- id = n->outputs->buffer[j]->id + 1;
- }
- }
- }
- return id;
- }
- void ui_nodes_bake_elements() {
- if (ui_socket_image.width != 0) {
- gpu_texture_destroy(&ui_socket_image);
- }
- gpu_render_target_init(&ui_socket_image, 24, 24, GPU_TEXTURE_FORMAT_RGBA32);
- draw_begin(&ui_socket_image, true, 0x00000000);
- draw_set_color(0xff111111);
- draw_filled_circle(12, 12, 11, 0);
- draw_set_color(0xffffffff);
- draw_filled_circle(12, 12, 9, 0);
- draw_end();
- ui_nodes_elements_baked = true;
- }
- ui_canvas_control_t *ui_on_default_canvas_control() {
- ui_t *current = ui_get_current();
- static ui_canvas_control_t c;
- c.pan_x = current->input_down_r ? current->input_dx : 0.0;
- c.pan_y = current->input_down_r ? current->input_dy : 0.0;
- c.zoom = -current->input_wheel_delta / 10.0;
- return &c;
- }
- void ui_draw_link(float x1, float y1, float x2, float y2, bool highlight) {
- ui_t *current = ui_get_current();
- int c1 = current->ops->theme->LABEL_COL;
- int c2 = current->ops->theme->ACCENT_COL;
- int c = highlight ? c1 : c2;
- draw_set_color(ui_color(ui_color_r(c), ui_color_g(c), ui_color_b(c), 210));
- if (current->ops->theme->LINK_STYLE == UI_LINK_STYLE_LINE) {
- draw_line_aa(x1, y1, x2, y2, 1.0);
- }
- else if (current->ops->theme->LINK_STYLE == UI_LINK_STYLE_CUBIC_BEZIER) {
- f32_array_t xa;
- f32_array_t ya;
- float x[] = { x1, x1 + fabs(x1 - x2) / 2.0, x2 - fabs(x1 - x2) / 2.0, x2 };
- float y[] = { y1, y1, y2, y2 };
- xa.buffer = x;
- ya.buffer = y;
- draw_cubic_bezier(&xa, &ya, 30, highlight ? 2.0 : 1.0);
- }
- }
- static void ui_remove_link_at(ui_node_canvas_t *canvas, int at) {
- canvas->links->buffer[at] = NULL;
- for (int i = at; i < canvas->links->length - 1; ++i) {
- canvas->links->buffer[i] = canvas->links->buffer[i + 1];
- }
- canvas->links->length--;
- }
- static void ui_remove_node_at(ui_node_canvas_t *canvas, int at) {
- canvas->nodes->buffer[at] = NULL;
- for (int i = at; i < canvas->nodes->length - 1; ++i) {
- canvas->nodes->buffer[i] = canvas->nodes->buffer[i + 1];
- }
- canvas->nodes->length--;
- }
- void ui_remove_node(ui_node_t *n, ui_node_canvas_t *canvas) {
- if (n == NULL) return;
- int i = 0;
- while (i < canvas->links->length) {
- ui_node_link_t *l = canvas->links->buffer[i];
- if (l->from_id == n->id || l->to_id == n->id) {
- ui_remove_link_at(canvas, i);
- }
- else {
- i++;
- }
- }
- ui_remove_node_at(canvas, ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, n->id));
- if (ui_nodes_on_node_remove != NULL) {
- (*ui_nodes_on_node_remove)(n);
- }
- }
- bool ui_is_selected(ui_node_t *node) {
- for (int i = 0; i < current_nodes->nodes_selected_id->length; ++i) {
- if (current_nodes->nodes_selected_id->buffer[i] == node->id) {
- return true;
- }
- }
- return false;
- }
- static void remove_from_selection(ui_node_t *node) {
- i32_array_remove(current_nodes->nodes_selected_id, node->id);
- }
- static void add_to_selection(ui_node_t *node) {
- i32_array_push(current_nodes->nodes_selected_id, node->id);
- }
- void ui_popup(int x, int y, int w, int h, void (*commands)(ui_t *, void *, void *), void *data, void *data2) {
- ui_popup_x = x;
- ui_popup_y = y;
- ui_popup_w = w;
- ui_popup_h = h;
- ui_popup_commands = commands;
- ui_popup_data = data;
- ui_popup_data2 = data2;
- }
- static void color_picker_callback(uint32_t color) {
- ui_t *current = ui_get_current();
- float *val = (float *)current_nodes->color_picker_callback_data;
- val[0] = ui_color_r(color) / 255.0f;
- val[1] = ui_color_g(color) / 255.0f;
- val[2] = ui_color_b(color) / 255.0f;
- current->changed = true;
- }
- void ui_color_wheel_picker(void *data) {
- current_nodes->color_picker_callback_data = data;
- current_nodes->color_picker_callback = &color_picker_callback;
- }
- static void rgba_popup_commands(ui_t *ui, void *data, void *data2) {
- ui_handle_t *nhandle = (ui_handle_t *)data;
- float *val = (float *)data2;
- nhandle->color = ui_color(val[0] * 255.0, val[1] * 255.0, val[2] * 255.0, 255.0);
- ui_color_wheel(nhandle, false, -1, -1, true, &ui_color_wheel_picker, val);
- val[0] = ui_color_r(nhandle->color) / 255.0f;
- val[1] = ui_color_g(nhandle->color) / 255.0f;
- val[2] = ui_color_b(nhandle->color) / 255.0f;
- }
- void ui_nodes_rgba_popup(ui_handle_t *nhandle, float *val, int x, int y) {
- ui_t *current = ui_get_current();
- ui_popup(x, y, 140.0 * current_nodes->scale_factor, current->ops->theme->ELEMENT_H * 10.0, &rgba_popup_commands, nhandle, val);
- }
- static float ui_nodes_snap(float f) {
- float w = ui_nodes_grid_snap_w * UI_NODES_SCALE();
- f = f * UI_NODES_SCALE();
- return roundf(f / w) * w;
- }
- static char_ptr_array_t enum_ar;
- static char enum_label[64];
- static char enum_texts_data[64][64];
- static char *enum_texts[64];
- static char_ptr_array_t temp_ar;
- static char temp_label[64];
- static char temp_texts_data[64][64];
- static char *temp_texts[64];
- void ui_draw_node(ui_node_t *node, ui_node_canvas_t *canvas) {
- ui_t *current = ui_get_current();
- float wx = current->_window_x;
- float wy = current->_window_y;
- float ui_x = current->_x;
- float ui_y = current->_y;
- float ui_w = current->_w;
- float w = UI_NODE_W(node);
- float h = UI_NODE_H(canvas, node);
- float nx = UI_NODE_X(node);
- float ny = UI_NODE_Y(node);
- char *text = ui_tr(node->name);
- float lineh = UI_LINE_H();
- // Disallow input if node is overlapped by another node
- current_nodes->_input_started = current->input_started;
- if (current->input_started) {
- for (int i = ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, node->id) + 1; i < canvas->nodes->length; ++i) {
- ui_node_t *n = canvas->nodes->buffer[i];
- 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 &&
- 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) {
- current->input_started = false;
- break;
- }
- }
- }
- // Grid snap preview
- if (ui_nodes_grid_snap && ui_is_selected(node) && current_nodes->nodes_drag) {
- draw_set_color(current->ops->theme->BUTTON_COL);
- ui_draw_rect(false,
- ui_nodes_snap(node->x) + UI_NODES_PAN_X(),
- ui_nodes_snap(node->y) + UI_NODES_PAN_Y(),
- w + 2,
- h + 2
- );
- // nx = ui_nodes_snap(node->x) + UI_NODES_PAN_X();
- // ny = ui_nodes_snap(node->y) + UI_NODES_PAN_Y();
- }
- // Shadow
- ui_draw_shadow(nx, ny, w, h);
- // Outline
- draw_set_color(ui_is_selected(node) ? current->ops->theme->LABEL_COL : current->ops->theme->PRESSED_COL);
- ui_draw_rect(true, nx - 1, ny - 1, w + 2, h + 2);
- // Body
- draw_set_color(current->ops->theme->WINDOW_BG_COL);
- ui_draw_rect(true, nx, ny, w, h);
- // Header line
- draw_set_color(node->color);
- draw_filled_rect(nx, ny + lineh - ui_p(3), w, ui_p(3));
- // Title
- draw_set_color(current->ops->theme->TEXT_COL);
- float textw = draw_string_width(current->ops->font, current->font_size, text);
- draw_string(text, nx + ui_p(10), ny + ui_p(6));
- ny += lineh * 0.5;
- // Outputs
- for (int i = 0; i < node->outputs->length; ++i) {
- ui_node_socket_t *out = node->outputs->buffer[i];
- ny += lineh;
- draw_set_color(out->color);
- draw_scaled_image(&ui_socket_image, nx + w - ui_p(6), ny - ui_p(3), ui_p(12), ui_p(12));
- }
- ny -= lineh * node->outputs->length;
- draw_set_color(current->ops->theme->LABEL_COL);
- for (int i = 0; i < node->outputs->length; ++i) {
- ui_node_socket_t *out = node->outputs->buffer[i];
- ny += lineh;
- float strw = draw_string_width(current->ops->font, current->font_size, ui_tr(out->name));
- draw_string(ui_tr(out->name), nx + w - strw - ui_p(12), ny - ui_p(3));
- if (ui_nodes_on_socket_released != NULL && current->input_enabled && (current->input_released || current->input_released_r)) {
- if (current->input_x > wx + nx && current->input_x < wx + nx + w && current->input_y > wy + ny && current->input_y < wy + ny + lineh) {
- ui_nodes_on_socket_released(out->id);
- ui_nodes_socket_released = true;
- }
- }
- }
- // Buttons
- ui_handle_t *nhandle = ui_nest(current_nodes->handle, node->id);
- ny -= lineh / 3.0; // Fix align
- for (int buti = 0; buti < node->buttons->length; ++buti) {
- ui_node_button_t *but = node->buttons->buffer[buti];
- if (strcmp(but->type, "RGBA") == 0) {
- ny += lineh; // 18 + 2 separator
- current->_x = nx + 1; // Offset for node selection border
- current->_y = ny;
- current->_w = w;
- float *val = node->outputs->buffer[but->output]->default_value->buffer;
- nhandle->color = ui_color(val[0] * 255.0, val[1] * 255.0, val[2] * 255.0, 255.0);
- ui_color_wheel(nhandle, false, -1, -1, true, &ui_color_wheel_picker, val);
- val[0] = ui_color_r(nhandle->color) / 255.0f;
- val[1] = ui_color_g(nhandle->color) / 255.0f;
- val[2] = ui_color_b(nhandle->color) / 255.0f;
- }
- else if (strcmp(but->type, "VECTOR") == 0) {
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- float min = but->min;
- float max = but->max;
- float text_off = current->ops->theme->TEXT_OFFSET;
- current->ops->theme->TEXT_OFFSET = 6;
- ui_text(ui_tr(but->name), UI_ALIGN_LEFT, 0);
- float *val = (float *)but->default_value->buffer;
- ui_handle_t *h = ui_nest(nhandle, buti);
- ui_handle_t *h0 = ui_nest(h, 0);
- if (h0->init) {
- h0->value = val[0];
- }
- ui_handle_t *h1 = ui_nest(h, 1);
- if (h1->init) {
- h1->value = val[1];
- }
- ui_handle_t *h2 = ui_nest(h, 2);
- if (h2->init) {
- h2->value = val[2];
- }
- val[0] = ui_slider(h0, "X", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- val[1] = ui_slider(h1, "Y", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- val[2] = ui_slider(h2, "Z", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- current->ops->theme->TEXT_OFFSET = text_off;
- if (but->output >= 0) {
- node->outputs->buffer[but->output]->default_value->buffer = but->default_value->buffer;
- }
- ny += lineh * 3.0;
- }
- else if (strcmp(but->type, "VALUE") == 0) {
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- ui_node_socket_t *soc = node->outputs->buffer[but->output];
- float min = but->min;
- float max = but->max;
- float prec = but->precision;
- float text_off = current->ops->theme->TEXT_OFFSET;
- current->ops->theme->TEXT_OFFSET = 6;
- ui_handle_t *soc_handle = ui_nest(nhandle, buti);
- if (soc_handle->init) {
- soc_handle->value = ((float *)soc->default_value->buffer)[0];
- }
- ((float *)soc->default_value->buffer)[0] = ui_slider(soc_handle, "Value", min, max, true, prec, true, UI_ALIGN_LEFT, true);
- current->ops->theme->TEXT_OFFSET = text_off;
- }
- else if (strcmp(but->type, "STRING") == 0) {
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- ui_node_socket_t *soc = but->output >= 0 ? node->outputs->buffer[but->output] : NULL;
- ui_handle_t *h = ui_nest(nhandle, buti);
- if (h->init) {
- h->text = soc != NULL ? soc->default_value->buffer : but->default_value->buffer != NULL ? but->default_value->buffer : "";
- }
- but->default_value->buffer = ui_text_input(h, ui_tr(but->name), UI_ALIGN_LEFT, true, false);
- but->default_value->length = strlen(but->default_value->buffer) + 1;
- if (soc != NULL) {
- soc->default_value->buffer = but->default_value->buffer;
- }
- }
- else if (strcmp(but->type, "ENUM") == 0) {
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- ui_handle_t *but_handle = ui_nest(nhandle, buti);
- but_handle->position = ((float *)but->default_value->buffer)[0];
- bool combo_select = current->combo_selected_handle == NULL && ui_get_released(UI_ELEMENT_H());
- char *label = combo_select ? temp_label : enum_label;
- char (*texts_data)[64] = combo_select ? temp_texts_data : enum_texts_data;
- char **texts = combo_select ? temp_texts : enum_texts;
- char_ptr_array_t *ar = combo_select ? &temp_ar : &enum_ar;
- int texts_count = 0;
- if (but->data != NULL && but->data->length > 1) {
- int wi = 0;
- for (int i = 0; i < but->data->length; ++i) {
- char c = ((char *)but->data->buffer)[i];
- if (c == '\0') {
- texts_data[texts_count][wi] = '\0';
- texts_count++;
- break;
- }
- if (c == '\n') {
- texts_data[texts_count][wi] = '\0';
- texts_count++;
- wi = 0;
- continue;
- }
- texts_data[texts_count][wi] = c;
- wi++;
- }
- for (int i = 0; i < texts_count; ++i) {
- strcpy(texts_data[i], ui_tr(texts_data[i]));
- texts[i] = texts_data[i];
- }
- ar->buffer = texts;
- ar->length = texts_count;
- }
- else {
- gc_unroot(ar);
- ar = (*ui_nodes_enum_texts)(node->type);
- gc_root(ar);
- }
- strcpy(label, ui_tr(but->name));
- ((float *)but->default_value->buffer)[0] = ui_combo(but_handle, ar, label, false, UI_ALIGN_LEFT, true);
- }
- else if (strcmp(but->type, "BOOL") == 0) {
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- ui_handle_t *h = ui_nest(nhandle, buti);
- if (h->init) {
- h->selected = ((float *)but->default_value->buffer)[0];
- }
- ((float *)but->default_value->buffer)[0] = ui_check(h, ui_tr(but->name), "");
- }
- else if (strcmp(but->type, "CUSTOM") == 0) { // Calls external function for custom button drawing
- ny += lineh;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- ui_nodes_on_custom_button(node->id, but->name);
- ny += lineh * (but->height - 1); // but->height specifies vertical button size
- }
- }
- ny += lineh / 3.0; // Fix align
- // Inputs
- for (int i = 0; i < node->inputs->length; ++i) {
- ui_node_socket_t *inp = node->inputs->buffer[i];
- ny += lineh;
- draw_set_color(inp->color);
- draw_scaled_image(&ui_socket_image, nx - ui_p(6), ny - ui_p(3), ui_p(12), ui_p(12));
- bool is_linked = ui_input_linked(canvas, node->id, i);
- if (!is_linked && strcmp(inp->type, "VALUE") == 0) {
- current->_x = nx + ui_p(6);
- current->_y = ny - ui_p(current_nodes->ELEMENT_H / 3.0);
- current->_w = w - ui_p(6);
- ui_node_socket_t *soc = inp;
- float min = soc->min;
- float max = soc->max;
- float prec = soc->precision;
- float text_off = current->ops->theme->TEXT_OFFSET;
- current->ops->theme->TEXT_OFFSET = 6;
- ui_handle_t *_handle = ui_nest(nhandle, ui_max_buttons);
- ui_handle_t *soc_handle = ui_nest(_handle, i);
- if (soc_handle->init) {
- soc_handle->value = ((float *)soc->default_value->buffer)[0];
- }
- ((float *)soc->default_value->buffer)[0] = ui_slider(soc_handle, ui_tr(inp->name), min, max, true, prec, true, UI_ALIGN_LEFT, true);
- current->ops->theme->TEXT_OFFSET = text_off;
- }
- else if (!is_linked && strcmp(inp->type, "STRING") == 0) {
- current->_x = nx + ui_p(6);
- current->_y = ny - ui_p(9);
- current->_w = w - ui_p(6);
- ui_node_socket_t *soc = inp;
- float text_off = current->ops->theme->TEXT_OFFSET;
- current->ops->theme->TEXT_OFFSET = 6;
- ui_handle_t *_handle = ui_nest(nhandle, ui_max_buttons);
- ui_handle_t *h = ui_nest(_handle, i);
- if (h->init) {
- strcpy(h->text, soc->default_value->buffer);
- }
- soc->default_value->buffer = ui_text_input(h, ui_tr(inp->name), UI_ALIGN_LEFT, true, false);
- current->ops->theme->TEXT_OFFSET = text_off;
- }
- else if (!is_linked && strcmp(inp->type, "RGBA") == 0) {
- draw_set_color(current->ops->theme->LABEL_COL);
- draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
- ui_node_socket_t *soc = inp;
- draw_set_color(0xff000000);
- draw_filled_rect(nx + w - ui_p(38), ny - ui_p(6), ui_p(36), ui_p(18));
- float *val = (float *)soc->default_value->buffer;
- draw_set_color(ui_color(val[0] * 255, val[1] * 255, val[2] * 255, 255));
- float rx = nx + w - ui_p(37);
- float ry = ny - ui_p(5);
- float rw = ui_p(34);
- float rh = ui_p(16);
- draw_filled_rect(rx, ry, rw, rh);
- float ix = current->input_x - wx;
- float iy = current->input_y - wy;
- if (current->input_started && ix > rx && iy > ry && ix < rx + rw && iy < ry + rh) {
- current_nodes->_input_started = current->input_started = false;
- ui_nodes_rgba_popup(nhandle, soc->default_value->buffer, (int)(rx), (int)(ry + UI_ELEMENT_H()));
- ui_popup_handle_node_id = node->id;
- ui_popup_handle_node_socket_id = soc->id;
- }
- if (ui_popup_commands != NULL && ui_popup_handle_node_id == node->id && ui_popup_handle_node_socket_id == soc->id) {
- // armpack data may have been moved in memory
- ui_popup_data = nhandle;
- ui_popup_data2 = soc->default_value->buffer;
- }
- }
- else if (!is_linked && strcmp(inp->type, "VECTOR") == 0 && inp->display == 1) {
- draw_set_color(current->ops->theme->LABEL_COL);
- draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
- ny += lineh / 2;
- current->_x = nx;
- current->_y = ny;
- current->_w = w;
- float min = inp->min;
- float max = inp->max;
- float text_off = current->ops->theme->TEXT_OFFSET;
- current->ops->theme->TEXT_OFFSET = 6;
- float *val = (float *)inp->default_value->buffer;
- ui_handle_t *h = ui_nest(nhandle, ui_max_buttons);
- ui_handle_t *hi = ui_nest(h, i);
- ui_handle_t *h0 = ui_nest(hi, 0);
- if (h0->init) {
- h0->value = val[0];
- }
- ui_handle_t *h1 = ui_nest(hi, 1);
- if (h1->init) {
- h1->value = val[1];
- }
- ui_handle_t *h2 = ui_nest(hi, 2);
- if (h2->init) {
- h2->value = val[2];
- }
- val[0] = ui_slider(h0, "X", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- val[1] = ui_slider(h1, "Y", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- val[2] = ui_slider(h2, "Z", min, max, true, 100, true, UI_ALIGN_LEFT, true);
- current->ops->theme->TEXT_OFFSET = text_off;
- ny += lineh * 2.5;
- }
- else {
- draw_set_color(current->ops->theme->LABEL_COL);
- draw_string(ui_tr(inp->name), nx + ui_p(12), ny - ui_p(3));
- }
- if (ui_nodes_on_socket_released != NULL && current->input_enabled && (current->input_released || current->input_released_r)) {
- if (current->input_x > wx + nx && current->input_x < wx + nx + w && current->input_y > wy + ny && current->input_y < wy + ny + lineh) {
- ui_nodes_on_socket_released(inp->id);
- ui_nodes_socket_released = true;
- }
- }
- }
- current->_x = ui_x;
- current->_y = ui_y;
- current->_w = ui_w;
- current->input_started = current_nodes->_input_started;
- }
- bool ui_is_node_type_excluded(char *type) {
- for (int i = 0; i < ui_nodes_exclude_remove->length; ++i) {
- if (strcmp(ui_nodes_exclude_remove->buffer[i], type) == 0) {
- return true;
- }
- }
- return false;
- }
- void ui_node_canvas(ui_nodes_t *nodes, ui_node_canvas_t *canvas) {
- current_nodes = nodes;
- ui_t *current = ui_get_current();
- if (!ui_nodes_elements_baked) {
- gpu_texture_t *current = _draw_current;
- draw_end();
- ui_nodes_bake_elements();
- draw_begin(current, false, 0);
- }
- if (ui_nodes_exclude_remove == NULL) {
- ui_nodes_exclude_remove = gc_alloc(sizeof(char_ptr_array_t));
- gc_root(ui_nodes_exclude_remove);
- }
- float wx = current->_window_x;
- float wy = current->_window_y;
- bool _input_enabled = current->input_enabled;
- current->input_enabled = _input_enabled && ui_popup_commands == NULL;
- ui_canvas_control_t *controls = ui_nodes_on_canvas_control != NULL ? ui_nodes_on_canvas_control() : ui_on_default_canvas_control();
- ui_nodes_socket_released = false;
- // Pan canvas
- if (current->input_enabled && (controls->pan_x != 0.0 || controls->pan_y != 0.0)) {
- current_nodes->pan_x += controls->pan_x / UI_NODES_SCALE();
- current_nodes->pan_y += controls->pan_y / UI_NODES_SCALE();
- }
- // Zoom canvas
- if (current->input_enabled && controls->zoom != 0.0) {
- current_nodes->zoom += controls->zoom;
- if (current_nodes->zoom < 0.1) {
- current_nodes->zoom = 0.1;
- }
- else if (current_nodes->zoom > 1.0) {
- current_nodes->zoom = 1.0;
- }
- current_nodes->zoom = round(current_nodes->zoom * 100.0) / 100.0;
- current_nodes->uiw = current->_w;
- current_nodes->uih = current->_h;
- if (ui_touch_scroll) {
- // Zoom to finger location
- current_nodes->pan_x -= (current->input_x - current->_window_x - current->_window_w / 2.0) * controls->zoom * 5.0 * (1.0 - current_nodes->zoom);
- current_nodes->pan_y -= (current->input_y - current->_window_y - current->_window_h / 2.0) * controls->zoom * 5.0 * (1.0 - current_nodes->zoom);
- }
- }
- current_nodes->scale_factor = UI_SCALE();
- current_nodes->ELEMENT_H = current->ops->theme->ELEMENT_H + 2;
- ui_set_scale(UI_NODES_SCALE()); // Apply zoomed scale
- current->elements_baked = true;
- draw_set_font(current->ops->font, current->font_size);
- for (int i = 0; i < canvas->links->length; ++i) {
- ui_node_link_t *link = canvas->links->buffer[i];
- ui_node_t *from = ui_get_node(canvas->nodes, link->from_id);
- ui_node_t *to = ui_get_node(canvas->nodes, link->to_id);
- float from_x = from == NULL ? current->input_x : wx + UI_NODE_X(from) + UI_NODE_W(from);
- float from_y = from == NULL ? current->input_y : wy + UI_NODE_Y(from) + UI_OUTPUT_Y(from->outputs->length, link->from_socket);
- float to_x = to == NULL ? current->input_x : wx + UI_NODE_X(to);
- 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);
- // Cull
- float left = to_x > from_x ? from_x : to_x;
- float right = to_x > from_x ? to_x : from_x;
- float top = to_y > from_y ? from_y : to_y;
- float bottom = to_y > from_y ? to_y : from_y;
- if (right < 0 || left > wx + current->_window_w ||
- bottom < 0 || top > wy + current->_window_h) {
- continue;
- }
- // Snap to nearest socket
- if (current_nodes->link_drag_id == link->id) {
- if (current_nodes->snap_from_id != -1) {
- from_x = current_nodes->snap_x;
- from_y = current_nodes->snap_y;
- }
- if (current_nodes->snap_to_id != -1) {
- to_x = current_nodes->snap_x;
- to_y = current_nodes->snap_y;
- }
- current_nodes->snap_from_id = current_nodes->snap_to_id = -1;
- for (int j = 0; j < canvas->nodes->length; ++j) {
- ui_node_t *node = canvas->nodes->buffer[j];
- ui_node_socket_t **inps = node->inputs->buffer;
- ui_node_socket_t **outs = node->outputs->buffer;
- float node_h = UI_NODE_H(canvas, node);
- float rx = wx + UI_NODE_X(node) - UI_LINE_H() / 2;
- float ry = wy + UI_NODE_Y(node) - UI_LINE_H() / 2;
- float rw = UI_NODE_W(node) + UI_LINE_H();
- float rh = node_h + UI_LINE_H();
- if (ui_input_in_rect(rx, ry, rw, rh)) {
- if (from == NULL && node->id != to->id) { // Snap to output
- for (int k = 0; k < node->outputs->length; ++k) {
- float sx = wx + UI_NODE_X(node) + UI_NODE_W(node);
- float sy = wy + UI_NODE_Y(node) + UI_OUTPUT_Y(node->outputs->length, k);
- float rx = sx - UI_LINE_H() / 2;
- float ry = sy - UI_LINE_H() / 2;
- if (ui_input_in_rect(rx, ry, UI_LINE_H(), UI_LINE_H())) {
- current_nodes->snap_x = sx;
- current_nodes->snap_y = sy;
- current_nodes->snap_from_id = node->id;
- current_nodes->snap_socket = k;
- break;
- }
- }
- }
- else if (to == NULL && node->id != from->id) { // Snap to input
- for (int k = 0; k < node->inputs->length; ++k) {
- float sx = wx + UI_NODE_X(node);
- 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);
- float rx = sx - UI_LINE_H() / 2.0;
- float ry = sy - UI_LINE_H() / 2.0;
- if (ui_input_in_rect(rx, ry, UI_LINE_H(), UI_LINE_H())) {
- current_nodes->snap_x = sx;
- current_nodes->snap_y = sy;
- current_nodes->snap_to_id = node->id;
- current_nodes->snap_socket = k;
- break;
- }
- }
- }
- }
- }
- }
- bool selected = false;
- for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
- int n_id = current_nodes->nodes_selected_id->buffer[j];
- if (link->from_id == n_id || link->to_id == n_id) {
- selected = true;
- break;
- }
- }
- ui_draw_link(from_x - wx, from_y - wy, to_x - wx, to_y - wy, selected);
- }
- for (int i = 0; i < canvas->nodes->length; ++i) {
- ui_node_t *node = canvas->nodes->buffer[i];
- // Cull
- if (UI_NODE_X(node) > current->_window_w || UI_NODE_X(node) + UI_NODE_W(node) < 0 ||
- UI_NODE_Y(node) > current->_window_h || UI_NODE_Y(node) + UI_NODE_H(canvas, node) < 0) {
- if (!ui_is_selected(node)) {
- continue;
- }
- }
- ui_node_socket_t **inps = node->inputs->buffer;
- ui_node_socket_t **outs = node->outputs->buffer;
- // Drag node
- float node_h = UI_NODE_H(canvas, node);
- 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())) {
- if (current->input_started) {
- if (current->is_shift_down || current->is_ctrl_down) {
- // Add to selection or deselect
- if (ui_is_selected(node)) {
- remove_from_selection(node);
- }
- else {
- add_to_selection(node);
- }
- }
- else if (current_nodes->nodes_selected_id->length <= 1) {
- // Selecting single node, otherwise wait for input release
- current_nodes->nodes_selected_id->length = 0;
- i32_array_push(current_nodes->nodes_selected_id, node->id);
- }
- current_nodes->move_on_top = node; // Place selected node on top
- current_nodes->nodes_drag = true;
- current_nodes->dragged = false;
- }
- else if (current->input_released && !current->is_shift_down && !current->is_ctrl_down && !current_nodes->dragged) {
- // No drag performed, select single node
- current_nodes->nodes_selected_id->length = 0;
- i32_array_push(current_nodes->nodes_selected_id, node->id);
- if (ui_on_header_released != NULL) {
- ui_on_header_released(node);
- }
- }
- }
- 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())) {
- // Check sockets
- if (current_nodes->link_drag_id == -1) {
- for (int j = 0; j < node->outputs->length; ++j) {
- float sx = wx + UI_NODE_X(node) + UI_NODE_W(node);
- float sy = wy + UI_NODE_Y(node) + UI_OUTPUT_Y(node->outputs->length, j);
- if (ui_input_in_rect(sx - UI_LINE_H() / 2.0, sy - UI_LINE_H() / 2.0, UI_LINE_H(), UI_LINE_H())) {
- // New link from output
- ui_node_link_t *l = (ui_node_link_t *)malloc(sizeof(ui_node_link_t)); // TODO: store at canvas->links without malloc
- l->id = ui_next_link_id(canvas->links);
- l->from_id = node->id;
- l->from_socket = j;
- l->to_id = -1;
- l->to_socket = -1;
- any_array_push(canvas->links, l);
- current_nodes->link_drag_id = l->id;
- current_nodes->is_new_link = true;
- break;
- }
- }
- }
- if (current_nodes->link_drag_id == -1) {
- for (int j = 0; j < node->inputs->length; ++j) {
- float sx = wx + UI_NODE_X(node);
- 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);
- if (ui_input_in_rect(sx - UI_LINE_H() / 2.0, sy - UI_LINE_H() / 2.0, UI_LINE_H(), UI_LINE_H())) {
- // Already has a link - disconnect
- for (int k = 0; k < canvas->links->length; ++k) {
- ui_node_link_t *l = canvas->links->buffer[k];
- if (l->to_id == node->id && l->to_socket == j) {
- l->to_id = l->to_socket = -1;
- current_nodes->link_drag_id = l->id;
- current_nodes->is_new_link = false;
- break;
- }
- }
- if (current_nodes->link_drag_id != -1) {
- break;
- }
- // New link from input
- ui_node_link_t *l = (ui_node_link_t *)malloc(sizeof(ui_node_link_t));
- l->id = ui_next_link_id(canvas->links);
- l->from_id = -1;
- l->from_socket = -1;
- l->to_id = node->id;
- l->to_socket = j;
- any_array_push(canvas->links, l);
- current_nodes->link_drag_id = l->id;
- current_nodes->is_new_link = true;
- break;
- }
- }
- }
- }
- else if (current->input_released) {
- if (current_nodes->snap_to_id != -1) { // Connect to input
- // Force single link per input
- for (int j = 0; j < canvas->links->length; ++j) {
- ui_node_link_t *l = canvas->links->buffer[j];
- if (l->to_id == current_nodes->snap_to_id && l->to_socket == current_nodes->snap_socket) {
- ui_remove_link_at(canvas, ui_get_link_index(canvas->links, l->id));
- break;
- }
- }
- ui_node_link_t *link_drag = ui_get_link(canvas->links, current_nodes->link_drag_id);
- link_drag->to_id = current_nodes->snap_to_id;
- link_drag->to_socket = current_nodes->snap_socket;
- current->changed = true;
- }
- else if (current_nodes->snap_from_id != -1) { // Connect to output
- ui_node_link_t *link_drag = ui_get_link(canvas->links, current_nodes->link_drag_id);
- link_drag->from_id = current_nodes->snap_from_id;
- link_drag->from_socket = current_nodes->snap_socket;
- current->changed = true;
- }
- else if (current_nodes->link_drag_id != -1) { // Remove dragged link
- current->changed = true;
- if (ui_nodes_on_link_drag != NULL) {
- ui_nodes_on_link_drag(current_nodes->link_drag_id, current_nodes->is_new_link);
- }
- ui_remove_link_at(canvas, ui_get_link_index(canvas->links, current_nodes->link_drag_id));
- }
- current_nodes->snap_to_id = current_nodes->snap_from_id = -1;
- current_nodes->link_drag_id = -1;
- current_nodes->nodes_drag = false;
- }
- if (current_nodes->nodes_drag && ui_is_selected(node) && !current->input_down_r) {
- if (current->input_dx != 0 || current->input_dy != 0) {
- current_nodes->dragged = true;
- node->x += current->input_dx / UI_NODES_SCALE();
- node->y += current->input_dy / UI_NODES_SCALE();
- // Absolute
- // node->x = (current->input_x - current->_window_x - UI_NODES_PAN_X()) / UI_NODES_SCALE();
- // node->y = (current->input_y - current->_window_y - UI_NODES_PAN_Y()) / UI_NODES_SCALE();
- }
- }
- if (ui_nodes_grid_snap && current->input_released && ui_is_selected(node)) {
- node->x = ui_nodes_snap(node->x) / UI_NODES_SCALE();
- node->y = ui_nodes_snap(node->y) / UI_NODES_SCALE();
- }
- ui_draw_node(node, canvas);
- }
- if (ui_nodes_on_canvas_released != NULL && current->input_enabled && (current->input_released || current->input_released_r) && !ui_nodes_socket_released) {
- ui_nodes_on_canvas_released();
- }
- if (ui_box_select) {
- draw_set_color(0x223333dd);
- 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);
- draw_set_color(0x773333dd);
- 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);
- draw_set_color(0xffffffff);
- }
- if (current->input_enabled && current->input_started && !current->is_alt_down &&
- current_nodes->link_drag_id == -1 && !current_nodes->nodes_drag && !current->changed &&
- ui_input_in_rect(current->_window_x, current->_window_y, current->_window_w, current->_window_h)) {
- ui_box_select = true;
- ui_box_select_x = current->input_x - current->_window_x;
- ui_box_select_y = current->input_y - current->_window_y;
- }
- else if (ui_box_select && !current->input_down) {
- ui_box_select = false;
- int left = ui_box_select_x;
- int top = ui_box_select_y;
- int right = current->input_x - current->_window_x;
- int bottom = current->input_y - current->_window_y;
- if (left > right) {
- int t = left;
- left = right;
- right = t;
- }
- if (top > bottom) {
- int t = top;
- top = bottom;
- bottom = t;
- }
- ui_node_t *nodes[32];
- int nodes_count = 0;
- for (int j = 0; j < canvas->nodes->length; ++j) {
- ui_node_t *n = canvas->nodes->buffer[j];
- if (UI_NODE_X(n) + UI_NODE_W(n) > left && UI_NODE_X(n) < right &&
- UI_NODE_Y(n) + UI_NODE_H(canvas, n) > top && UI_NODE_Y(n) < bottom) {
- nodes[nodes_count] = n;
- nodes_count++;
- }
- }
- if (current->is_shift_down || current->is_ctrl_down) {
- for (int j = 0; j < nodes_count; ++j) {
- add_to_selection(nodes[j]);
- }
- }
- else {
- current_nodes->nodes_selected_id->length = 0;
- for (int j = 0; j < nodes_count; ++j) {
- add_to_selection(nodes[j]);
- }
- }
- }
- // Place selected node on top
- if (current_nodes->move_on_top != NULL) {
- int index = ui_get_node_index(canvas->nodes->buffer, canvas->nodes->length, current_nodes->move_on_top->id);
- ui_remove_node_at(canvas, index);
- any_array_push(canvas->nodes, current_nodes->move_on_top);
- current_nodes->move_on_top = NULL;
- }
- // Node copy & paste
- bool cut_selected = false;
- if (ui_is_copy && !current->is_typing) {
- ui_node_t *copy_nodes[32];
- int copy_nodes_count = 0;
- for (int i = 0; i < current_nodes->nodes_selected_id->length; ++i) {
- int id = current_nodes->nodes_selected_id->buffer[i];
- ui_node_t *n = ui_get_node(canvas->nodes, id);
- if (ui_is_node_type_excluded(n->type)) {
- continue;
- }
- copy_nodes[copy_nodes_count] = n;
- copy_nodes_count++;
- }
- ui_node_link_t *copy_links[64];
- int copy_links_count = 0;
- for (int i = 0; i < canvas->links->length; ++i) {
- ui_node_link_t *l = canvas->links->buffer[i];
- ui_node_t *from = NULL;
- for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
- if (current_nodes->nodes_selected_id->buffer[j] == l->from_id) {
- from = ui_get_node(canvas->nodes, l->from_id);
- break;
- }
- }
- ui_node_t *to = NULL;
- for (int j = 0; j < current_nodes->nodes_selected_id->length; ++j) {
- if (current_nodes->nodes_selected_id->buffer[j] == l->to_id) {
- to = ui_get_node(canvas->nodes, l->to_id);
- break;
- }
- }
- if (from != NULL && !ui_is_node_type_excluded(from->type) &&
- to != NULL && !ui_is_node_type_excluded(to->type)) {
- copy_links[copy_links_count] = l;
- copy_links_count++;
- }
- }
- if (copy_nodes_count > 0) {
- ui_node_canvas_t copy_canvas = {};
- copy_canvas.name = canvas->name;
- ui_node_array_t nodes = { .buffer = copy_nodes, .length = copy_nodes_count };
- ui_node_link_array_t links = { .buffer = copy_links, .length = copy_links_count };
- copy_canvas.nodes = &nodes;
- copy_canvas.links = &links;
- gc_unroot(ui_clipboard);
- ui_clipboard = ui_node_canvas_to_json(©_canvas);
- gc_root(ui_clipboard);
- }
- cut_selected = ui_is_cut;
- ui_is_copy = false;
- ui_is_cut = false;
- }
- if (ui_is_paste && !current->is_typing) {
- ui_is_paste = false;
- bool is_json = ui_clipboard[0] == '{';
- if (is_json) {
- ui_node_canvas_t *paste_canvas = json_parse(ui_clipboard);
- gc_root(paste_canvas); // TODO
- // Convert button data from string to u8 array
- for (int i = 0; i < paste_canvas->nodes->length; ++i) {
- for (int j = 0; j < paste_canvas->nodes->buffer[i]->buttons->length; ++j) {
- ui_node_button_t *but = paste_canvas->nodes->buffer[i]->buttons->buffer[j];
- if (but->data != NULL) {
- but->data = u8_array_create_from_raw(but->data, strlen(but->data) + 1);
- }
- }
- }
- for (int i = 0; i < paste_canvas->links->length; ++i) {
- ui_node_link_t *l = paste_canvas->links->buffer[i];
- // Assign unique link id
- l->id = ui_next_link_id(canvas->links);
- any_array_push(canvas->links, l);
- }
- 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;
- 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;
- for (int i = 0; i < paste_canvas->nodes->length; ++i) {
- ui_node_t *n = paste_canvas->nodes->buffer[i];
- // Assign unique node id
- int old_id = n->id;
- n->id = ui_next_node_id(canvas->nodes);
- for (int j = 0; j < n->inputs->length; ++j) {
- ui_node_socket_t *soc = n->inputs->buffer[j];
- soc->id = ui_get_socket_id(canvas->nodes);
- soc->node_id = n->id;
- }
- for (int j = 0; j < n->outputs->length; ++j) {
- ui_node_socket_t *soc = n->outputs->buffer[j];
- soc->id = ui_get_socket_id(canvas->nodes);
- soc->node_id = n->id;
- }
- for (int j = 0; j < paste_canvas->links->length; ++j) {
- ui_node_link_t *l = paste_canvas->links->buffer[j];
- if (l->from_id == old_id) l->from_id = n->id;
- else if (l->to_id == old_id) l->to_id = n->id;
- }
- n->x += offset_x;
- n->y += offset_y;
- any_array_push(canvas->nodes, n);
- }
- current_nodes->nodes_drag = true;
- current_nodes->nodes_selected_id->length = 0;
- for (int i = 0; i < paste_canvas->nodes->length; ++i) {
- i32_array_push(current_nodes->nodes_selected_id, paste_canvas->nodes->buffer[i]->id);
- }
- current->changed = true;
- }
- }
- // Select all nodes
- if (current->is_ctrl_down && current->key_code == IRON_KEY_A && !current->is_typing) {
- current_nodes->nodes_selected_id->length = 0;
- for (int i = 0; i < canvas->nodes->length; ++i) {
- add_to_selection(canvas->nodes->buffer[i]);
- }
- }
- // Node removal
- bool node_removal = current->input_enabled && (current->is_backspace_down || current->is_delete_down) && !current->is_typing;
- if (node_removal || cut_selected) {
- int i = current_nodes->nodes_selected_id->length - 1;
- while (i >= 0) {
- int nid = current_nodes->nodes_selected_id->buffer[i--];
- ui_node_t *n = ui_get_node(canvas->nodes, nid);
- if (ui_is_node_type_excluded(n->type)) {
- continue;
- }
- ui_remove_node(n, canvas);
- current->changed = true;
- }
- current_nodes->nodes_selected_id->length = 0;
- }
- ui_set_scale(current_nodes->scale_factor); // Restore non-zoomed scale
- current->elements_baked = true;
- current->input_enabled = _input_enabled;
- if (ui_popup_commands != NULL) {
- current->_x = ui_popup_x;
- current->_y = ui_popup_y;
- current->_w = ui_popup_w;
- ui_draw_shadow(current->_x - 5, current->_y - 5, current->_w + 10, ui_popup_h * UI_SCALE() + 10);
- draw_set_color(current->ops->theme->SEPARATOR_COL);
- ui_draw_rect(true, current->_x - 5, current->_y - 5, current->_w + 10, ui_popup_h * UI_SCALE() + 10);
- (*ui_popup_commands)(current, ui_popup_data, ui_popup_data2);
- 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);
- if (hide || current->is_escape_down) {
- ui_popup_commands = NULL;
- }
- }
- }
- void ui_node_canvas_encode(ui_node_canvas_t *canvas) {
- // armpack_encode_start(encoded);
- armpack_encode_map(3);
- armpack_encode_string("name");
- armpack_encode_string(canvas->name);
- armpack_encode_string("nodes");
- armpack_encode_array(canvas->nodes->length);
- for (int i = 0; i < canvas->nodes->length; ++i) {
- armpack_encode_map(10);
- armpack_encode_string("id");
- armpack_encode_i32(canvas->nodes->buffer[i]->id);
- armpack_encode_string("name");
- armpack_encode_string(canvas->nodes->buffer[i]->name);
- armpack_encode_string("type");
- armpack_encode_string(canvas->nodes->buffer[i]->type);
- armpack_encode_string("x");
- armpack_encode_f32(canvas->nodes->buffer[i]->x);
- armpack_encode_string("y");
- armpack_encode_f32(canvas->nodes->buffer[i]->y);
- armpack_encode_string("color");
- armpack_encode_i32(canvas->nodes->buffer[i]->color);
- armpack_encode_string("inputs");
- armpack_encode_array(canvas->nodes->buffer[i]->inputs->length);
- for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
- armpack_encode_map(10);
- armpack_encode_string("id");
- armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->id);
- armpack_encode_string("node_id");
- armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->node_id);
- armpack_encode_string("name");
- armpack_encode_string(canvas->nodes->buffer[i]->inputs->buffer[j]->name);
- armpack_encode_string("type");
- armpack_encode_string(canvas->nodes->buffer[i]->inputs->buffer[j]->type);
- armpack_encode_string("color");
- armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->color);
- armpack_encode_string("default_value");
- armpack_encode_array_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
- armpack_encode_string("min");
- armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->min);
- armpack_encode_string("max");
- armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->max);
- armpack_encode_string("precision");
- armpack_encode_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->precision);
- armpack_encode_string("display");
- armpack_encode_i32(canvas->nodes->buffer[i]->inputs->buffer[j]->display);
- }
- armpack_encode_string("outputs");
- armpack_encode_array(canvas->nodes->buffer[i]->outputs->length);
- for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
- armpack_encode_map(10);
- armpack_encode_string("id");
- armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->id);
- armpack_encode_string("node_id");
- armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->node_id);
- armpack_encode_string("name");
- armpack_encode_string(canvas->nodes->buffer[i]->outputs->buffer[j]->name);
- armpack_encode_string("type");
- armpack_encode_string(canvas->nodes->buffer[i]->outputs->buffer[j]->type);
- armpack_encode_string("color");
- armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->color);
- armpack_encode_string("default_value");
- armpack_encode_array_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
- armpack_encode_string("min");
- armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->min);
- armpack_encode_string("max");
- armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->max);
- armpack_encode_string("precision");
- armpack_encode_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->precision);
- armpack_encode_string("display");
- armpack_encode_i32(canvas->nodes->buffer[i]->outputs->buffer[j]->display);
- }
- armpack_encode_string("buttons");
- armpack_encode_array(canvas->nodes->buffer[i]->buttons->length);
- for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
- armpack_encode_map(9);
- armpack_encode_string("name");
- armpack_encode_string(canvas->nodes->buffer[i]->buttons->buffer[j]->name);
- armpack_encode_string("type");
- armpack_encode_string(canvas->nodes->buffer[i]->buttons->buffer[j]->type);
- armpack_encode_string("output");
- armpack_encode_i32(canvas->nodes->buffer[i]->buttons->buffer[j]->output);
- armpack_encode_string("default_value");
- armpack_encode_array_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
- armpack_encode_string("data");
- u8_array_t *u8 = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
- armpack_encode_array_u8(u8);
- armpack_encode_string("min");
- armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->min);
- armpack_encode_string("max");
- armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->max);
- armpack_encode_string("precision");
- armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->precision);
- armpack_encode_string("height");
- armpack_encode_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->height);
- }
- armpack_encode_string("width");
- armpack_encode_i32(canvas->nodes->buffer[i]->width);
- }
- armpack_encode_string("links");
- armpack_encode_array(canvas->links->length);
- for (int i = 0; i < canvas->links->length; ++i) {
- armpack_encode_map(5);
- armpack_encode_string("id");
- armpack_encode_i32(canvas->links->buffer[i]->id);
- armpack_encode_string("from_id");
- armpack_encode_i32(canvas->links->buffer[i]->from_id);
- armpack_encode_string("from_socket");
- armpack_encode_i32(canvas->links->buffer[i]->from_socket);
- armpack_encode_string("to_id");
- armpack_encode_i32(canvas->links->buffer[i]->to_id);
- armpack_encode_string("to_socket");
- armpack_encode_i32(canvas->links->buffer[i]->to_socket);
- }
- }
- uint32_t ui_node_canvas_encoded_size(ui_node_canvas_t *canvas) {
- uint32_t size = 0;
- size += armpack_size_map();
- size += armpack_size_string("name");
- size += armpack_size_string(canvas->name);
- size += armpack_size_string("nodes");
- size += armpack_size_array();
- for (int i = 0; i < canvas->nodes->length; ++i) {
- size += armpack_size_map();
- size += armpack_size_string("id");
- size += armpack_size_i32();
- size += armpack_size_string("name");
- size += armpack_size_string(canvas->nodes->buffer[i]->name);
- size += armpack_size_string("type");
- size += armpack_size_string(canvas->nodes->buffer[i]->type);
- size += armpack_size_string("x");
- size += armpack_size_f32();
- size += armpack_size_string("y");
- size += armpack_size_f32();
- size += armpack_size_string("color");
- size += armpack_size_i32();
- size += armpack_size_string("inputs");
- size += armpack_size_array();
- for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
- size += armpack_size_map();
- size += armpack_size_string("id");
- size += armpack_size_i32();
- size += armpack_size_string("node_id");
- size += armpack_size_i32();
- size += armpack_size_string("name");
- size += armpack_size_string(canvas->nodes->buffer[i]->inputs->buffer[j]->name);
- size += armpack_size_string("type");
- size += armpack_size_string(canvas->nodes->buffer[i]->inputs->buffer[j]->type);
- size += armpack_size_string("color");
- size += armpack_size_i32();
- size += armpack_size_string("default_value");
- size += armpack_size_array_f32(canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
- size += armpack_size_string("min");
- size += armpack_size_f32();
- size += armpack_size_string("max");
- size += armpack_size_f32();
- size += armpack_size_string("precision");
- size += armpack_size_f32();
- size += armpack_size_string("display");
- size += armpack_size_i32();
- }
- size += armpack_size_string("outputs");
- size += armpack_size_array();
- for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
- size += armpack_size_map();
- size += armpack_size_string("id");
- size += armpack_size_i32();
- size += armpack_size_string("node_id");
- size += armpack_size_i32();
- size += armpack_size_string("name");
- size += armpack_size_string(canvas->nodes->buffer[i]->outputs->buffer[j]->name);
- size += armpack_size_string("type");
- size += armpack_size_string(canvas->nodes->buffer[i]->outputs->buffer[j]->type);
- size += armpack_size_string("color");
- size += armpack_size_i32();
- size += armpack_size_string("default_value");
- size += armpack_size_array_f32(canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
- size += armpack_size_string("min");
- size += armpack_size_f32();
- size += armpack_size_string("max");
- size += armpack_size_f32();
- size += armpack_size_string("precision");
- size += armpack_size_f32();
- size += armpack_size_string("display");
- size += armpack_size_i32();
- }
- size += armpack_size_string("buttons");
- size += armpack_size_array();
- for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
- size += armpack_size_map();
- size += armpack_size_string("name");
- size += armpack_size_string(canvas->nodes->buffer[i]->buttons->buffer[j]->name);
- size += armpack_size_string("type");
- size += armpack_size_string(canvas->nodes->buffer[i]->buttons->buffer[j]->type);
- size += armpack_size_string("output");
- size += armpack_size_i32();
- size += armpack_size_string("default_value");
- size += armpack_size_array_f32(canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
- size += armpack_size_string("data");
- u8_array_t *u8 = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
- size += armpack_size_array_u8(u8);
- size += armpack_size_string("min");
- size += armpack_size_f32();
- size += armpack_size_string("max");
- size += armpack_size_f32();
- size += armpack_size_string("precision");
- size += armpack_size_f32();
- size += armpack_size_string("height");
- size += armpack_size_f32();
- }
- size += armpack_size_string("width");
- size += armpack_size_f32();
- }
- size += armpack_size_string("links");
- size += armpack_size_array();
- for (int i = 0; i < canvas->links->length; ++i) {
- size += armpack_size_map();
- size += armpack_size_string("id");
- size += armpack_size_i32();
- size += armpack_size_string("from_id");
- size += armpack_size_i32();
- size += armpack_size_string("from_socket");
- size += armpack_size_i32();
- size += armpack_size_string("to_id");
- size += armpack_size_i32();
- size += armpack_size_string("to_socket");
- size += armpack_size_i32();
- }
- return size;
- }
- char *ui_node_canvas_to_json(ui_node_canvas_t *canvas) {
- json_encode_begin();
- json_encode_string("name", canvas->name);
- json_encode_begin_array("nodes");
- for (int i = 0; i < canvas->nodes->length; ++i) {
- json_encode_begin_object();
- json_encode_i32("id", canvas->nodes->buffer[i]->id);
- json_encode_string("name", canvas->nodes->buffer[i]->name);
- json_encode_string("type", canvas->nodes->buffer[i]->type);
- json_encode_i32("x", canvas->nodes->buffer[i]->x);
- json_encode_i32("y", canvas->nodes->buffer[i]->y);
- json_encode_i32("color", canvas->nodes->buffer[i]->color);
- json_encode_begin_array("inputs");
- for (int j = 0; j < canvas->nodes->buffer[i]->inputs->length; ++j) {
- json_encode_begin_object();
- json_encode_i32("id", canvas->nodes->buffer[i]->inputs->buffer[j]->id);
- json_encode_i32("node_id", canvas->nodes->buffer[i]->inputs->buffer[j]->node_id);
- json_encode_string("name", canvas->nodes->buffer[i]->inputs->buffer[j]->name);
- json_encode_string("type", canvas->nodes->buffer[i]->inputs->buffer[j]->type);
- json_encode_i32("color", canvas->nodes->buffer[i]->inputs->buffer[j]->color);
- json_encode_f32_array("default_value", canvas->nodes->buffer[i]->inputs->buffer[j]->default_value);
- json_encode_f32("min", canvas->nodes->buffer[i]->inputs->buffer[j]->min);
- json_encode_f32("max", canvas->nodes->buffer[i]->inputs->buffer[j]->max);
- json_encode_f32("precision", canvas->nodes->buffer[i]->inputs->buffer[j]->precision);
- json_encode_i32("display", canvas->nodes->buffer[i]->inputs->buffer[j]->display);
- json_encode_end_object();
- }
- json_encode_end_array();
- json_encode_begin_array("outputs");
- for (int j = 0; j < canvas->nodes->buffer[i]->outputs->length; ++j) {
- json_encode_begin_object();
- json_encode_i32("id", canvas->nodes->buffer[i]->outputs->buffer[j]->id);
- json_encode_i32("node_id", canvas->nodes->buffer[i]->outputs->buffer[j]->node_id);
- json_encode_string("name", canvas->nodes->buffer[i]->outputs->buffer[j]->name);
- json_encode_string("type", canvas->nodes->buffer[i]->outputs->buffer[j]->type);
- json_encode_i32("color", canvas->nodes->buffer[i]->outputs->buffer[j]->color);
- json_encode_f32_array("default_value", canvas->nodes->buffer[i]->outputs->buffer[j]->default_value);
- json_encode_f32("min", canvas->nodes->buffer[i]->outputs->buffer[j]->min);
- json_encode_f32("max", canvas->nodes->buffer[i]->outputs->buffer[j]->max);
- json_encode_f32("precision", canvas->nodes->buffer[i]->outputs->buffer[j]->precision);
- json_encode_i32("display", canvas->nodes->buffer[i]->outputs->buffer[j]->display);
- json_encode_end_object();
- }
- json_encode_end_array();
- json_encode_begin_array("buttons");
- for (int j = 0; j < canvas->nodes->buffer[i]->buttons->length; ++j) {
- json_encode_begin_object();
- json_encode_string("name", canvas->nodes->buffer[i]->buttons->buffer[j]->name);
- json_encode_string("type", canvas->nodes->buffer[i]->buttons->buffer[j]->type);
- json_encode_i32("output", canvas->nodes->buffer[i]->buttons->buffer[j]->output);
- json_encode_f32_array("default_value", canvas->nodes->buffer[i]->buttons->buffer[j]->default_value);
- u8_array_t *data = canvas->nodes->buffer[i]->buttons->buffer[j]->data;
- if (data != NULL) {
- json_encode_string("data", data->buffer);
- }
- else {
- json_encode_null("data");
- }
- json_encode_f32("min", canvas->nodes->buffer[i]->buttons->buffer[j]->min);
- json_encode_f32("max", canvas->nodes->buffer[i]->buttons->buffer[j]->max);
- json_encode_f32("precision", canvas->nodes->buffer[i]->buttons->buffer[j]->precision);
- json_encode_f32("height", canvas->nodes->buffer[i]->buttons->buffer[j]->height);
- json_encode_end_object();
- }
- json_encode_end_array();
- json_encode_f32("width", canvas->nodes->buffer[i]->width);
- json_encode_end_object();
- }
- json_encode_end_array();
- json_encode_begin_array("links");
- for (int i = 0; i < canvas->links->length; ++i) {
- json_encode_begin_object();
- json_encode_i32("id", canvas->links->buffer[i]->id);
- json_encode_i32("from_id", canvas->links->buffer[i]->from_id);
- json_encode_i32("from_socket", canvas->links->buffer[i]->from_socket);
- json_encode_i32("to_id", canvas->links->buffer[i]->to_id);
- json_encode_i32("to_socket", canvas->links->buffer[i]->to_socket);
- json_encode_end_object();
- }
- json_encode_end_array();
- return json_encode_end();
- }
|