Blame nuklear-demos/node_editor.c

9b098f
/* nuklear - v1.00 - public domain */
9b098f
/* This is a simple node editor just to show a simple implementation and that
9b098f
 * it is possible to achieve it with this library. While all nodes inside this
9b098f
 * example use a simple color modifier as content you could change them
9b098f
 * to have your custom content depending on the node time.
9b098f
 * Biggest difference to most usual implementation is that this example does
9b098f
 * not have connectors on the right position of the property that it links.
9b098f
 * This is mainly done out of laziness and could be implemented as well but
9b098f
 * requires calculating the position of all rows and add connectors.
9b098f
 * In addition adding and removing nodes is quite limited at the
9b098f
 * moment since it is based on a simple fixed array. If this is to be converted
9b098f
 * into something more serious it is probably best to extend it.*/
9b098f
struct node {
9b098f
    int ID;
9b098f
    char name[32];
9b098f
    struct nk_rect bounds;
9b098f
    float value;
9b098f
    struct nk_color color;
9b098f
    int input_count;
9b098f
    int output_count;
9b098f
    struct node *next;
9b098f
    struct node *prev;
9b098f
};
9b098f
9b098f
struct node_link {
9b098f
    int input_id;
9b098f
    int input_slot;
9b098f
    int output_id;
9b098f
    int output_slot;
9b098f
    struct nk_vec2 in;
9b098f
    struct nk_vec2 out;
9b098f
};
9b098f
9b098f
struct node_linking {
9b098f
    int active;
9b098f
    struct node *node;
9b098f
    int input_id;
9b098f
    int input_slot;
9b098f
};
9b098f
9b098f
struct node_editor {
9b098f
    int initialized;
9b098f
    struct node node_buf[32];
9b098f
    struct node_link links[64];
9b098f
    struct node *begin;
9b098f
    struct node *end;
9b098f
    int node_count;
9b098f
    int link_count;
9b098f
    struct nk_rect bounds;
9b098f
    struct node *selected;
9b098f
    int show_grid;
9b098f
    struct nk_vec2 scrolling;
9b098f
    struct node_linking linking;
9b098f
};
9b098f
static struct node_editor nodeEditor;
9b098f
9b098f
static void
9b098f
node_editor_push(struct node_editor *editor, struct node *node)
9b098f
{
9b098f
    if (!editor->begin) {
9b098f
        node->next = NULL;
9b098f
        node->prev = NULL;
9b098f
        editor->begin = node;
9b098f
        editor->end = node;
9b098f
    } else {
9b098f
        node->prev = editor->end;
9b098f
        if (editor->end)
9b098f
            editor->end->next = node;
9b098f
        node->next = NULL;
9b098f
        editor->end = node;
9b098f
    }
9b098f
}
9b098f
9b098f
static void
9b098f
node_editor_pop(struct node_editor *editor, struct node *node)
9b098f
{
9b098f
    if (node->next)
9b098f
        node->next->prev = node->prev;
9b098f
    if (node->prev)
9b098f
        node->prev->next = node->next;
9b098f
    if (editor->end == node)
9b098f
        editor->end = node->prev;
9b098f
    if (editor->begin == node)
9b098f
        editor->begin = node->next;
9b098f
    node->next = NULL;
9b098f
    node->prev = NULL;
9b098f
}
9b098f
9b098f
static struct node*
9b098f
node_editor_find(struct node_editor *editor, int ID)
9b098f
{
9b098f
    struct node *iter = editor->begin;
9b098f
    while (iter) {
9b098f
        if (iter->ID == ID)
9b098f
            return iter;
9b098f
        iter = iter->next;
9b098f
    }
9b098f
    return NULL;
9b098f
}
9b098f
9b098f
static void
9b098f
node_editor_add(struct node_editor *editor, const char *name, struct nk_rect bounds,
9b098f
    struct nk_color col, int in_count, int out_count)
9b098f
{
9b098f
    static int IDs = 0;
9b098f
    struct node *node;
9b098f
    assert((nk_size)editor->node_count < NK_LEN(editor->node_buf));
9b098f
    node = &editor->node_buf[editor->node_count++];
9b098f
    node->ID = IDs++;
9b098f
    node->value = 0;
9b098f
    node->color = nk_rgb(255, 0, 0);
9b098f
    node->input_count = in_count;
9b098f
    node->output_count = out_count;
9b098f
    node->color = col;
9b098f
    node->bounds = bounds;
9b098f
    strcpy(node->name, name);
9b098f
    node_editor_push(editor, node);
9b098f
}
9b098f
9b098f
static void
9b098f
node_editor_link(struct node_editor *editor, int in_id, int in_slot,
9b098f
    int out_id, int out_slot)
