Skip to content

Commit 2c0e774

Browse files
committed
mitigate the broken-ness of screenshots with UI
1 parent 4c69446 commit 2c0e774

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

include/polyscope/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ namespace internal {
2323
// Get a unique identifier
2424
uint64_t getNextUniqueID();
2525

26+
// How many layers deep in the polyscope context stack are we
27+
extern int contextStackSize;
28+
2629
// track various fire-once warnings
2730
extern bool& pointCloudEfficiencyWarningReported;
2831

src/internal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ uint64_t uniqueID = 42;
1111

1212
uint64_t getNextUniqueID() { return uniqueID++; }
1313

14+
int contextStackSize = 0;
15+
1416
bool& pointCloudEfficiencyWarningReported = state::globalContext.pointCloudEfficiencyWarningReported;
1517
FloatingQuantityStructure*& globalFloatingQuantityStructure = state::globalContext.globalFloatingQuantityStructure;
1618

src/polyscope.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ void init(std::string backend) {
244244
// Create an initial context based context. Note that calling show() never actually uses this context, because it
245245
// pushes a new one each time. But using frameTick() may use this context.
246246
contextStack.push_back(ContextEntry{ImGui::GetCurrentContext(), ImPlot::GetCurrentContext(), nullptr, true});
247+
internal::contextStackSize++;
247248

248249
view::invalidateView();
249250

@@ -293,6 +294,7 @@ void pushContext(std::function<void()> callbackFunction, bool drawDefaultUI) {
293294
ImGuizmo::PushContext();
294295

295296
contextStack.push_back(ContextEntry{newContext, newPlotContext, callbackFunction, drawDefaultUI});
297+
internal::contextStackSize++;
296298

297299
if (contextStack.size() > 50) {
298300
// Catch bugs with nested show()
@@ -340,6 +342,7 @@ void popContext() {
340342
return;
341343
}
342344
contextStack.pop_back();
345+
internal::contextStackSize--;
343346
}
344347

345348
ImGuiContext* getCurrentContext() { return contextStack.empty() ? nullptr : contextStack.back().context; }
@@ -743,7 +746,7 @@ void buildPolyscopeGui() {
743746
if (ImGui::BeginPopup("ScreenshotOptionsPopup")) {
744747

745748
ImGui::Checkbox("with transparency", &options::screenshotTransparency);
746-
ImGui::Checkbox("with UI", &options::screenshotWithImGuiUI);
749+
// ImGui::Checkbox("with UI", &options::screenshotWithImGuiUI); // temporarily disabled while broken
747750

748751
if (ImGui::BeginMenu("file format")) {
749752
if (ImGui::MenuItem(".png", NULL, options::screenshotExtension == ".png")) options::screenshotExtension = ".png";
@@ -1201,6 +1204,7 @@ void shutdown(bool allowMidFrameShutdown) {
12011204
render::engine->shutdown();
12021205
delete render::engine;
12031206
contextStack.clear();
1207+
internal::contextStackSize = 0;
12041208
render::engine = nullptr;
12051209
state::backend = "";
12061210
state::initialized = false;

src/screenshot.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ bool hasExtension(std::string str, std::string ext) {
4141
std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options = {}) {
4242
checkInitialized();
4343

44+
if (options.includeUI && internal::contextStackSize > 1) {
45+
error("Screenshot with includeUI=true is not supported within show(). See docs for details and workarounds.");
46+
return std::vector<unsigned char>();
47+
}
48+
4449
render::engine->useAltDisplayBuffer = true;
4550
if (options.transparentBackground) render::engine->lightCopy = true; // copy directly in to buffer without blending
4651

@@ -67,6 +72,7 @@ std::vector<unsigned char> getRenderInBuffer(const ScreenshotOptions& options =
6772
ImGuiIO* newIO;
6873
if (options.includeUI) {
6974

75+
// NOTE: error check above ensure internal::contextStackSize == 1 (cannot be called during show())
7076

7177
// Create a new context and push it on to the stack
7278
oldIO = &ImGui::GetIO(); // used to GLFW + OpenGL data to the new IO object

0 commit comments

Comments
 (0)