Node eval functions

This commit is contained in:
Peter Schulman 2023-04-19 19:38:58 +03:00
parent 5f1e3efd29
commit f165244c8e
4 changed files with 46 additions and 36 deletions

View File

@ -66,6 +66,7 @@ struct node_editor {
int initialized; int initialized;
struct node *node_buf[32]; struct node *node_buf[32];
struct node_link links[64]; struct node_link links[64];
struct node *outputNode;
struct node *begin; struct node *begin;
struct node *end; struct node *end;
int node_count; int node_count;
@ -196,6 +197,9 @@ node_editor_link(struct node_editor *editor, struct node *in_node, int in_slot,
link = &editor->links[editor->link_count++]; link = &editor->links[editor->link_count++];
out_node->inputs[out_slot].isConnected = nk_true; out_node->inputs[out_slot].isConnected = nk_true;
in_node->outputs[in_slot].isConnected = nk_true; in_node->outputs[in_slot].isConnected = nk_true;
out_node->inputs[out_slot].connectedNode = in_node;
out_node->inputs[out_slot].connectedSlot = in_slot;
link->input_node = in_node; link->input_node = in_node;
link->input_slot = in_slot; link->input_slot = in_slot;
link->output_node = out_node; link->output_node = out_node;
@ -209,8 +213,10 @@ node_editor_init(struct node_editor *editor)
memset(editor, 0, sizeof(*editor)); memset(editor, 0, sizeof(*editor));
editor->begin = NULL; editor->begin = NULL;
editor->end = NULL; editor->end = NULL;
editor->outputNode = node_output_create(editor, (struct nk_vec2){600, 400});
node_color_create(editor, (struct nk_vec2){40, 10}); node_color_create(editor, (struct nk_vec2){40, 10});
node_output_create(editor, (struct nk_vec2){200, 200});
editor->show_grid = nk_true; editor->show_grid = nk_true;
} }

View File

@ -4,46 +4,37 @@ struct node_type_color {
struct nk_colorf outputVal; struct nk_colorf outputVal;
}; };
/* static struct nk_colorf *node_color_eval(struct node* node, int oIndex)
static void* node_color_get(struct node* self, int oIndex)
{ {
struct node_type_color *node = ((struct node_type_color*)self); NK_ASSERT(oIndex == 0); /* only one output node */
for (int i = 0; i < self->input_count; i++) struct node_type_color *colornode = (struct node_type_color*)node;
{
if (self->inputs[i].isConnected)
{
/*
call getter func of connected node and assign to ((node_type_color*)self)->inputVal[i]
*//*
}
}
node->outputVal->r = node->inputVal[0];
node->outputVal->g = node->inputVal[1];
node->outputVal->b = node->inputVal[2];
node->outputVal->a = node->inputVal[3];
struct nk_colorf *returnpointer = ((struct node_type_color*)self)->outputVal; return &colornode->outputVal;
return (void*) returnpointer; }
}*/
static void node_color_draw(struct nk_context *ctx, struct node *node) static void node_color_draw(struct nk_context *ctx, struct node *node)
{ {
struct node_type_color *colornode = (struct node_type_color*)node; struct node_type_color *colornode = (struct node_type_color*)node;
float evalResult; /* Get the values from connected nodes into this so the inputs revert on disconnect */
char* labels[4] = {"#R:","#G:","B:","A:"};
float colorVals[4]; /* Because we can't just loop through the struct... */
nk_layout_row_dynamic(ctx, 25, 1); nk_layout_row_dynamic(ctx, 25, 1);
nk_button_color(ctx, nk_rgba_f(colornode->inputVal[0],colornode->inputVal[1],colornode->inputVal[2],colornode->inputVal[3])); nk_button_color(ctx, nk_rgba_cf(colornode->outputVal));
colornode->inputVal[0] = colornode->node.inputs[0].isConnected ?
nk_propertyf(ctx, "#R:", colornode->inputVal[0], colornode->inputVal[0], colornode->inputVal[0], 0.05f, 0.05f) : for (int i = 0; i < 4; i++) {
nk_propertyf(ctx, "#R:", 0.0f, colornode->inputVal[0], 1.0f, 0.01f, 0.01f); if (colornode->node.inputs[i].isConnected) {
colornode->inputVal[1] = colornode->node.inputs[1].isConnected ? evalResult = *(float*)node->inputs[i].connectedNode->evalFunc(node->inputs[i].connectedNode, node->inputs[i].connectedSlot);
nk_propertyf(ctx, "#G:", colornode->inputVal[1], colornode->inputVal[1], colornode->inputVal[1], 0.05f, 0.05f) : evalResult = nk_propertyf(ctx, labels[i], evalResult, evalResult, evalResult, 0.05f, 0.05f);
nk_propertyf(ctx, "#G:", 0.0f, colornode->inputVal[1], 1.0f, 0.01f, 0.01f); colorVals[i] = evalResult;
colornode->inputVal[2] = colornode->node.inputs[2].isConnected ? }
nk_propertyf(ctx, "#B:", colornode->inputVal[2], colornode->inputVal[2], colornode->inputVal[2], 0.05f, 0.05f) : else {
nk_propertyf(ctx, "#B:", 0.0f, colornode->inputVal[2], 1.0f, 0.01f, 0.01f); colornode->inputVal[i] = nk_propertyf(ctx, labels[i], 0.0f, colornode->inputVal[i], 1.0f, 0.05f, 0.05f);
colornode->inputVal[3] = colornode->node.inputs[3].isConnected ? colorVals[i] = colornode->inputVal[i];
nk_propertyf(ctx, "#A:", colornode->inputVal[3], colornode->inputVal[3], colornode->inputVal[3], 0.05f, 0.05f) : }
nk_propertyf(ctx, "#A:", 0.0f, colornode->inputVal[3], 1.0f, 0.01f, 0.01f); }
colornode->outputVal = (struct nk_colorf){colorVals[0],colorVals[1],colorVals[2],colorVals[3]};
} }
void node_color_create(struct node_editor *editor, struct nk_vec2 position) void node_color_create(struct node_editor *editor, struct nk_vec2 position)
@ -66,4 +57,5 @@ void node_color_create(struct node_editor *editor, struct nk_vec2 position)
colornode->outputVal = (struct nk_colorf){1.0f, 1.0f, 1.0f, 1.0f}; colornode->outputVal = (struct nk_colorf){1.0f, 1.0f, 1.0f, 1.0f};
colornode->node.displayFunc = node_color_draw; colornode->node.displayFunc = node_color_draw;
colornode->node.evalFunc = node_color_eval;
} }