9b098f
{
9b098f
    struct node_link *link;
9b098f
    assert((nk_size)editor->link_count < NK_LEN(editor->links));
9b098f
    link = &editor->links[editor->link_count++];
9b098f
    link->input_id = in_id;
9b098f
    link->input_slot = in_slot;
9b098f
    link->output_id = out_id;
9b098f
    link->output_slot = out_slot;
9b098f
}
9b098f
9b098f
static void
9b098f
node_editor_init(struct node_editor *editor)
9b098f
{
9b098f
    memset(editor, 0, sizeof(*editor));
9b098f
    editor->begin = NULL;
9b098f
    editor->end = NULL;
9b098f
    node_editor_add(editor, "Source", nk_rect(40, 10, 180, 220), nk_rgb(255, 0, 0), 0, 1);
9b098f
    node_editor_add(editor, "Source", nk_rect(40, 260, 180, 220), nk_rgb(0, 255, 0), 0, 1);
9b098f
    node_editor_add(editor, "Combine", nk_rect(400, 100, 180, 220), nk_rgb(0,0,255), 2, 2);
9b098f
    node_editor_link(editor, 0, 0, 2, 0);
9b098f
    node_editor_link(editor, 1, 0, 2, 1);
9b098f
    editor->show_grid = nk_true;
9b098f
}
9b098f
9b098f
static int
9b098f
node_editor(struct nk_context *ctx)
9b098f
{
9b098f
    int n = 0;
9b098f
    struct nk_rect total_space;
9b098f
    const struct nk_input *in = &ctx->input;
9b098f
    struct nk_command_buffer *canvas;
9b098f
    struct node *updated = 0;
9b098f
    struct node_editor *nodedit = &nodeEditor;
9b098f
9b098f
    if (!nodeEditor.initialized) {
9b098f
        node_editor_init(&nodeEditor);
9b098f
        nodeEditor.initialized = 1;
9b098f
    }
9b098f
9b098f
    if (nk_begin(ctx, "NodeEdit", nk_rect(0, 0, 800, 600),
9b098f
        NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE))
9b098f
    {
9b098f
        /* allocate complete window space */
9b098f
        canvas = nk_window_get_canvas(ctx);
9b098f
        total_space = nk_window_get_content_region(ctx);
9b098f
        nk_layout_space_begin(ctx, NK_STATIC, total_space.h, nodedit->node_count);
9b098f
        {
9b098f
            struct node *it = nodedit->begin;
9b098f
            struct nk_rect size = nk_layout_space_bounds(ctx);
9b098f
            struct nk_panel *node = 0;
9b098f
9b098f
            if (nodedit->show_grid) {
9b098f
                /* display grid */
9b098f
                float x, y;
9b098f
                const float grid_size = 32.0f;
9b098f
                const struct nk_color grid_color = nk_rgb(50, 50, 50);
9b098f
                for (x = (float)fmod(size.x - nodedit->scrolling.x, grid_size); x < size.w; x += grid_size)
9b098f
                    nk_stroke_line(canvas, x+size.x, size.y, x+size.x, size.y+size.h, 1.0f, grid_color);
9b098f
                for (y = (float)fmod(size.y - nodedit->scrolling.y, grid_size); y < size.h; y += grid_size)
9b098f
                    nk_stroke_line(canvas, size.x, y+size.y, size.x+size.w, y+size.y, 1.0f, grid_color);
9b098f
            }
9b098f
9b098f
            /* execute each node as a movable group */
9b098f
            while (it) {
9b098f
                /* calculate scrolled node window position and size */
9b098f
                nk_layout_space_push(ctx, nk_rect(it->bounds.x - nodedit->scrolling.x,
9b098f
                    it->bounds.y - nodedit->scrolling.y, it->bounds.w, it->bounds.h));
9b098f
9b098f
                /* execute node window */
9b098f
                if (nk_group_begin(ctx, it->name, NK_WINDOW_MOVABLE|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER|NK_WINDOW_TITLE))
9b098f
                {
9b098f
                    /* always have last selected node on top */
9b098f
9b098f
                    node = nk_window_get_panel(ctx);
9b098f
                    if (nk_input_mouse_clicked(in, NK_BUTTON_LEFT, node->bounds) &&
9b098f
                        (!(it->prev && nk_input_mouse_clicked(in, NK_BUTTON_LEFT,
9b098f
                        nk_layout_space_rect_to_screen(ctx, node->bounds)))) &&
9b098f
                        nodedit->end != it)
9b098f
                    {
9b098f
                        updated = it;
9b098f
                    }
9b098f
9b098f
                    /* ================= NODE CONTENT =====================*/
9b098f
                    nk_layout_row_dynamic(ctx, 25, 1);
9b098f
                    nk_button_color(ctx, it->color);
9b098f
                    it->color.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, it->color.r, 255, 1,1);
9b098f
                    it->color.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, it->color.g, 255, 1,1);
9b098f
                    it->color.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, it->color.b, 255, 1,1);
9b098f
                    it->color.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, it->color.a, 255, 1,1);
9b098f
                    /* ====================================================*/
9b098f
                    nk_group_end(ctx);
9b098f
                }
