Skip to content

Commit fcddfad

Browse files
committed
clean up implot context management, add some tests
1 parent faec5a5 commit fcddfad

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/polyscope.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
207207

208208
// Create a new context and push it on to the stack
209209
ImGuiContext* newContext = ImGui::CreateContext();
210+
ImPlotContext* newPlotContext = ImPlot::CreateContext();
210211
ImGuiIO& oldIO = ImGui::GetIO(); // used to GLFW + OpenGL data to the new IO object
211212
#ifdef IMGUI_HAS_DOCK
212213
ImGuiPlatformIO& oldPlatformIO = ImGui::GetPlatformIO();
213214
#endif
214215
ImGui::SetCurrentContext(newContext);
216+
ImPlot::SetCurrentContext(newPlotContext);
215217
#ifdef IMGUI_HAS_DOCK
216218
// Propagate GLFW window handle to new context
217219
ImGui::GetMainViewport()->PlatformHandle = oldPlatformIO.Viewports[0]->PlatformHandle;
@@ -221,8 +223,6 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
221223

222224
render::engine->configureImGui();
223225

224-
// Implot context too
225-
ImPlotContext* newPlotContext = ImPlot::CreateContext();
226226

227227
contextStack.push_back(ContextEntry{newContext, newPlotContext, callbackFunction, drawDefaultUI});
228228

@@ -264,6 +264,7 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
264264
// Workaround overzealous ImGui assertion before destroying any inner context
265265
// https://github.com/ocornut/imgui/pull/7175
266266
ImGui::SetCurrentContext(newContext);
267+
ImPlot::SetCurrentContext(newPlotContext);
267268
ImGui::GetIO().BackendPlatformUserData = nullptr;
268269
ImGui::GetIO().BackendRendererUserData = nullptr;
269270

src/render/opengl/gl_engine_glfw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ void GLEngineGLFW::shutdownImGui() {
199199
ImGui_ImplOpenGL3_Shutdown();
200200
ImGui_ImplGlfw_Shutdown();
201201
ImGui::DestroyContext();
202+
ImPlot::DestroyContext();
202203
}
203204

204205
void GLEngineGLFW::ImGuiNewFrame() {

src/screenshot.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,20 @@ std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options =
5050
// Create a new context and push it on to the stack
5151
ImGuiContext* oldContext;
5252
ImGuiContext* newContext;
53+
ImPlotContext* oldPlotContext;
54+
ImPlotContext* newPlotContext;
5355
if (options.includeUI) {
5456
// WARNING: code duplicated here and in pushContext()
5557
oldContext = ImGui::GetCurrentContext();
5658
newContext = ImGui::CreateContext();
59+
oldPlotContext = ImPlot::GetCurrentContext();
60+
newPlotContext = ImPlot::CreateContext();
5761
ImGuiIO& oldIO = ImGui::GetIO(); // used to GLFW + OpenGL data to the new IO object
5862
#ifdef IMGUI_HAS_DOCK
5963
ImGuiPlatformIO& oldPlatformIO = ImGui::GetPlatformIO();
6064
#endif
6165
ImGui::SetCurrentContext(newContext);
66+
ImPlot::SetCurrentContext(newPlotContext);
6267
#ifdef IMGUI_HAS_DOCK
6368
// Propagate GLFW window handle to new context
6469
ImGui::GetMainViewport()->PlatformHandle = oldPlatformIO.Viewports[0]->PlatformHandle;
@@ -81,11 +86,15 @@ std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options =
8186
// Workaround overzealous ImGui assertion before destroying any inner context
8287
// https://github.com/ocornut/imgui/pull/7175
8388
ImGui::SetCurrentContext(newContext);
89+
ImPlot::SetCurrentContext(newPlotContext);
8490
ImGui::GetIO().BackendPlatformUserData = nullptr;
8591
ImGui::GetIO().BackendRendererUserData = nullptr;
8692

8793
ImGui::DestroyContext(newContext);
94+
ImPlot::DestroyContext(newPlotContext);
95+
8896
ImGui::SetCurrentContext(oldContext);
97+
ImPlot::SetCurrentContext(oldPlotContext);
8998
}
9099

91100

test/src/basics_test.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ TEST_F(PolyscopeTest, FrameTickWithImgui) {
4545
polyscope::state::userCallback = nullptr;
4646
}
4747

48+
4849
// We should be able to nest calls to show() via the callback. ImGUI causes headaches here
4950
TEST_F(PolyscopeTest, NestedShow) {
5051

@@ -148,6 +149,55 @@ TEST_F(PolyscopeTest, ScreenshotBuffer) {
148149
EXPECT_EQ(buff2.size(), polyscope::view::bufferWidth * polyscope::view::bufferHeight * 4);
149150
}
150151

152+
TEST_F(PolyscopeTest, ImPlotBasic) {
153+
154+
std::vector<float> xvals = {0., 2., 4., 8.};
155+
156+
auto showCallback = [&]() {
157+
ImGui::Button("do something");
158+
if(ImPlot::BeginPlot("test plot")) {
159+
ImPlot::PlotLine("test line", &xvals.front(), xvals.size());
160+
ImPlot::EndPlot();
161+
}
162+
};
163+
polyscope::state::userCallback = showCallback;
164+
165+
polyscope::show(3);
166+
167+
for (int i = 0; i < 3; i++) {
168+
polyscope::frameTick();
169+
}
170+
171+
polyscope::state::userCallback = nullptr;
172+
}
173+
174+
175+
TEST_F(PolyscopeTest, ImPlotScreenshot) {
176+
// test this because there is some context logic duplicated there
177+
178+
std::vector<float> xvals = {0., 2., 4., 8.};
179+
180+
auto showCallback = [&]() {
181+
ImGui::Button("do something");
182+
if(ImPlot::BeginPlot("test plot")) {
183+
ImPlot::PlotLine("test line", &xvals.front(), xvals.size());
184+
ImPlot::EndPlot();
185+
}
186+
};
187+
polyscope::state::userCallback = showCallback;
188+
189+
polyscope::show(3);
190+
191+
polyscope::ScreenshotOptions opts;
192+
opts.includeUI = true;
193+
opts.transparentBackground = false;
194+
polyscope::screenshot(opts);
195+
196+
polyscope::show(3);
197+
198+
polyscope::state::userCallback = nullptr;
199+
}
200+
151201
// ============================================================
152202
// =============== View and navigation
153203
// ============================================================

0 commit comments

Comments
 (0)