Immediate-Mode-UI / Nuklear

A single-header ANSI C immediate mode cross-platform GUI library
https://immediate-mode-ui.github.io/Nuklear/doc/index.html
8.86k stars 535 forks source link

Events not working with multiple Nuklear windows #626

Open lautriva opened 3 months ago

lautriva commented 3 months ago

Hi, I have a problem with multiple Nuklear windows When I add another window, the events on the first one are not handled correctly anymore For example, hovers only work when I also left-click and mouse scroll not work

Everything work when I use only one window

Here is in pseudo-code how its done:

update_gui_events()
{
    nk_input_begin(context);

    // ...
    // Calls to nk_input_*** to send inputs to Nuklear based on the current state
    // I'm using NK_KEYSTATE_BASED_INPUT
    // ...

    nk_input_end(context);
}

generate_gui()
{
    if (nk_begin_titled(context, "win1", ...)
    {
        // ... Window 1 content
    }
    nk_end(context);

    if (nk_begin_titled(context, "win2", ...)
    {
        // ... Window 2 content
    }
    nk_end(context);

    // Hack
    // If I uncomment this line, the hover events seems to work on both windows
    //     but the scroll problem is still present
    // nk_window_set_focus(context, "win1");
}

render_gui()
{
    // ...
    // Engine specific calls to init the render
    // ...

    nk_buffer_init_fixed(&vertices, ...);
    nk_buffer_init_fixed(&elements, ...);
    nk_convert(ctx, command_buffer, &vertices, &elements, ...);

    nk_draw_foreach(cmd, ctx, command_buffer)
    {
        // ...
        // Engine specific calls to draw the vertices
        // ...
    }

    nk_clear(ctx);
    nk_buffer_free(&vertices);
    nk_buffer_free(&elements);

    // ...
    // Engine specific calls to end the render
    // ...
}

Then on each frame

update_gui_events()
generate_gui()
render_gui();