From f350e7b7a7e59c2a21cfccf30de4affef8fb7050 Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Fri, 21 Feb 2020 00:57:21 -0800 Subject: [PATCH 1/2] Add CimguiStorage and ImGuiPlatformIO redirect functions. --- generator/cimgui_template.cpp | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/generator/cimgui_template.cpp b/generator/cimgui_template.cpp index 9002923..e466e2e 100644 --- a/generator/cimgui_template.cpp +++ b/generator/cimgui_template.cpp @@ -59,3 +59,62 @@ CIMGUI_API void ImVector_ImWchar_UnInit(ImVector_ImWchar* p) p->~ImVector(); } +#ifdef IMGUI_DOCKING + +// NOTE: Some function pointers in the ImGuiPlatformIO structure are not C-compatible because of their +// use of a complex return type. To work around this, we store a custom CimguiStorage object inside +// ImGuiIO::BackendLanguageUserData, which contains C-compatible function pointer variants for these +// functions. When a user function pointer is provided, we hook up the underlying ImGuiPlatformIO +// function pointer to a thunk which accesses the user function pointer through CimguiStorage. + +struct CimguiStorage +{ + void(*Platform_GetWindowPos)(ImGuiViewport* vp, ImVec2* out_pos); + void(*Platform_GetWindowSize)(ImGuiViewport* vp, ImVec2* out_pos); +}; + +// Gets a reference to the CimguiStorage object stored in the current ImGui context's BackendLanguageUserData. +CimguiStorage& GetCimguiStorage() +{ + ImGuiIO& io = ImGui::GetIO(); + if (io.BackendLanguageUserData == NULL) + { + io.BackendLanguageUserData = new CimguiStorage(); + } + + return *(CimguiStorage*)io.BackendLanguageUserData; +} + +// Thunk satisfying the signature of ImGuiPlatformIO::Platform_GetWindowPos. +ImVec2 Platform_GetWindowPos_hook(ImGuiViewport* vp) +{ + ImVec2 pos; + GetCimguiStorage().Platform_GetWindowPos(vp, &pos); + return pos; +}; + +// Fully C-compatible function pointer setter for ImGuiPlatformIO::Platform_GetWindowPos. +CIMGUI_API void ImGuiPlatformIO_Set_Platform_GetWindowPos(ImGuiPlatformIO* platform_io, void(*user_callback)(ImGuiViewport* vp, ImVec2* out_pos)) +{ + CimguiStorage& storage = GetCimguiStorage(); + storage.Platform_GetWindowPos = user_callback; + platform_io->Platform_GetWindowPos = &Platform_GetWindowPos_hook; +} + +// Thunk satisfying the signature of ImGuiPlatformIO::Platform_GetWindowSize. +ImVec2 Platform_GetWindowSize_hook(ImGuiViewport* vp) +{ + ImVec2 size; + GetCimguiStorage().Platform_GetWindowSize(vp, &size); + return size; +}; + +// Fully C-compatible function pointer setter for ImGuiPlatformIO::Platform_GetWindowSize. +CIMGUI_API void ImGuiPlatformIO_Set_Platform_GetWindowSize(ImGuiPlatformIO* platform_io, void(*user_callback)(ImGuiViewport* vp, ImVec2* out_size)) +{ + CimguiStorage& storage = GetCimguiStorage(); + storage.Platform_GetWindowSize = user_callback; + platform_io->Platform_GetWindowSize = &Platform_GetWindowSize_hook; +} + +#endif From 39336d98433f0bd99dae17efcebc27eb7c15d288 Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Fri, 21 Feb 2020 08:02:16 -0800 Subject: [PATCH 2/2] Add a note in README.md about the IMGUI_DOCKING preprocessor define. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6c778bb..e17fa73 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Notes: * edit config_generator.lua for adding includes needed by your chosen implementations. * Run generator.bat or generator.sh with gcc, clang or cl and LuaJIT on your PATH. * as a result some files are generated: `cimgui.cpp` and `cimgui.h` for compiling and some lua/json files with information about the binding: `definitions.json` with function info, `structs_and_enums.json` with struct and enum info, `impl_definitions.json` with functions from the implementations info. +* If you are generating bindings for the [docking](https://github.com/ocornut/imgui/tree/docking) branch of imgui, you must define the `IMGUI_DOCKING` constant in CMakeLists.txt. This includes extra custom definitions that are needed for some functionality on the docking branch. # generate binding * C interface is exposed by cimgui.h when you define CIMGUI_DEFINE_ENUMS_AND_STRUCTS