RobLoach / nuklear_console

Console-like user interface for Nuklear with gamepad, keyboard or mouse support
https://robloach.github.io/nuklear_console/
MIT License
13 stars 3 forks source link

Expand callback capabilities #71

Closed brianwatling closed 4 months ago

brianwatling commented 4 months ago

Fixes #69

A new struct nk_console_event_handler is introduced to expand what callbacks can do by adding per-callback data and a destructor. The per-callback data is passed to every call and the destructor is called when overwriting the callback or when the associated widget is destroyed (exactly like c++ object lifetime). nk_console_event is left intact and a shim layer is added to allow nearly perfect backwards compatibility (ex. users should call nk_console_set_onchange() instead of setting onchange directly)

This enables users to perform more complex logic and/or use the same callback for multiple widgets.

C++ wrappers are provided to allow users to easily set a C++ lambda as a callback:

nk_console_set_onchange_handler(console, [&](nk_console* console) {
        // Custom logic here
    });
brianwatling commented 4 months ago

Just mostly concerned that we now have two different methods of invoking events now. But overall this is a good approach that does give more control over how data is passed across the widgets.

I did this to preserve backwards compatibility and avoid an unused user_data parameter on all existing callback functions. But I think this concern is valid enough to warrant breaking backwards compatibility, so I've updated things to always have a user_data parameter on callbacks. I did keep two versions of the functions to set event handlers - one without user_data and destructor - to keep things simple/clean for the basic case (but LMK if you think we should just have one way to set events always with user_data and dtor)