From 4c8c3e3fa03d41ce98f70eaaa3e03e9eeee2198f Mon Sep 17 00:00:00 2001 From: Peter Schulman Date: Mon, 24 Apr 2023 11:26:38 +0300 Subject: [PATCH] Comment clean up + add node_editor_eval_connected & node_output_get --- demo/common/node_editor.c | 41 +++++++++++------------ demo/common/nodeeditor/node_type_color.c | 10 +++--- demo/common/nodeeditor/node_type_float.c | 6 ++-- demo/common/nodeeditor/node_type_output.c | 14 ++++++-- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/demo/common/node_editor.c b/demo/common/node_editor.c index 75306ab1..8b55cacb 100644 --- a/demo/common/node_editor.c +++ b/demo/common/node_editor.c @@ -27,17 +27,16 @@ struct node { int ID; char name[32]; struct nk_rect bounds; - /* enum node_type type; */ int input_count; int output_count; - struct node_connector *inputs; /* These could be made into arrays to get rid of the allocation at node creation. */ - struct node_connector *outputs; /* For this demo we probably only need a max of four inputs and one output. */ + struct node_connector *inputs; + struct node_connector *outputs; struct { float in_top; float in_space; float out_top; float out_space; - } slot_spacing; /* not sure what to call this really... */ + } slot_spacing; /* Maybe this should be called "node_layout" and include the bounds? */ struct node *next; /* Z ordering only */ struct node *prev; /* Z ordering only */ @@ -51,8 +50,6 @@ struct node_link { struct node* output_node; int output_slot; nk_bool isActive; - /* struct nk_vec2 in; - struct nk_vec2 out; */ }; struct node_linking { @@ -80,13 +77,12 @@ struct node_editor { static struct node_editor nodeEditor; /* === PROTOTYPES === */ - -static struct node* node_editor_add(struct node_editor *editor, size_t nodeSize, const char *name, struct nk_rect bounds, +/* These should go in a header file along with the node struct */ +struct node* node_editor_add(struct node_editor *editor, size_t nodeSize, const char *name, struct nk_rect bounds, int in_count, int out_count); - +void* node_editor_eval_connected(struct node *node, int inputSlot); /* ================== */ - #include "nodeeditor/node_type_color.c" #include "nodeeditor/node_type_float.c" #include "nodeeditor/node_type_output.c" @@ -150,15 +146,15 @@ node_editor_find_link_by_output(struct node_editor *editor, struct node *output_ return NULL; } -static struct node* +struct node* node_editor_add(struct node_editor *editor, size_t nodeSize, const char *name, struct nk_rect bounds, int in_count, int out_count) { - static int IDs = 0; /* static duration */ + static int IDs = 0; NK_ASSERT((nk_size)editor->node_count < NK_LEN(editor->node_buf)); struct node *node = malloc(nodeSize); - editor->node_buf[editor->node_count++] = node; /* next node in buffer */ - node->ID = IDs++; /* increment IDs and set as node ID */ + editor->node_buf[editor->node_count++] = node; + node->ID = IDs++; node->bounds = bounds; @@ -170,11 +166,11 @@ int in_count, int out_count) for (int i = 0; i < node->input_count; i++) { node->inputs[i].isConnected = nk_false; - node->inputs[i].type = fValue; + node->inputs[i].type = fValue; /* default connector type */ } for (i = 0; i < node->output_count; i++) { node->outputs[i].isConnected = nk_false; - node->outputs[i].type = fValue; + node->outputs[i].type = fValue; /* default connector type */ } /* default connector spacing */ @@ -187,6 +183,12 @@ int in_count, int out_count) return node; } +void * +node_editor_eval_connected(struct node* node, int inputSlot) { + NK_ASSERT(node->inputs[inputSlot].isConnected); + return node->inputs[inputSlot].connectedNode->evalFunc(node->inputs[inputSlot].connectedNode, node->inputs[inputSlot].connectedSlot); +} + static void node_editor_link(struct node_editor *editor, struct node *in_node, int in_slot, struct node *out_node, int out_slot) @@ -381,8 +383,8 @@ node_editor_main(struct nk_context *ctx) for (n = 0; n < nodedit->link_count; ++n) { struct node_link *link = &nodedit->links[n]; if (link->isActive == nk_true){ - struct node *ni = link->input_node;//node_editor_find(nodedit, link->input_id); - struct node *no = link->output_node;//node_editor_find(nodedit, link->output_id); + struct node *ni = link->input_node; + struct node *no = link->output_node; struct nk_vec2 l0 = nk_layout_space_to_screen(ctx, nk_vec2(ni->bounds.x + ni->bounds.w, 3.0f + ni->bounds.y + ni->slot_spacing.out_top + ni->slot_spacing.out_space * (float)(link->input_slot))); struct nk_vec2 l1 = nk_layout_space_to_screen(ctx, @@ -422,9 +424,6 @@ node_editor_main(struct nk_context *ctx) if (nk_contextual_begin(ctx, 0, nk_vec2(150, 220), nk_window_get_bounds(ctx))) { const char *grid_option[] = {"Show Grid", "Hide Grid"}; nk_layout_row_dynamic(ctx, 25, 1); - /* if (nk_contextual_item_label(ctx, "New", NK_TEXT_CENTERED)) - node_editor_add(nodedit, "New", nk_rect(in->mouse.pos.x, in->mouse.pos.y, 180, 220), - nk_rgb(255, 255, 255), 1, 2); */ if (nk_contextual_item_label(ctx, "Add Color node", NK_TEXT_CENTERED)) node_color_create(nodedit, in->mouse.pos); if (nk_contextual_item_label(ctx, "Add Float node", NK_TEXT_CENTERED)) diff --git a/demo/common/nodeeditor/node_type_color.c b/demo/common/nodeeditor/node_type_color.c index 6fc674d8..5349cfe7 100644 --- a/demo/common/nodeeditor/node_type_color.c +++ b/demo/common/nodeeditor/node_type_color.c @@ -6,7 +6,7 @@ struct node_type_color { static struct nk_colorf *node_color_eval(struct node* node, int oIndex) { - NK_ASSERT(oIndex == 0); /* only one output node */ + NK_ASSERT(oIndex == 0); /* only one output connector */ struct node_type_color *colornode = (struct node_type_color*)node; return &colornode->outputVal; @@ -24,12 +24,12 @@ static void node_color_draw(struct nk_context *ctx, struct node *node) for (int i = 0; i < 4; i++) { if (colornode->node.inputs[i].isConnected) { - evalResult = *(float*)node->inputs[i].connectedNode->evalFunc(node->inputs[i].connectedNode, node->inputs[i].connectedSlot); - evalResult = nk_propertyf(ctx, labels[i], evalResult, evalResult, evalResult, 0.05f, 0.05f); + evalResult = *(float*)node_editor_eval_connected(node, i); + evalResult = nk_propertyf(ctx, labels[i], evalResult, evalResult, evalResult, 0.01f, 0.01f); colorVals[i] = evalResult; } else { - colornode->inputVal[i] = nk_propertyf(ctx, labels[i], 0.0f, colornode->inputVal[i], 1.0f, 0.05f, 0.05f); + colornode->inputVal[i] = nk_propertyf(ctx, labels[i], 0.0f, colornode->inputVal[i], 1.0f, 0.01f, 0.01f); colorVals[i] = colornode->inputVal[i]; } } @@ -39,7 +39,7 @@ static void node_color_draw(struct nk_context *ctx, struct node *node) void node_color_create(struct node_editor *editor, struct nk_vec2 position) { - struct node_type_color *colornode = (struct node_type_color*)node_editor_add(editor, sizeof(struct node_type_color), "Color", nk_rect(position.x, position.y, 180, 220), 4, 1); + struct node_type_color *colornode = (struct node_type_color*)node_editor_add(editor, sizeof(struct node_type_color), "Color", nk_rect(position.x, position.y, 180, 190), 4, 1); colornode->node.slot_spacing.in_top = 72.0f; colornode->node.slot_spacing.in_space = 29.0f; colornode->node.slot_spacing.out_top = 42.0f; diff --git a/demo/common/nodeeditor/node_type_float.c b/demo/common/nodeeditor/node_type_float.c index 6cb8fcb9..008b3a0c 100644 --- a/demo/common/nodeeditor/node_type_float.c +++ b/demo/common/nodeeditor/node_type_float.c @@ -1,3 +1,5 @@ +#include + struct node_type_float { struct node node; float outputVal; @@ -12,11 +14,11 @@ static float *node_float_eval(struct node* node, int oIndex) { static void node_float_draw(struct nk_context *ctx, struct node *node) { struct node_type_float *floatnode = (struct node_type_float*)node; nk_layout_row_dynamic(ctx, 25, 1); - floatnode->outputVal = nk_propertyf(ctx, "#Value:", 0.0f, floatnode->outputVal, 1.0f, 0.01f, 0.01f); + floatnode->outputVal = nk_propertyf(ctx, "#Value:", -FLT_MAX, floatnode->outputVal, FLT_MAX, 0.01f, 0.01f); } void node_float_create(struct node_editor *editor, struct nk_vec2 position) { - struct node_type_float *floatnode = (struct node_type_float*)node_editor_add(editor, sizeof(struct node_type_float), "Float", nk_rect(position.x, position.y, 180, 100), 0, 1); + struct node_type_float *floatnode = (struct node_type_float*)node_editor_add(editor, sizeof(struct node_type_float), "Float", nk_rect(position.x, position.y, 180, 75), 0, 1); floatnode->outputVal = 1.0f; floatnode->node.displayFunc = node_float_draw; diff --git a/demo/common/nodeeditor/node_type_output.c b/demo/common/nodeeditor/node_type_output.c index 5a3ac94f..f4bdf36e 100644 --- a/demo/common/nodeeditor/node_type_output.c +++ b/demo/common/nodeeditor/node_type_output.c @@ -3,11 +3,19 @@ struct node_type_output { struct nk_colorf inputVal; }; +struct nk_colorf *node_output_get(struct node* node) { + struct node_type_output *outputnode = (struct node_type_output*)node; + if (!node->inputs[0].isConnected) + outputnode->inputVal = (struct nk_colorf){0.0f, 0.0f, 0.0f, 0.0f}; + return &outputnode->inputVal; +} + static void node_output_display(struct nk_context *ctx, struct node *node) { if (node->inputs[0].isConnected) { - struct nk_colorf inputVal = *(struct nk_colorf*)(node->inputs[0].connectedNode->evalFunc(node->inputs[0].connectedNode, node->inputs[0].connectedSlot)); - nk_layout_row_dynamic(ctx, 25, 1); - nk_button_color(ctx, nk_rgba_cf(inputVal)); + struct node_type_output *outputnode = (struct node_type_output*)node; + outputnode->inputVal = *(struct nk_colorf*)node_editor_eval_connected(node, 0); + nk_layout_row_dynamic(ctx, 60, 1); + nk_button_color(ctx, nk_rgba_cf(outputnode->inputVal)); } }