9b098f
                {
9b098f
                    /* node connector and linking */
9b098f
                    float space;
9b098f
                    struct nk_rect bounds;
9b098f
                    bounds = nk_layout_space_rect_to_local(ctx, node->bounds);
9b098f
                    bounds.x += nodedit->scrolling.x;
9b098f
                    bounds.y += nodedit->scrolling.y;
9b098f
                    it->bounds = bounds;
9b098f
9b098f
                    /* output connector */
9b098f
                    space = node->bounds.h / (float)((it->output_count) + 1);
9b098f
                    for (n = 0; n < it->output_count; ++n) {
9b098f
                        struct nk_rect circle;
9b098f
                        circle.x = node->bounds.x + node->bounds.w-4;
9b098f
                        circle.y = node->bounds.y + space * (float)(n+1);
9b098f
                        circle.w = 8; circle.h = 8;
9b098f
                        nk_fill_circle(canvas, circle, nk_rgb(100, 100, 100));
9b098f
9b098f
                        /* start linking process */
9b098f
                        if (nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, circle, nk_true)) {
9b098f
                            nodedit->linking.active = nk_true;
9b098f
                            nodedit->linking.node = it;
9b098f
                            nodedit->linking.input_id = it->ID;
9b098f
                            nodedit->linking.input_slot = n;
9b098f
                        }
9b098f
9b098f
                        /* draw curve from linked node slot to mouse position */
9b098f
                        if (nodedit->linking.active && nodedit->linking.node == it &&
9b098f
                            nodedit->linking.input_slot == n) {
9b098f
                            struct nk_vec2 l0 = nk_vec2(circle.x + 3, circle.y + 3);
9b098f
                            struct nk_vec2 l1 = in->mouse.pos;
9b098f
                            nk_stroke_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y,
9b098f
                                l1.x - 50.0f, l1.y, l1.x, l1.y, 1.0f, nk_rgb(100, 100, 100));
9b098f
                        }
9b098f
                    }
9b098f
9b098f
                    /* input connector */
9b098f
                    space = node->bounds.h / (float)((it->input_count) + 1);
9b098f
                    for (n = 0; n < it->input_count; ++n) {
9b098f
                        struct nk_rect circle;
9b098f
                        circle.x = node->bounds.x-4;
9b098f
                        circle.y = node->bounds.y + space * (float)(n+1);
9b098f
                        circle.w = 8; circle.h = 8;
9b098f
                        nk_fill_circle(canvas, circle, nk_rgb(100, 100, 100));
9b098f
                        if (nk_input_is_mouse_released(in, NK_BUTTON_LEFT) &&
9b098f
                            nk_input_is_mouse_hovering_rect(in, circle) &&
9b098f
                            nodedit->linking.active && nodedit->linking.node != it) {
9b098f
                            nodedit->linking.active = nk_false;
9b098f
                            node_editor_link(nodedit, nodedit->linking.input_id,
9b098f
                                nodedit->linking.input_slot, it->ID, n);
9b098f
                        }
9b098f
                    }
9b098f
                }
9b098f
                it = it->next;
9b098f
            }
9b098f
9b098f
            /* reset linking connection */
