From 0478176e4743a4399a2b5114ee7fa930b8d88c6a Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 30 Jul 2025 21:29:24 -0700 Subject: [PATCH] [simgui] Add GUI context getter hooks (#8127) This enables GUI libraries to be linked statically with shared context. --- .../halsim_gui/src/main/native/cpp/main.cpp | 9 +++++++++ .../src/main/native/include/HALSimGuiExt.h | 19 +++++++++++++++++++ wpigui/src/main/native/cpp/wpigui.cpp | 8 ++++++++ wpigui/src/main/native/include/wpigui.h | 16 ++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/simulation/halsim_gui/src/main/native/cpp/main.cpp b/simulation/halsim_gui/src/main/native/cpp/main.cpp index de3eae4bab..2df10c84f0 100644 --- a/simulation/halsim_gui/src/main/native/cpp/main.cpp +++ b/simulation/halsim_gui/src/main/native/cpp/main.cpp @@ -65,6 +65,12 @@ __declspec(dllexport) reinterpret_cast((AddGuiEarlyExecuteFn)&AddGuiEarlyExecute)); HAL_RegisterExtension(HALSIMGUI_EXT_GUIEXIT, reinterpret_cast((GuiExitFn)&GuiExit)); + HAL_RegisterExtension( + HALSIMGUI_EXT_GETGUICONTEXT, + reinterpret_cast((GetGuiContextFn)&gui::GetCurrentContext)); + HAL_RegisterExtension( + HALSIMGUI_EXT_GETGLASSCONTEXT, + reinterpret_cast((GetGlassContextFn)&glass::GetCurrentContext)); HALSimGui::GlobalInit(); DriverStationGui::GlobalInit(); @@ -158,6 +164,9 @@ __declspec(dllexport) ImGuiConfigFlags_DockingEnable)) { return 0; } + HAL_RegisterExtension( + HALSIMGUI_EXT_GETIMGUICONTEXT, + reinterpret_cast((GetImguiContextFn)&ImGui::GetCurrentContext)); HAL_RegisterExtensionListener( nullptr, [](void*, const char* name, void* data) { if (std::string_view{name} == "ds_socket") { diff --git a/simulation/halsim_gui/src/main/native/include/HALSimGuiExt.h b/simulation/halsim_gui/src/main/native/include/HALSimGuiExt.h index e60976e346..377518f6db 100644 --- a/simulation/halsim_gui/src/main/native/include/HALSimGuiExt.h +++ b/simulation/halsim_gui/src/main/native/include/HALSimGuiExt.h @@ -6,6 +6,16 @@ #include +struct ImGuiContext; + +namespace glass { +class Context; +} // namespace glass + +namespace wpi::gui { +struct Context; +} // namespace wpi::gui + namespace halsimgui { // These functions can be used to hook into the GUI, and can be accessed @@ -23,4 +33,13 @@ using AddGuiEarlyExecuteFn = void (*)(std::function execute); #define HALSIMGUI_EXT_GUIEXIT "halsimgui::GuiExit" using GuiExitFn = void (*)(); +#define HALSIMGUI_EXT_GETIMGUICONTEXT "halsimgui::GetImguiContext" +using GetImguiContextFn = ImGuiContext* (*)(); + +#define HALSIMGUI_EXT_GETGUICONTEXT "halsimgui::GetGuiContext" +using GetGuiContextFn = wpi::gui::Context* (*)(); + +#define HALSIMGUI_EXT_GETGLASSCONTEXT "halsimgui::GetGlassContext" +using GetGlassContextFn = glass::Context* (*)(); + } // namespace halsimgui diff --git a/wpigui/src/main/native/cpp/wpigui.cpp b/wpigui/src/main/native/cpp/wpigui.cpp index 0fe3f24104..71e6ab5700 100644 --- a/wpigui/src/main/native/cpp/wpigui.cpp +++ b/wpigui/src/main/native/cpp/wpigui.cpp @@ -169,6 +169,14 @@ void gui::DestroyContext() { gContext = nullptr; } +Context* gui::GetCurrentContext() { + return gContext; +} + +void gui::SetCurrentContext(Context* context) { + gContext = context; +} + static void UpdateFontScale() { // Scale based on OS window content scaling float windowScale = 1.0; diff --git a/wpigui/src/main/native/include/wpigui.h b/wpigui/src/main/native/include/wpigui.h index 6c3315fc55..761e02b2f8 100644 --- a/wpigui/src/main/native/include/wpigui.h +++ b/wpigui/src/main/native/include/wpigui.h @@ -14,6 +14,8 @@ extern "C" struct GLFWwindow; namespace wpi::gui { +struct Context; + /** * Creates GUI context. Must be called prior to calling any other functions. */ @@ -24,6 +26,20 @@ void CreateContext(); */ void DestroyContext(); +/** + * Gets GUI context. + * + * @return context + */ +Context* GetCurrentContext(); + +/** + * Sets GUI context. + * + * @param context context + */ +void SetCurrentContext(Context* context); + /** * Initializes the GUI. *