View File

@ -3,6 +3,12 @@ struct node_type_float {
float outputVal; float outputVal;
}; };
static float *node_float_eval(struct node* node, int oIndex) {
NK_ASSERT(oIndex == 0);
struct node_type_float *floatnode = (struct node_type_float*)node;
return &floatnode->outputVal;
}
static void node_float_draw(struct nk_context *ctx, struct node *node) { static void node_float_draw(struct nk_context *ctx, struct node *node) {
struct node_type_float *floatnode = (struct node_type_float*)node; struct node_type_float *floatnode = (struct node_type_float*)node;
nk_layout_row_dynamic(ctx, 25, 1); nk_layout_row_dynamic(ctx, 25, 1);
@ -14,4 +20,5 @@ void node_float_create(struct node_editor *editor, struct nk_vec2 position) {
floatnode->outputVal = 1.0f; floatnode->outputVal = 1.0f;
floatnode->node.displayFunc = node_float_draw; floatnode->node.displayFunc = node_float_draw;
floatnode->node.evalFunc = node_float_eval;
} }

View File

@ -1,15 +1,20 @@
struct node_type_output { struct node_type_output {
struct node node; struct node node;
struct nk_color inputVal; struct nk_colorf inputVal;
}; };
static void node_output_display(struct nk_context *ctx, struct node *node) { 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));
}
} }
void node_output_create(struct node_editor *editor, struct nk_vec2 position) { struct node* node_output_create(struct node_editor *editor, struct nk_vec2 position) {
struct node_type_output *outputNode = (struct node_type_output*)node_editor_add(editor, sizeof(struct node_type_output), "Output", nk_rect(position.x, position.y, 100, 100), 1, 0); struct node_type_output *outputNode = (struct node_type_output*)node_editor_add(editor, sizeof(struct node_type_output), "Output", nk_rect(position.x, position.y, 100, 100), 1, 0);
outputNode->node.inputs[0].type = fColor; outputNode->node.inputs[0].type = fColor;
outputNode->node.displayFunc = node_output_display; outputNode->node.displayFunc = node_output_display;
return outputNode;
} }