pthom / imgui_bundle

Dear ImGui Bundle: an extensive set of Ready-to-use widgets and libraries, based on ImGui. Start your first app in 5 lines of code, or less. Whether you prefer Python or C++, this pack has your back!
https://pthom.github.io/imgui_bundle/
MIT License
667 stars 66 forks source link

Imgui::InputText with callback #160

Closed dcnieho closed 9 months ago

dcnieho commented 9 months ago

Currently the callback argument is dropped when imgui.input_text is wrapped (ImGuiInputTextCallback callback = nullptr is the signature). I wonder if it would be possible to wrap this (and thereby callbacks more generally i guess), since i'd like to use the input text with ImGuiInputTextFlags_CallbackHistory.

pthom commented 9 months ago

Hello again,

I cannot work on this, during this christmas period. This modification is a bit more involved, since callbacks are C function pointers (unbindable with pybind).

If you want to give it a try, a possible path would be:

  1. Modify imgui.h like this: (imgui_bundle/external/imgui/imgui/imgui.h) (imgui_bundle is alread using a fork)
    // Callback and functions types
    #ifdef IMGUI_BUNDLE_PYTHON_API
    using ImGuiInputTextCallback = std::function<int(ImGuiInputTextCallbackData*)>;  // Callback function for ImGui::InputText()
    using ImGuiSizeCallback = std::function<void(ImGuiSizeCallbackData*)>;           // Callback function for ImGui::SetNextWindowSizeConstraints()
    #else
    #ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API
    typedef int     (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data);    // Callback function for ImGui::InputText()
    typedef void    (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data);              // Callback function for ImGui::SetNextWindowSizeConstraints()
    #endif
    #endif

i.e. replace C function pointers (unbindable) by std::function, when compiling the python bindings.

  1. Modify external/imgui/bindings/litgen_options_imgui.py and change: options.fn_params_exclude_types__regex = r"Callback$|size_t[ ]\"

  2. run external/imgui/bindings/generate_imgui.py (it requires to install litgen

Merry christmas!

dcnieho commented 9 months ago

Hi Pascal,

No worries, this is another nice to have (sorry, should have specified that). Like for the other case, i won't be able to try this out myself.

There is maybe a dirty trick (I don't quite oversee it after a quick read, but seems like not cheap) to make this work without changing the signatures of all the callback functions: https://stackoverflow.com/a/18422878/3103767

But thats a lot of work on the generator side i suppose!

Merry christmas!

pthom commented 9 months ago

This should be fixed by https://github.com/pthom/imgui_bundle/commit/b722ca917e48311a49aa7e0bd6353e29fa3f4c59 and its cousin https://github.com/pthom/imgui/commit/43d9ae1afa035bc3285ed5d49366e51cd0145be9

dcnieho commented 9 months ago

Super cool. Can't wait to try it out :)

dcnieho commented 9 months ago

I just realize i have two usecases for this callback: I want to add a button to my GUI that allows inserting a path at the cursor position in the textbox. Right now the only way to get the cursor position in the callback is through this callback. So, double super nice you've enabled it!

dcnieho commented 8 months ago

@pthom: This is working very well, thanks a lot!