9b098f
            if (nodedit->linking.active && nk_input_is_mouse_released(in, NK_BUTTON_LEFT)) {
9b098f
                nodedit->linking.active = nk_false;
9b098f
                nodedit->linking.node = NULL;
9b098f
                fprintf(stdout, "linking failed\n");
9b098f
            }
9b098f
9b098f
            /* draw each link */
9b098f
            for (n = 0; n < nodedit->link_count; ++n) {
9b098f
                struct node_link *link = &nodedit->links[n];
9b098f
                struct node *ni = node_editor_find(nodedit, link->input_id);
9b098f
                struct node *no = node_editor_find(nodedit, link->output_id);
9b098f
                float spacei = node->bounds.h / (float)((ni->output_count) + 1);
9b098f
                float spaceo = node->bounds.h / (float)((no->input_count) + 1);
9b098f
                struct nk_vec2 l0 = nk_layout_space_to_screen(ctx,
9b098f
                    nk_vec2(ni->bounds.x + ni->bounds.w, 3.0f + ni->bounds.y + spacei * (float)(link->input_slot+1)));
9b098f
                struct nk_vec2 l1 = nk_layout_space_to_screen(ctx,
9b098f
                    nk_vec2(no->bounds.x, 3.0f + no->bounds.y + spaceo * (float)(link->output_slot+1)));
9b098f
9b098f
                l0.x -= nodedit->scrolling.x;
9b098f
                l0.y -= nodedit->scrolling.y;
9b098f
                l1.x -= nodedit->scrolling.x;
9b098f
                l1.y -= nodedit->scrolling.y;
9b098f
                nk_stroke_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y,
9b098f
                    l1.x - 50.0f, l1.y, l1.x, l1.y, 1.0f, nk_rgb(100, 100, 100));
9b098f
            }
9b098f
9b098f
            if (updated) {
9b098f
                /* reshuffle nodes to have least recently selected node on top */
9b098f
                node_editor_pop(nodedit, updated);
9b098f
                node_editor_push(nodedit, updated);
9b098f
            }
9b098f
9b098f
            /* node selection */
9b098f
            if (nk_input_mouse_clicked(in, NK_BUTTON_LEFT, nk_layout_space_bounds(ctx))) {
9b098f
                it = nodedit->begin;
9b098f
                nodedit->selected = NULL;
9b098f
                nodedit->bounds = nk_rect(in->mouse.pos.x, in->mouse.pos.y, 100, 200);
9b098f
                while (it) {
9b098f
                    struct nk_rect b = nk_layout_space_rect_to_screen(ctx, it->bounds);
9b098f
                    b.x -= nodedit->scrolling.x;
9b098f
                    b.y -= nodedit->scrolling.y;
9b098f
                    if (nk_input_is_mouse_hovering_rect(in, b))
9b098f
                        nodedit->selected = it;
9b098f
                    it = it->next;
9b098f
                }
9b098f
            }
9b098f
9b098f
            /* contextual menu */
9b098f
            if (nk_contextual_begin(ctx, 0, nk_vec2(100, 220), nk_window_get_bounds(ctx))) {
9b098f
                const char *grid_option[] = {"Show Grid", "Hide Grid"};
9b098f
                nk_layout_row_dynamic(ctx, 25, 1);
9b098f
                if (nk_contextual_item_label(ctx, "New", NK_TEXT_CENTERED))
9b098f
                    node_editor_add(nodedit, "New", nk_rect(400, 260, 180, 220),
9b098f
                            nk_rgb(255, 255, 255), 1, 2);
9b098f
                if (nk_contextual_item_label(ctx, grid_option[nodedit->show_grid],NK_TEXT_CENTERED))
9b098f
                    nodedit->show_grid = !nodedit->show_grid;
9b098f
                nk_contextual_end(ctx);
9b098f
            }
9b098f
        }
9b098f
        nk_layout_space_end(ctx);
9b098f
9b098f
        /* window content scrolling */
9b098f
        if (nk_input_is_mouse_hovering_rect(in, nk_window_get_bounds(ctx)) &&
9b098f
            nk_input_is_mouse_down(in, NK_BUTTON_MIDDLE)) {
9b098f
            nodedit->scrolling.x += in->mouse.delta.x;
9b098f
            nodedit->scrolling.y += in->mouse.delta.y;
9b098f
        }
9b098f
    }
9b098f
    nk_end(ctx);
9b098f
    return !nk_window_is_closed(ctx, "NodeEdit");
9b098f
}
9b098f