Skip to content

Commit

Permalink
Update ImGui bindings: publish ImGuiInputTextCallback and ImGuiSizeCa…
Browse files Browse the repository at this point in the history
…llback as std::function
  • Loading branch information
pthom committed Dec 26, 2023
1 parent 191532f commit b722ca9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
23 changes: 18 additions & 5 deletions bindings/imgui_bundle/imgui/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,17 @@ typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data);
typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd);
#endif
"""
InputTextCallback = Any # These types are C function pointers
SizeCallback = Any # and thus are hard to create from python
MemAllocFunc = Any
MemFreeFunc = Any
ImDrawCallback = Any

#using ImGuiInputTextCallback = std::function<int(ImGuiInputTextCallbackData*)>; // Callback function for ImGui::InputText()
#using ImGuiSizeCallback = std::function<void(ImGuiSizeCallbackData*)>; // Callback function for ImGui::SetNextWindowSizeConstraints()
InputTextCallback = Callable[[InputTextCallbackData], int]
SizeCallback = Callable[[SizeCallbackData], None]



"""
// Helpers macros to generate 32-bit encoded colors
// User can declare their own format by #defining the 5 _SHIFT/_MASK macros in their imconfig file.
Expand Down Expand Up @@ -743,7 +748,10 @@ def set_next_window_size(size: ImVec2, cond: Cond = 0) -> None:

# IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); /* original C++ signature */
def set_next_window_size_constraints(
size_min: ImVec2, size_max: ImVec2, custom_callback_data: Optional[Any] = None
size_min: ImVec2,
size_max: ImVec2,
custom_callback: SizeCallback = None,
custom_callback_data: Optional[Any] = None,
) -> None:
"""set next window size limits. use 0.0 or FLT_MAX if you don't want limits. Use -1 for both min and max of same axis to preserve current size (which itself is a constraint). Use callback to apply non-trivial programmatic constraints."""
pass
Expand Down Expand Up @@ -7930,7 +7938,6 @@ class IO:
# float IniSavingRate; /* original C++ signature */
ini_saving_rate: float
# = 5.0 // Minimum time between saving positions/sizes to .ini file, in seconds.

# #ifdef IMGUI_BUNDLE_PYTHON_API
#
# std::string LogFilename; /* original C++ signature */
Expand Down Expand Up @@ -10447,7 +10454,11 @@ class PlatformImeData:
# Because text input needs dynamic resizing, we need to setup a callback to grow the capacity
# IMGUI_API bool InputText(const char* label, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void* user_data = nullptr); /* original C++ signature */
def input_text(
label: str, str: str, flags: InputTextFlags = 0, user_data: Optional[Any] = None
label: str,
str: str,
flags: InputTextFlags = 0,
callback: InputTextCallback = None,
user_data: Optional[Any] = None,
) -> Tuple[bool, str]:
pass

Expand All @@ -10457,6 +10468,7 @@ def input_text_multiline(
str: str,
size: ImVec2 = ImVec2(0, 0),
flags: InputTextFlags = 0,
callback: InputTextCallback = None,
user_data: Optional[Any] = None,
) -> Tuple[bool, str]:
pass
Expand All @@ -10468,6 +10480,7 @@ def input_text_with_hint(
hint: str,
str: str,
flags: InputTextFlags = 0,
callback: InputTextCallback = None,
user_data: Optional[Any] = None,
) -> Tuple[bool, str]:
pass
Expand Down
2 changes: 2 additions & 0 deletions bindings/imgui_bundle/imgui/internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,8 @@ class NextWindowData:
collapsed_val: bool
# ImRect SizeConstraintRect; /* original C++ signature */
size_constraint_rect: ImRect
# ImGuiSizeCallback SizeCallback; /* original C++ signature */
size_callback: SizeCallback
# void* SizeCallbackUserData; /* original C++ signature */
size_callback_user_data: Any
# float BgAlphaVal; /* original C++ signature */
Expand Down
7 changes: 4 additions & 3 deletions external/imgui/bindings/litgen_options_imgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,15 @@ def litgen_options_imgui(
[
# typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd);
# ImDrawCallback UserCallback; // 4-8 // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally.
r"Callback$",
r"^TexPixelsAlpha8$",
]
)

