Explorar el Código

Blend nodes added

Peter Schulman hace 2 años
padre
commit
151ed01f69
Se han modificado 2 ficheros con 82 adiciones y 3 borrados
  1. 9 3
      demo/common/node_editor.c
  2. 73 0
      demo/common/nodeeditor/node_type_blend.c

+ 9 - 3
demo/common/node_editor.c

@@ -86,6 +86,7 @@ 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"
+#include "nodeeditor/node_type_blend.c"
 
 static void
 node_editor_push(struct node_editor *editor, struct node *node)
@@ -180,12 +181,14 @@ int in_count, int out_count)
     struct node *node = NULL;
 
     if ((nk_size)editor->node_count < NK_LEN(editor->node_buf))
-    {  
+    {
+        /* node_buf has unused slots */
         node = malloc(nodeSize);
         editor->node_buf[editor->node_count++] = node;
         node->ID = IDs++;
     }
     else {
+        /* check for freed up slots in node_buf */
         for (i = 0; i < editor->node_count; i++)
         {
             if (editor->node_buf[i] == NULL) {
@@ -229,7 +232,8 @@ int in_count, int out_count)
 }
 
 void *
-node_editor_eval_connected(struct node* node, int inputSlot) {
+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);
 }
@@ -240,7 +244,7 @@ node_editor_link(struct node_editor *editor, struct node *in_node, int in_slot,
 {
     /* Confusingly, in and out nodes/slots here refer to the inputs and outputs OF THE LINK ITSELF, not the nodes */
     struct node_link *link = NULL;
-    
+
     if ((nk_size)editor->link_count < NK_LEN(editor->links))
         link = &editor->links[editor->link_count++];
     else {
@@ -517,6 +521,8 @@ node_editor_main(struct nk_context *ctx)
                     node_color_create(nodedit, in->mouse.pos);
                 if (nk_contextual_item_label(ctx, "Add Float node", NK_TEXT_CENTERED))
                     node_float_create(nodedit, in->mouse.pos);
+                if (nk_contextual_item_label(ctx, "Add Blend Node", NK_TEXT_CENTERED))
+                    node_blend_create(nodedit, in->mouse.pos);
                 if (nk_contextual_item_label(ctx, grid_option[nodedit->show_grid],NK_TEXT_CENTERED))
                     nodedit->show_grid = !nodedit->show_grid;
                 nk_contextual_end(ctx);

+ 73 - 0
demo/common/nodeeditor/node_type_blend.c

@@ -0,0 +1,73 @@
+struct node_type_blend {
+    struct node node;
+    struct nk_colorf inputVal[2];
+    struct nk_colorf outputVal;
+    float blendVal;
+};
+
+static struct nk_colorf *node_blend_eval(struct node *node, int oIndex) {
+    struct node_type_blend* blendNode = (struct node_type_blend*)node;
+    return &blendNode->outputVal;
+}
+
+static void node_blend_display(struct nk_context *ctx, struct node *node) {
+    struct node_type_blend *blendNode = (struct node_type_blend*)node;
+    const struct nk_colorf blank = {0.0f, 0.0f, 0.0f, 0.0f};
+    float blendAmnt;
+    int i;
+
+    nk_layout_row_dynamic(ctx, 25, 1);
+    for (i = 0; i < (int)NK_LEN(blendNode->inputVal); i++){
+        if(node->inputs[i].isConnected) {
+            blendNode->inputVal[i] = *(struct nk_colorf*)node_editor_eval_connected(node, i);
+        }
+        else {
+            blendNode->inputVal[i] = blank;
+        }
+        nk_button_color(ctx, nk_rgba_cf(blendNode->inputVal[i]));
+    }
+
+        if (node->inputs[2].isConnected) {
+            blendAmnt = *(float*)node_editor_eval_connected(node, 2);
+            blendAmnt = nk_propertyf(ctx, "#Blend", blendAmnt, blendAmnt, blendAmnt, 0.01f, 0.01f);
+        }
+        else {
+            blendNode->blendVal = nk_propertyf(ctx, "#Blend", 0.0f, blendNode->blendVal, 1.0f, 0.01f, 0.01f);
+            blendAmnt = blendNode->blendVal;
+        }
+    
+    
+    if(node->inputs[0].isConnected && node->inputs[1].isConnected) {
+        blendNode->outputVal.r = blendNode->inputVal[0].r * blendAmnt + blendNode->inputVal[1].r * (1.0f-blendAmnt);
+        blendNode->outputVal.g = blendNode->inputVal[0].g * blendAmnt + blendNode->inputVal[1].g * (1.0f-blendAmnt);
+        blendNode->outputVal.b = blendNode->inputVal[0].b * blendAmnt + blendNode->inputVal[1].b * (1.0f-blendAmnt);
+        blendNode->outputVal.a = blendNode->inputVal[0].a * blendAmnt + blendNode->inputVal[1].a * (1.0f-blendAmnt);
+    }
+    else {
+        blendNode->outputVal = blank;
+    }
+}
+
+void node_blend_create(struct node_editor *editor, struct nk_vec2 position) {
+    struct node_type_blend* blendNode = (struct node_type_blend*)node_editor_add(editor, sizeof(struct node_type_blend), "Blend", nk_rect(position.x, position.y, 180, 130), 3, 1);
+    if (blendNode) {
+        const struct nk_colorf blank = {0.0f, 0.0f, 0.0f, 0.0f};
+        int i;
+        for (i = 0; i < (int)NK_LEN(blendNode->inputVal); i++)
+            blendNode->node.inputs[i].type = fColor;
+        blendNode->node.outputs[0].type = fColor;
+
+        blendNode->node.slot_spacing.in_top = 42.0f;
+        blendNode->node.slot_spacing.in_space = 29.0f;
+
+        for (i = 0; i < (int)NK_LEN(blendNode->inputVal); i++)
+            blendNode->inputVal[i] = blank;
+        blendNode->outputVal = blank;
+
+        blendNode->blendVal = 0.5f;
+
+        blendNode->node.displayFunc = node_blend_display;
+        blendNode->node.evalFunc = (void*(*)(struct node*, int)) node_blend_eval;
+
+    }
+}