options.member_exclude_by_type__regex = join_string_by_pipe_char(
[
# r"^char\s*\*",
r"^ImDrawCallback$",
r"^ContextHookCallback$",
r"^ImGuiContextHookCallback$",
r"const ImWchar\s*\*",
r"unsigned char\s*\*",
r"unsigned int\s*\*",
Expand Down Expand Up @@ -391,7 +392,7 @@ def litgen_options_imgui(

# Exclude callbacks from the params when they have a default value
# (since imgui use bare C function pointers, not easily portable)
options.fn_params_exclude_types__regex = r"Callback$|size_t[ ]*\*"
options.fn_params_exclude_types__regex = r"size_t[ ]*\*"
# Exclude functions that take char or const ImWchar * params
options.fn_exclude_by_param_type__regex = (
"^char$|^const ImWchar \*$|^ImGuiErrorLogCallback$"
Expand Down
57 changes: 17 additions & 40 deletions external/imgui/bindings/pybind_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,8 @@ void py_init_module_imgui_main(py::module& m)
"set next window size. set axis to 0.0 to force an auto-fit on this axis. call before Begin()");

m.def("set_next_window_size_constraints",
[](const ImVec2 & size_min, const ImVec2 & size_max, void * custom_callback_data = NULL)
{
auto SetNextWindowSizeConstraints_adapt_exclude_params = [](const ImVec2 & size_min, const ImVec2 & size_max, void * custom_callback_data = NULL)
{
ImGui::SetNextWindowSizeConstraints(size_min, size_max, NULL, custom_callback_data);
};

SetNextWindowSizeConstraints_adapt_exclude_params(size_min, size_max, custom_callback_data);
},
py::arg("size_min"), py::arg("size_max"), py::arg("custom_callback_data") = py::none(),
ImGui::SetNextWindowSizeConstraints,
py::arg("size_min"), py::arg("size_max"), py::arg("custom_callback") = py::none(), py::arg("custom_callback_data") = py::none(),
"set next window size limits. use 0.0 or FLT_MAX if you don't want limits. Use -1 for both min and max of same axis to preserve current size (which itself is a constraint). Use callback to apply non-trivial programmatic constraints.");

m.def("set_next_window_content_size",
Expand Down Expand Up @@ -6302,61 +6294,46 @@ void py_init_module_imgui_main(py::module& m)

//////////////////// <generated_from:imgui_stdlib.h> ////////////////////
m.def("input_text",
[](const char * label, std::string str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
[](const char * label, std::string str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
auto InputText_adapt_exclude_params = [](const char * label, std::string * str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> bool
{
auto lambda_result = ImGui::InputText(label, str, flags, nullptr, user_data);
return lambda_result;
};
auto InputText_adapt_modifiable_immutable_to_return = [&InputText_adapt_exclude_params](const char * label, std::string str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
auto InputText_adapt_modifiable_immutable_to_return = [](const char * label, std::string str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
std::string * str_adapt_modifiable = & str;

bool r = InputText_adapt_exclude_params(label, str_adapt_modifiable, flags, user_data);
bool r = ImGui::InputText(label, str_adapt_modifiable, flags, callback, user_data);
return std::make_tuple(r, str);
};

return InputText_adapt_modifiable_immutable_to_return(label, str, flags, user_data);
}, py::arg("label"), py::arg("str"), py::arg("flags") = 0, py::arg("user_data") = py::none());
return InputText_adapt_modifiable_immutable_to_return(label, str, flags, callback, user_data);
}, py::arg("label"), py::arg("str"), py::arg("flags") = 0, py::arg("callback") = py::none(), py::arg("user_data") = py::none());

m.def("input_text_multiline",
[](const char * label, std::string str, const ImVec2 & size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
[](const char * label, std::string str, const ImVec2 & size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
auto InputTextMultiline_adapt_exclude_params = [](const char * label, std::string * str, const ImVec2 & size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> bool
{
auto lambda_result = ImGui::InputTextMultiline(label, str, size, flags, nullptr, user_data);
return lambda_result;
};
auto InputTextMultiline_adapt_modifiable_immutable_to_return = [&InputTextMultiline_adapt_exclude_params](const char * label, std::string str, const ImVec2 & size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
auto InputTextMultiline_adapt_modifiable_immutable_to_return = [](const char * label, std::string str, const ImVec2 & size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
std::string * str_adapt_modifiable = & str;

bool r = InputTextMultiline_adapt_exclude_params(label, str_adapt_modifiable, size, flags, user_data);
bool r = ImGui::InputTextMultiline(label, str_adapt_modifiable, size, flags, callback, user_data);
return std::make_tuple(r, str);
};

return InputTextMultiline_adapt_modifiable_immutable_to_return(label, str, size, flags, user_data);
}, py::arg("label"), py::arg("str"), py::arg("size") = ImVec2(0, 0), py::arg("flags") = 0, py::arg("user_data") = py::none());
return InputTextMultiline_adapt_modifiable_immutable_to_return(label, str, size, flags, callback, user_data);
}, py::arg("label"), py::arg("str"), py::arg("size") = ImVec2(0, 0), py::arg("flags") = 0, py::arg("callback") = py::none(), py::arg("user_data") = py::none());

m.def("input_text_with_hint",
[](const char * label, const char * hint, std::string str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
[](const char * label, const char * hint, std::string str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
auto InputTextWithHint_adapt_exclude_params = [](const char * label, const char * hint, std::string * str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> bool
{
auto lambda_result = ImGui::InputTextWithHint(label, hint, str, flags, nullptr, user_data);
return lambda_result;
};
auto InputTextWithHint_adapt_modifiable_immutable_to_return = [&InputTextWithHint_adapt_exclude_params](const char * label, const char * hint, std::string str, ImGuiInputTextFlags flags = 0, void * user_data = nullptr) -> std::tuple<bool, std::string>
auto InputTextWithHint_adapt_modifiable_immutable_to_return = [](const char * label, const char * hint, std::string str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void * user_data = nullptr) -> std::tuple<bool, std::string>
{
std::string * str_adapt_modifiable = & str;

bool r = InputTextWithHint_adapt_exclude_params(label, hint, str_adapt_modifiable, flags, user_data);
bool r = ImGui::InputTextWithHint(label, hint, str_adapt_modifiable, flags, callback, user_data);
return std::make_tuple(r, str);
};

return InputTextWithHint_adapt_modifiable_immutable_to_return(label, hint, str, flags, user_data);
}, py::arg("label"), py::arg("hint"), py::arg("str"), py::arg("flags") = 0, py::arg("user_data") = py::none());
return InputTextWithHint_adapt_modifiable_immutable_to_return(label, hint, str, flags, callback, user_data);
}, py::arg("label"), py::arg("hint"), py::arg("str"), py::arg("flags") = 0, py::arg("callback") = py::none(), py::arg("user_data") = py::none());
//////////////////// </generated_from:imgui_stdlib.h> ////////////////////


Expand Down
1 change: 1 addition & 0 deletions external/imgui/bindings/pybind_imgui_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ void py_init_module_imgui_internal(py::module& m)
.def_readwrite("pos_undock", &ImGuiNextWindowData::PosUndock, "")
.def_readwrite("collapsed_val", &ImGuiNextWindowData::CollapsedVal, "")
.def_readwrite("size_constraint_rect", &ImGuiNextWindowData::SizeConstraintRect, "")
.def_readwrite("size_callback", &ImGuiNextWindowData::SizeCallback, "")
.def_readwrite("size_callback_user_data", &ImGuiNextWindowData::SizeCallbackUserData, "")
.def_readwrite("bg_alpha_val", &ImGuiNextWindowData::BgAlphaVal, "Override background alpha")
.def_readwrite("viewport_id", &ImGuiNextWindowData::ViewportId, "")
Expand Down
2 changes: 1 addition & 1 deletion external/imgui/imgui
Submodule imgui updated 1 files
+7 −1 imgui.h

0 comments on commit b722ca9

Please sign in to comment.